对于我的任务,我应该使用'容忍标志'。然而,到目前为止,我能够完成任务。我的老师对宽容旗帜的意义是什么?
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);
}
}
输出:
1.5
1.4166666666666665
1.4142156862745097
1.4142135623746899
1.414213562373095
答案 0 :(得分:4)
我想这意味着:
while(Math.abs(x*x - 2) > TOLERANCE);
输出:
1.5
1.4166666666666665
1.4142156862745097
1.4142135623746899