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的怪癖?
答案 0 :(得分:23)
这是在规范中。您可以在the Haskell report的第6.4.6节中看到它:
round x
返回最接近x
的整数,如果x
则为偶数 两个整数之间等距。
正如@dflemstr指出的那样,这与IEEE Standard for Floating-Point Arithmetic一致。