简体   繁体   English

使用巴比伦算法计算平方根

[英]Calculating the Square Root using the Babylon Algorithm

For my assignment, I am supposed to use a 'Tolerance Flag'. 对于我的任务,我应该使用“容差标志”。 Yet, I was able to complete the task with what I have so far. 但是,到目前为止,我已经能够完成我的任务。 What does my teacher mean by tolerance flag? 我的老师宽容标志是什么意思?

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

_ _

My Work: 我的工作:

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: OUTPUT:

1.5
1.4166666666666665
1.4142156862745097
1.4142135623746899
1.414213562373095

I suppose it means something like: 我想这意味着:

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

which outputs: 输出:

1.5
1.4166666666666665
1.4142156862745097
1.4142135623746899

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM