Why is Matlab Mod different from Wolfram Alpha
Asked Answered
D

2

5

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.

Discord answered 26/4, 2015 at 23:19 Comment(6)
Some Overflow perhaps?Wampumpeag
might be, let me run a quick test with a smaller valueDiscord
with mod(4^3,24) both wolfram and matlab shows the same result == 16Discord
1570 is correct (calculated with python). Try mod(double(688^79), 3337)Wampumpeag
nope, still getting me the 364Discord
Just store them as sym and you will be able to calculate it correctly.Wampumpeag
S
11

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
Shunt answered 26/4, 2015 at 23:32 Comment(5)
@JuanZamora ¡Un placer! :-)Shunt
Your example is using full symbolic math, not variable precision. There doesn't seem to be any reason to use variable precision here. Using variable precision with vpa means you have a finite, but adjustable, number of digits of precision. Maybe I don't understand what you're trying to say – you don't lose precision with respect to your specified value for digits, but in manu cases you absolutely can lose precision relative to the true value using vpa.Ravish
@LuisMendo check this one: 79^-1 mod 3220 = 1019 in wolfram. In matlab is just returning 1/79 = 0.0126... I tried the symbolic was, but no luck.Discord
@JuanZamora That's a little more nuanced. WolframAlpha appears to be solving the modular inverse problem for the input 79^-1 mod 3220 (interpreted as PowerMod[79,-1,3220] but will solve the standard remainder problem for 1/79 mod 3220 (interpreted as Mod[79^-1,3220]).Trull
@Ravish You are completely right. I was trying several things and ended up not using vpa, but the explanation was wrong. Thanks! I've corrected itShunt
F
3

My calculator is sending me the same answer than Wolfram, it also calculated the value for 688^79 so I would tend to believe Wolfram is right. You probably have overrun the capacities of Matlab with such a huge number and it is why it did not send the right answer.

Fayum answered 26/4, 2015 at 23:28 Comment(1)
for the record your number is 147736096754175303230053677431108078993422779056521621788516135638187994995899192814258977221160914022825628299180402236602527982600775671266557420283463691946979929718178787089203370822514764608144873171356044647950190641152 Full stopFayum

© 2022 - 2024 — McMap. All rights reserved.