欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

浮点数的比较

程序员文章站 2022-03-30 16:37:12
...
//功能:循环一直提醒用户继续输入,除非用户输入的值与正确值之间相差0.0001;
//伪代码:用到fabs()引用math.h  const 定义一个值  交互
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
	const float answer = 3.14159;
	float response;
	printf("what is the value of pi?\n");
		scanf("%f", &response);
	while (fabs(response - answer) > 0.0001)
	{
		printf("try it again!\n");
			scanf("%f", &response);
	}
	printf("close enough!\n");
	system("pause");
	return 0;
}