It suffices taking the log of the absolute value.
This commit is contained in:
Enrico Forestieri 2015-06-26 22:49:17 +02:00
parent c9d9309c1e
commit 67c52ae773

View File

@ -1408,10 +1408,7 @@ std::string formatFPNumber(double x)
// Prevent outputs of 23.4200000000000017 but output small numbers
// with at least 6 significant digits.
bool const neg = x < 0;
// Treat all doubles as positive for the formatting
if (neg)
x = -x;
double const logarithm = log10(x);
double const logarithm = log10(abs(x));
os << std::setprecision(max(6 - static_cast<int>(round(logarithm)), 0)) << x;
string result = os.str();
if (result.find('.') != string::npos) {
@ -1419,9 +1416,6 @@ std::string formatFPNumber(double x)
if (result[result.length()-1] == '.')
result = rtrim(result, ".");
}
// Re-add negativity
if (neg)
result = "-" + result;
return result;
}