square root

Hello :)
i have written a function that gives me the square root of a number. Yes i know there is already a function in <cmath> that gives square roots but i thought it would be pretty challenging (and therefore fun) to try writing one on my own. I wrote my code while i was in school, listening to my Irish teacher ramble on and on about useless shit, so it's probably not the best possible way of finding the square root of a number.

so here's where you come in ;)
i'd love if i could get some advice on my function and maybe different ways of achieving the same thing but with less code. thank you :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
double msqrt(double x)
{
	double adder=1;
	double y = x;


	for(int a=0; ; a++)
	{
		if(y*y > x) y = y/2;
		if(y*y <= x) break;
	}
//*************************************************************************

	while (y*y < (x - 0.00001) || y*y > (x + 0.00001))
	{
		for (int a=0; ; a++)
		{
			if(y*y < (x-0.00001))
			{
				y = y + adder;
				if (y*y > (x-0.00001))
				{
					adder = adder/2;
					break;
				}
			}

			if(y*y > (x+0.00001))
			{
				y = y-adder;
				if(y*y < (x+0.00001))
				{
					adder = adder/2;
					break;
				}
			}


			if(y*y > (x-0.00001) && y*y < (x+0.00001)) break;
		}
//*************************************************************************
	}


	return y;
}
Purely as a matter of style, when a value such as 0.00001 is used more than once in a program, it is better to use a defined constant, such as
const double accuracy = 0.00001; - use whatever name is appropriate, instead of typing the same value repeatedly. This has several advantage, one being that if you want or need to change the value, it only has to be done once, in one place.

As for the algorithm, it looks a little complex.

One way to find a square root is to start with an initial estmate, which can be any reasonable value, such as 1.0, or the the number itself.

Then improve the accuracy of the estimate by repeatedly carrying out this calculation:
1
2
new_estimate = ((number/old_estimate) + old_estimate) * 0.5;
old_estimate = new_estimate;
Last edited on
I thought it looked overly complicated as well. Though there are at least a dozen well known algorithms for finding square roots. They can get pretty interesting.
yeah .. i am relatively new to c++ (and programming in general) and i tend to use over complicated methods to carry out functions which shouldn't be so complicated..

the advice seems legit though :) thank you for that
It's more of a lack of mathematical knowledge :) But it'll come. If you're curious, wikipedia has a pretty decent article on several different methods:
http://en.wikipedia.org/wiki/Methods_of_computing_square_roots

EDIT:
Some of the math may be intimidating, but most if it's pretty simple. If you have any questions just ask. There's lots of math gurus here.
Last edited on
wow.
beautiful!
thank you so much.. i've always loved math and am looking for ways to improve daily :) im only in my last year of secondary school and as you can imagine, the math is not that complex but i'll look into it :) thanks again!
what about this? could i simplify this further?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
double msqrt(double x)
{
	double adder = 1;
	double y = x;
	double cst = 0.00001;
	while (y*y > x) y /= 2;

	while (y*y < (x-cst) || y*y > (x+cst))
	{
		if(y*y < x)
		{
			y += adder;
			if(y*y > x)adder /= 2;
		}

		if(y*y > x)
		{
			y -= adder;
			if(y*y < x)adder /= 2;
		}
	}
		return y;
}
Topic archived. No new replies allowed.