[racket] Rounding

From: Stephen Bloch (bloch at adelphi.edu)
Date: Sun Mar 17 22:46:43 EDT 2013

On Mar 16, 2013, at 11:47 PM, Eric Nadeau <nado18 at gmail.com> wrote:

> The point of rounding is coming up with the best possible integer approximation for a decimal and this nearest even number rule does not qualify.

Why not?  In these cases there is no "the" best possible integer approximation, but two equally-close integer approximations, so either one would qualify.  In fact, choosing one or the other at random would meet the above criterion, but would be non-deterministic and annoy a lot of programmers.

> This "logic" was used by my grandparents' generation because odd numbers were seen as less pure than even ones.

I've never heard that "logic".  Citation?

On the contrary, my impression is that our grandparents' generation chose "round halves up" because the most common source of decimal numbers that needed rounding was long division, which generates decimal digits one at a time.  If you generated the first digit after the decimal point and you got a "5", it meant the remainder was AT LEAST half of the divisor, but possibly more, so rounding halves up meant that you'd never round in the wrong direction, and most of the time (1 - 1/divisor) you'd be rounding in the right direction.  (The remaining 1/divisor of the time would be the "exact half" case in which either direction would be equally close.)

In computer arithmetic, by contrast, typically all the bits are generated (including guard, round, and sticky bits) before you even think about rounding, so you don't need to make your rounding decision based on only the first digit after the point.

> I'm ditching Scheme altogether based on this, but thanks all for the good explanations, it can be confirmed by running the following (I thought it was only a fluke with 0.5):
> 
> (round 0.5) (round 1.5) (round 2.5) (round 3.5)

Oh, there's no dispute that it happens consistently.  If it didn't, a bug report would be in order.

BTW, before you blame this on Scheme or Racket, see http://docs.oracle.com/javase/1.5.0/docs/api/java/math/RoundingMode.html and http://en.wikipedia.org/wiki/IEEE_floating_point#Rounding_rules


Stephen Bloch
sbloch at adelphi.edu



Posted on the users mailing list.