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
Yes, for negative numbers. If we read the documentation, we see:
truncate :: Integral b => a -> b
truncate x
returns the integer nearestx
between zero andx
and:
floor :: Integral b => a -> b
floor x
returns the greatest integer not greater thanx
So if we enter a negative number, like -3.5
we obtain:
Prelude> truncate (-3.5)
-3
Prelude> floor (-3.5)
-4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With