That has to do with how float numbers are calculated and stored in binary. If you did if ((3/10 - 2/10) == 1/10)
you would actually get false
because the value the computer is getting is 3.0000000001
.
That happens because 1/10
can't be written in as a decimal binary. It's something like 0.0011
where the 0011
bit repeats forever.
It's similar to how 1/3
can't be rewitten as a decimal in our number system. We normally do 0.333333334
. This causes a lot of weird and annoying problems.
If you want more accurate numbers, use decimal
. The decimal numbers are actually stored in base ten, not base 2, so that sort of stuff doesn't happen. Note that these are slower, however.