[Math] What makes a floating point number finite

binaryfloating point

We started studying about the binary floating point system, and talked mainly about the limitations when trying to represent fractions on computer systems, and how mostly what we get when manipulating floating point in a computer, is approximations to the "real" decimal number.

That popped some question into my mind, about something that obviously I noticed before, but never occurred to me to ask.

Irrational numbers, in their decimal (or any other base for that matter) floating point representation, are infinite, or in other words, every number that cannot be expressed as a ratio of two integers, has infinitely many digits after the point in its decimal floating point representation.

But some numbers, such like the number $\frac{1}{6}$, or $\frac{1}{3}$, can be expressed as a ratio of two integers, but still have infinite decimal floating point representations. What makes them different from, e.g., $\frac{1}{2}$, which, in a binary system, can be finitely represented as $0.1$, but $\frac{1}{3}$ or $\frac{1}{6}$ cannot?

What so special about $\frac{1}{3}$ or $\frac{1}{6}$ (or any other number that isn't irrational) but that would require infinitely many digits after the point to be represented in binary?

Best Answer

As you pointed out, irrationals never have a terminating representation. So let's restrict our attention to rationals.

I believe that a rational number $p/q$ (in lowest terms) is representable as a terminating floating point expression in base $b$ if and only if each prime factor of $q$ is a divisor of $b$.

This is because if you write the fractional part of a number as $x=0.a_1a_2a_3\ldots$, then it terminates if there is integral $n$ with $x=0.a_1a_2a_3\ldots a_n$ in base $b$. That's the same as saying $b^n x=m\in \mathbb{Z}$, so you can write $$x=\frac{m}{b^n}$$ Supposing for simplicity that the original number is between $0$ and $1$ (so you only have to deal with the fractional part--that's okay because adding integers doesn't affect the part past the radix point), this means you can write the number as a fraction whose denominator is a power of $b$ (this form will probably not be in lowest terms).

That's another way to say it--there is a terminating base $b$ floating point representation of $x$ if and only if $x=m/b^n$ for some integers $m$ and $n$.

Related Question