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.

About these ads

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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 630 other followers

%d bloggers like this: