Mercurial > hg > octave-lyh
diff scripts/general/num2str.m @ 15732:82b0ad43a939
num2str: Take into account inf inputs. Add tests for inf and NaN
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Wed, 05 Dec 2012 16:53:53 -0500 |
parents | 34c932e669c8 |
children | 576daea679fe |
line wrap: on
line diff
--- a/scripts/general/num2str.m +++ b/scripts/general/num2str.m @@ -83,8 +83,14 @@ endif else if (isnumeric (x)) - ## Setup a suitable format string - dgt = floor (log10 (max (abs (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; + endif + if (any (x(:) != fix (x(:)))) ## Floating point input dgt = max (dgt + 4, 5); # Keep 4 sig. figures after decimal point @@ -169,6 +175,8 @@ %!assert (num2str (-2^33), "-8589934592") %!assert (num2str (2^33+1i), "8589934592+1i") %!assert (num2str (-2^33+1i), "-8589934592+1i") +%!assert (num2str (inf), "Inf") +%!assert (num2str (nan), "NaN") ## FIXME: Integers greater than bitmax() should be masked to show just ## 16 digits of precision.