在我的作业中,我应该使用“宽容旗”。然而,我能够用我到目前为止所拥有的完成这项任务。我的老师所说的宽容旗是什么意思?
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发布于 2014-01-14 20:29:27
我想这意味着:
while(Math.abs(x*x - 2) > TOLERANCE);其中产出:
1.5
1.4166666666666665
1.4142156862745097
1.4142135623746899https://stackoverflow.com/questions/21123319
复制相似问题