Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between floor and truncate in Haskell

Tags:

haskell

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
like image 375
Tom Gijselinck Avatar asked Aug 22 '17 07:08

Tom Gijselinck


1 Answers

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

truncate :: Integral b => a -> b

truncate x returns the integer nearest x between zero and x

and:

floor :: Integral b => a -> b

floor x returns the greatest integer not greater than x

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

Prelude> truncate (-3.5)
-3
Prelude> floor (-3.5)
-4
like image 181
Willem Van Onsem Avatar answered Nov 03 '22 23:11

Willem Van Onsem