繁体   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