Integral operators quot vs. div

后端 未结 2 1749
说谎
说谎 2020-11-29 02:04

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. Assu

2条回答
  •  孤城傲影
    2020-11-29 02:54

    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.

提交回复
热议问题