每日一道面试题-02
已知 sqrt (2)约等于 1.414,要求不用数学库,求 sqrt (2)精确到小数点后 10 位
思路:二分法
double sqrt2() {
double EPSINON = 0.0000000001;
double low = 1.4;
double high = 1.5;
double mid = 0;
while ((high - low) >EPSINON) {
double mid = (high - low) / 2;
if (mid * mid < 2) {
high = mid;
}else {
low = mid;
}
}
return mid;
}