Wrong results with "mod"
11 ビュー (過去 30 日間)
古いコメントを表示
Why does Matlab give me the wrong answer to mod(14^27,55) = 32, it should equal 9. What am I doing wrong?
2 件のコメント
回答 (1 件)
John D'Errico
2019 年 2 月 10 日
This fails because MATLAB cannot represent an integer larger than 2^53-1 as an exact integer, when working in double precision.
14^27 is far larger than that of course. So it fails. This leaves you with two basic options, to use symbolic arithmetic, (or perhaps my VPI toolbox) or to use powermod.
mod(sym(14)^27,55)
ans =
9
powermod(14,27,55)
ans =
9
The powermod solution is better of course, in the sense that the huge number 14^27 never had to be fully computed.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Number Theory についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!