#include <math.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
const double ANSWER=3.14159;
double response;
printf("what is the value of pi?\n");
scanf("%lf",&response);
while(fabs(response-ANSWER)>0.0001)
{printf("Try again!\n");
scanf("%lf",&response);
}
printf("Close enough!\n");
return 0;
}
使用fabs()函數(shù)(聲明在math.h)可以方便地比較浮點數(shù)熙参,該函數(shù)返回一個浮點值的絕對值。
如果不適用fabs()函數(shù)while(response-ANSWER>0.0001)