试图弄清楚如何编写一个递归谓词divide_by(X, D, I, R),它将正整数X和除数D作为输入,并将答案作为整数部分I和余数部分返回R,但是,我似乎无法理解 Prolog.我该怎么做呢?
Trying to figure out how to write a recursive predicate divide_by(X, D, I, R) that takes as input a positive integer X and a divisor D, and returns the answer as the whole number part I and the remainder part R, however, I can't seem to get my head around Prolog. How would I go about doing this?
推荐答案为此有预定义的可评估函子.
There are predefined evaluable functors for this.
(div)/2 和 (mod)/2 总是向下取整.LIA-1、Knuth 等推荐
(div)/2 and (mod)/2 always rounding down. Recommended by LIA-1, Knuth etc.
(//)/2 和 (rem)/2 向零舍入(实际上,它是实现定义的,但所有当前的实现都是这样做的).你可以通过 current_prolog_flag(integer_rounding_function, F) 来询问这个问题,它给出了当前的实现 toward_zero.
(//)/2 and (rem)/2 rounding toward zero (actually, it's implementation defined, but all current implementations do it like that). You can ask this via current_prolog_flag(integer_rounding_function, F) which gives in current implementations toward_zero.
这些对之间的差异仅在涉及负数时显示.这是一场人们更喜欢的宗教战争.ISO/IEC 10967:2012 语言独立算术 (vl. LIA-1) 仅提供向下舍入 由于易于错误使用" (C.5.1.2.2) 的向零,而 Fortran 和追随者喜欢C 去朝向_zero 称它为 "algebraic" (6.5.5).也可以看看:对负无穷大使用截断与走向零
The difference between those pairs shows only when negative numbers are involved. It is kind of a religious war which one to prefer. ISO/IEC 10967:2012 Language independent arithmetic (vl. LIA-1) only provides rounding down "due to proneness for erroneous use" (C.5.1.2.2) of toward_zero, whereas Fortran and followers like C go for toward_zero calling it "algebraic" (6.5.5). See also: Advantages of using truncation towards minus infinity vs towards zero
更多推荐
Prolog中的除法和余数
发布评论