Prolog中的除法和余数

编程入门 行业动态 更新时间:2024-10-09 05:23:13
本文介绍了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中的除法和余数

本文发布于:2023-11-29 09:04:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645950.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:余数   除法   Prolog

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!