在Haskell中,floor和truncate之间有区别吗 [英] Is there a difference between floor and truncate in Haskell

查看:137
本文介绍了在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 :: Integral b => a -> b

truncate :: Integral b => a -> b

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

truncate x returns the integer nearest x between zero and x

和:

floor :: Integral b => a -> b

floor :: Integral b => a -> b

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之间有区别吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆