70,022
社区成员




if (FLOAT_EQ(c, 3.500))
//这样当C在(3.500-0.001,3.500+0.0001)区间内,执行if
// Floating-point_number_precision.c
// 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 <stdio.h>
#define EPSILON 0.0001 // Define your own tolerance//定义你的可以容忍的范围
#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\n",c,c);
}