乘法后 CLISP 溢出
让第一个 lisp 程序使用 CLISP 实现来工作
(print (mod (+ (* 28433 (expt 2 7830457) 1)) (expt 10 10))))
我正在尝试通过输入REPL 来
。但它给了我*** - 大数乘法期间溢出
。我认为 lisp 具有任意大小/精度的特点。那怎么会发生这种事呢?
i'm trying to get a first lisp program to work using the CLISP implementation, by typing
(print (mod (+ (* 28433 (expt 2 7830457) 1)) (expt 10 10))))
in the REPL.
but it gives me *** - overflow during multiplication of large numbers
. i thought lisp features arbitrary size/precision. how could that ever happen then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
函数“mod-expt”(或 EXT:mod-expt)
CLIsp 提供了相当快的 。并且为了您的目的而有效。
CLisp provided the function "mod-expt" (or EXT:mod-expt)
which is pretty fast. And for your purpose that works.
Lisp 是一个语言家族,拥有数十种方言和数百种不同的实现。
计算机的内存是有限的。某些操作系统下的程序可能对内存大小有限制。不同的 Common Lisp 实现使用不同的数字库。
您可能需要查阅 CLISP 手册以了解其各种数据类型的限制。
Lisp is a family of languages with dozens of dialects and hundreds of different implementations.
Computers have finite memory. Programs under some operating systems may have limitations about the memory size. Different Common Lisp implementations use different numeric libraries.
You may want to consult your CLISP manual for its limitations of its various data types.
很可能有更好的方法来解决问题。我在体育方面还没有做到那么远,但我知道到目前为止我所做的少数人往往会发出“啊哈!”的感叹。解决似乎超出计算机程序范围的问题。
尤其是这个 - 2^7830457 是一个巨大的数字 - 尝试
(format t "~r" (expt 2 160))
。您可能会尝试从新的角度看待问题,看看是否有一种您没有想到的看待问题的方法。Chances are there's a better way to solve the problem. I haven't made it that far on PE, but I know the few that I've done so far tend to have "aha!" solutions to problems that seem out of a computer programs range.
This one especially - 2^7830457 is a huge number -- try
(format t "~r" (expt 2 160))
. You might try to look at the problem in a new light and see if there's a way to look at it that you haven't thought of.根据 http://clisp.cons.org/impnotes/num-concepts.html< /a> bignum 的最大大小是 (2^2097088 - 1) 而你的 2^7830457 比这个大得多。
也许你可以考虑分解这个数字 - 也许分离出一些较小的 2^X 因子......
According to http://clisp.cons.org/impnotes/num-concepts.html the maximum size for a bignum is (2^2097088 - 1) and your 2^7830457 is much larger than that.
Perhaps you can look at breaking down that number - perhaps separate out a number of smaller 2^X factors...
Lisp 的 bignum 可能包含非常大的数字,但它们也有其局限性。
在您的情况下,您可以将求幂和模数合并到一个过程中,例如 http://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method。
Lisp's bignums may hold really large numbers, but they too have their limits.
In your case, you can combine exponentiation and modulus into a single procedure, e.g. as in http://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method.