2 ms·
Do it in Python and many other languages you'll get the same result. >>> 0.1 + 0.2 0.30000000000000004 That's the expected behavior of floating-point
by 29athrowaway 6y ago
Do it in Python and many other languages you'll get the same result.
>>> 0.1 + 0.2
0.30000000000000004
That's the expected behavior of floating-point numbers, more specifically, IEEE 754.
If you don't want this to happen, use fixed-point numbers, if they're supported by your language, or integers with a shifted decimal point.
Personally, I think if you don't know this, it's not safe for you to write computer programs professionally, because this can have real consequences when dealing with currency.