2014-01-14 119 views
0

對於我的任務,我應該使用'容差標誌'。然而,我完成了迄今爲止所完成的任務。我的老師用容忍旗說什麼?使用巴比倫算法計算平方根

Use a tolerance flag and make your variables a member of the double class. 
const double TOLERANCE = 5e-8; 
double x = 2.0; 

_

我的工作:

public class Sqrt_of_Two { 

    public static void main(String[] args) { 

     final double TOLERANCE = 5e-8; 
     double x = 2; 

     do{ 
      x = (x + 2/x)/2; 

      System.out.println(x); 
     }while(Math.ceil(x*x) > 2); 


    } 

} 

OUTPUT:

1.5 
1.4166666666666665 
1.4142156862745097 
1.4142135623746899 
1.414213562373095 
+0

@SotiriosDelimanolis這是一個在線分配。我還沒有得到答覆。 –

回答

4

我想它的意思是這樣的:

while(Math.abs(x*x - 2) > TOLERANCE); 

,輸出:

1.5 
1.4166666666666665 
1.4142156862745097 
1.4142135623746899 
+0

啊,好吧。謝謝! –