6 ms·
Indeed, and therefore: 0.1 + 0.2 != 0.3 You can check it in the JavaScript console. This actually makes me wonder if anyone's ever attempted a floating-poi
by crazygringo 6y ago
Indeed, and therefore:
0.1 + 0.2 != 0.3
You can check it in the JavaScript console.
This actually makes me wonder if anyone's ever attempted a floating-point representation that builds in an error range, and correctly propagated/amplified error over operations.
E.g. a simple operation like "1 / 10" (to generate 0.1) would be stored not as a single floating-point value, but really as the range between the closest representation greater than and less than it. The same with "2 / 10", and then when asking if 0.1 + 0.2 == 0.3, it would find an overlap in ranges between the left-hand and right-hand sides and return true. Every floating-point operation would then take and return these ranges.
Then floating point arithmetic could be used to actually reliably test equality without ever generating false negatives. And if you examined the result of calculation of 10,000 operations, you'd also be able to get a sense of how off it might maximally be.
I've search online and can't find anything like it, though maybe I'm missing an important keyword.
- AnimalMuppet 6y agoBut what you would actually get is something like this: x---0.1 + 0.2 ---x x---0.3---x That is, the range of 0.1 + 0.2 would be wider than the range of 0.3. And now what do you do? There is overlap, so are they equal? But there are parts that don't overlap, so are they different?
- Nullabillity 6y agoMake equality checks illegal, and instead define specific operations for contains and overlaps.
- crazygringo 6y agoWell right now you basically can't ever check for equality with floating-point arithmetic and trust that two numbers that should intuitively be equal are reported as equal. For me, floating-point equality would be if there are any parts that overlap. Basically "=" would mean "to the extent of the floating-point accuracy of this system, these values could be equal". If you're doing a reasonably limited number of operations with values reasonably larger than the error range, then it would meet a lot of purposes -- you can add 0.5 somewhere in your code, subtract 0.5 elsewhere, and still rely on the value being equal to the original.
- mvanaltvorst 6y agoThat sounds interesting, but I would imagine it would become very complicated once you start applying nontrivial functions (discontinuous functions, for example). In that case the range of possible values could actually become discontinuous. I would imagine accounting for that is actually more computationally expensive than just using arbitrary precision decimals.
- OskarS 6y agoYeah, you call tan() on that number, and suddenly your interval is like most of the number line. Actually, you don't even have to be that fancy: if the number is close to epsilon, the error bars on 1/x would be huge.
- crazygringo 6y agoBut isn't that a feature, rather than a bug? It prevents you from getting "false accuracy".
- klyrs 6y agoYes, that's a feature. If you're using interval arithmetic and your result is an unreasonably large interval, then there's a good chance the algorithm in use is numerically unstable.
- yarg 6y agoSure, but what's the use case for mathematics where you don't know what side of an asymptote you're on?
- deleted 6y ago[deleted]
- thaumasiotes 6y ago>> if the number is close to epsilon, the error bars on 1/x would be huge. > Sure, but what's the use case for mathematics where you don't know what side of an asymptote you're on? Knowing which side of the asymptote you're on does not solve this problem or even ameliorate it.
- drsopp 6y agoPerhaps something like https://pythonhosted.org/mcerp/index.html https://pythonhosted.org/mcerp/index.html
- AaronFriel 6y agoInterval arithmetic is what you're looking for, and there's an IEEE standard and many implementations.
- crazygringo 6y agoThank you!! Yes, that turns out to be exactly it [1]. Looks like there's even at least one JavaScript library for it [2]. It seems like such a useful and intuitive idea I have to wonder why it isn't a primitive in any of the common programming languages. [1] https://en.wikipedia.org/wiki/Interval_arithmetic https://en.wikipedia.org/wiki/Interval_arithmetic [2] https://github.com/mauriciopoppe/interval-arithmetic https://github.com/mauriciopoppe/interval-arithmetic
- gugagore 6y agoInterval arithmetic certainly has its place. However, you don't find it used more often because a naive implementation results in intervals that are often uselessly huge. Consider x in [-1, 1], and y in [-1, 1]. x*y is also in [-1,1], and x-y in [-2, 2]. But now consider actually that y=x. That's consistent, but our intervals could be smaller than what we've computed.
- crazygringo 6y agoSure, but wouldn't realistic intervals be more like x in [0.29999999999999996, 0.30000000000000004]? I mean, intervals as large as whole numbers might make sense if your calculations are dealing with values in the trillions and beyond... but isn't the point of interval arithmetic to deal with the usually tiny errors that occur in FP representation?
- enriquto 6y ago> It seems like such a useful and intuitive idea I have to wonder why it isn't a primitive in any of the common programming languages. It is basically useless for numerical computation when you perform iterations. Even good convergent algorithms can diverge with interval arithmetic. As you accumulate operations on the same numbers, their intervals become larger and larger, growing eventually to infinity. It has some applications, but they are quite niche.
- deleted 6y ago[deleted]
- 29athrowaway 6y agoYou cannot compare floating point numbers like that. The equality test in floating point numbers is comparing against the epsilon. Math.abs(0.3 - (0.1 + 0.2)) < Number.EPSILON Which is the same you other languages. Using the epsilon for comparison is not mentioned in the article. Floating point absorption is also not mentioned in the article. This entire discussion and the fact this is on the front page of HN is pretty disappointing and sad. Is this really a surprise for you? if it is... have you ever implemented any logic involving currency? You may want to take another look at it.
- neolog 6y ago> have you ever implemented any logic involving currency? You may want to take another look at it. Floating point arithmetic is good enough for science, should be good enough for commerce too, no? Why is commerce special?
- lamp987 6y agorounding error after multiple operations can be more than just epsilon
- 29athrowaway 6y agoThat's why there are algorithms to do those multiple operations.
- crazygringo 6y agoWell, that was trivially easy to disprove with a little jiggling of numbers in the console: Math.abs(1.8 - (0.1 + 0.2 + 0.9 + 0.6)) < Number.EPSILON returns false. Also, you generally really shouldn't be implementing any currency logic using floating point numbers, yikes. Stick to integers that represent the value in cents, or tenths of cents, or similar. Or, even better, a DECIMAL data type if your platform supports it. I genuinely hope you've never written financial software that judges if the results of two calculations are equal via the method you've described.
- 6y ago
- naniwaduni 6y ago> Then floating point arithmetic could be used to actually reliably test equality without ever generating false negatives. The flip side is that you generate plenty of false positives once your error ranges get large enough. This happens pretty readily if you e.g. perform iterations that are supposed to keep the numbers at roughly the same scale.