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,

1 次查看(过去 30 天)
Clearly, 23 = -1 mod 24. So, mod(23^12,24) should result in 1.

采纳的回答

Walter Roberson
Walter Roberson 2019-6-12

更多回答(3 个)

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

AdamE
AdamE 2019-6-12
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.
  3 个评论
Walter Roberson
Walter Roberson 2019-6-12
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
AdamE 2019-6-12
John,
Firstly, calm down. I've been using MATLAB since the late 1980s and have a PhD in Math. I wasn't implying that MATLAB performed erroneously; I was implying that there should be a modulo script which can employ some basic number theory to reduce incorrect results. Now that Walter provided the links, I see that there are such scripts.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by