Prolog中的除法和余数 [英] Divison and remainder in Prolog

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

问题描述

试图弄清楚如何编写一个递归谓词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中的除法和余数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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