From b4fdf6e9df82ea5a424440436dcc4ac0474decce Mon Sep 17 00:00:00 2001
From: jpmag <biojppm@users.noreply.github.com>
Date: Fri, 13 Jan 2017 22:19:42 +0000
Subject: [PATCH] HumanReadableNumber(): Simplify output for simple numbers.
 Examples: (#291)

* HumanReadableNumber(): Simplify output for simple numbers. Examples:

HumanReadableNumber(    0.0)=      0    ---->         0
HumanReadableNumber(    0.5)=   512m    ---->       0.5
HumanReadableNumber(    0.9)= 921.6m    ---->       0.9
HumanReadableNumber(    1.0)=  1024m    ---->         1
HumanReadableNumber(   1.05)=1075.2m    ---->      1.05
HumanReadableNumber(    1.1)=    1.1    ---->       1.1
HumanReadableNumber(    1.2)=    1.2    ---->       1.2
HumanReadableNumber( 0.0e-1)=      0    ---->         0
HumanReadableNumber( 0.5e-1)=  51.2m    ---->      0.05
HumanReadableNumber( 0.9e-1)= 92.16m    ---->      0.09
HumanReadableNumber( 1.0e-1)= 102.4m    ---->       0.1
HumanReadableNumber(1.05e-1)=107.52m    ---->     0.105
HumanReadableNumber( 1.1e-1)=112.64m    ---->      0.11
HumanReadableNumber( 1.2e-1)=122.88m    ---->      0.12
HumanReadableNumber( 0.0e-3)=      0    ---->         0
HumanReadableNumber( 0.5e-3)=524.288u   ---->   524.288u
HumanReadableNumber( 0.9e-3)=943.718u   ---->   943.718u
HumanReadableNumber( 1.0e-3)=1048.58u   ---->   1048.58u
HumanReadableNumber(1.05e-3)=  1101u    ---->   0.00105
HumanReadableNumber( 1.1e-3)=1.1264m    ---->    0.0011
HumanReadableNumber( 1.2e-3)=1.2288m    ---->    0.0012

* HumanReadableNumber(): change simple printing threshold to 0.01.

* ToExponentAndMantissa(): refactor branch sequence.
---
 src/string_util.cc | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/string_util.cc b/src/string_util.cc
index 4cefbfba..cd4e7cfd 100644
--- a/src/string_util.cc
+++ b/src/string_util.cc
@@ -45,6 +45,8 @@ void ToExponentAndMantissa(double val, double thresh, int precision,
       std::max(thresh, 1.0 / std::pow(10.0, precision));
   const double big_threshold = adjusted_threshold * one_k;
   const double small_threshold = adjusted_threshold;
+  // Values in ]simple_threshold,small_threshold[ will be printed as-is
+  const double simple_threshold = 0.01;
 
   if (val > big_threshold) {
     // Positive powers
@@ -62,14 +64,16 @@ void ToExponentAndMantissa(double val, double thresh, int precision,
     *exponent = 0;
   } else if (val < small_threshold) {
     // Negative powers
-    double scaled = val;
-    for (size_t i = 0; i < arraysize(kSmallSIUnits); ++i) {
-      scaled *= one_k;
-      if (scaled >= small_threshold) {
-        mantissa_stream << scaled;
-        *exponent = -static_cast<int64_t>(i + 1);
-        *mantissa = mantissa_stream.str();
-        return;
+    if (val < simple_threshold) {
+      double scaled = val;
+      for (size_t i = 0; i < arraysize(kSmallSIUnits); ++i) {
+        scaled *= one_k;
+        if (scaled >= small_threshold) {
+          mantissa_stream << scaled;
+          *exponent = -static_cast<int64_t>(i + 1);
+          *mantissa = mantissa_stream.str();
+          return;
+        }
       }
     }
     mantissa_stream << val;