判断浮点数是否为零的问题
如果是double类型的数a,判断是否为零,应该是fabs(a)<=1e-6呢?还是fabs(a)<=1e-15?
#include <stdio.h>
#include <float.h>
/*****************************************************
const float FLT_EPSILON = 1.192092896e-07F;
const double DBL_EPSILON = 2.2204460492503131e-016;
*****************************************************/
#define FLOAT_ZERO(z, v) ((v < (z + DBL_EPSILON)) && (z <( v + DBL_EPSILON)))
int main() {
double a, b, c, z, A, Z;
a = 1.345000000000001;
A = 1.3450000000000001;
b = 1.123;
c = 0.222;
z = a - b - c;
Z = A - b - c;
if(FLOAT_ZERO(z, 0.0)) printf("z == 0\nz = %g\n", z);
else printf("z != 0\nz = %g\n", z);
printf("\n");
if(FLOAT_ZERO(Z, 0.0)) printf("Z == 0\nZ = %g\n", Z);
else printf("Z != 0\nZ = %g\n", Z);
return 0;
}