For some reason, 2 ^ 52 is 2 ^ 52 + 1 in Matlab, but why? And how can I fix this? For more information, please run the code below and check the results.
Here are the outputs (with vpa):
>> format long
>> digits(500)
>> vpa(2^52)
ans =
4503599627370496.0
>> vpa(2^52+1)
ans =
4503599627370496.0
>> isequal(vpa(2^52), vpa(2^52+1))
ans =
1
>> vpa(2^52+1)
ans =
4503599627370496.0
>> ans+1
ans =
4503599627370497.0
>> vpa(2^52+1000)
ans =
4503599627371496.0
https://ibb.co/iDDAwF
(outputs without vpa)
>> 2^52
ans =
4.503599627370496e+015
>> 2^52+1
ans =
4.503599627370497e+015
>> isequal(2^52, 2^52+1)
ans =
0
>> 2^52+1
ans =
4.503599627370497e+015
>> ans+1
ans =
4.503599627370498e+015
>> 2^52+1000
ans =
4.503599627371496e+015
Edit: this is not a duplicate and has nothing to do with floating point errors.
source
share