# MATLABs double arithmetic

this is a classical problem, but I need help to pinpoint what I am missing.

Problem: In MATLAB

(exp(1) + 10^12) - 10^12

gives you a double which equal to e, up to 5 correct digits. But I thought it would be 4.

One has exp(1) + 10^12 = 1.000000000002718e+12, where 1.000000000002718 contains 16 digits, the precision we are working at. All that is left of e is 2.718, but (exp(1) + 10^12) - 10^12 = 2.7182 (I have left out the digits following the last decimal 2), meaning I have one more digit saved.

Question: Where or how is the extra digit stored? I know that the last the 53 bit for unsigned 64 bit IEEE is used for rounding, is it related to my question?

• Note that "[...] contains 16 digits, the precision we are working at" is incorrect. The number is stored in bits, and the decimal representation of those bits can require more than 16 digits. For example, 001 represents 0.25, so that 3 bits correspond to 3 decimals. – Jannis Teunissen Mar 30 '16 at 12:26
• As far as I know, there is no such thing as an "unsigned 64 bit IEEE". Floating point numbers are always signed, in IEEE standard formats. – Federico Poloni Dec 17 '17 at 22:23

You can use dec2bin(typecast((exp(1) + 1e12) - 1e12, 'uint64')) to obtain the binary representation of the double precision floating point.

100000000000101101111110000101010001011000101000101100000000000 100001001101101000110101001010010100010000000000101100000000000 100000000000101101111110000000000000000000000000000000000000000 100000000000101101111100111011011001000101101000011100000000000 100000000000101101111101101111110100100001111111110010000000000

are the binary representations of

• e
• e + 1e12
• (e + 1e12) - 1e12
• 2.718
• 2.7182

Firstly, I can't guarantee it, but I suspect that matlab will return a signed double rather than an unsigned, so you've probably only got 52 bits to play with.

However to actually address the question, consider a clock that has stopped with the hour hand on 1 o'clock. It has a terrible precision, but if you look at it at 12.30, it's accurate to within half an hour. Similarly, the $(1/2)^{52}$ step in the representation of doubles is only useful to tell us the maximum truncation error in the representation of mathematics, the actual accuracy can be far better.

As a concrete example of something closer to the bound, compare your example with the result of $(\pi+10^{12})-10^{12}$.