Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Matlab Mod different from Wolfram Alpha

688^79 mod 3337 = 1570.

When I tried this at wolfram alpha I got: enter image description here

but When I entered the same thing in Matlab, I get 364 as the answer. I got to be doing something wrong.

enter image description here

Any light on this will be appreciated.

like image 560
Juan Zamora Avatar asked Dec 08 '22 03:12

Juan Zamora


1 Answers

The reason is that Matlab uses double floating-point arithmetic by default. A number as large as 688^79 can't be represented accurately as a double. (The largest integer than can be accurately represented as a double is of the order of 2^53).

To obtain the right result you can use symbolic variables, which ensures you don't lose accuracy:

>> x = sym('688^79');
>> y = sym('3337');
>> mod(x, y)
ans =
1570
like image 93
Luis Mendo Avatar answered Dec 10 '22 17:12

Luis Mendo