changeset 25603:e55fb9685803

num2str.m: Ensure Matlab compatibility for numbers greater than 1e15 (bug #36133) * num2str.m: Check if numbers are greater than 1e15 and use floating point format rather than integer format. Update BIST tests for bug #36133 to be regressions if they fail.
author Rik <rik@octave.org>
date Fri, 13 Jul 2018 09:16:26 -0700
parents 8f8d3fef29a8
children ca413f326224
files scripts/general/num2str.m
diffstat 1 files changed, 7 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/num2str.m	Fri Jul 13 08:54:12 2018 -0700
+++ b/scripts/general/num2str.m	Fri Jul 13 09:16:26 2018 -0700
@@ -104,7 +104,7 @@
           ndgt = 0;  # All Inf or all zero array
         endif
 
-        if (any (x(valid) != fix (x(valid))))
+        if (ndgt > 15 || any (x(valid) != fix (x(valid))))
           ## Floating point input
           ndgt = max (ndgt + 5, 5);   # Keep at least 5 significant digits
           ndgt = min (ndgt, 16);      # Cap significant digits at 16
@@ -115,8 +115,6 @@
           if (any (! valid))
             ndgt = max (ndgt, 5);     # Allow space for Inf/NaN
           endif
-          ## FIXME: Integers must be masked to show only 16 significant digits
-          ##        See test case for bug #36133 below
           fmt = sprintf ("%%%d.0f", ndgt);
         endif
       else
@@ -241,14 +239,14 @@
 ## Clear shared variables
 %!shared
 
-## FIXME: Integers greater than 1e15 should switch to exponential notation
-%!assert <36133> (num2str (1e15), "1000000000000000")
-%!assert <36133> (num2str (1e16), "1e+16")
+## Integers greater than 1e15 should switch to exponential notation
+%!assert <*36133> (num2str (1e15), "1000000000000000")
+%!assert <*36133> (num2str (1e16), "1e+16")
 ## Even exact integers in IEEE notation should use exponential notation
-%!assert <36133> (num2str(2^512), "1.34078079299426e+154");
+%!assert <*36133> (num2str(2^512), "1.34078079299426e+154")
 ## Mixed integer/floating point arrays
-%!assert <36133> (num2str ([2.1, 1e23, pi]),
-%!                         "2.1  9.999999999999999e+22      3.141592653589793")
+%!assert <*36133> (num2str ([2.1, 1e23, pi]),
+%!                 "2.1  9.999999999999999e+22      3.141592653589793")
 
 ## Test for extra rows generated from newlines in format
 %!assert <*44864> (rows (num2str (magic (3), "%3d %3d %3d\n")), 3)