Commit b4fdf6e9 by jpmag Committed by Dominic Hamon

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.
parent 34010bee
...@@ -45,6 +45,8 @@ void ToExponentAndMantissa(double val, double thresh, int precision, ...@@ -45,6 +45,8 @@ void ToExponentAndMantissa(double val, double thresh, int precision,
std::max(thresh, 1.0 / std::pow(10.0, precision)); std::max(thresh, 1.0 / std::pow(10.0, precision));
const double big_threshold = adjusted_threshold * one_k; const double big_threshold = adjusted_threshold * one_k;
const double small_threshold = adjusted_threshold; 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) { if (val > big_threshold) {
// Positive powers // Positive powers
...@@ -62,14 +64,16 @@ void ToExponentAndMantissa(double val, double thresh, int precision, ...@@ -62,14 +64,16 @@ void ToExponentAndMantissa(double val, double thresh, int precision,
*exponent = 0; *exponent = 0;
} else if (val < small_threshold) { } else if (val < small_threshold) {
// Negative powers // Negative powers
double scaled = val; if (val < simple_threshold) {
for (size_t i = 0; i < arraysize(kSmallSIUnits); ++i) { double scaled = val;
scaled *= one_k; for (size_t i = 0; i < arraysize(kSmallSIUnits); ++i) {
if (scaled >= small_threshold) { scaled *= one_k;
mantissa_stream << scaled; if (scaled >= small_threshold) {
*exponent = -static_cast<int64_t>(i + 1); mantissa_stream << scaled;
*mantissa = mantissa_stream.str(); *exponent = -static_cast<int64_t>(i + 1);
return; *mantissa = mantissa_stream.str();
return;
}
} }
} }
mantissa_stream << val; mantissa_stream << val;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment