There have been several questions posted to SO about floating-point representation. For example, the decimal number 0.1 doesn\'t have an exact binary representation, so it\'
For example, the number 61.0 has an exact binary representation because the integral portion of any number is always exact. But the number 6.10 is not exact. All I did was move the decimal one place and suddenly I've gone from Exactopia to Inexactville. Mathematically, there should be no intrinsic difference between the two numbers -- they're just numbers.
Let's step away for a moment from the particulars of bases 10 and 2. Let's ask - in base b
, what numbers have terminating representations, and what numbers don't? A moment's thought tells us that a number x
has a terminating b
-representation if and only if there exists an integer n
such that x b^n
is an integer.
So, for example, x = 11/500
has a terminating 10-representation, because we can pick n = 3
and then x b^n = 22
, an integer. However x = 1/3
does not, because whatever n
we pick we will not be able to get rid of the 3.
This second example prompts us to think about factors, and we can see that for any rational x = p/q
(assumed to be in lowest terms), we can answer the question by comparing the prime factorisations of b
and q
. If q
has any prime factors not in the prime factorisation of b
, we will never be able to find a suitable n
to get rid of these factors.
Thus for base 10, any p/q
where q
has prime factors other than 2 or 5 will not have a terminating representation.
So now going back to bases 10 and 2, we see that any rational with a terminating 10-representation will be of the form p/q
exactly when q
has only 2
s and 5
s in its prime factorisation; and that same number will have a terminating 2-representatiion exactly when q
has only 2
s in its prime factorisation.
But one of these cases is a subset of the other! Whenever
q
has only2
s in its prime factorisation
it obviously is also true that
q
has only2
s and5
s in its prime factorisation
or, put another way, whenever p/q
has a terminating 2-representation, p/q
has a terminating 10-representation. The converse however does not hold - whenever q
has a 5 in its prime factorisation, it will have a terminating 10-representation , but not a terminating 2-representation. This is the 0.1
example mentioned by other answers.
So there we have the answer to your question - because the prime factors of 2 are a subset of the prime factors of 10, all 2-terminating numbers are 10-terminating numbers, but not vice versa. It's not about 61 versus 6.1 - it's about 10 versus 2.
As a closing note, if by some quirk people used (say) base 17 but our computers used base 5, your intuition would never have been led astray by this - there would be no (non-zero, non-integer) numbers which terminated in both cases!