2014-01-14 249 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

啊,好吧。谢谢! –