Fix Something Annoying

November 6, 2012

My solution to the rounding problem in Standard Scheme is to use the built-in round function to do the hard work, feeding it an appropriate value:

(set! system-round round)

(define (round x . args)
  (if (null? args)
      (system-round x)
      (let ((tens (expt 10 (car args))))
                (/ (system-round (* x tens)) tens))))

Here are some examples:

> (round 123.4567)
123.0
> (round 123.4567 2)
123.46
> (round 12345.67 -2)
12300.0
> (round -123.4567 2)
-123.46

You can run the program at http://programmingpraxis.codepad.org/wGEsn794.

Pages: 1 2

3 Responses to “Fix Something Annoying”

  1. Chris said

    I implemented the Gasoline Mileage Log in Erlang, and in doing so was annoyed that I couldn’t find an Erlang function that works like Clojure’s core/partition. Here’s my fix.

  2. […] today’s Programming Praxis exercise, our goal is to fix something that annoys us in our programming […]

  3. I created a library to resolve my annoyances with the Parsec library. For more information, see the blog post.

Leave a comment