## There is a mistake with such expressions as mod(23^12,24). MATLAB gives an answer of 0 which, given that 23 and 24 are relative primes,

### AdamE (view profile)

on 12 Jun 2019
Latest activity Commented on by AdamE

### AdamE (view profile)

on 12 Jun 2019
Accepted Answer by Walter Roberson

### Walter Roberson (view profile)

Clearly, 23 = -1 mod 24. So, mod(23^12,24) should result in 1.

### Walter Roberson (view profile)

Answer by Walter Roberson

on 12 Jun 2019

on 12 Jun 2019
Thank you!

### Rik (view profile)

on 12 Jun 2019

The number you're trying to process is larger than flintmax(), so you can rely on single digit precision operations.

### AdamE (view profile)

on 12 Jun 2019

I the function mod could recognize -1 = 23 mod 24, then act on -1, the number would not be too large. This logic should be employed in the function.

John D'Errico

### John D'Errico (view profile)

on 12 Jun 2019
Please stop adding an answer just to make a comment. And, NO, you are wrong. You don't understand how a language like MATLAB works, IF you think that.
MATLAB does not understand number theory, even something as trivial as that. It does what you tell it to do. You wrote this:
mod(23^12,24)
So, MATLAB sees the number 23, with a power applied to it. It computes that number, raising 23 to the desired power. It does NOT think about what might be done to it in the future. If you expect that, then you expect far too much of computers. Computers don't think at all. They cannot apply mathematics to a result, knowing that the result can indeed be simplified.
For example, suppose you wrote this in MATLAB:
theta = rand(1);
sin(theta)^2 + 1 + cos(theta)^2
ans =
2
Do you honestly think that MATLAB should be smart enough to see that, then to know that oh, I can combine those terms with an identity, avoiding needing to compute sin(theta) and cos(theta) at all, replacing the sum of two of those terms with 1, and then adding 1, to get 2? If not, then why would you expect that MATLAB should know that 23 is congruent to -1, mod 24, and then to foresee that it can simplify that numerical computation?
So MATLAB does this:
23^12
ans =
2.19146244320203e+16
As you see, it is indeed larger than flintmax.
flintmax
ans =
9.00719925474099e+15
So 23^12 is a number too large to be exactly represented as an integer.
Once MATLAB computes that result, it then passes it to the mod function. If the number is not exactly representable as an integer, tough luck. You, as the person driving this vehicle, are expected to know what you are doing.
Walter Roberson

### Walter Roberson (view profile)

on 12 Jun 2019
No, it should not be. Doing that would require rewriting the entire way that MATLAB operates on parameters.
MATLAB currently always evaluates all parameters before the function is called, so when you call mod(23^12, 24) then 23^12 and 24 are evaluated and their full evaluated results are passed to mod() .
In order for MATLAB to do what you are suggesting automatically, it would have to somehow record the formula 23^12 and 24 and pass that into mod() and then mod would have to know how to decompose formulas in terms of modular arithmetic. It would have to be pretty deep, such as knowing that
mod( (23^12)^3 + 5*23^12 + 7, 24)
could be reduced in modular arithmetic.

### AdamE (view profile)

on 12 Jun 2019
I completely agree. I meant that there should be, and apparently are, scripts to handle this situation.