public class SqrtCalculate {
public static double recursion(double low, double high, double target) {
double temp = (low + high) / 2;
if (temp - low < 0.00000000001) {
return temp;
}
if (temp * temp > target) {
return recursion(low, temp, target);
} else {
return recursion(temp, high, target);
}
}
/**
*
* getSqrt:根号计算保留小数点10位. <br/>
*
* @author huangpf
* @param num
* @return
* @since JDK 1.6
*/
public static String getSqrt(double num) {
return String.format("%.10f", recursion(1, num, num));
}
public static void main(String[] args) {
System.out.println(getSqrt(5));
}
}
运行结果: