mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
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:
parent
4375aa150f
commit
a785212e80
4 changed files with 13 additions and 6 deletions
|
@ -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");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue