find Sqrt witout math.h

i've created this code but gives me wrong values ! So what is wrong ??

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
#include <iostream.h>
#include <conio.h>

main()
{ 
double n,end,start=0,mid;

cin>>n;
end=n/2;

while(start<end)
{
mid=(start+end)/2;


if ((n-(mid*mid)<=0.001) && (n-(mid*mid)>=0))
{cout<<mid;break; }

 if(n>mid*mid)
{start=mid+0.001 ;
end=end;}

if(n<mid*mid)
{end=mid-0.001;
 start=start;}


 }

 getch();
 }
Last edited on
Why are you dividing n by 2 at the beginning?
Your compiler is extremely outdated. update it ASAP.

Let me show what happens in your code:
Imagine that you have entered 88.
in the begunning:
1
2
3
start = 0;
end = 44;
mid = 22;
First check: if(n-(mid*mid)<=0.001)if(88 - (22*22) <= 0.001)true
Last edited on
Because the Sqrt of any number(n) can't be greater than (n/2) .. so i decreased the Possibilities
@MiiNiPaa ok i edit it and the new if condition beacome

if ((n-(mid*mid)<=0.001) && (n-(mid*mid)>=0))

but still gives worng values or not gives anything!
Last edited on
worng values
How are they wrong? show an example?

not gives anything
Precision problems. For example 402 = 1600 and 40.001 = 1600,080001. Difference of 0.08: it might skip over checked range.
Also: difference of -0.000000001 will not be taken by your program.
ok no wrong values ..
but there is no results!! i tried to find Sqrt of number from (2 to 20)
guess what only 16 gives Sqrt =4 ?!
1) if you enter 2, you will check values in range [0..1] when real value is ≈1.7

Values of mid and calcualted difference, which are checked for input = 25:
1
2
3
4
5
6
7
8
9
10
11
12
13
6.25
3.1244999999999998
4.6872499999999997
5.4686249999999994
5.0779374999999991
4.8825937499999998
4.9802656249999995
5.0291015624999993
5.0046835937499994
4.9924746093749999
4.9985791015625001
5.0016313476562502
5.0001052246093751
-14.0625
15.237499750000001
3.0296874375000016
4.9058593906249932
0.78544925390623987
1.1602782724609391
0.19695430444336637
-0.29186252593993345
-0.046857873550408158
0.075197274745942622
0.014206965422630446
-0.016316137857678825
-0.0010522571659699054
As you can see no values suits your condition.
@MiiNiPaa So what could i do for the range to make it possible to find Sqrt of any numbers with accuracy 0.001 ??
Help!
Use another algorithm. You can use Tailor series for that for example.
So u say that there is nothing to do to make this code right ?
Well, you can adjust step size and comparsion precision depending on input value. But you need to mathematically analyse your algorithm and deduct number/constants relations
@MiiNiPaa Thank u 4 Helping
closed account (2AoiNwbp)
I also think that Taylor's suits better for finding a square root. You can google Taylor`s formula which divides a function by its derivative to get a good approximation. And that`s all you`ll get, an approximation, not an exact value. But let`s take a look at your algorithm.

With n/2 what you`re doing is to reduce the search space in halves, so you are using binary search, which is good to get a good approximation to a square root.

On the other hand, you also seem to be using something like a brute force algorithm, increassing start by epsilon (0.001) in line 20, which doesn`t sound good for a square root, in terms of computing costs.

Why are you testing (n-(mid*mid)>=0) in line 16? just for clarification, it might be abs()...? if you cannot use math.h, then, define your own abs. It will clarify your code.

If you can`t find anwers for numbers between 0 and 1, then you might want to take a look at the search space you are searching for.

regards,
Alejandro
Topic archived. No new replies allowed.