So I've been thinking about floating point numbers recently. Mostly I've been thinking about comparing floating numbers for _relative_ equality. I know this certainly isn't a new issue for most, especially if you have worked with a "lower" level language like C/C++, but for the average SAS programmer it may come as a surprise that 7.4 may not = 7.4! In fact the rules of real numbers dictate that 7.4 can never = 7.4 since they are both approximations ( or shorthand ) for an infinetly precise number with decimal places stretching from here to Mars and back again ad infinitum.
In the general world we don't really care that much about floating point inequality because our precision, or more specifically lack-of-precision, makes it a moot point.
But in the world of computers and real numbers precision is always an issue. As anyone who has been unfortunate enough to write code such as this has (painfully) learned:
// add .10 cents rebate to the customers account till they have reached
// the rebate maximum
// called by perVisit() function
const float REBATE_MAXIMUM = 2.5; // $2.50 rebate max
void addRebate( Customer &c)
{
if ( c.accumulatedRebate == REBATE_MAXIMUM ) return;
else
{
// add the ten cents to their account and update their accumulated rebate
// so they do not go over
c.account += .1;
c.accumulatedRebate += .1;
}
}
Now who's going to explain to the CEO why all the 3rd quarter revenue got eliminated in massive rebates? GULP.
Hopefully you recognize the error in the above code? Since the values being compared are floats they are not really 2.50 but really something closer to 2.50000000007 or 2.5000000000001 or well _anything_ once you get past the signifigant digits of 2.50.
But the above code is C++ and as a SAS programmer you don't have to worry about those kinds of hairy details right? Try this code from
Data Savant Consulting(which has a nice page discussing this very issue):
data _null_;
x = 7.3;
x = x+ 0.1;
y = 7.4;
if x = y then put "Duh! of course they are equal.";
else put "Doooh! " x " and " y " are not equal!!";
run;
Then go read this!
What Every Computer Scientist Should Know About Floating-Point ArithmeticOr if you don't have the time to grind through that, just remember comparing floats for equality is not usually a good idea.