help-octave
[Top][All Lists]
Advanced

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

Re: reading NaN with fscanf


From: John W. Eaton
Subject: Re: reading NaN with fscanf
Date: Wed, 2 Mar 2005 14:28:50 -0500

On  2-Mar-2005, Claudio Belotti <address@hidden> wrote:

| Gerald Ebberink wrote:
| > -----BEGIN PGP SIGNED MESSAGE-----
| > Hash: SHA1
| > 
| > 
| > what do you want to do with the NaN, you want them in your data or do
| > you replace them or even omit them. because if you do you might want to
| > use sed or a other command-line tool which are designed for these kind
| > of operations.
| 
| Gerald,
| I want the NaN in the data, so I can't filter the out from the input file.

Please try the following patch.

Note that with this change, Octave will only recognize only recognize

  NaN
  Inf
  NA

and not things like Infinity, NaNQ, nan, inf, etc.  The three strings
Octave does recognize are the ones that it produces when writing these
values with printf.  If you need something else, you will have to
transform your data in some other way before reading.  Also note that
there could be some trouble reading from a pipe as backing up by more
than one character might not be possible.  But I'm not sure that this
is a significant problem.

jwe


2005-03-02  John W. Eaton  <address@hidden>

        * oct-stream.cc (octave_scan (std::istream&, const
        scanf_format_elt&, double*)): New specialization to handle Inf,
        NaN, and NA.


Index: src/oct-stream.cc
===================================================================
RCS file: /cvs/octave/src/oct-stream.cc,v
retrieving revision 1.110
diff -u -r1.110 oct-stream.cc
--- src/oct-stream.cc   4 Nov 2004 20:12:11 -0000       1.110
+++ src/oct-stream.cc   2 Mar 2005 19:13:18 -0000
@@ -1142,6 +1142,181 @@
 octave_scan (std::istream&, const scanf_format_elt&, float*);
 #endif
 
+template <>
+std::istream&
+octave_scan (std::istream& is, const scanf_format_elt& fmt, double* valptr)
+{
+  double& ref = *valptr;
+
+  switch (fmt.type)
+    {
+    case 'e':
+    case 'f':
+    case 'g':
+      {
+       int c1;
+
+       while (is && (c1 = is.get ()) != EOF && isspace (c1))
+         /* skip whitespace */;
+
+       if (c1 != EOF)
+         {
+           if (c1 == 'N')
+             {
+               int c2 = is.get ();
+
+               if (c2 != EOF)
+                 {
+                   if (c2 == 'A')
+                     {
+                       int c3 = is.get ();
+
+                       if (c3 != EOF)
+                         {
+                           is.putback (c3);
+
+                           if (isspace (c3) || ispunct (c3))
+                             ref = octave_NA;
+                           else
+                             {
+                               is.putback (c2);
+                               is.putback (c1);
+
+                               is >> ref;
+                             }
+                         }
+                       else
+                         {
+                           is.clear ();
+
+                           ref = octave_NA;
+                         }
+                     }
+                   else if (c2 == 'a')
+                     {
+                       int c3 = is.get ();
+
+                       if (c3 != EOF)
+                         {
+                           if (c3 == 'N')
+                             {
+                               int c4 = is.get ();
+
+                               if (c4 != EOF)
+                                 {
+                                   is.putback (c4);
+
+                                   if (isspace (c4) || ispunct (c4))
+                                     ref = octave_NaN;
+                                   else
+                                     {
+                                       is.putback (c3);
+                                       is.putback (c2);
+                                       is.putback (c1);
+
+                                       is >> ref;
+                                     }
+                                 }
+                               else
+                                 {
+                                   is.clear ();
+
+                                   ref = octave_NaN;
+                                 }
+                             }
+                           else
+                             {
+                               is.putback (c3);
+                               is.putback (c2);
+                               is.putback (c1);
+
+                               is >> ref;
+                             }
+                         }
+                     }
+                   else
+                     {
+                       is.putback (c2);
+                       is.putback (c1);
+
+                       is >> ref;
+                     }
+                 }
+             }
+           else if (c1 == 'I')
+             {
+               int c2 = is.get ();
+
+               if (c2 != EOF)
+                 {
+                   if (c2 == 'n')
+                     {
+                       int c3 = is.get ();
+
+                       if (c3 != EOF)
+
+                         if (c3 == 'f')
+                           {
+                             int c4 = is.get ();
+
+                             if (c4 != EOF)
+                               {
+                                 is.putback (c4);
+
+                                 if (isspace (c4) || ispunct (c4))
+                                   ref = octave_Inf;
+                                 else
+                                   {
+                                     is.putback (c3);
+                                     is.putback (c2);
+                                     is.putback (c1);
+
+                                     is >> ref;
+                                   }
+                               }
+                             else
+                               {
+                                 is.clear ();
+
+                                 ref = octave_Inf;
+                               }
+                           }
+                         else
+                           {
+                             is.putback (c3);
+                             is.putback (c2);
+                             is.putback (c1);
+
+                             is >> ref;
+                           }
+                     }
+                   else
+                     {
+                       is.putback (c2);
+                       is.putback (c1);
+
+                       is >> ref;
+                     }
+                 }
+             }
+           else
+             {
+               is.putback (c1);
+
+               is >> ref;
+             }
+         }
+      }
+      break;
+
+    default:
+      panic_impossible ();
+      break;
+    }
+
+  return is;
+}
+
 template std::istream&
 octave_scan (std::istream&, const scanf_format_elt&, double*);
 



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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