Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to make a square root function in c++ without stl

double sqrt(double number)
{
    double error = 0.00001; //define the precision of your result
    double s = number;

    while ((s - number / s) > error) //loop until precision satisfied 
    {
        s = (s + number / s) / 2;
    }
    return s;
}
Comment

how to make a square root function in c++ without stl

    #include <iostream>
    using namespace std;

    double SqrtNumber(double num)
    {
             double lower_bound=0; 
             double upper_bound=num;
             double temp=0;                    /* ek edited this line */

             int nCount = 50;

        while(nCount != 0)
        {
               temp=(lower_bound+upper_bound)/2;
               if(temp*temp==num) 
               {
                       return temp;
               }
               else if(temp*temp > num)

               {
                       upper_bound = temp;
               }
               else
               {
                       lower_bound = temp;
               }
        nCount--;
     }
        return temp;
     }

     int main()
     {
     double num;
     cout<<"Enter the number
";
     cin>>num;

     if(num < 0)
     {
     cout<<"Error: Negative number!";
     return 0;
     }

     cout<<"Square roots are: +"<<sqrtnum(num) and <<" and -"<<sqrtnum(num);
     return 0;
     } 
Comment

how to make a square root function in c++ without stl

#include <math.h>

double sqrt(double x) {
    if (x <= 0)
        return 0;       // if negative number throw an exception?
    int exp = 0;
    x = frexp(x, &exp); // extract binary exponent from x
    if (exp & 1) {      // we want exponent to be even
        exp--;
        x *= 2;
    }
    double y = (1+x)/2; // first approximation
    double z = 0;
    while (y != z) {    // yes, we CAN compare doubles here!
        z = y;
        y = (y + x/y) / 2;
    }
    return ldexp(y, exp/2); // multiply answer by 2^(exp/2)
}
Comment

how to make a square root function in c++ without stl

double SqrtNumber(double num)
{
    double lower_bound=0; 
    double upper_bound=num;
    double temp=0;

    while(fabs(num - (temp * temp)) > SOME_SMALL_VALUE)
    {
           temp = (lower_bound+upper_bound)/2;
           if (temp*temp >= num)
           {
                   upper_bound = temp;
           }
           else
           {
                   lower_bound = temp;
           }
    }
    return temp;
 }
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to replace part of string with new string c++ 
Cpp :: c++ changing string to double 
Cpp :: enum c++ 
Cpp :: c++ inheritance 
Cpp :: char to int in c++ 
Cpp :: bfs to detect cycle in undirected graph 
Cpp :: c++ read matttrix from text file 
Cpp :: lower bound and upper bound in c++ 
Cpp :: preorder 
Cpp :: il2cpp stuck unity 
Cpp :: put text on oled 
Cpp :: operator precedence in cpp 
Cpp :: C++ fibo 
Cpp :: onoverlapbegin ue4 c++ 
Cpp :: Initialize Vector Iterator 
Cpp :: how to write int variable c++ 
Cpp :: rethrow exception c++ 
Cpp :: C++ String Concatenation Example 
Cpp :: arduino falling edge 
Cpp :: c++ queue 
Cpp :: shortest path in unweighted graph bfs 
Cpp :: C++ to specify size and value 
Cpp :: C++, binary search recursive 
Cpp :: how we can write code to remove a character in c++ 
Cpp :: pow c++ 
Cpp :: Implicit conversion casting 
Cpp :: linkedlist in c++ 
Cpp :: bool nullable to bool c# 
Cpp :: Common elements gfg in c++ 
Cpp :: arduino bleutooth module hc-05 with led 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =