sqrt, sqrtl and sqrtf in C++
Last Updated :
04 Jul, 2024
There are various functions available in the C++ Library to calculate the square root of a number. Most prominently, sqrt is used that is defined in <cmath> header file. 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.
Following are the functions used for calculating square root in C++ :
- sqrt (double)
- sqrtf (float)
- sqrtl (long double)
The above functions have been discussed in detail below:
1. sqrt()
The sqrt() function returns the square root of a number of type double.
Syntax
double sqrt(double arg);
Example: Program to illustrate the use of sqrt function
C++
// 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;
// Calculate and print the square root of val1
cout << fixed << setprecision(12) << sqrt(val1) << endl;
// Calculate and print the square root of val2
cout << fixed << setprecision(12) << sqrt(val2) << endl;
return (0);
}
Output15.000000000000
17.320508075689
Complexity Analysis
- Time Complexity: O(log n)
- Auxiliary Space: O(1)
Errors and Exceptions Associated with sqrt() 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.
C++
// CPP Program to demonstrate errors in double sqrt()
#include <cmath>
#include <iostream>
using namespace std;
// Driver Code
int main()
{
double answer;
// no value is passed as parameter
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();
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;
// Calculate the square root of a
answer = sqrt(a);
cout << "Square root of " << a << " is " << answer
<< endl;
return 0;
}
OutputSquare root of -2 is -nan
2. sqrtf()
The sqrtf() function returns the square root of a number of type float.
Syntax
float sqrtf(float arg)
Example: Program to illustrate the use of sqrtf function
C++
// 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;
// Calculate and print the square root of val1
cout << fixed << setprecision(12) << sqrtf(val1)
<< endl;
// Calculate and print the square root of val2
cout << fixed << setprecision(12) << sqrtf(val2)
<< endl;
return (0);
}
Output15.000000000000
17.320508956909
Complexity Analysis
- Time Complexity: O(log n)
- Auxiliary Space: O(1)
3. sqrtl()
The sqrtl() function returns the square root of a number of type long double with more precision.
Syntax
long double sqrtl(long double arg)
Example: Program to illustrate the use of sqrtl function
C++
// CPP program to illustrate the use of sqrtl function
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
long long int var1 = 1000000000000000000;
long long int var2 = 999999999999999999;
// Calculate and print the square root of val1
cout << fixed << setprecision(12) << sqrtl(var1)
<< endl;
// Calculate and print the square root of val2
cout << fixed << setprecision(12) << sqrtl(var2)
<< endl;
return 0;
}
Output1000000000.000000000000
999999999.999999999476
Complexity Analysis
- Time Complexity: O(log n)
- Auxiliary Space: O(1)
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 work with floats/doubles. But sqrtl function will always give an accurate answer.
Following is an illustration given below shows the exact difference when working with long integers with sqrt and sqrtl.
1) Using sqrt function
C++
// 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;
// Calculate and print the square root of val1
cout << fixed << setprecision(12) << sqrt(val1) << endl;
// Calculate and print the square root of val2
cout << fixed << setprecision(12) << sqrt(val2) << endl;
return (0);
}
Output1000000000.000000000000
1000000000.000000000000
2) Using sqrtl function
C++
// 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;
// Calculate and print the square root of val1
cout << fixed << setprecision(12) << sqrtl(val1)
<< endl;
// Calculate and print the square root of val2
cout << fixed << setprecision(12) << sqrtl(val2)
<< endl;
return (0);
}
Output1000000000.000000000000
999999999.999999999476
Similar Reads
std::stoul and std::stoull in C++
std::stoul Convert string to unsigned integer. Parses str interpreting its content as an integral number of the specified base, which is returned as an unsigned long value. unsigned long stoul (const string& str, size_t* idx = 0, int base = 10); Parameters : str : String object with the represen
3 min read
lrint() and llrint() in C++
lrint() in C++ The lrint() function rounds the fractional value given in the argument to an integral value using the current rounding mode. This function is defined in <cmath> library. The current mode is determined by the function fesetround(). Note:This function returns the final value in lo
5 min read
strtol() function in C++ STL
The strtol() function is a builtin function in C++ STL which converts the contents of a string as an integral number of the specified base and return its value as a long int. Syntax: strtol(s, &end, b) Parameters: The function accepts three mandatory parameters which are described as below: s: s
4 min read
rint(), fesetround(), rintf(), rintl() in C++
We will be discussing about rint(), fesetround(), rintf() and rintl() functions in C++ in this article. 1) rint(): It is used to roundoff the floating-point arguments to an integer value (in floating-point format). You can also determine the current rounding mode using a function fesetround() accord
4 min read
std::stod, std::stof, std::stold in C++
std::stod() : It convert string into double. Syntax: double stod( const std::string& str, std::size_t* pos = 0 ); double stod( const std::wstring& str, std::size_t* pos = 0 ); Return Value: return a value of type double Parameters str : the string to convert pos : address of an integer to st
3 min read
Square Root (Sqrt) Decomposition Algorithm
Square Root Decomposition Technique is one of the most common query optimization techniques used by competitive programmers. This technique helps us to reduce Time Complexity by a factor of sqrt(N) The key concept of this technique is to decompose a given array into small chunks specifically of size
15+ min read
valarray sqrt() function in C++
The sqrt() function is defined in valarray header file. This function is used to calculate square root of the value of each element in valarray. Syntax: sqrt(varr); Parameter: This function takes a mandatory parameter varr which represents valarray. Returns: This function returns a valarray containi
2 min read
asinh() function in C++ STL
The asinh() is an inbuilt function in C++ STL that returns the inverse hyperbolic sine of an angle given in radians. The function belongs to <cmath> header file.Syntax:Â asinh(data_type x) Time Complexity: O(1)Auxiliary Space: O(1) Parameter: The function accepts one mandatory parameter x whic
3 min read
Python math.sqrt() function | Find Square Root in Python
math.sqrt() returns the square root of a number. It is an inbuilt function in the Python programming language, provided by the math module. In this article, we will learn about how to find the square root using this function.Example:Pythonimport math # square root of 0 print(math.sqrt(0)) # square r
2 min read
Babylonian method for square root
Algorithm: This method can be derived from (but predates) NewtonâRaphson method. 1 Start with an arbitrary positive start value x (the closer to the root, the better). 2 Initialize y = 1. 3. Do following until desired approximation is achieved. a) Get the next approximation for root using average of
9 min read