Is it possible to create a square root program without math.h
I have created a program without using sqrt and the pwr function but instead have used logs and exponent.
The professor said that math.h is not allowed. I am aware that this library is important for computation, but how do I compute the math without using the math.h?
Would I need to create my library if so how?
tformed
Junior Poster in Training
58 posts since May 2007
Reputation Points: 21
Solved Threads: 1
Can't you use loops and division and multiplication to get the square root ? For example, the square root of 16 can be found by trial-and-error multiplying 1*1, then 2*2, then 3*3, then 4*4 until you get a number that is close to the original number.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Keep dividing by "test numbers" until the modulus and the test number are equal. Of coarse things will get a bit stickier when the square root is a non-integer...
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
More specifically, one way is binary search. Given a range [low, high] in which your square root lies, you can test if the square of (low + high) / 2 is greater than the value you're square rooting or not. If it's greater, then your new range is [low, (low + high)/2], if less, then your new range is [(low + high)/2, high]. Keep narrowing this range in half until the value (low + high)/2 is either <= low or >= high (which it will be, eventually, because you're using doubles.)
You need to pick appropriate starting values of low and high, of course -- one choice is 1 and the number you're taking the square root of.
This algorithm is rather slow, requiring about 52 or 64 iterations for reasonable numbers (if you're using the double datatype), but if if the number you're square-rooting is very, very close to zero, it will have to run thousands of iterations before it terminates (because instead of running into the limits of a double's precision, you'll be descending down to lower and lower exponents). Maybe you'd then want to make 0 a special case, tested for at the beginning.
A faster algorithm to find the square root of x is to compute f(f(...(f(f(f(f(1))))...)), where f(y) = (x/y + y)*0.5, and where f is iterated sufficiently many times.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
Why not use e^((ln(x))/2) ? That's the formula they use to find square roots (x is the number to be calculated and e is the know constant).
mikrosfoititis
Junior Poster in Training
74 posts since Nov 2011
Reputation Points: 18
Solved Threads: 11
and where do you get the ln() function? Frommath.h? Which can't be used?
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Of course you could always write your own ln function.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>> just see documentation of math.h or cmath copy and paste the fucntions for sqrt and done.
The code for the functions isn't in math.h, only function prototypes. So copying them is little or no value.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
and where do you get the ln() function? Frommath.h? Which can't be used?
Uhm... Yes, you have a point there :P
mikrosfoititis
Junior Poster in Training
74 posts since Nov 2011
Reputation Points: 18
Solved Threads: 11