Example: Rounding Errors

PDF Print E-mail
Written by Graham Stoney   

One of the most common coding errors I come across are rounding errors. These occur because languages like C or C++ truncate towards zero when performing operations like floating point to integer conversions and integer division.

Most of the time what you really want is the closest integral representation of the answer; not the one closest to zero. You don't get this by default, so you have to code for it explicitly. You could spend hours tracking down each instance of this subtle bug in an application, or find a whole swathe of them in a single code review.

I often see code like this:
double f;
int i;
...
i = f; // probably wrong; truncation not rounding
i = f + 0.5; // correct; has explicit rounding

And it's a similar deal when using an explicit cast or doing the type conversion deep in the middle of a more complex expression. Any time you're converting an floating point type to an integer, you probably want some explicit rounding. Integer division often leads to a similar bug:

int a, b, c;            // we're using integer arithmetic for speed
...
a = b / c; // probably wrong; truncation not rounding
a = (b + c/2) / c; // correct; has explicit rounding

The “correct” examples above assume you're only dealing with positive numbers; it may even be worth asserting that this is so. If your calculations could include negative values, then more thought is required to get what you really want. You also have to consider the posibility of integer overflow in the numerator. Of course, there are occasions where truncation is what you want; but my point is that in my experience these are in the minority and yet truncation is what you get by default.



Social Bookmark this page:Reddit! Del.icio.us! Google! Live! Facebook! StumbleUpon! Spurl! Yahoo! Ask!
 

Add your comment

Your name:
Your email:
Subject:
Comment:

Sponsored Links