[racket] Rounding

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Sat Mar 16 21:04:34 EDT 2013

That's an overly specific solution.  I believe the more general rounding
function taught in grade-school math would be:

(define (my-round n)
  (floor (+ n 1/2)))

This is also untested.  And it might need tweaking for negative numbers if
they are supposed to round away from zero, I forget how that works.

Carl Eastlund


On Sat, Mar 16, 2013 at 8:32 PM, Eric Dobson <eric.n.dobson at gmail.com>wrote:

> The docs say that it is round to even, which is the standard for
> floating point numbers [1]. You can write your own rounding operator
> that rounds .5 up if need be.
>
> ;; Untested
> (define (my-round n)
>   (let ((r (round n))) (if (= .5 (- n r)) (add1 r) r)))
>
> [1] http://en.wikipedia.org/wiki/Rounding#Round_half_to_even
>
> On Sat, Mar 16, 2013 at 7:21 AM, Eric Nadeau <nado18 at gmail.com> wrote:
> > Hi,
> >
> > I was having problems with a mathematical application I'm writing and
> narrowed it down to:
> >
> >         (round 0.5)
> >         (round (/ 1 2))
> >         (round 1.5)
> >         (round (/ 3 2))
> >
> > giving me
> >
> >         0.0
> >         0
> >         2.0
> >         2
> >
> > Since when is round 0.5 not giving 1?!
> >
> > Cheers,
> > Eric
> > ____________________
> >   Racket Users list:
> >   http://lists.racket-lang.org/users
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130316/b580757a/attachment.html>

Posted on the users mailing list.