Class RealFrac: round

Rijk-Jan van Haaften rjchaaft@cs.uu.nl
Wed, 09 May 2001 14:05:56 +0200


Hello,

While I were writing a RealFrac implementation for BigDouble's (for
high-precision computations) I found a strange implementation of
round in the standard prelude (from the haskell definition):

round x =
	let (n,r) =
		properFraction x
		m = if r < 0 then n - 1 else n + 1
	in case signum (abs r - 0.5) of
		-1 -> n
		0 -> if even n then n else m
		1 -> m

The strange case is if signum (abs r - 0.5) is 0:
such numbers are round to the nearest EVEN integer. In mathematics,
computer science (the studies I'm doing) and physics, as far as I
know, it is usual to round such numbers up, rather than to the nearest
integer. For example:

Number   Hugs    Math
  0.5      0       1
  1.5      2       2
-3.5     -4      -4
  6.5      6       7

Why did the Haskell designers choose this behaviour?

Thanks in advance,

Rijk-Jan van Haaften