本文介绍了在Haskell中,floor和truncate之间有区别吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Haskell中的floortruncate在功能上是否存在差异?

Does there exist a difference in functionality between floor and truncate in Haskell?

它们似乎执行相同的功能,并且具有相同的类型签名:

They seem to perform the same functionality and they have the same type signature:

  • truncate :: (Integral b, RealFrac a) => a -> b
  • floor :: (Integral b, RealFrac a) => a -> b
  • truncate :: (Integral b, RealFrac a) => a -> b
  • floor :: (Integral b, RealFrac a) => a -> b

推荐答案

是的,用于负数.如果阅读文档,将会看到:

Yes, for negative numbers. If we read the documentation, we see:

truncate x返回零和x之间最接近x的整数

truncate x returns the integer nearest x between zero and x

和:

floor x返回不大于x

因此,如果我们输入负数,例如-3.5,我们将获得:

So if we enter a negative number, like -3.5 we obtain:

Prelude> truncate (-3.5)
-3
Prelude> floor (-3.5)
-4

这篇关于在Haskell中,floor和truncate之间有区别吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 09:40