changeset 4305:98e65d1728a1

[project @ 2003-01-23 16:48:11 by jwe]
author jwe
date Thu, 23 Jan 2003 16:48:11 +0000
parents fd7d9a6e15ff
children 6d3df3900252
files scripts/ChangeLog scripts/general/int2str.m src/ChangeLog src/oct-stream.cc
diffstat 4 files changed, 78 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Thu Jan 23 05:22:57 2003 +0000
+++ b/scripts/ChangeLog	Thu Jan 23 16:48:11 2003 +0000
@@ -1,3 +1,7 @@
+2003-01-23  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* general/int2str.m: Eliminate leading spaces.
+
 2003-01-22  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* general/int2str.m: Do a better job with 0, Inf, and NaN, 
--- a/scripts/general/int2str.m	Thu Jan 23 05:22:57 2003 +0000
+++ b/scripts/general/int2str.m	Thu Jan 23 16:48:11 2003 +0000
@@ -31,39 +31,57 @@
 
 function retval = int2str (x)
 
-  ## XXX FIXME XXX -- this will fail for very large values.
-
   if (nargin == 1)
     x = round (x);
-    t = abs (x(:));
-    t = t(t != 0);
-    if (isempty (t))
-      ## All zeros.
-      fmt = "%3d";
+    nc = columns (x);
+    if (nc > 1)
+      ifmt = get_fmt (x(:,1), 0);
+      rfmt = get_fmt (x(:,2:end), 2);
+      fmt = strcat (ifmt, repmat (rfmt, 1, nc-1), "\n")
     else
-      ## Maybe have some zeros.
-      nan_inf = isinf (t) | isnan (t);
-      if (any (nan_inf))
-	min_fw = 5;
-      else
-	min_fw = 3;
-      endif
-      t = t(! nan_inf);
-      if (isempty (t))
-	## Only zeros, Inf, and NaN.
-	fmt = "%5d";
-      else
-	## Could have anything.
-	fw = max (floor (max (log10 (t) + 3)), min_fw);
-	fmt = sprintf ("%%%dd", fw);
-      endif
+      fmt = strcat (get_fmt (x, 0), "\n");
     endif
-    fmt = strcat (repmat (fmt, 1, columns (x)), "\n");
     tmp = sprintf (fmt, round (x.'));
-    tmp(length (tmp)) = "";
+    tmp(end) = "";
     retval = split (tmp, "\n");
   else
     usage ("int2str (x)");
   endif
 
 endfunction
+
+function fmt = get_fmt (x, sep)
+
+  t = x(:);
+  t = t(t != 0);
+  if (isempty (t))
+    ## All zeros.
+    fmt = sprintf ("%%%dd", 1 + sep);
+  else
+    ## Maybe have some zeros.
+    nan_inf = isinf (t) | isnan (t);
+    if (any (nan_inf))
+      if (any (t(nan_inf) < 0))
+	min_fw = 4 + sep;
+      else
+	min_fw = 3 + sep;
+      endif
+    else
+      min_fw = 1 + sep;
+    endif
+    t = t(! nan_inf);
+    if (isempty (t))
+      ## Only zeros, Inf, and NaN.
+      fmt = sprintf ("%%%dd", min_fw);
+    else
+      ## Could have anything.
+      tfw = floor (log10 (abs (t))) + 1 + sep;
+      fw = max (tfw)
+      if (any (t(tfw == fw) < 0))
+	fw++;
+      endif
+      fmt = sprintf ("%%%dd", max (fw, min_fw));
+    endif
+  endif
+
+endfunction
\ No newline at end of file
--- a/src/ChangeLog	Thu Jan 23 05:22:57 2003 +0000
+++ b/src/ChangeLog	Thu Jan 23 16:48:11 2003 +0000
@@ -1,3 +1,9 @@
+2003-01-23  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* oct-stream.cc (octave_base_stream::do_printf): Handle values
+	outside the range of integers in int conversions for
+	compatibilitiy wtih Matlab.
+
 2003-01-22  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* load-save.cc (get_mat_data_input_line): Handle lines with CR
--- a/src/oct-stream.cc	Thu Jan 23 05:22:57 2003 +0000
+++ b/src/oct-stream.cc	Thu Jan 23 16:48:11 2003 +0000
@@ -2299,23 +2299,34 @@
 		  if (val_cache)
 		    {
 		      if ((xisnan (val) || xisinf (val)
-			   && (elt->type == 'd'
-			       || elt->type == 'i'
-			       || elt->type == 'c'
-			       || elt->type == 'o'
-			       || elt->type == 'x'
-			       || elt->type == 'X'
-			       || elt->type == 'u')))
+			   || val > INT_MAX || val < INT_MIN)
+			  && (elt->type == 'd'
+			      || elt->type == 'i'
+			      || elt->type == 'c'
+			      || elt->type == 'o'
+			      || elt->type == 'x'
+			      || elt->type == 'X'
+			      || elt->type == 'u'))
 			{
 			  std::string tfmt = fmt;
 
-			  tfmt.replace (tfmt.rfind (elt->type), 1, 1, 's');
-
-			  const char *tval = xisinf (val)
-			    ? (val < 0 ? "-Inf" : "Inf") : "NaN";
-
-			  retval += do_printf_conv (os, tfmt.c_str (),
-						    nsa, sa_1, sa_2, tval);
+			  if (xisnan (val) || xisinf (val))
+			    {
+			      tfmt.replace (tfmt.rfind (elt->type), 1, 1, 's');
+
+			      const char *tval = xisinf (val)
+				? (val < 0 ? "-Inf" : "Inf") : "NaN";
+
+			      retval += do_printf_conv (os, tfmt.c_str (),
+							nsa, sa_1, sa_2, tval);
+			    }
+			  else
+			    {
+			      tfmt.replace (tfmt.rfind (elt->type), 1, ".f");
+
+			      retval += do_printf_conv (os, tfmt.c_str (),
+							nsa, sa_1, sa_2, val);
+			    }
 			}
 		      else
 			{