Is there a difference between gender and truncation in Haskell

Is there a difference in functionality between floor and truncate in Haskell?

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

  • truncate :: (Integral b, RealFrac a) => a -> b
  • floor :: (Integral b, RealFrac a) => a -> b
+7
haskell
source share
4 answers

Yes, for negative numbers. If we read the documentation, we will 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 largest integer not greater than x

So, if we introduce a negative number , for example -3.5 , we get:

 Prelude> truncate (-3.5) -3 Prelude> floor (-3.5) -4 
+16
source share

It is not haskell specific, but there is a difference between these functions. Floor means the highest integer not exceeding the given number. Truncation means removing at a certain length, in this case a fractional part. They have the same effect for zero and positive numbers, but are not negative.

Here's a quick comparison in Python:

 >>> for i in range(-5,6): ... j=0.5*i ... print(j,floor(j),ceil(j),trunc(j),round(j)) ... -2.5 -3 -2 -2 -2 -2.0 -2 -2 -2 -2 -1.5 -2 -1 -1 -2 -1.0 -1 -1 -1 -1 -0.5 -1 0 0 0 0.0 0 0 0 0 0.5 0 1 0 0 1.0 1 1 1 1 1.5 1 2 1 2 2.0 2 2 2 2 2.5 2 3 2 2 

Essentially, trunc () goes to zero and floor () to negative infinity.

+6
source share

Looking at the source code, the difference comes up pretty quickly:

 truncate x = m where (m,_) = properFraction x 

and

 floor x = if r < 0 then n - 1 else n where (n,r) = properFraction x 

we see that the difference will appear only on negative numbers and so:

 Prelude> floor (negate 2.1) -3 Prelude> truncate (negate 2.1) -2 
+3
source share

Rounding:

 1 --> 1 3.1 --> 3 3.9 --> 3 -2.1 --> -2 -2.9 --> -2 

Floor:

 1 --> 1 3.1 --> 3 3.9 --> 3 -2.1 --> -2 -2.9 --> -3 (Different!...) 
+1
source share

All Articles