https://leetcode-cn.com/problems/sqrtx/solution/er-fen-cha-zhao-niu-dun-fa-python-dai-ma-by-liweiw/
牛顿迭代法原理
利用一级泰勒级数展开式
public double mySqrt(int m, int n) {
if (m == 0) return 0;
double x0 = 1;
double pre;
while (true) {
pre = x0;
x0 = (x0 + m / x0) / 2;
if (Math.abs(x0 - pre) < Math.pow(10, -n)) {
break;
}
}
BigDecimal bg = new BigDecimal(x0);
double res = bg.setScale(n, BigDecimal.ROUND_FLOOR).doubleValue();
return res;
}
public double mySqrt3(int m, int n) {
if (m == 0) return 0;
int flag = 1;
if (m < 0) {
flag = -1;
}
int absM = Math.abs(m);
double x0 = 1;
double pre;
while (true) {
pre = x0;
x0 = (2 * Math.pow(x0, 3) + absM) / (3 * Math.pow(x0, 2));
if (Math.abs(x0 - pre) < Math.pow(10, -n)) {
break;
}
}
BigDecimal bg = new BigDecimal(x0);
double res = bg.setScale(n, BigDecimal.ROUND_FLOOR).doubleValue();
return res * flag;
}