GH-1244: Change modp_dtoa2() to use scientific notation for small values

This fixes problems where printing floating point numbers less than
10^-6 output as "0.0".  Such numbers now use using scientific notation
and preserve the value's actual floating point representation.
This commit is contained in:
Jon Siwek 2021-05-17 17:40:24 -07:00
parent f66b4f5340
commit 48ee0f31a1
4 changed files with 81 additions and 0 deletions

View file

@ -23,6 +23,8 @@
*/
static const double _pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000,
10000000, 100000000, 1000000000};
static const double _pow10r[] = {1, .1, .01, .001, .0001, .00001, .000001,
.0000001, .00000001, .000000001};
static void strreverse(char* begin, char* end)
{
@ -287,6 +289,14 @@ void modp_dtoa2(double value, char* str, int prec)
prec = 9;
}
double smallest = _pow10r[prec];
if (value != 0.0 && value < smallest) {
sprintf(str, "%.*e", DBL_DECIMAL_DIG - 1, neg ? -value : value);
sn_strip_trailing_zeros(str);
return;
}
int whole = (int) value;
double tmp = (value - whole) * _pow10[prec];
uint32_t frac = (uint32_t)(tmp);

View file

@ -91,6 +91,10 @@ void modp_dtoa(double value, char* buf, int precision);
* will be switched exponential format and include as many precision digits
* as needed to preserve information.
*
* If a non-zero input value is less than 10^(-precision), the output format
* will be switched exponential format and include as many precision digits
* as needed to preserve information.
*
* \param[in] value
* \param[out] buf The allocated output buffer. Should be 32 chars or more.
* \param[in] precision Number of digits to the right of the decimal point.