java - 使用近似方法实现 sqrt 方法。即使条件为假也无法退出循环

标签 java infinite-loop square-root

我已经很接近完成我的实际问题了,只是卡在不知道为什么我在得到正确结果后无法退出循环。

问题要求使用近似方法实现 sqrt 方法。

  1. num是应用sqrt方法的数字。
  2. 对于num> 1 , lowerLimit = 1upperLimit = number
  3. 然后找到midpointlowerLimitupperLimit这是 (lowerLimit+upperLimit)/2
  4. 平方 midpoint , squareMidpoint = Math.pow(midpoint, 2)
  5. 如果 squareMidpoint > num , upperLimit = midpoint ,否则lowerLimit= midpoint .
  6. 再次重复第三步,直到得到具有 8 位有效数字的 num。

从第 1 步到第 5 步,我认为我做得正确,因为输出是正确的。

我其实不太明白第6步。

我的问题是如果 num = 4 ,程序不断打印2永远。

这是我的代码:

import java.lang.Math; 
public class p2q4 {

    public static void main(String[] args) {
        
        //Implement the sqrt method using the approximation approach
        
        //initialized lowerLimit and upperLimit
        double lowerLimit = 0, upperLimit = 0;
        
        //num is the number to square root.
        double num = 5;
        
        //For number greater than one,
        if (num > 1) {
            lowerLimit = 1; //lower limit to one
            upperLimit = num; //upper limit to the number
        }

        double squareMidpoint;
        double midpoint;

        do {
            //Determine the midpoint between the lower and upper limits
            midpoint = (lowerLimit + upperLimit) / 2;
            
            //Evaluate the square of the midpoint
            squareMidpoint = Math.pow(midpoint, 2);

            //If the square of the midpoint is greater than the number
            if (squareMidpoint > num) {
                //upper limit to the midpoint
                upperLimit = midpoint;
            } else {
                //lower limit to the midpoint
                lowerLimit = midpoint;
            }
            
            //for debugging purpose
            System.out.printf("midpoint=%f  squareMidpoint=%f upperLimit=%f lowerLimit=%f upperLimit/lowerLimit=%f\n", midpoint, squareMidpoint, upperLimit, lowerLimit, upperLimit/lowerLimit);

          
        //even though upperLimit/lowerLimit is '1' but still keep looping
        } while (upperLimit/lowerLimit != 1); //I not sure this condition is correct.
        
        //Output
        System.out.printf("x = %.0f, root = %f\n", num, midpoint);
    }
}

这是我的实际问题:

Instead of using the sqrt method in the Math class, you have been asked to implement the sqrt method using the approximation approach described below:

For numbers greater than one, the square root method must initially set a lower limit to one and an upper limit to the number (since the square root of the number always lies between one and the number).

It must then determine the midpoint between the lower and upper limits and evaluate the square of the midpoint. If the square of the midpoint is greater than the number, the square root method must move the upper limit to the midpoint and similarly if the square of the midpoint is less than the number, it must move the lower limit to the midpoint.

After moving the appropriate limit, the square root method must evaluate a new midpoint and repeat the process until the desired precision is obtained.

The required precision for double precision floating point numbers is 8 significant digits. The precision at any iteration can be determined by dividing the difference between the limits by the lower limit.

When this is less than 1/108 any number between the limits will be an estimate of the square root of the number to the required precision. To minimize the error, the square root method should return the midpoint between the final limits that satisfy the precision requirement.

The square root method must return exact values for the special cases of zero and one.

If an application attempts to calculate the square root of a negative number, the square root method should display an appropriate message and terminate the program.

感谢任何帮助!

最佳答案

如果您使用更高的有效数字打印 upperLimit/lowerLimit ,您会发现它会小至 1.0000000000000002,但永远不会达到 1,这就是循环的原因永无止境。

不要停留在循环中,只要:

upperLimit/lowerLimit != 1

您应该将条件更改为:

while (upperLimit - lowerLimit > 0.0000000001)

当限制彼此足够接近时,它将退出循环。

这就是第 6 步中“8 个有效数字”的含义 - 您的近似值应该使前 8 个有效数字正确。

关于java - 使用近似方法实现 sqrt 方法。即使条件为假也无法退出循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44481255/

相关文章:

reactjs - 具有调度函数的 useSelector 和 UseEffect 由于不更新状态而创建循环

c - 求一个数的近似平方根

c# - 检查完美广场的最短方法?

java - 将一个列表中的元素添加到另一个列表中

java - 如何使用 java WebDriver 在 Firefox 中打开 "about:preferences"

java - 使用双数组和用户输入查找数组的最小值/最大值

Prolog - 避免无限循环

python-3.x - 异步、等待和无限循环

c - C 中的平方根算法

java - 如何在windows server 2008上通过java程序创建、挂载、卸载VHD?