簡體   English   中英

在C#中查找root的math.sqrt與Newton-Raphson方法

[英]math.sqrt vs. Newton-Raphson Method for finding roots in c#

我正在做一個作業項目,需要這樣做:

在下面,您將找到我編寫的用於使用Newton-Raphson方法計算數字平方根的代碼。 將其包括在您的項目中。 對於這個項目,您的工作是編寫一個測試工具,測試我編寫的代碼。 仔細閱讀方法序言,以了解該功能應如何工作。 您的測試工具將提供一個循環:

  1. 提示用戶輸入測試值。
  2. 獲取用戶的輸入。 如果輸入零,程序將打印出報告並終止。
  3. 調用此項目中提供的Sqrt方法,並將返回值保存在double變量中。
  4. 調用內置於Math類中的Sqrt方法,並將返回值保存在第二個double變量中。
  5. 比較這兩個值以查看它們是否相等。
  6. 當用戶表明已完成操作(輸入零)時,將顯示一個報告,其中顯示以下信息:*您執行了多少個測試用例*通過了多少個*失敗了多少個

因此,我在大約15分鍾內完成了所有操作,沒有任何問題,但是為了獲得額外的榮譽,他要求我們找出他的Sqrt方法有什么問題並進行修復,以使其返回值等於.net框架的Math.Sqrt返回值。 我似乎無法在他的方法中找到問題,而我想找到它,所以我想知道是否有人可以針對我的Sqrt方法出什么問題向我指出正確的方向? 謝謝。

這是我完整的代碼:

// declare variables
    double userInput = 0.0;
    double debrySqrtReturnValue = 0.0;
    double dotNetSqrtReturnValue = 0.0;
    int testCasesExecuted = 0;
    int testsPassed = 0;
    int testsFailed = 0;
    bool isEqual = false;

    do
    {
        // Prompt the user to enter in a test value
        Console.Write("Please enter a positive integer value: ");
        userInput = double.Parse(Console.ReadLine());

        if (userInput != 0)
        {
            debrySqrtReturnValue = Sqrt(userInput);
            dotNetSqrtReturnValue = Math.Sqrt(userInput);

            Console.WriteLine("The square root of {0} is: {1}", userInput, debrySqrtReturnValue);
            Console.WriteLine("The real square root of {0} is: {1}\n", userInput, dotNetSqrtReturnValue);

            if (debrySqrtReturnValue == dotNetSqrtReturnValue)
                isEqual = true;
            else
                isEqual = false;

            if (isEqual)
                testsPassed++;
            else
                testsFailed++;

            testCasesExecuted++;
        }
    } while (userInput != 0);

    Console.WriteLine("\n\n--------------------------------Report---------------------------------");
    Console.WriteLine("test cases excecuted: {0}", testCasesExecuted);
    Console.WriteLine("tests passed: {0}", testsPassed);
    Console.WriteLine("tests failed: {0}", testsFailed); 

    Console.ReadLine();
}


// The Sqrt method
// Purpose: to compute the square root of a number
// Parameters: a positive, non-zero integer
// returns: a double, which is the square root of the number
// ---------------------------------------------------------
static double Sqrt(double number)
{
    // constants to use in the calculation
    const int FIRST_APPROX = 2;
    const double EPS = 0.001;

    // a local variable
    double xN = 0;

    // pick 2 as first approximation
    double xNPlus1 = FIRST_APPROX;
    do
    {
        xN = xNPlus1; 
        xNPlus1 = xN - ((xN * xN - number) / (FIRST_APPROX * xN));

    } while (Math.Abs(xNPlus1 - xN) > EPS);
    return xN;
}

}

嘗試將const double EPS = 0.000000001;設置const double EPS = 0.000000001; -那就是你的epsilon。 這就是確定答案精確度的原因。

如果參數為負,Math.sqrt返回NaN。 另外,我認為EPS會小得多。

暫無
暫無

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

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