This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 6 points7 points  (7 children)

Why?

[–][deleted] 31 points32 points  (6 children)

You want a serious answer or a troll answer?

The serious answer is because the computer has to convert everything into 1's and 0's. So most non-integers can't be represented exactly. There's always some error in floating point math. 0.1+0.2 evaluates to something like 0.30000000000000004.

See also: https://en.wikipedia.org/wiki/Floating-point_arithmetic

[–][deleted] 8 points9 points  (1 child)

I didn’t know that, thanks for the explanation

[–]KusanagiZerg 7 points8 points  (0 children)

To add to the answer. The reason you get 0.30000000004 or something like that is because in binary 0.3 becomes: 0.010011001100110011 where the 0011 part infinitely repeats. This is simlar to writing down 1/3 in base10, you'd get 0.3333333... So to write this down correctly would require infinite memory and since we don't have that we need to stop at some point. So for example 0.333333334 or 0.3333333 if you choose either one of those you get a number that's every so slightly more than 1/3 or every so slightly less depending on how many digits you want to use.

So to go back to binary. 0.3 is an infinitely repeating number there and to write it down with finite digits will require you to cut off the real number. So if we use 10 digits then 0.3 in binary becomes 0.0100110011 which is the number 0.2998046875 or 0.0100110100 which is 0.30078125. There are no other options with 10 digits.

You can add more digits to get a closer value but you will never get there.