diff scripts/general/num2str.m @ 17292:c5073ed27cdc

Handle '+' format modifier in sprintf (bug #39773) * libinterp/corefcn/oct-stream.cc: Print '+' character even for Inf and NaN when '+' format specified. * scripts/general/num2str.m: Handle Inf values correctly when generating format for sprintf. Add new %!tests. * test/io.tst: Add %! tests for sprintf behavior. Use Octave coding conventions on rest of file.
author Rik <rik@octave.org>
date Tue, 20 Aug 2013 11:06:20 -0700
parents 333243133364
children e351b499311e
line wrap: on
line diff
--- a/scripts/general/num2str.m	Tue Aug 20 19:11:17 2013 +0200
+++ b/scripts/general/num2str.m	Tue Aug 20 11:06:20 2013 -0700
@@ -92,10 +92,9 @@
       if (isnumeric (x))
         ## Setup a suitable format string, ignoring inf entries
         dgt = floor (log10 (max (abs (x(!isinf (x(:)))))));
-
-        ## If the whole input array is inf...
         if (isempty (dgt))
-          dgt = 0;
+          ## If the whole input array is inf...
+          dgt = 1;
         endif
 
         if (any (x(:) != fix (x(:))))
@@ -130,8 +129,15 @@
       endif
     else
       ## Setup a suitable format string
-      dgt = floor (log10 (max (max (abs (real (x(:)))),
-                               max (abs (imag (x(:)))))));
+      #dgt = floor (log10 (max (max (abs (real (x(:)))),
+      #                         max (abs (imag (x(:)))))));
+      dgt = floor (log10 (max (max (abs (real (x(!isinf (real (x(:))))))),
+                               max (abs (imag (x(!isinf (imag (x(:))))))))));
+      if (isempty (dgt))
+        ## If the whole input array is inf...
+        dgt = 1;
+      endif
+
       if (any (x(:) != fix (x(:))))
         ## Floating point input
           dgt = max (dgt + 4, 5);   # Keep 4 sig. figures after decimal point
@@ -183,7 +189,16 @@
 %!assert (num2str (2^33+1i), "8589934592+1i")
 %!assert (num2str (-2^33+1i), "-8589934592+1i")
 %!assert (num2str (inf), "Inf")
+%!assert (num2str ([inf -inf]), "Inf -Inf")
+%!assert (num2str ([complex(Inf,0), complex(0,-Inf)]), "Inf+0i   0-Infi")
+%!assert (num2str (complex(Inf,1)), "Inf+1i")
+%!assert (num2str (complex(1,Inf)), "1+Infi")
 %!assert (num2str (nan), "NaN")
+%!assert (num2str (complex (NaN, 1)), "NaN+1i")
+%!assert (num2str (complex (1, NaN)), "1+NaNi")
+%!assert (num2str (NA), "NA")
+%!assert (num2str (complex (NA, 1)), "NA+1i")
+%!assert (num2str (complex (1, NA)), "1+NAi")
 
 ## FIXME: Integers greater than bitmax() should be masked to show just
 ##        16 digits of precision.