(我只是 GMP 库的间接用户,主要通过swi-prolog和yap。但我对解决这个问题非常感兴趣。)
当执行具有大得离谱的值的幂运算时,主机系统或 GMP 不再能够适当地处理溢出。我已经与上述系统的开发人员进行了交谈,但他们没有看到一个简单的解决方法。
其他 GMP 系统/用户是否知道这个问题?你如何处理这样的溢出?
作为健全性检查,首先测试 7^7^7 的值,它应该是:375982...32343
例如,在 32 位系统上,查询会?- X is 13^1150000000.产生这样的溢出。以下是 YAP 提供的内容:
GNU gdb (GDB) 7.0-ubuntu
版权所有 (C) 2009 Free Software Foundation, Inc.
许可 GPLv3+:GNU GPL 版本 3 或更高版本
这是免费软件:您可以自由更改和重新分发它。
在法律允许的范围内,不提供任何保证。输入“显示复制”
和“显示保修”了解详情。
这个 GDB 被配置为“i486-linux-gnu”。
有关错误报告说明,请参阅:
...
从 /opt/gupu/src/yap-6.3/narch-gupu2/yap...读取符号...完成。
(gdb) 运行 -f
启动程序:/opt/gupu/src/yap-6.3/narch-gupu2/yap -f
YAP 6.3.2 (i686-linux):2012 年 11 月 11 日星期日 04:19:37 CET
?- X 是 13^1150000000。
程序收到信号 SIGSEGV,分段错误。
0x001638d8 在?? () 来自 /usr/lib/libgmp.so.3
(gdb) BT
#0 0x001638d8 在?? () 来自 /usr/lib/libgmp.so.3
#1 0x00164470 in __gmpn_mul_fft () from /usr/lib/libgmp.so.3
#2 0x001646c2 in __gmpn_mul_fft_full () from /usr/lib/libgmp.so.3
#3 0x00165f28 in __gmpn_sqr_n () from /usr/lib/libgmp.so.3
#4 0x0014b58b in __gmpz_n_pow_ui () from /usr/lib/libgmp.so.3
#5 0x0014c4a1 in __gmpz_pow_ui () from /usr/lib/libgmp.so.3
#6 0x080c4a1d in Yap_gmp_exp_int_int (i1=13, i2=1150000000) at ../C/gmp_support.c:939
#7 0x0815f9df in p_exp (t1=, t2=3082051592) at ../C/arith2.c:609
#8 0x080b1f19 in Eval (t=0) at ../C/eval.c:147
#9 0x080b2251 in p_is () at ../C/eval.c:186
#10 0x0806b56a 在 Yap_absmi (inp=0) at ../C/absmi.c:6912
#11 0x080b3655 in exec_absmi (top=) at ../C/exec.c:1002
#12 0x080b3b1f 在 do_goal (t=, CodeAdr=, arity=,
pt=0x0,顶部=1) 在 ../C/exec.c:1068
#13 0x080b3d1d in Yap_RunTopGoal (t=135918154) at ../C/exec.c:1291
#14 0x08061a6f 在 YAP_RunGoalOnce (t=135918154) at ../C/c_interface.c:2511
#15 0x0805c2f5 in do_top_goal (argc=2, argv=0xbffff4c4) at ../console/yap.c:84
#16 exec_top_level (argc=2, argv=0xbffff4c4) at ../console/yap.c:131
#17 主要 (argc=2, argv=0xbffff4c4) 在 ../console/yap.c:172
(gdb)
编辑:对于 64 位系统也是如此;像这样:
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 6.3.5)
Copyright (c) 1990-2012 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
For help, use ?- help(Topic). or ?- apropos(Word).
?- X is 3445^2^62.
gmp: overflow in mpz type
Abort
然而,
?- X is 2^2^63.
ERROR: Out of global stack
?- X is 2^2^62.
gmp: overflow in mpz type
Abort
从下面:
?- X is 2^2^36.
ERROR: Out of global stack
?- X is 2^2^37.
gmp: overflow in mpz type
Abort
因此,如果数字足够大,则 SWI 会检测到错误 - 因此可以由 SWI 处理(错误:消息由 SWI 处理)。