【问题标题】:math.sqrt vs. Newton-Raphson Method for finding roots in c#在 c# 中求根的 math.sqrt 与 Newton-Raphson 方法
【发布时间】:2009-11-08 06:44:45
【问题描述】:

我正在做一个需要这个的家庭作业项目:

您将在下面找到我编写的代码,用于使用 Newton-Raphson 方法计算数字的平方根。将其包含在您的项目中。对于这个项目,您的工作将是编写一个测试工具来测试我编写的代码。仔细阅读方法序言以了解该功能应如何工作。您的测试工具将提供一个循环:

  1. 提示用户输入测试值。
  2. 获取用户的输入。如果输入零,您的程序将打印出一份报告并终止。
  3. 调用本项目提供的Sqrt方法,并将返回值保存在double变量中。
  4. 调用内置在 Math 类中的 Sqrt 方法并将返回值保存在第二个双精度变量中。
  5. 比较这两个值是否相等。
  6. 当用户指示他们完成(通过输入零)时,会显示一个显示此信息的报告: * 你执行了多少个测试用例 * 通过了多少 * 有多少失败

所以我在大约 15 分钟内完成了所有这一切,没有任何问题,但是为了额外的功劳,他要求我们找出他的 Sqrt 方法的问题并修复它,使其返回值等于 Math.Sqrt 的返回值.net 框架。我似乎无法在他的方法中找到问题,我想找到它,所以我想知道是否有人能指出我正确的方向,他的 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;
}

}

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    尝试设置const double EPS = 0.000000001; - 这是您的 epsilon。这决定了您的答案的准确性。

    【讨论】:

    • 啊谢谢!这很有意义!我不敢相信我以前没有考虑过。非常感谢。
    • 对不起,0.000000001 只是我选择的一个随机值。就像我说的,它是平方根近似值精确度的决定因素,因此您的平方根将精确到 +/- EPS。
    【解决方案2】:

    如果参数为负,Math.sqrt 返回 NaN。另外,我认为 EPS 会小得多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多