For some reason 2^52 is equal to 2^52+1 in Matlab, but why ? And how can i fix this ? For more info please run the code below and check the results.
Here it is 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
(the 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 isn't a duplicate and has nothing to do with floating point errors.
vpa_item=vpa('2^52');
vpa_item2=vpa('1+2^52');
disp(isequal(vpa_item, vpa_item2));
results in 0
You can use "symbolic expressions" to bypass limitations of finite or floating point arithmetic on the input end.
--edit--
The linked page says
vpa(1+sym(2)^52)
is the paradigmatic expression, although both methods should work.
What will not work (in general) is
value_affected_by_imprecise_arithemetic = 1+2^52;
vpa(value_affected_by_imprecise_arithemetic)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With