已知 sqrt (2)约等于 1.414,要求不用数学库,求 sqrt (2)精确到小数点后 10 位
yum install -y gcc yum install -y gcc-c++ yum install -y cmake3
编辑~/.zshrc,增加一行,设置别名
alias cmake="cmake3"
#include <stdio.h>
#include <cmath>
int main() {
double left = 1.41;
double right = 1.42;
double current = 0.0d;
double result = 0.0d;
int loop_count = 0;
while (true) {
++loop_count;
current = (left + right)/2;
result = current * current;
if (fabs(result - 2.0d) < 0.0000000000001d) {
break;
}
if (result < 2.0d) {
left = current;
} else {
right = current;
}
}
printf("current:%lf, loop:%d, result:%lf", current, loop_count, result);
return 1;
}
cmake_minimum_required (VERSION 2.8) project (sqrttest) add_executable(Demo sqrt.cpp)
