0

Using double precision, what is ((0.5 + 1e-19) - 0.5) * 1e19?

Should it be 0 or 1? Why?

EDIT: Matlab is giving me 0, but I was expecting 1. Why? Shouldn't 0.5 + 1e-19 produce a submornal number that is greater than 0?

> >> ((0.5 + 1e-19) - 0.5) * 1e19
> 
> ans =
> 
>      0
user1559897
  • 1,807

3 Answers3

2

In IEEE double precision there are no floating point numbers between $\frac{1}{2}$ and $\frac{1}{2} + u$ where $u = 2^{-53}$ is the unit roundoff in double precision. Therefore, $0.5 + 10^{-19}$ is rounded to $0.5$ regardless of the rounding mode. It follows that $((0.5 + 10^{-19}) - 0.5)\cdot10^{19}$ evaluates to zero.

Carl Christian
  • 12,583
  • 1
  • 14
  • 37
  • The bias for the exponent is -126. Why isn't the difference between two contiguous subnormal numbers $2^{-126} * 2^{-23}$? – user1559897 Oct 13 '19 at 02:26
  • 1
    @user1559897 The bias of single precision is 127 not 126. Regardless, 0.5 is deep inside the range of normal double precision numbers, which Matlab uses by default. – Carl Christian Oct 13 '19 at 11:59
  • you are right... so there is no way to represent 0.5 + 1e-19 using double precision? – user1559897 Oct 13 '19 at 17:08
  • 1
    @user1559897: It cannot be done using a single double precision number. There are however advanced algorithms that allow us to track the rounding errors using several words of memory. The key words are error free transformations. – Carl Christian Oct 14 '19 at 04:21
1

For doubles, there are 52 bits for the significand field. You have one bit for the sign, and the rest are the exponent. Therefore, when you add a small number to something on the order of $1$, you get a different value only if the small number is greater than $~2^{-53}\approx 1.1\cdot 10^{-16}$. This is known as Machine epsilon. So you would have gotten the sam eanswer with $10^{-17}$ as well.

Andrei
  • 37,370
  • The bias for the exponent is -126 and there are 23 bits of mantissa. Why isnt the difference between two contiguous subnormal number $2^{-126} * 2^{-23}$? – user1559897 Oct 13 '19 at 02:26
  • This has nothing to do with exponents. When you do addition, you represent them with the same exponent. Then you are limited by the number of bits for the significand field. See for example https://en.wikipedia.org/wiki/Floating-point_arithmetic#Addition_and_subtraction – Andrei Oct 13 '19 at 02:40
0

I am not sure about the specifics of the implementation of double precision here. But this looks more like an order of precedence issue, where, in the innermost set of parentheses, $0.5 + 1 \times 10^{-19}$ was first evaluated to $0.5$. The final output is therefore zero. I would infer that, despite double precision, the effect of adding numbers of highly differing magnitudes still results in one number being ignored in favour of the other.

Deepak
  • 26,801