舍入到最接近的整数

时间:2012-05-24 13:35:07

标签: haskell floating-point

Haskell中有round函数的官方规范吗?在GHCi版本7.0.3中,我看到以下行为:

ghci> round (0.5 :: Double)
0
ghci> round (1.5 :: Double)
2

由于0.5和1.5都可以表示为浮点数,我希望看到与Python相同的行为:

>>> round(0.5)
1.0
>>> round(1.5)
2.0

是否存在差异的理由,或者是GHCi的怪癖?

1 个答案:

答案 0 :(得分:23)

这是在规范中。您可以在the Haskell report的第6.4.6节中看到它:

  

round x返回最接近x的整数,如果x则为偶数   两个整数之间等距。

正如@dflemstr指出的那样,这与IEEE Standard for Floating-Point Arithmetic一致。