0
点赞
收藏
分享

微信扫一扫

迭代法求平方根

guanguans 2022-01-26 阅读 64
c语言

主要是由于浮点型数在计算机中存储的原因,浮点数比较相等不能用==,而要这样。

#include<stdio.h>
#include<math.h>
int main()
{
	double a,x;
	scanf("%lf",&a);
	x=1;
	while(fabs(1.0/2*(x+a/x)-x)>=0.00001)
	{
		x=1.0/2*(x+a/x);
	}
	x=1.0/2*(x+a/x);
	printf("%.3lf",x);
}
举报

相关推荐

0 条评论