圖1 牛頓迭代法計算平方根過程
推導過程:
因爲
所以
// 牛頓迭代法計算平方根
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)//也就是1-c/(t^2)=err
t = (c/t + t)/2.0;
return t;
}
圖1 牛頓迭代法計算平方根過程
推導過程:
因爲
所以
// 牛頓迭代法計算平方根
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)//也就是1-c/(t^2)=err
t = (c/t + t)/2.0;
return t;
}
unsigned int MAX = 32; // 2的5次方 unsigned int index = 31; index = (index + 100) % MAX; printf ("inde