The number 2.5 passes the test because 0.5 has an exact representation as a binary fraction. It is 2-1. In fact, every negative power of 2 has 5 as its last digit. Here are a few examples
2^-1 = 0.5000000000000000
2^-2 = 0.2500000000000000
2^-3 = 0.1250000000000000
2^-4 = 0.0625000000000000
2^-5 = 0.0312500000000000
2^-6 = 0.0156250000000000
2^-7 = 0.0078125000000000
2^-8 = 0.0039062500000000
2^-9 = 0.0019531250000000
2^-10 = 0.0009765625000000
2^-11 = 0.0004882812500000
Things to note:
- Most implementations of the C language express floating point
numbers as the sum of negative powers of 2 (the sum itself is then
multiplied by a power of 2).
- The pattern you see above will continue for all negative powers of
2 . That's because when the 50 at the end is divided by 2, it's always going to yield a number with a 5 at the end, and that 5 will
be one more decimal place to the right.
- When you sum any combination of the negative powers of 2, the
smallest number will always contribute the 5 at the end of the
number, and that 5 will not be summed with a non-zero digit from any
of the larger numbers.
So all numbers with an exact representation end with 5. However, that's not to say that every number that ends in 5 has an exact representation. For example 0.385 does not have an exact representation, and will fail the equality test.