Improve formatting of doubles that are close to integers

Now checks for approximate floating point equality so that more doubles
get properly disambiguated from integers
This commit is contained in:
Jon Siwek 2020-02-15 00:54:44 -08:00
parent 4375aa150f
commit a785212e80
4 changed files with 13 additions and 6 deletions

View file

@ -164,7 +164,14 @@ void ODesc::Add(double d, bool no_exp)
Add(tmp);
if ( nearbyint(d) == d && isfinite(d) && ! strchr(tmp, 'e') )
auto approx_equal = [](double a, double b, double tolerance = 1e-6) -> bool
{
auto v = a - b;
return v < 0 ? -v < tolerance : v < tolerance;
};
if ( approx_equal(d, nearbyint(d), 1e-9) &&
isfinite(d) && ! strchr(tmp, 'e') )
// disambiguate from integer
Add(".0");
}