整数平方根适用于 SWI-Prolog 和 YAP,但不适用于 GNU-Prolog
Integer square root works in SWI-Prolog and YAP, but but not in GNU-Prolog
我通过将以下代码添加到 swipl、gprolog 和 yap 中的用户文件来测试它:
isqrt(N, _) :-
N < 0, !, fail.
isqrt(N, N) :-
N < 2.
isqrt(N, R) :-
X is N,
Y is (N // 2),
isqrt(N, X, Y, R).
isqrt(_, X, Y, X) :-
Y >= X.
isqrt(N, _, Y, R) :-
Z is ((Y + N // Y) // 2),
isqrt(N, Y, Z, R).
这在 swipl 和 yap 中按预期工作,但在 gprolog 中我收到以下关于 N > 1 的错误消息:
uncaught exception: error(existence_error(procedure,isqrt/0),isqrt/0)
这对我来说很奇怪,因为我代码中的 none 个谓词依赖于 isqrt/0
。这可能是 GNU-Prolog 中的错误吗?作为解决方法,我可以做些什么?
编辑:这正是我在 ubuntu 上的 gprolog 中产生此错误的方法:
$ gprolog
GNU Prolog 1.4.5 (64 bits)
Compiled Feb 5 2017, 10:30:08 with gcc
By Daniel Diaz
Copyright (C) 1999-2016 Daniel Diaz
| ?- [user].
compiling user for byte code...
isqrt(N, _) :-
N < 0, !, fail.
isqrt(N, N) :-
N < 2.
isqrt(N, R) :-
X is N,
Y is (N // 2),
isqrt(N, X, Y, R).
isqrt(_, X, Y, X) :-
Y >= X.
isqrt(N, _, Y, R) :-
Z is ((Y + N // Y) // 2),
isqrt(N, Y, Z, R).
user compiled, 17 lines read - 1656 bytes written, 10751 ms
yes
| ?- isqrt(100, X).
uncaught exception: error(existence_error(procedure,isqrt/0),isqrt/0)
有一些报告,包括在 GNU Prolog 邮件列表中,在 Linux 下有类似的错误,特别是 Ubuntu/kubuntu:
http://lists.gnu.org/archive/html/bug-prolog/2018-09/msg00002.html
在报告的案例中,从源代码编译 GNU Prolog 解决了问题。
我通过将以下代码添加到 swipl、gprolog 和 yap 中的用户文件来测试它:
isqrt(N, _) :-
N < 0, !, fail.
isqrt(N, N) :-
N < 2.
isqrt(N, R) :-
X is N,
Y is (N // 2),
isqrt(N, X, Y, R).
isqrt(_, X, Y, X) :-
Y >= X.
isqrt(N, _, Y, R) :-
Z is ((Y + N // Y) // 2),
isqrt(N, Y, Z, R).
这在 swipl 和 yap 中按预期工作,但在 gprolog 中我收到以下关于 N > 1 的错误消息:
uncaught exception: error(existence_error(procedure,isqrt/0),isqrt/0)
这对我来说很奇怪,因为我代码中的 none 个谓词依赖于 isqrt/0
。这可能是 GNU-Prolog 中的错误吗?作为解决方法,我可以做些什么?
编辑:这正是我在 ubuntu 上的 gprolog 中产生此错误的方法:
$ gprolog
GNU Prolog 1.4.5 (64 bits)
Compiled Feb 5 2017, 10:30:08 with gcc
By Daniel Diaz
Copyright (C) 1999-2016 Daniel Diaz
| ?- [user].
compiling user for byte code...
isqrt(N, _) :-
N < 0, !, fail.
isqrt(N, N) :-
N < 2.
isqrt(N, R) :-
X is N,
Y is (N // 2),
isqrt(N, X, Y, R).
isqrt(_, X, Y, X) :-
Y >= X.
isqrt(N, _, Y, R) :-
Z is ((Y + N // Y) // 2),
isqrt(N, Y, Z, R).
user compiled, 17 lines read - 1656 bytes written, 10751 ms
yes
| ?- isqrt(100, X).
uncaught exception: error(existence_error(procedure,isqrt/0),isqrt/0)
有一些报告,包括在 GNU Prolog 邮件列表中,在 Linux 下有类似的错误,特别是 Ubuntu/kubuntu:
http://lists.gnu.org/archive/html/bug-prolog/2018-09/msg00002.html
在报告的案例中,从源代码编译 GNU Prolog 解决了问题。