本文介绍了积分运算符“与div”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类型类Integral有两个操作 quot 和 div ,但是在Haskell 2010语言报告中没有指定它他们应该做什么。假设 div 是整数除法, quot 有什么不同,或者的用途是什么? QUOT ?何时使用一个,何时使用另一个?

解决方案
从Haskell报告中引用第6.4.2节:



quot , rem , div 和 mod 类方法在y不为零时满足这些定律:

<$ p $ y +(x`rem` y)== x
(x`div` y)* y +(x`mod` y) == x

quot 是整数除法朝向零,而 div 的结果被截断为负无穷。
$ b

div 函数通常是更自然的函数,而 quot 函数对应于现代机器上的机器指令,因此效率更高。 / p>

Type class Integral has two operations quot and div, yet in the Haskell 2010 Language Report it is not specified what they're supposed to do. Assuming that div is integral division, what does quot differently, or what is the purpose of quot? When do you use one, and when the other?

解决方案

To quote section 6.4.2 from the Haskell report:

The quot, rem, div, and mod class methods satisfy these laws if y is non-zero:

(x `quot` y)*y + (x `rem` y) == x
(x `div`  y)*y + (x `mod` y) == x

quot is integer division truncated toward zero, while the result of div is truncated toward negative infinity.

The div function is often the more natural one to use, whereas the quot function corresponds to the machine instruction on modern machines, so it's somewhat more efficient.

这篇关于积分运算符“与div”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:40