乐趣区

关于算法:给double类型的数据开根号

须要给 double 类型的数据开根号
要求精度在 0.0001 之内

public double getSqrtNum(double num){
    double left;
    double right;
    double mid;
    if(n<1&&n>0){
        left = num;
        right = 1;
    }
    if(n>1){
        left = 0;
        right = num;
    }
    mid = (left+right)/2;
    while(Math.abs(mid*mid-n)>0.0001){if(mid*mid<n){left = mid;}else{right = mid;}
        mid = (left+right)/2;
    }
    return mid;
}
退出移动版