Computer Arithematic

2 views (last 30 days)
Ashish Bhatt
Ashish Bhatt on 29 Jan 2012
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?

Accepted Answer

James Tursa
James Tursa on 29 Jan 2012
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:
  2 Comments
Ashish Bhatt
Ashish Bhatt on 29 Jan 2012
Thank you. You said closed number is picked. Then why is num2strexact(1+0.5*10^-15) not equal to either of the nearest numbers (num2strexact((1+0.5*10^-15)+eps(1+0.5*10^-15)) or num2strexact((1+0.5*10^-15)-eps(1+0.5*10^-15))) in your reply above?
James Tursa
James Tursa on 29 Jan 2012
The three numbers in the IEEE floating point model nearest 1+0.5*10^-15 in order are:
1.0000000000000002220446049250313080847263336181640625
1.000000000000000444089209850062616169452667236328125
1.0000000000000006661338147750939242541790008544921875
The one that is closest to 1+0.5*10^-15 is the middle one in the above list. All of these are shown in my post.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!