Monday, January 31, 2005

"It was my understanding that there would be no math"

Ok, quick math quiz.

No wait; don’t run away, this is interesting, I swear.

This has to do with rounding. You know the old 3.5 rounds up to 4. Simple stuff really (and not that compelling to read about, I’m sure). Before you click away, grumbling about nerdy programmers and overly simplistic tutorials – take this quiz.

The only caveat is that you must round these numbers as if you were doing financial calculations – assume you’re a banker if it suits your fancy.

Round to the hundredths please (that’s the second place to the right of the decimal for any of my wife’s family.).

  • 4.445

  • 4.435

That’s it - simple yet effective.

If you’re not a programmer (and even if you are a programmer, depending on the language you use) you answered as such:

4.445 rounds up to 4.45
4.435 rounds up to 4.44

Here’s the problem, and if you’re me, it’s a big, big problem. Most modern programming languages use something called ‘Bankers Rounding’. In standard, school-taught rounding, there is an inherent bias. Or more succinctly, five numbers consistently round up (5, 6, 7, 8, 9), four numbers round down (1, 2, 3, 4 – zero doesn’t round). In financial terms, this means you’re giving away money in the long run.

To handle this, Bankers Rounding treats the 5 as a wild card. On even numbers, such as our example number 4.445, the five rounds down, giving us 4.44. On odd numbers, such as our example number 4.435, the five rounds up, giving us 4.44 as well. This will statistically even out over the course of rounding, creating more of a balance (and not losing money for the banks).

Because the application I’m working on requires standard rounding, and because the language I write in only supports bankers rounding (btw – both languages come from the same company. Nice work on interoperability guys.) I’ve been forced to implement a custom rounding feature.

I know, you and Justin Timberlake are singing ‘Cry me a river’ right now (+3 for pop culture reference). It’s true this isn’t the worst thing that’s happened to me, but it is annoying – and not that intuitive to figure out.

I’m just sayin….


At 5:33 AM, Blogger Jay said...

Well, at least there was a cute Chevy Chase reference at the top. That's what saved it for me, really. :)

You know, you learn something every day.


Post a Comment

<< Home