#include /** * A cheap way to compare doubles within a small threshold * @author Alisa Neeman aneeman@cse.ucsc.edu 11/29/2007 */ int sameDouble( double t1, double t2) { int i,j,k,m, rank; double bigger; double smaller; if ( t1 > 0 ) { // positive value: establish squeeze bigger = t1*1.01; smaller = t1*0.99; } else { bigger = t1*0.99; smaller = t1*1.01; } if ( t2 > bigger || t2 < smaller ){ printf("%f squeezes [ %f to %f ], against %f and fails\n", t1,smaller,bigger, t2 ); return 0; } return 1; // they are the same within 0.01 } int main() { sameDouble( -9.999, -9.111); sameDouble( 0.001, 0.00099 ); return 0; }