簡體   English   中英

使用近似方法實現sqrt方法。 即使條件為假,也無法退出循環

[英]Implement sqrt method using the approximation approach. Cannot exit loop even the condition is false

我是如此接近完成我的實際問題,只是堅持不知道為什么我得到正確的結果后無法退出循環。

問題要求使用近似方法實現sqrt方法。

  1. num是應用sqrt方法的數字。
  2. 對於num> 1lowerLimit = 1lowerLimit = 1 upperLimit = number
  3. 然后找到midpointlowerLimitupperLimit其是(lowerLimit+upperLimit)/2
  4. 平方midpointsquareMidpoint = Math.pow(midpoint, 2)
  5. 如果squareMidpoint > numsquareMidpoint > 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);
    }
}

這是我的實際問題:

您已經要求使用下面描述的近似方法實現sqrt方法,而不是在Math類中使用sqrt方法:

對於大於1的數字,平方根方法必須首先將下限設置為1和數字的上限(因為數字的平方根始終位於1和數字之間)。

然后必須確定下限和上限之間的中點並評估中點的平方。 如果中點的平方大於數字,則平方根方法必須將上限移動到中點,類似地,如果中點的平方小於數字,則必須將下限移動到中點。

移動適當的限制后,平方根方法必須評估新的中點並重復該過程,直到獲得所需的精度。

雙精度浮點數所需的精度為8位有效數字。 任何迭代的精度可以通過將限制之間的差除以下限來確定。

當這小於1/108時,限制之間的任何數字都將是對所需精度的數字的平方根的估計。 為了最小化誤差,平方根方法應返回滿足精度要求的最終限制之間的中點。

平方根方法必須返回零和一的特殊情況的精確值。

如果應用程序嘗試計算負數的平方根,則平方根方法應顯示相應的消息並終止該程序。

任何幫助表示贊賞!

如果使用更高位數打印upperLimit/lowerLimit ,您將看到它小到1.0000000000000002 ,但永遠不會達到1,這就是您的循環永遠不會結束的原因。

只要:不要停留在循環中:

upperLimit/lowerLimit != 1

你應該把條件改為:

while (upperLimit - lowerLimit > 0.0000000001)

當限制彼此足夠接近時,它將退出循環。

這就是步驟#6的意思是“8位有效數字” - 你的近似應該得到前8位有效數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM