Functions for calculating the square root in C++ are provided by the <cmath> header file. These functions allow you to compute the square root of different floating-point data types with better type compatibility.
- sqrt(), sqrtf(), and sqrtl() calculate the square root for double, float, and long double values respectively.
- Choosing the appropriate function helps improve type compatibility and avoids unnecessary type conversions.
sqrt()
The sqrt() function returns the square root of a number of type double.
#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);
}
Output
15.000000000000 17.320508075689
Syntax
double sqrt(double arg);
Errors and Exceptions Associated with sqrt() Function
It is mandatory to give the argument otherwise, it will give an error no matching function for call to 'sqrt()' as shown below.
// 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();
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 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;
}
Output
Square root of -2 is -nan
sqrtf()
The sqrtf() function returns the square root of a number of type float.
#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);
}
Output
15.000000000000 17.320508956909
Syntax
float sqrtf(float arg)
sqrtl()
The sqrtl() function returns the square root of a number of type long double with more precision.
#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;
}
Output
1000000000.000000000000 999999999.999999999476
Syntax
long double sqrtl(long double arg)
Advantage of sqrtl function
The sqrtl() function is used to calculate the square root of long double values with higher precision than sqrt(). It is especially useful when working with very large numbers where precision is important.
- Provides higher precision than sqrt(), making it suitable for large numeric values.
- Reduces precision errors when calculating the square root of long double or very large integers.
Following is an illustration given below shows the exact difference when working with long integers with sqrt and sqrtl.
1) Using 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);
}
Output
1000000000.000000000000 1000000000.000000000000
2) Using 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);
}
Output
1000000000.000000000000 999999999.999999999476