News for hackers. Stuff that matters.
Re:0.30000000000000004 by jacobolus on Sunday April 02, 2017 @03:56PM (#14018718)
Python does this, for example (but likely in a more efficient way): http://bugs.python.org/issue1580

See also http://www.netlib.org/fp/ http://web.archive.org/web/20060908072403/http://ftp.ccs.neu...

So for instance 0.29999999999999993 + 0.00000000000000003 -> 0.3, but 0.30000000000000002 -> 0.30000000000000004

Note that this will still not solve the 0.1 + 0.2 problem from the OP, since the nearest float to 0.3 is not actually the same as the nearest float to 0.1 + 0.2.

Re:0.30000000000000004 by amelius on Sunday April 02, 2017 @04:42PM (#14018939)
> Note that this will still not solve the 0.1 + 0.2 problem from the OP, since the nearest float to 0.3 is not actually the same as the nearest float to 0.1 + 0.2.

Interval arithmetic can be used, although that requires double the storage for all the intermediate results, and extra work.

For example, .1 can be represented by x=[x0, x1], where x0 and x1 are floating point values bracketing .1, and similarly .2 can be represented by y=[y0, y1].

Then z=[z0, z1] can be computed by "z=x+y", where the interval z respects the sum of both intervals x and y.

When printing, you can determine the smallest representation within the interval z.

I'm not sure if it's worth all the trouble though :)

Re:0.30000000000000004 by kccqzy on Sunday April 02, 2017 @10:36PM (#14020238)
Interval arithmetic is far from trivial. 1/(1/x+1/y) is not the same as xy/(x+y). It breaks things more than normal floating point math does.
Re:0.30000000000000004 by danbruc on Sunday April 02, 2017 @06:02PM (#14019289)
This unfortunately breaks with something like [+1, +1] / [-1, +1] which yields (-∞, -1] and [+1, +∞), the result falls into two separate intervals. Now you can either turn this into (-∞, +∞) or use sets of intervals instead of single intervals. The first option gives pretty useless results, the second one can become really slow and consume a lot of memory. You can make this work, but you have to be really careful what you are calculating and how you are doing it.
Re:0.30000000000000004 by daurnimator on Sunday April 02, 2017 @08:41PM (#14019892)
> The first option gives pretty useless results

Does it though? Wouldn't a result of `(-∞, +∞)` be a solid indication to go and reduce the range of your input parameters?

Re:0.30000000000000004 by sametmax on Monday April 03, 2017 @03:17AM (#14021122)
But Python also warns about it in the documentation, and provide the factions (https://docs.python.org/3.1/library/fractions.html) module and the decimal module thttps://docs.python.org/3.1/library/decimal.html) in the stdlib to avoid the problem.
Re:0.30000000000000004 by int_19h on Monday April 03, 2017 @05:26PM (#14027316)
One thing I really wish Python had is decimal literals. I mean, it does have imaginary literals, which is arguably more of a corner case...
Re:0.30000000000000004 by FabHK on Monday April 03, 2017 @01:44PM (#14025137)
> the nearest float to 0.3 is not actually the same as the nearest float to 0.1 + 0.2.

Or even more precisely, the nearest float to 0.3 is not actually the same as the nearest float to (the nearest float to 0.1) + (the nearest float to 0.2).