Prolog中的Divison和其余部分 [英] Divison and remainder in Prolog

查看:191
本文介绍了Prolog中的Divison和其余部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图找出如何写一个递归谓词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)询问,这在当前实现中给出 towards_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)of towards_zero,而Fortran和追随者喜欢C转向towards_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中的Divison和其余部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆