[Haskell-cafe] Rounding
Magnus Therning
magnus at therning.org
Tue Jun 2 07:30:22 UTC 2015
On 2 June 2015 at 04:08, <ok at cs.otago.ac.nz> wrote:
> The Haskell 2010 report defines, in chapter 9,
> round :: (Real a, Fractional a, Integral b) => a -> b
>
> round x =
> let (n, r) = properFraction x
> -- n = truncate x, r = x-n (same sign as x)
> m = if r < 0 then n - 1 else n + 1
> in case signum (abs r - 0.5) of
> -1 -> n -- round in if |r| < 0.5
> 1 -> m -- round out if |r| > 0.5
> 0 -> if even n then n else m
>
>
> (commented and slightly rearranged). The traditional
> definition of rounding to integer, so traditional that it
> is actually given in the OED, is basically
>
> round x = truncate (x + signum x * 0.5)
>
> There was a discussion of rounding recently in another mailing
> list and I put together this table:
>
> * Round x.5 OUT
> Ada, Algol W, C, COBOL, Fortran, Matlab, Pascal, PL/I,
> Python, Quintus Prolog, Smalltalk. The pre-computing tradition.
>
> * Round x.5 to EVEN
> Common Lisp, R, Haskell, SML, F#, Wolfram Language.
>
> * Round x.5 UP to positive infinity
> Java, JavaScript, ISO Prolog, Algol 60
>
> * Rounding of x.5 UNSPECIFIED
> Algol 68, IMP 77
>
> What I was wondering was whether anyone on this list knew why
> Haskell has the break-ties-to-even definition instead of the
> traditional break-ties-out one. (And please don't say that it
> is to get statistical unbiasedness, because given the kinds of
> data distribution I see, it _isn't_ unbiased.)
This is pure conjecture, but reading about "round half to even" on
Wikipedia (<http://en.wikipedia.org/wiki/Rounding#Round_half_to_even>)
shows that it has the most aliases of all tie-breaking strategies, it
is also the rounding used in IEEE 754.
/M
--
Magnus Therning OpenPGP: 0xAB4DFBA4
email: magnus at therning.org jabber: magnus at therning.org
twitter: magthe http://therning.org/magnus
More information about the Haskell-Cafe
mailing list