Why is 2 ^ 52 equal to 2 ^ 52 + 1 in Matlab? And how to fix it?

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.

+4
source share
1 answer
vpa_item=vpa('2^52');
vpa_item2=vpa('1+2^52');
disp(isequal(vpa_item, vpa_item2));

leads to 0

You can use "symbolic expressions" to bypass the limitations of arithmetic with a trailing or floating point at the input end.

- change -

vpa(1+sym(2)^52) , .

( )

value_affected_by_imprecise_arithemetic = 1+2^52;
vpa(value_affected_by_imprecise_arithemetic)
+7

Source: https://habr.com/ru/post/1670497/


All Articles