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
Rijk-Jan van Haaften wrote:
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:
Rounding to the nearest even number is considered the best practice by people doing numerical analysis. Even when I went to school (25 - 30 years ago) we were taught to round to the nearest even number, so it's not exactly new. -- Lennart
Lennart Augustsson comment to :
Rijk-Jan van Haaften wrote: ...
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: ... Rounding to the nearest even number is considered the best practice by people doing numerical analysis. Even when I went to school (25 - 30 years ago) we were taught to round to the nearest even number, so it's not exactly new.
-- Lennart
I wonder whether it has anything to do with the "best practice". I suppose that this is the natural way to get rid of one more bit of mantissa. Anyway, there is the standard IEEE 754 (with its 4 rounding modes...) and it is good to have a published standard, even if it results in some hangover of some people. The reported behaviour is also visible in Clean, and I spent 2 days on debugging because of that "round-ties-to-even rule"... It is not very "natural" psychologically, and from time to time I do some mistakes, although I know the stuff. Look here: (among 100 other references I could give you) http://www.validgh.com/goldberg/addendum.html http://developer.intel.com/technology/itj/q41999/articles/art_6.htm http://www.cs.umass.edu/~weems/CmpSci535/535lecture6.html Jerzy Karczmarczuk Caen, France
participants (3)
-
Jerzy Karczmarczuk -
Lennart Augustsson -
Rijk-Jan van Haaften