#include <stdio.h>
#include <math.h>
double getsqrt(double a) {
double x1 = a, x2;
while (1) {
x2 = (x1 + a / x1) / 2;
if (fabs(x1 - x2) < 1e-6)
break;
x1 = x2;
}
return x1;
}
void main() {
int i;
for (i = 1; i <= 5; i++) {
printf("%12.6f\n", getsqrt(i));
}
}
求平方根:编写函数double getsqrt(double a),计算x=√a(只计算a=1,2,3,4,5的值)。已知计算x=√a的迭代公式如下所示,要求累加到某项的绝对值小于1e-6时为止。
猜你喜欢
转载自blog.csdn.net/qq_38490457/article/details/104739077
今日推荐
周排行