Operator Error

adventures in software development and continuous learning

Floating Point Numbers

I just recently finished up a project at work that required float-point number comparison and initially stumbled into the float-point comparison pitfall displayed below:

if (float_a == float_b)
    // compare evaluates true.
    // compare evaluates false.

Of course, my team lead quickly pointed out my mistake while reviewing my code and I promptly smacked myself in the head for making such a rookie error!

After fixing the code in question following his recommendation, I just so happened to stumble upon an interesting article on Slashdot calledĀ What Every Computer Scientist Should Know About Floating-Point Arithmetic and figured it was right up my alley! Below is a piece of the abstract that I think sums of the paper nicely:

This paper presents a tutorial on those aspects of floating-point that have a direct impact on designers of computer systems. It begins with background on floating-point representation and rounding error, continues with a discussion of the IEEE floating-point standard, and concludes with numerous examples of how computer builders can better support floating-point.

If you’re rusty on the ins and outs of floating-point (it happens to the best of us) and short on time, then you should check out the digested version over at The Floating-Point Guide. This is a great high-level summary of the original article and covers topics such asĀ number formats, IEEE 754 standard, and comparison errors just to name a few.

BONUS LINK: Comparing float point numbers