Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

matlab

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.

like image 524
Kitiara Avatar asked Mar 09 '23 19:03

Kitiara


1 Answers

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)
like image 174
Mikhail Avatar answered Mar 24 '23 18:03

Mikhail