使用绝对差和容差值的比较示例

例如:

#include

#include

#define EPSILON 1e-6

int float_equal(double a, double b) {

return fabs(a - b) < EPSILON;

}

int main() {

double x = 0.1 * 3;

double y = 0.3;

if (float_equal(x, y)) {

printf("x 和 y 被认为相等\n");

} else {

printf("x 和 y 不相等\n");

}

return 0;

}

这里通过比较两数差的绝对值是否小于EPSILON来判断相等。