Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
August 25th, 2013, 08:56 PM
 WhatNow
Registered User

Join Date: Aug 2013
Posts: 16
Time spent in forums: 2 h 48 m 50 sec
Reputation 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
August 26th, 2013, 01:31 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,252
Time spent in forums: 2 Months 4 Weeks 1 Day 12 h 25 m 37 sec
Reputation Power: 1809
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

#3
August 26th, 2013, 02:35 AM
 WhatNow
Registered User

Join Date: Aug 2013
Posts: 16
Time spent in forums: 2 h 48 m 50 sec
Reputation Power: 0
Initial Value..

Quote:
 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?

#4
August 26th, 2013, 02:51 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,252
Time spent in forums: 2 Months 4 Weeks 1 Day 12 h 25 m 37 sec
Reputation Power: 1809
> 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.

#5
August 26th, 2013, 11:52 AM
 WhatNow
Registered User

Join Date: Aug 2013
Posts: 16
Time spent in forums: 2 h 48 m 50 sec
Reputation Power: 0
Apologies...

Quote:
 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%.

#6
August 26th, 2013, 01:21 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,252
Time spent in forums: 2 Months 4 Weeks 1 Day 12 h 25 m 37 sec
Reputation Power: 1809

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.

#7
August 26th, 2013, 02:36 PM
 WhatNow
Registered User

Join Date: Aug 2013
Posts: 16
Time spent in forums: 2 h 48 m 50 sec
Reputation Power: 0
Quote:
 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...

#8
August 26th, 2013, 03:44 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,252
Time spent in forums: 2 Months 4 Weeks 1 Day 12 h 25 m 37 sec
Reputation Power: 1809
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);
```

#9
August 26th, 2013, 04:15 PM
 WhatNow
Registered User

Join Date: Aug 2013
Posts: 16
Time spent in forums: 2 h 48 m 50 sec
Reputation Power: 0
Thank you!!!

Got it working! Thank you! ^_^

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Function to perform Square Root...