fabs 是计算绝对值 %lf 是代表double类型.
下面是个小例子,浮点数比较
//浮点数比较 #include <stdio.h> #include <math.h> int main (void){ const double answer = 3.1415926; double response; printf("what is value of pi? "); scanf("%lf",&response); while (fabs(response - answer) > 0.0001) { printf("Try again "); scanf("%lf",&response); } printf("Close enough! "); return 0; }