how to make a square root function in c 2b 2b without stl

Solutions on MaxInterview for how to make a square root function in c 2b 2b without stl by the best coders in the world

showing results for - "how to make a square root function in c 2b 2b without stl"
Ivan
26 Sep 2019
1#include <math.h>
2
3double sqrt(double x) {
4    if (x <= 0)
5        return 0;       // if negative number throw an exception?
6    int exp = 0;
7    x = frexp(x, &exp); // extract binary exponent from x
8    if (exp & 1) {      // we want exponent to be even
9        exp--;
10        x *= 2;
11    }
12    double y = (1+x)/2; // first approximation
13    double z = 0;
14    while (y != z) {    // yes, we CAN compare doubles here!
15        z = y;
16        y = (y + x/y) / 2;
17    }
18    return ldexp(y, exp/2); // multiply answer by 2^(exp/2)
19}
20
Debora
13 Nov 2017
1double sqrt(double number)
2{
3    double error = 0.00001; //define the precision of your result
4    double s = number;
5
6    while ((s - number / s) > error) //loop until precision satisfied 
7    {
8        s = (s + number / s) / 2;
9    }
10    return s;
11}
12
Lynette
08 Jul 2017
1    #include <iostream>
2    using namespace std;
3
4    double SqrtNumber(double num)
5    {
6             double lower_bound=0; 
7             double upper_bound=num;
8             double temp=0;                    /* ek edited this line */
9
10             int nCount = 50;
11
12        while(nCount != 0)
13        {
14               temp=(lower_bound+upper_bound)/2;
15               if(temp*temp==num) 
16               {
17                       return temp;
18               }
19               else if(temp*temp > num)
20
21               {
22                       upper_bound = temp;
23               }
24               else
25               {
26                       lower_bound = temp;
27               }
28        nCount--;
29     }
30        return temp;
31     }
32
33     int main()
34     {
35     double num;
36     cout<<"Enter the number\n";
37     cin>>num;
38
39     if(num < 0)
40     {
41     cout<<"Error: Negative number!";
42     return 0;
43     }
44
45     cout<<"Square roots are: +"<<sqrtnum(num) and <<" and -"<<sqrtnum(num);
46     return 0;
47     } 
48
Giovanni
20 Jan 2019
1double SqrtNumber(double num)
2{
3    double lower_bound=0; 
4    double upper_bound=num;
5    double temp=0;
6
7    while(fabs(num - (temp * temp)) > SOME_SMALL_VALUE)
8    {
9           temp = (lower_bound+upper_bound)/2;
10           if (temp*temp >= num)
11           {
12                   upper_bound = temp;
13           }
14           else
15           {
16                   lower_bound = temp;
17           }
18    }
19    return temp;
20 }
21
queries leading to this page
find square root using the function in c 2b 2bc 2b 2b root and squaresquare root in c 2b 2bfind square root of number without using sqrt function in c 2b 2bc 2b 2b square root without mathhow to use square root in c 2b 2bhow to do sqrt in c 2b 2b without includesquare root funcction in c 2b 2bstl function to find square roothow to get square root in c 2b 2broot square c 2b 2bhow to take the square root of a number in c 2b 2bsquare root without using sqrt function in c 2b 2bfunction to find square root in c 2b 2bsquare root c 2b 2b without mathhow to find square root in c 2b 2b without sqrtbuilt in function in c 2b 2b to find square roothow to do square root in c 2b 2bhow to write square root in c 2b 2b programhow to take square root in c 2b 2bhow to make square root function in c 2b 2bc 2b 2b program to print the square rootsquare root program in cppsquare root c 2b 2b using functionget square root c 2b 2bhow to make a square root function in c 2b 2b without stlsquare root c 2b 2b without sqrtfunction header square root c 2b 2bsquare root of a number in c 2b 2b without sqrthow to get a square root in c 2b 2bhow to square root in c 2b 2bsquare root function c 2b 2bc 2b 2b square root implementationsquare root function in c 2b 2bsquare root function in cppsquare root in c 2b 2b without sqrthow to find square root in function c 2b 2bfind square root of a number without using sqrt function in c 2b 2bsquare root without sqrt in c 2b 2bsquare root c 2b 2bhow to make a square root function in c 2b 2b without stl