From 4082acd24f0049604f840fb5e60977e247aafbdf Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 14 Sep 2015 23:30:03 -0700 Subject: 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. --- 010vm.cc | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to '010vm.cc') diff --git a/010vm.cc b/010vm.cc index c7ac4d0a..5e375409 100644 --- a/010vm.cc +++ b/010vm.cc @@ -287,9 +287,55 @@ vector property(const reagent& r, const string& name) { void dump_memory() { for (map::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 using std::pair; +#include -- cgit 1.4.1-2-gfad0