[Haskell-cafe] Why 'round' does not just round numbers ?

Peter Gavin pgavin at gmail.com
Mon Oct 27 12:42:19 EDT 2008


L.Guo wrote:
> Hi all:
> 
> 
> I just read about definitions of Prelude [1], and noticing that.
> 
> In 6.4.6 Coercions and Component Extraction, it discribes like this:
> 
> 
> "round x returns the nearest integer to x, the even integer if x is equidistant between two integers."
> 
> 
> I think this is unresonable. then try it in GHC 6.8.3.
> 
> 
> Prelude> round 3.5
> 4
> Prelude> round 2.5
> 2
> 
> 
> Is there any explanation about that ?
> 

This is the same exact behavior found in the FPU of most processors, except that 
you usually don't notice because the rounding occurs between very small values.

The reason for doing it this way is that e.g. 2.5 is exactly between 2 and 3, 
and rounding *up* every time would cause an uneven bias toward 3.  To counteract 
that effect, rounding to the nearest even integer is used, which causes the half 
of the x.5 values to round up, and the other half to round down.

Pete


More information about the Haskell-Cafe mailing list