I am writing a numerical code that needs to make extensive (and possibly fast) comparisons among double precision numbers. My solution to compare two numbers A and B involves shifting A to the left (or right) by an epsilon and checking whether the result is bigger (or smaller) than B. If so, the two doubles are the same. (Extra coding needs to be done for negative or zero numbers).
This is the comparing function:
#define S_
inline double s_l (double x){
if(x>0){return 0.999999999*x;}
else if(x<0){return 1.00000001*x;}
else {return x-0.000000000001;}
}
inline double s_r (double x){
if(x>0){return 1.00000001*x;}
else if(x<0){return 0.999999999*x;}
else{return x+0.000000000001;}
}
inline bool s_equal (double x,double y){
if(x==y){return true;}
else if(x<y && s_r(x)>y){return true;}
else if(x>y && s_l(x)<y){return true;}
else{return false;}
}
#endif
Since this is part of a MonteCarlo algorithm and s_equal(x,y) is called millions of times, I wonder if there is any better or faster way to code this, understandable at a simple level.