diff src/ov-base-sparse.cc @ 11439:c2f44cba24c9

improve display of percentage full when printing sparse matrices
author John W. Eaton <jwe@octave.org>
date Tue, 04 Jan 2011 16:57:19 -0500
parents b4d2080b6df7
children fd0a3ac60b0e
line wrap: on
line diff
--- a/src/ov-base-sparse.cc	Tue Jan 04 14:09:16 2011 -0500
+++ b/src/ov-base-sparse.cc	Tue Jan 04 16:57:19 2011 -0500
@@ -321,7 +321,31 @@
   double dnel = matrix.numel ();
 
   if (dnel > 0)
-    os << " [" << std::setprecision (2) << (nz / dnel * 100) << "%]";
+    {
+      double pct = (nz / dnel * 100);
+
+      int prec = 2;
+
+      // Display at least 2 significant figures and up to 4 as we
+      // approach 100%.  Avoid having limited precision of the display
+      // result in reporting 100% for matrices that are not actually
+      // 100% full.
+
+      if (pct == 100)
+        prec = 3;
+      else
+        {
+          if (pct > 99.9)
+            prec = 4;
+          else if (pct > 99)
+            prec = 3;
+
+          if (pct > 99.99)
+            pct = 99.99;
+        }
+
+      os << " [" << std::setprecision (prec) << pct << "%]";
+    }
 
   os << ")\n";