4 ms·
Floating point is intrinsically inaccurate. You can't use it to handle money. With floating point, the assumption (x*y)/y = x does simply not hold.
by wayn3 9y ago
Floating point is intrinsically inaccurate. You can't use it to handle money.
With floating point, the assumption (x*y)/y = x does simply not hold.
- Thiez 9y agoFloating point numbers can accurately represent integers, so if you have all your prices in cents, you end up with (x * 3) / 3, where each number in that calculation is an integer. No inaccuracy there. Of course there is no reason for a register to actually perform this division in a three-for-one action, it can just replace (x * 3) with x, or subtract (x * 2). I agree as much as the next person that you shouldn't represent money with floating point numbers, but I disagree that cash register software written in Javascript must automatically be incorrect (or more so that similar software in a different language). And I don't even like Javascript.
- wayn3 9y agoFloating point can accurately represent more than just integers. That does not mean that they can accurately perform mathematical operations on them. I'd assume that division is implemented as multiplying with the reciprocal (because thats faster). If that's correct, then any division by 3 (or any other number that is not of the form 2^i) breaks your cash register. Because 1/3 represented as a floating point number equals 0.33333333.. and so on but not indefinitely, 3 * 1/3 = 0.99999999.. - which would be equal to 1 if you were using real numbers. But you don't.