DSP

开方

2019-07-13 15:42发布

public static void main(String[] args) { System.out.println(开方(3,1E-10)); } public static double 开方(int n, double precision) { double k = 10000000.0; double w = 0; int i = 1; do { k = (k + n / k) / 2; w = Math.abs(k * k - n); System.out.println("次数=" + i + ",平方根=" + k + ",误差=" + w); i++; } while (w > precision); return k; }