参照MSDN定义:
/* Compile options needed: none. Value of c is printed with a decimal
point precision of 10 and 6 (printf rounded value by default) to
show the difference
*/
#include
// Define your own tolerance
const double EPSILON = 1.00e-07;
const float FLT_EPSILON = 1.192092896e-07F;
const double DBL_EPSILON = 2.2204460492503131e-016;
#define FLOAT_EQ(x,v) (((v - EPSILON) < x) && (x <( v + EPSILON)))
int main()
{
float a, b, c;
a = 1.345f;
b = 1.123f;
c = a + b;
// if (FLOAT_EQ(c, 2.468)) // Remove comment for correct result
if (c == 2.468) // Comment this line for correct result
printf("They are equal.\n");
else
printf("They are not equal! The value of c is %13.10f,or %f",c,c);
}
原文:http://my.oschina.net/u/572632/blog/305603