about summary refs log tree commit diff stats
path: root/010vm.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-09-14 23:30:03 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-09-14 23:37:12 -0700
commit4082acd24f0049604f840fb5e60977e247aafbdf (patch)
treeee0e15149954af80c20f010c47b2f412961ee199 /010vm.cc
parente28fa5f1508ef7ffeb0b72406558534928c28f9e (diff)
downloadmu-4082acd24f0049604f840fb5e60977e247aafbdf.tar.gz
2199 - stop printing numbers in scientific notation
Turns out the default format for printing floating point numbers is
neither 'scientific' nor 'fixed' even though those are the only two
options offered. Reading the C++ standard I found out that the default
(modulo locale changes) is basically the same as the printf "%g" format.
And "%g" is basically the shorter of:
  a) %f with trailing zeros trimmed
  b) %e
So we'll just do %f and trim trailing zeros.
Diffstat (limited to '010vm.cc')
-rw-r--r--010vm.cc48
1 files changed, 47 insertions, 1 deletions
diff --git a/010vm.cc b/010vm.cc
index c7ac4d0a..5e375409 100644
--- a/010vm.cc
+++ b/010vm.cc
@@ -287,9 +287,55 @@ vector<string> property(const reagent& r, const string& name) {
 
 void dump_memory() {
   for (map<long long int, double>::iterator p = Memory.begin(); p != Memory.end(); ++p) {
-    cout << p->first << ": " << p->second << '\n';
+    cout << p->first << ": " << no_scientific(p->second) << '\n';
   }
 }
+
+:(before "End Types")
+struct no_scientific {
+  double x;
+  explicit no_scientific(double y) :x(y) {}
+};
+
+:(code)
+ostream& operator<<(ostream& os, no_scientific x) {
+  if (!isfinite(x.x)) {
+    // Infinity or NaN
+    os << x.x;
+    return os;
+  }
+  ostringstream tmp;
+  tmp << std::fixed << x.x;
+  os << trim_floating_point(tmp.str());
+  return os;
+}
+
+string trim_floating_point(const string& in) {
+  long long int len = SIZE(in);
+  while (len > 1) {
+    if (in.at(len-1) != '0') break;
+    --len;
+  }
+  if (in.at(len-1) == '.') --len;
+//?   cerr << in << ": " << in.substr(0, len) << '\n';
+  return in.substr(0, len);
+}
+
+void test_trim_floating_point() {
+  CHECK_EQ("0", trim_floating_point("000000000"));
+  CHECK_EQ("23.000001", trim_floating_point("23.000001"));
+  CHECK_EQ("23", trim_floating_point("23.000000"));
+  CHECK_EQ("23", trim_floating_point("23.0"));
+  CHECK_EQ("23", trim_floating_point("23."));
+  CHECK_EQ("23", trim_floating_point("23"));
+  CHECK_EQ("3", trim_floating_point("3.000000"));
+  CHECK_EQ("3", trim_floating_point("3.0"));
+  CHECK_EQ("3", trim_floating_point("3."));
+  CHECK_EQ("3", trim_floating_point("3"));
+  CHECK_EQ("1.5", trim_floating_point("1.5000"));
+}
+
 :(before "End Includes")
 #include<utility>
 using std::pair;
+#include<math.h>