help-octave
[Top][All Lists]
Advanced

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

Storing/loading "-Inf" values


From: John W. Eaton
Subject: Storing/loading "-Inf" values
Date: Thu, 30 Nov 2006 21:08:18 -0500

On 30-Nov-2006, Alex Zvoleff wrote:

|       I recently attempted to load an ASCII datafile saved under 2.9.9 (on
| Linux i86) that contained several matrices containing -Inf values.
| Whenever I tried to load the file, I would get an error.  Positive Inf
| values will load correctly, but not negative.  An example is below.
| 
| octave:77> testvariable=[Inf]
| testvariable = Inf
| octave:78> save testfile
| warning: save: Inf or NaN values may not be reloadable
| octave:79> clear
| octave:80> load testfile
| octave:81> testvariable
| testvariable = Inf
| 
| octave:82> testvariable=[-Inf]
| testvariable = -Inf
| octave:83> save testfile
| warning: save: Inf or NaN values may not be reloadable
| octave:84> clear
| octave:85> load testfile
| error: load: failed to load scalar constant
| error: load: trouble reading ascii file `testfile'
| error: load: reading file testfile
| octave:85> version
| ans = 2.9.9
| 
| 
|       The following post from John that I found led me to believe this
| behavior was fixed in recent versions of Octave, but I seem to still be
| encountering the error.  Is there a simple solution?
| 
| Thanks for your help,
| 
| Alex Zvoleff
| 
| 
| > On  2-Jun-2005, Keith Goodman wrote:
| > 
| > | When I save matrices with NaNs I get the following warning: "warning:
| > | save: Inf or NaN values may not be reloadable."
| > 
| > This should only happen if you are using the Octave text format for
| > saving data.  It used to be true that Octave might fail to read Inf
| > and NaN values from such files because it relied on the system
| > iostream library for reading Inf and NaN.  In some cases it would
| > fail.  But now I think Octave has been fixed to always write Inf and
| > NaN values with the same represenatation, and to also be able to read
| > them back in.  You could still have trouble if you tried to load a
| > text format save file with an older version of Octave.  But Maybe now
| > it is reasonable to remove the warning.

Oops, I forgot about signs.  Please try the following patch.

BTW, it is generally best to report bugs on the bugs list.

Thanks,

jwe


liboctave/ChangeLog:

2006-11-30  John W. Eaton  <address@hidden>

        * lo-utils.cc (octave_read_double, read_inf_nan_na):
        Also recognize [+-][Ii]nf.


Index: liboctave/lo-utils.cc
===================================================================
RCS file: /cvs/octave/liboctave/lo-utils.cc,v
retrieving revision 1.12
diff -u -u -r1.12 lo-utils.cc
--- liboctave/lo-utils.cc       24 Apr 2006 19:13:07 -0000      1.12
+++ liboctave/lo-utils.cc       1 Dec 2006 02:02:22 -0000
@@ -210,7 +210,7 @@
 }
 
 static inline double
-read_inf_nan_na (std::istream& is, char c)
+read_inf_nan_na (std::istream& is, char c, char sign = '+')
 {
   double d = 0.0;
 
@@ -223,7 +223,7 @@
          {
            is >> c;
            if (c == 'f' || c == 'F')
-             d = octave_Inf;
+             d = sign == '-' ? -octave_Inf : octave_Inf;
            else
              is.putback (c);
          }
@@ -263,18 +263,46 @@
 {
   double d = 0.0;
 
-  char c = 0;
+  char c1 = 0;
 
-  is >> c;
-  switch (c)
+  is >> c1;
+  switch (c1)
     {
+    case '-':
+      {
+       char c2 = 0;
+       is >> c2;
+       if (c2 == 'i' || c2 == 'I')
+         d = read_inf_nan_na (is, c2, c1);
+       else
+         {
+           is.putback (c2);
+           is.putback (c1);
+         }
+      }
+      break;
+
+    case '+':
+      {
+       char c2 = 0;
+       is >> c2;
+       if (c2 == 'i' || c2 == 'I')
+         d = read_inf_nan_na (is, c2, c1);
+       else
+         {
+           is.putback (c2);
+           is.putback (c1);
+         }
+      }
+      break;
+
     case 'i': case 'I':
     case 'n': case 'N':
-      d = read_inf_nan_na (is, c);
+      d = read_inf_nan_na (is, c1);
       break;
 
     default:
-      is.putback (c);
+      is.putback (c1);
       is >> d;
     }
 

reply via email to

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