MATLAB: Computer Arithematic

floating pointMATLABprecision

I want to subtract 1 from 1+0.5*10^-15. When I enter 1+0.5*10^-15 in MATLAB command prompt, I get 1.000000000000000e+000 and not 1.0000000000000005e+000. I understand it is because of the displaying limitation of format long e. But when I subtract 1 from this number (1.000000000000000e+000), I get 4.440892098500626e-016 which is very close to 5.000000000000000e-016 but not exactly equal to it. Now this is not a displaying limitation of format long e. Why is this happening?

Best Answer

The limitations of floating point arithmetic. IEEE double precision can't store your numbers exactly, so the closest number in the floating point model is picked. E.g., here are the exact decimal equivalents of your numbers:
>> num2strexact(0.5*10^-15)
ans =
5.00000000000000038852699938330539619153592800597507572746280857245437800884246826171875e-16
>> num2strexact(1+0.5*10^-15)
ans =
1.000000000000000444089209850062616169452667236328125
>> num2strexact((1+0.5*10^-15)-1)
ans =
4.44089209850062616169452667236328125e-16
For example, the nearest numbers in the IEEE double precision floating point model to the one picked for your 1+0.5*10^-15 are:
>> num2strexact((1+0.5*10^-15)+eps(1+0.5*10^-15))
ans =
1.0000000000000006661338147750939242541790008544921875
>> num2strexact((1+0.5*10^-15)-eps(1+0.5*10^-15))
ans =
1.0000000000000002220446049250313080847263336181640625
You can find the num2strexact utility on the FEX here: