I have tried two sentences on my computer :

2 + 10^(-18) == 2
2^(-55) + 2^(-57) == 2^(-55)

My computer gives TRUE and FALSE respectively. Why does the computer give two different results? Shouldn't the computer give the same output?

  • $\begingroup$ 10^(-18) is around 2^(-60) $\endgroup$
    – TEMLIB
    Mar 12 '16 at 13:29

Floating point numbers have limited accuracy. They are stored in the following way: $\pm 1.x_0\ldots x_{n-1} \times 2^y$ (here $x_0,\ldots,x_{n-1}$ are binary digits) for an appropriate $n$; the value of the exponent $y$ is also bounded. The value of $n$ and the bounds on $y$ depend on the exact type of the floating point number.

When you add $2$ and $10^{-18}$, the answer is rounded to $2$ since the contribution of $10^{-18}$ is too small to store in the number (i.e., $n$ is too small). When you add $2^{-55}$ and $2^{-57}$ there is no such problem: this is stored as $1.01 \times 2^{-55}$.

For more, check out one of the multitudinous resources on floating point numbers.

  • $\begingroup$ What is the standard of being 'too small'? $\endgroup$
    – Jin
    Mar 12 '16 at 12:05
  • $\begingroup$ It depends on the exact type of the floating point number. You can check the documentation of the standard (IEEE 754), or run an experiment. $\endgroup$ Mar 12 '16 at 12:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.