tags:

views:

499

answers:

4

Hi, I'm doing a homework project that requires this:

Below you will find the code that I have written to compute the square root of a number using the Newton-Raphson method. Include it in your project. For this project your job will be to write a test harness that tests the code that I have written. Carefully read the method prologue to understand how the function should work. Your test harness will provide a loop that:

  1. Prompts the user to enter in a test value.
  2. Gets the user's input. If a zero is entered, your program will print out a report and terminate.
  3. Calls the Sqrt method provided in this project, and saves the return value in a double variable.
  4. Calls the Sqrt method that is built into the Math class and saves the return value in a second double variable.
  5. Compare these two values to see if they are equal.
  6. When the user indicates that they are done (by entering a zero) display a report that shows this information: * How many test cases you executed * How many passed * How many failed

So I've done all this without any problems in about 15 minutes, however for extra credit he asks us to find what is wrong with his Sqrt method and fix it so its return value equals the Math.Sqrt return value of the .net framework. I can't seem to find the problem in his method, and I want to find it, so I was wondering if anyone could point me in the right direction as to what the problem is with his Sqrt method? Thanks.

Here is my complete code:

// 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;
}

}

A: 

Math.sqrt returns NaN if the argument is negative. Also, I think EPS would be much smaller.

Artelius
Also, thank you, as that was indeed what i needed to do
Alex
Then please upvote my answer.
Artelius
+2  A: 

Try setting const double EPS = 0.000000001; - that's your epsilon. That's what determines the precision of your answer.

Rooke
ahh thanks! That makes perfect sense! I cannot believe i didn't think about that before. Thanks a ton.
Alex
Sorry, 0.000000001 is just a random value I chose. Like I said, it's the determinant of how precise your square root approximation is, so your square root will be accurate to +/- EPS.
Rooke
Ok, that makes sense, thanks again.
Alex
A: 

Thanks to both of you! That's what I needed to do! It makes sense, and I am very grateful to both of you for making me think about that. My question has been answered.

Alex
You don't have to tell us like this -- that's what the checkmark under each answer is for. Have a look at this: http://meta.stackoverflow.com/questions/5234/accepting-answers-what-is-it-all-about
Jed Smith
ok, thanks, im new around here, so i'm still learning the functions.
Alex
np :) I'd delete this answer as well, using the link under the text.
Jed Smith
A: 

May I ask why Espilon should exactly be 0.000000001;, and not even smaller?

Alex