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");
}

View file

@ -8,12 +8,12 @@
1.0 day 10.0 hrs 17.0 mins 36.0 secs 789.0 msecs 123.449984 usecs
-1.0 day -10.0 hrs -17.0 mins -36.0 secs -789.0 msecs -123.449984 usecs
1.001 usecs
1.0 msec 1 usec
1.0 msec 1.0 usec
11.0 msecs
8.0 days 12.0 hrs
7.0 hrs 30.0 mins
6.0 mins 30.0 secs
5.0 secs 500.0 msecs
4.0 msecs 500 usecs
4.0 msecs 500.0 usecs
3.5 usecs
2.0 days 2.0 secs

View file

@ -22,8 +22,8 @@
178999999999999996376899522972626047077637637819240219954027593177370961667659291027329061638406108931437333529420935752785895444161234074984843178962619172326295244262722141766382622299223626438470088150218987997954747866198184686628013966119769261150988554952970462018533787926725176560021258785656871583744.0
-178999999999999996376899522972626047077637637819240219954027593177370961667659291027329061638406108931437333529420935752785895444161234074984843178962619172326295244262722141766382622299223626438470088150218987997954747866198184686628013966119769261150988554952970462018533787926725176560021258785656871583744.0
0.000012
0
-0
0.0
-0.0
inf
-inf
0.0

View file

@ -1 +1 @@
da47ae786562da18910d994b7868530929db6271
96e78328a1a9e6c07e725b282c293728f70737f0