Quote:Original post by prowst
Quote:
Nothing is 'exactly' in floating point math. If you want to write 0.9 exacly in FP mathy you need 'exactly' infinite digits
I dont understand why this is the case for rational numbers.
If it was an irrational number, then I see why you would need
"exactly infinite digits".
- prowst
Rational means that a number can be expressed as the ratio of two whole numbers, but that's simply not how computers store numbers. Rational numbers can also have infinitely-repeating digits in decimal (e.g. 1/3; rational but takes an infinite number of digits in base-10 to express).
.9 can be represented as a single-digit number in a decimal numbering system where each digit is the coefficient of the place:
...00.900...
= ...0*10^1 + 0*10^0 + 9*10^-1 + 0*10^-2 + 0*10^-3...
= ...0*10 + 0*1 + 9*.1 + 0*.01 + 0*.001...
You can represent any number in decimal without repeating digits if it can be expressed a sum of powers of 10.
Since computers store numbers in binary, you're only going to have a finite number of non-zero digits if it exactly fits with the binary coefficients:
...ab.cd...
= ...a*2^1 + b*2^0 + c*2^-1 + d*2^-2...
= ...a*2 + b*1 + c*.5 + d*.25
Where the coefficients are either 0 or 1. Try working out .9 and you'll find you go on forever.
In summation, you can only represent a number in floating point exactly if it can be represented as a sum of powers of two. .9 is not such a number, but all whole numbers are.