Why we can’t have 0.2
Floating points often get made fun of because they can’t compute accurately. This often gets explained as those numbers being inexpressible in a binary format.
We can define a floating point in a binary system with precision as the set , where is the mantissa and is the exponent, given natural numbers () (where starts at ) and integral numbers () :
Lets represent an annoying value such as . We need to find an and . Since it’s smaller than , we know that . Additionally we derive the domain of the -term:
Such that:
To find , first find a power-of-two multiplier that gets it into the range of :
For we get:
To solve , we look at the fractional part:
The only way to make is to multiply by a multiple of (i.e. ) which is impossible since there will be no (a prime) in the factorization of (power of prime), see fundamental theorem of arithmetic.