#include <stdio.h>
#include <math.h>
double my_sqrt(double x) {
double head = 0,tail = x, mid;
if (x < 1.0) tail = 1.0;
#define EPSL 1e-7
while (tail - head > EPSL) {
mid = (head + tail) / 2.0;
if (mid * mid < x) head = mid;
else tail = mid;
}
#undef EPSL
return head;
}
int main() {
double x;
while (~scanf("%lf", &x)) {
printf("sqrt(%g) = %g\n", x, sqrt(x));
printf("my_sqrt(%g) = %g\n", x, my_sqrt(x));
}
return 0;
}
二分查找法求平方根
最新推荐文章于 2024-01-09 22:16:02 发布