|
|
|
|
|
by crazygringo
1895 days ago
|
|
Well, 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. |
|
Fixed-point numbers (aka "decimal" type in Java, C# and others) are the preferred way to deal with this problem as I mentioned in this same discussion hours ago.
Now, in the example you presented, you have a round-off error amplication problem where the round-off error grows larger than the epsilon. You can avoid that using the Kahan summation algorithm.
https://en.wikipedia.org/wiki/Kahan_summation_algorithm
Now you evaluate sum, and it's 1.8 as expected.