cyy3900

博客园 首页 联系 订阅 管理

 

 

public class CalcUtils {
    public static void main(String[] args) {
        System.out.println(sqrt(8));
    }
    public static double sqrt(double c) {
        if (c < 0) return Double.NaN;
        double err = 1e-15;
        double t = c;
        while ( Math.abs(t - c/t)>err*t){
            t = (c/t + t) / 2.0;
        }
        return t;
    }
}

 

posted on 2024-01-12 21:47  cyy3900  阅读(2)  评论(0编辑  收藏  举报