# What makes a floating point number finite?

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?

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$.

To answer you bottom-line question metaphorically:

The reason why $\frac13$ and $\frac16$ require infinitely many digits after the point to be represented in binary, is the same reason for which you cannot be $\frac13$ Spanish or $\frac16$ German - you have exactly $2$ parents (and each one of them has exactly $2$ parents, and so on).

No matter how you choose your family tree, you will never be able to reach full accuracy...

• I know I asked this question over two years ago, but this is just a beautiful analogy. – so.very.tired Sep 12 '16 at 17:25
• @so.very.tired: Haha, thanks. I noticed that it was an old question only after I had answered it. I guess that someone has answered it, and then it popped up in the question list as a result... – barak manos Sep 12 '16 at 18:51

Only rational numbers having integer powers of 2 and 5, and their products in the denominator give finite floating point. So I believe 2, 5 are the special numbers.

Actually only 2 is special. Since reciprocals of powers of 5 give powers of 0.2 which is 2/ base of number system .