octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Built-in variables


From: John W. Eaton
Subject: Re: Built-in variables
Date: Tue, 9 May 2006 11:51:52 -0400

On  8-May-2006, John W. Eaton wrote:

| On  8-May-2006, David Bateman wrote:
| 
| | I think I've applied your changes to the fixed point package in octave 
| | forge. See
| | 
| | 
http://sourceforge.net/mailarchive/forum.php?thread_id=10291192&forum_id=33111
| | 
| | for details..
| 
| Yes, those changes are also needed, but there are some more that I
| just made today.  But now can't get diffs because of Source Forge CVS
| problems.  I was able to check out a fresh copy earlier today, but now
| I am unable to generate diffs or check out a new copy.

OK, Source Forge is suddenly working again for me so I was able to
generate the following diff.

I don't see a better way to save and restore the output_precision
internal variable given that the real value (Voutput_precision) is
static in pr-output.cc.  Should we put all of these internal variables
in a structure and make the structure extern?  Of course that still
doesn't prevent future problems as the contents of the structure could
change.

If you agree that these changes are OK, then you can apply them or I
can try to check them in.

jwe


Index: main/fixed/Makefile
===================================================================
RCS file: /cvsroot/octave/octave-forge/main/fixed/Makefile,v
retrieving revision 1.17
diff -u -r1.17 Makefile
--- main/fixed/Makefile 16 Feb 2006 18:50:06 -0000      1.17
+++ main/fixed/Makefile 9 May 2006 15:41:41 -0000
@@ -9,7 +9,10 @@
        ffloor fceil fprod fsum fsumsq fcumsum \
        fcumprod display_fixed_operations reset_fixed_operations \
        freshape fdiag fatan2 freal fimag fconj \
-       farg fangle) 
+       farg fangle \
+       fixed_point_warn_overflow fixed_point_debug \
+       fixed_point_count_operations fixed_point_version \
+       fixed_point_library_version) 
 
 SUBDIRS = examples doc
 
Index: main/fixed/fixed-var.cc
===================================================================
RCS file: /cvsroot/octave/octave-forge/main/fixed/fixed-var.cc,v
retrieving revision 1.3
diff -u -r1.3 fixed-var.cc
--- main/fixed/fixed-var.cc     1 May 2006 20:26:34 -0000       1.3
+++ main/fixed/fixed-var.cc     9 May 2006 15:41:41 -0000
@@ -34,52 +34,52 @@
 
 #include <octave/variables.h>
 #include <octave/utils.h>
-#include <octave/defun.h>
+#include <octave/defun-dld.h>
 
-#define FIXED_VAR( TYP, VAR, VAR_INT) \
-TYP &V ## VAR = VAR_INT; \
-static int VAR (void) \
-{ \
-  V ## VAR = check_preference (#VAR); \
-  return 0; \
+DEFUN_DLD (fixed_point_warn_overflow, args, nargout,
+  "-*- texinfo -*-\n\
address@hidden {Loadable Function} address@hidden =} fixed_point_warn_overflow 
()\n\
address@hidden {Loadable Function} address@hidden =} fixed_point_warn_overflow 
(@var{new_val})\n\
+Query or set the internal variable @code{fixed_point_warn_overflow}.\n\
+If enabled, Octave warns of overflows in fixed point operations.\n\
+By default, these warnings are disabled.\n\
address@hidden deftyepfn")
+{
+  return set_internal_variable (Fixed::FP_Overflow, args, nargout,
+                               "fixed_point_warn_overflow");
 }
 
-FIXED_VAR ( bool, fixed_point_warn_overflow, Fixed::FP_Overflow)
-FIXED_VAR ( bool, fixed_point_debug, Fixed::FP_Debug)
-FIXED_VAR ( bool, fixed_point_count_operations, Fixed::FP_CountOperations)
-
-void
-symbols_of_fixed (void)
+DEFUN_DLD (fixed_point_debug, args, nargout,
+  "-*- texinfo -*-\n\
address@hidden {Loadable Function} address@hidden =} fixed_point_debug ()\n\
address@hidden {Loadable Function} address@hidden =} fixed_point_debug 
(@var{new_val})\n\
+Query or set the internal variable @code{fixed_point_debug}.  If\n\
+enabled, Octave keeps a copy of the value of fixed point variable\n\
+internally. This is useful for use with a debug to allow easy access\n\
+to the variables value.  By default this feature is disabled.\n\
address@hidden deftypefn")
 {
-  DEFVAR (fixed_point_warn_overflow, false, fixed_point_warn_overflow,
-    "-*- texinfo -*-\n"
-"@defvr {Loadable Variable} {} fixed_point_warn_overflow\n\
-If the value of @code{fixed_point_warn_overflow} is nonzero, Octave warns\n\
-the user of overflows in fixed point operations. The default is 0.\n\
address@hidden defvr");
-
-  DEFVAR (fixed_point_debug, false, fixed_point_debug,
-    "-*- texinfo -*-\n"
-"@defvr {Loadable Variable} {} fixed_point_debug\n\
-If the value of @code{fixed_point_debug} is nonzero, Octave keeps\n\
-a copy of the value of fixed point variable internally. This is useful\n\
-for use with a debug to allow easy access to the variables value.\n\
-The default value is 0.\n\
address@hidden defvr");
-
-  DEFVAR (fixed_point_count_operations, false, fixed_point_count_operations,
-    "-*- texinfo -*-\n"
-"@defvr {Loadable Variable} {} fixed_point_count_operations\n\
-If the value of @code{fixed_point_count_operations} is nonzero, Octave keeps\n\
-track of how many times each type of floating point operation has been used\n\
-internally. This can be used to give an approximation of the algorithms\n\
-complexity. The default value is 0.\n\
address@hidden defvr\n\
address@hidden");
+  return set_internal_variable (Fixed::FP_Debug, args, nargout,
+                               "fixed_point_debug");
+}
 
+DEFUN_DLD (fixed_point_count_operations, args, nargout,
+  "-*- texinfo -*-\n\
address@hidden {Loadable Function} address@hidden =} 
fixed_point_count_operations ()\n\
address@hidden {Loadable Function} address@hidden =} 
fixed_point_count_operations (@var{new_val})\n\
+Query or set the internal variable @code{fixed_point_count_operations}.\n\
+If enabled, Octave keeps track of how many times each type of floating\n\
+point operation has been used internally.  This can be used to give an\n\
+approximation of the algorithms complexity.  By default, this feature is\n\
+disabled.\n\
address@hidden
address@hidden deftypefn")
+{
+  return set_internal_variable (Fixed::FP_CountOperations, args, nargout,
+                               "fixed_point_count_operations");
 }
 
-DEFUN (fixed_point_version, args, ,
+DEFUN_DLD (fixed_point_version, args, ,
     "-*- texinfo -*-\n"
 "@deftypefn {Loadable Function} {} fixed_point_version ()\n\
 A function returning the version number of the fixed point package used.\n\
@@ -95,7 +95,7 @@
   return retval;
 }
 
-DEFUN (fixed_point_library_version, args, ,
+DEFUN_DLD (fixed_point_library_version, args, ,
     "-*- texinfo -*-\n"
 "@deftypefn {Loadable Function} {} fixed_point_library_version ()\n\
 A function returning the version number of the fixed point library used.\n\
Index: main/fixed/fixed-var.h
===================================================================
RCS file: /cvsroot/octave/octave-forge/main/fixed/fixed-var.h,v
retrieving revision 1.2
diff -u -r1.2 fixed-var.h
--- main/fixed/fixed-var.h      26 May 2004 14:18:51 -0000      1.2
+++ main/fixed/fixed-var.h      9 May 2006 15:41:41 -0000
@@ -31,16 +31,11 @@
 #endif
 
 #if !defined(fixed_var_cc)
-extern bool &Vfixed_point_warn_overflow;
-extern bool &Vfixed_point_debug;
-extern bool &Vfixed_point_count_operations;
 extern int &Vfixed_point_print_width;
 extern char &fixed_point_version;
 extern char &fixed_point_library_version;
 #endif
 
-void symbols_of_fixed (void);
-
 #endif
 
 /*
Index: main/fixed/fixed.cc
===================================================================
RCS file: /cvsroot/octave/octave-forge/main/fixed/fixed.cc,v
retrieving revision 1.4
diff -u -r1.4 fixed.cc
--- main/fixed/fixed.cc 28 Jun 2004 19:35:19 -0000      1.4
+++ main/fixed/fixed.cc 9 May 2006 15:41:41 -0000
@@ -94,8 +94,6 @@
   install_fil_fcs_ops ();
   install_fil_fcm_ops ();
 
-  symbols_of_fixed ();
-
   fixed_type_loaded = true;
 
   // Lock constructor function in place, otherwise 
@@ -117,7 +115,7 @@
   octave_value retval;
   if ((args.length() != 0) || !fixed_type_loaded)
     print_usage("display_fixed_operations");
-  else if (Vfixed_point_count_operations)
+  else if (Fixed::FP_CountOperations)
     octave_stdout << Fixed::FP_Operations;
   else
     error("display_fixed_operations: variable fixed_point_count_operations is 
zero");
Index: main/fixed/ov-fixed-complex.cc
===================================================================
RCS file: /cvsroot/octave/octave-forge/main/fixed/ov-fixed-complex.cc,v
retrieving revision 1.6
diff -u -r1.6 ov-fixed-complex.cc
--- main/fixed/ov-fixed-complex.cc      2 May 2006 20:48:45 -0000       1.6
+++ main/fixed/ov-fixed-complex.cc      9 May 2006 15:41:41 -0000
@@ -33,7 +33,9 @@
 #include <octave/oct-obj.h>
 #include <octave/ov.h>
 #include <octave/gripes.h>
+#include <octave/parse.h>
 #include <octave/utils.h>
+#include <octave/unwind-prot.h>
 #include <octave/variables.h>
 
 #ifdef CLASS_HAS_LOAD_SAVE
@@ -262,23 +264,31 @@
   return retval;
 }
 
+static void
+restore_precision (void *p)
+{
+  bind_internal_variable ("output_precision", *(static_cast<int *> (p)));
+}
+
 void
 octave_fixed_complex::print_raw (std::ostream& os, 
                                 bool pr_as_read_syntax) const
 {
-  int prec = check_preference("output_precision");
   double min_num = std::max(abs(real(scalar)).fixedpoint(),
                            abs(imag(scalar)).fixedpoint());
   int new_prec = (int)std::max(real(scalar.getdecsize()),
                               imag(scalar.getdecsize()))
     + (min_num >= 1. ? (int)log10(min_num) + 1 : 0);
 
-  bind_builtin_variable ("output_precision", new_prec); 
+  octave_value_list tmp = feval ("output_precision");
+  int prec = tmp(0).int_value ();
+  unwind_protect::add (restore_precision, &prec);
+  bind_internal_variable ("output_precision", new_prec);
 
   indent (os);
   octave_print_internal (os, complex_value(), pr_as_read_syntax);
 
-  bind_builtin_variable ("output_precision", prec);
+  unwind_protect::run ();
 }
 
 #ifdef CLASS_HAS_LOAD_SAVE
Index: main/fixed/ov-fixed-cx-mat.cc
===================================================================
RCS file: /cvsroot/octave/octave-forge/main/fixed/ov-fixed-cx-mat.cc,v
retrieving revision 1.7
diff -u -r1.7 ov-fixed-cx-mat.cc
--- main/fixed/ov-fixed-cx-mat.cc       2 May 2006 20:48:45 -0000       1.7
+++ main/fixed/ov-fixed-cx-mat.cc       9 May 2006 15:41:41 -0000
@@ -38,7 +38,9 @@
 #include <octave/gripes.h>
 #include <octave/unwind-prot.h>
 #include <octave/cmd-edit.h>
+#include <octave/parse.h>
 #include <octave/utils.h>
+#include <octave/unwind-prot.h>
 #include <octave/variables.h>
 
 #ifdef CLASS_HAS_LOAD_SAVE
@@ -427,23 +429,31 @@
   return retval;
 }
 
+static void
+restore_precision (void *p)
+{
+  bind_internal_variable ("output_precision", *(static_cast<int *> (p)));
+}
+
 void
 octave_fixed_complex_matrix::print_raw (std::ostream& os,
                                   bool pr_as_read_syntax) const
 {
-  int prec = check_preference("output_precision");
   double min_num = std::max(abs(real(matrix)).row_min().min().fixedpoint(),
                            abs(imag(matrix)).row_min().min().fixedpoint());
   int new_prec = (int)std::max(real(matrix).getdecsize().row_max().max(),
                               imag(matrix).getdecsize().row_max().max()) +
     (min_num >= 1. ? (int)log10(min_num) + 1 : 0);
 
-  bind_builtin_variable ("output_precision", new_prec); 
+  octave_value_list tmp = feval ("output_precision");
+  int prec = tmp(0).int_value ();
+  unwind_protect::add (restore_precision, &prec);
+  bind_internal_variable ("output_precision", new_prec);
 
   octave_print_internal (os, complex_matrix_value(), false, 
                         current_print_indent_level ());
 
-  bind_builtin_variable ("output_precision", prec);
+  unwind_protect::run ();
 }
 
 #ifdef CLASS_HAS_LOAD_SAVE
Index: main/fixed/ov-fixed-mat.cc
===================================================================
RCS file: /cvsroot/octave/octave-forge/main/fixed/ov-fixed-mat.cc,v
retrieving revision 1.8
diff -u -r1.8 ov-fixed-mat.cc
--- main/fixed/ov-fixed-mat.cc  2 May 2006 20:48:45 -0000       1.8
+++ main/fixed/ov-fixed-mat.cc  9 May 2006 15:41:41 -0000
@@ -38,7 +38,9 @@
 #include <octave/gripes.h>
 #include <octave/unwind-prot.h>
 #include <octave/cmd-edit.h>
+#include <octave/parse.h>
 #include <octave/utils.h>
+#include <octave/unwind-prot.h>
 #include <octave/variables.h>
 #ifdef CLASS_HAS_LOAD_SAVE
 #include <octave/ls-oct-ascii.h>
@@ -416,21 +418,29 @@
   return retval;
 }
 
+static void
+restore_precision (void *p)
+{
+  bind_internal_variable ("output_precision", *(static_cast<int *> (p)));
+}
+
 void
 octave_fixed_matrix::print_raw (std::ostream& os,
                                   bool pr_as_read_syntax) const
 {
-  int prec = check_preference("output_precision");
   double min_num = matrix.abs().row_min().min().fixedpoint();
   int new_prec = (int)matrix.getdecsize().row_max().max() +
     (min_num >= 1. ? (int)log10(min_num) + 1 : 0);
 
-  bind_builtin_variable ("output_precision", new_prec); 
+  octave_value_list tmp = feval ("output_precision");
+  int prec = tmp(0).int_value ();
+  unwind_protect::add (restore_precision, &prec);
+  bind_internal_variable ("output_precision", new_prec);
 
   octave_print_internal (os, matrix_value(), false, 
                         current_print_indent_level ());
 
-  bind_builtin_variable ("output_precision", prec);
+  unwind_protect::run ();
 }
 
 #ifdef CLASS_HAS_LOAD_SAVE
Index: main/fixed/ov-fixed.cc
===================================================================
RCS file: /cvsroot/octave/octave-forge/main/fixed/ov-fixed.cc,v
retrieving revision 1.4
diff -u -r1.4 ov-fixed.cc
--- main/fixed/ov-fixed.cc      9 Nov 2004 23:34:49 -0000       1.4
+++ main/fixed/ov-fixed.cc      9 May 2006 15:41:41 -0000
@@ -32,7 +32,9 @@
 #include <octave/config.h>
 #include <octave/oct-obj.h>
 #include <octave/ov.h>
+#include <octave/parse.h>
 #include <octave/utils.h>
+#include <octave/unwind-prot.h>
 #include <octave/variables.h>
 
 #ifdef CLASS_HAS_LOAD_SAVE
@@ -222,20 +224,28 @@
   return retval;
 }
 
+static void
+restore_precision (void *p)
+{
+  bind_internal_variable ("output_precision", *(static_cast<int *> (p)));
+}
+
 void
 octave_fixed::print_raw (std::ostream& os, bool pr_as_read_syntax) const
 {
-  int prec = check_preference("output_precision");
   double min_num = abs(scalar).fixedpoint();
   int new_prec = scalar.getdecsize() +
     (min_num >= 1. ? (int)log10(min_num) + 1 : 0);
 
-  bind_builtin_variable ("output_precision", new_prec); 
+  octave_value_list tmp = feval ("output_precision");
+  int prec = tmp(0).int_value ();
+  unwind_protect::add (restore_precision, &prec);
+  bind_internal_variable ("output_precision", new_prec);
 
   indent (os);
   octave_print_internal (os, scalar_value(), pr_as_read_syntax);
 
-  bind_builtin_variable ("output_precision", prec);
+  unwind_protect::run ();
 }
 
 #ifdef CLASS_HAS_LOAD_SAVE


reply via email to

[Prev in Thread] Current Thread [Next in Thread]