#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    16
    Rep Power
    0

    Function to perform Square Root...


    Hello. ^_^

    I'm working on a program that will estimate the square root of a given number with < 1/10 error. This is what I have so far:

    Code:
    #include <stdio.h>
    
    double mysqrt(double x);
    int main(void)
    {
    
    double mysqrt(double x);
      {
    
      double x;
      double a;
      int n = 0;
    
      printf("Please enter the number you want to find the square root for:\n");
      scanf("%lf", &a);
    
        if(a < 0)
        {
          printf("Invalid number entered.\n");
          return 0;
        }
        x = a;
        do
        {
          x = (1/2.0 * (x + (a / x)));
          n = n + 1;
        }
        while(n < 20);
      printf("The square root of that number is %f.\n", x);
    }
    return 0;
    }
    It seems to be working fine on the output. My question is, how do I run a check vs. error < 1/10 to make sure it meets that requirement? Can someone point me in the correct direction?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Well the first question would be, 1/10th of what?

    The initial value?
    The actual result?

    In the actual loop, you do things like
    error = fabs( x*x - a ); // the original input
    or
    error = fabs(x-a);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    16
    Rep Power
    0

    Initial Value..


    Originally Posted by salem
    Well the first question would be, 1/10th of what?

    The initial value?
    The actual result?

    In the actual loop, you do things like
    error = fabs( x*x - a ); // the original input
    or
    error = fabs(x-a);
    Sorry, 1/10 of the initial value, which I believe would be checked against a/2, correct?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > Sorry, 1/10 of the initial value, which I believe would be checked against a/2, correct?
    This doesn't make much sense.

    10% of 10000 is 1000.

    The square root of 10000 is 100.


    Saying the answer is 100 +/- 1000 isn't an especially accurate result.

    It gets much worse with larger numbers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    16
    Rep Power
    0

    Apologies...


    Originally Posted by salem
    > Sorry, 1/10 of the initial value, which I believe would be checked against a/2, correct?
    This doesn't make much sense.

    10% of 10000 is 1000.

    The square root of 10000 is 100.


    Saying the answer is 100 +/- 1000 isn't an especially accurate result.

    It gets much worse with larger numbers.
    I'm sorry, I'm terrible at explaining what I'm thinking... Our initial guess is supposed to be a/2 then run iterations from there. The relative error should be < 1/10%.
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    OK, let's start with a concrete example.

    sqrt(123456) is 351.363060096

    Now one interpretation of 1/10 is 0.1, which may mean computing the answer to 1 decimal point.
    Perhaps this means you want to stop if the computed answer is anywhere between 351.27 to 351.45.
    That is, fabs( sqrt(123456) - mysqrt(123456) ) < 0.1

    Another interpretation is that the square of the result should be within 0.1 of the original number.
    That is, fabs( 123456 - mysqrt(123456)*mysqrt(123456) ) < 0.1

    Or it could be something else.

    The point is, you need to find out exactly what the intention is, if you're going to write the correct code to meet the objective.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by salem
    The point is, you need to find out exactly what the intention is, if you're going to write the correct code to meet the objective.
    This is exactly what is written:

    " |(x_(k+1) - x_(k))/(x_(k+1))|<0.001 (i.e. error is less than 1/10%) "

    So, basically it wants us to iterate until the difference between x_(k) and x_(k+1) is less than the relative error.

    If I am understanding this correctly, I need to figure out how to set the first iteration to begin a/2 and continue until the outcome is meets the relative error parameter. So, I'm not sure if I need to change the n< equation to a different number of iterations to achieve that, or if I need to compare the result and continue the iteration until the result meets the parameter...
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Well the first thing to do is write the code using the same variable names as the spec.

    Eg.
    Code:
        xk = a / 2;
        do
        {
          xk_1 = (1/2.0 * (xk + (a / xk)));
          n = n + 1;
          // calculate error term here
          xk = xk_1;
        }
        while(n < 20);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    16
    Rep Power
    0

    Thank you!!!


    Got it working! Thank you! ^_^

IMN logo majestic logo threadwatch logo seochat tools logo