Skip to content
Related Articles

Related Articles

sqrt, sqrtl and sqrtf in C++

View Discussion
Improve Article
Save Article
  • Last Updated : 13 Jun, 2022
View Discussion
Improve Article
Save Article

There are various functions available in the C++ Library to calculate the square root of a number. Most prominently, sqrt is used. It takes double as an argument. The <cmath> header defines two more inbuilt functions for calculating the square root of a number (apart from sqrt) which has an argument of type float and long double. Therefore, all the functions used for calculating square root in C++ are:

Function

Datatype

sqrt

double

sqrtf

float

sqrtl

long double

The functions have been discussed in detail below:

A) double sqrt(double arg): It returns the square root of a number to type double. 

Syntax:

double sqrt(double arg)

CPP




// CPP code to illustrate the use of sqrt function
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    double val1 = 225.0;
    double val2 = 300.0;
 
    cout << fixed << setprecision(12) << sqrt(val1) << endl;
    cout << fixed << setprecision(12) << sqrt(val2) << endl;
 
    return (0);
}

Output

15.000000000000
17.320508075689

Time Complexity: O(√n)
Auxiliary Space: O(1)

Errors and Exceptions Associated with this Function: 

1. It is mandatory to give the argument otherwise, it will give an error no matching function for call to ‘sqrt()’ as shown below, 

CPP




// CPP Program to demonstrate errors in double sqrt()
#include <cmath>
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    double answer;
 
    answer = sqrt();
    cout << "Square root of " << a << " is " << answer
         << endl;
 
    return 0;
}

Output

prog.cpp:9:19: error: no matching function for call to ‘sqrt()’
     answer = sqrt();

Time Complexity: O(√n)
Auxiliary Space: O(1)

2. If we pass a negative value in the argument domain error occurs and the output will be the Square root of -a, which is -nan.

CPP




// CPP Program to demonstrate errors in double sqrt()
#include <cmath>
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    double a = -2, answer;
 
    answer = sqrt(a);
    cout << "Square root of " << a << " is " << answer
         << endl;
 
    return 0;
}

Output:

Square root of -2 is -nan

Time Complexity: O(√n)
Auxiliary Space: O(1)
B) float sqrtf(float arg): It returns the square root of a number to type float.

Syntax:

float sqrtf(float arg)

CPP




// CPP code to illustrate the use of sqrtf function
#include <cmath>
#include <iomanip>
#include <iostream>
 
using namespace std;
 
int main()
{
    float val1 = 225.0;
    float val2 = 300.0;
 
    cout << fixed << setprecision(12) << sqrtf(val1)
         << endl;
    cout << fixed << setprecision(12) << sqrtf(val2)
         << endl;
 
    return (0);
}

Output

15.000000000000
17.320508956909

Time Complexity: O(√n)
Auxiliary Space: O(1)
C) long double sqrtl(long double arg): It returns the square root of a number to type long double with more precision. 

Advantage of sqrtl function: When working with integers of the order 1018, calculating its square root with sqrt function may give an incorrect answer due to precision errors as default functions in programming language works with floats/doubles. But this will always give an accurate answer. 
Syntax:

long double sqrtl(long double arg)

Following is an illustration given below shows the exact difference when working with long integers with sqrt and sqrtl,
1) Using sqrt function:

CPP




// CPP code to illustrate the incorrectness of sqrt
// function
#include <cmath>
#include <iomanip>
#include <iostream>
 
using namespace std;
 
int main()
{
    long long int val1 = 1000000000000000000;
    long long int val2 = 999999999999999999;
 
    cout << fixed << setprecision(12) << sqrt(val1) << endl;
    cout << fixed << setprecision(12) << sqrt(val2) << endl;
 
    return (0);
}

Output

1000000000.000000000000
1000000000.000000000000

Time Complexity: O(√n)
Auxiliary Space: O(1)

2) Using sqrtl function:

CPP




// CPP code to illustrate the correctness of sqrtl function
#include <cmath>
#include <iomanip>
#include <iostream>
 
using namespace std;
 
int main()
{
    long long int val1 = 1000000000000000000;
    long long int val2 = 999999999999999999;
 
    cout << fixed << setprecision(12) << sqrtl(val1)
         << endl;
    cout << fixed << setprecision(12) << sqrtl(val2)
         << endl;
 
    return (0);
}

Output

1000000000.000000000000
999999999.999999999476

Time Complexity: O(√n)
Auxiliary Space: O(1)

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!