|
|
|
|
|
by 29athrowaway
1895 days ago
|
|
You 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. |
|
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.