
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
sqrt, sqrtl and sqrtf in C++
In the cmath library of C++, there are different functions for getting the square root except from sqrt. The sqrt is used basically for double type input. The others are used for float, long type data etc. Let us see the usage of these functions.
The sqrt() Function
This function is used for double type data. So this returns square root of type double. The syntax is like below.
double sqrt(double argument)
Example
#include <cmath> #include <iomanip> #include <iostream> using namespace std; main() { double x = 144.0; double y = 180.0; cout << fixed << setprecision(12) << sqrt(x) << endl; cout << fixed << setprecision(12) << sqrt(y) << endl; }
Output
12.000000000000 13.416407864999
Please note that we have to put the argument, otherwise it will return error. And if the argument is negative, then also it will return NaN.
The sqrtf() Function
This function is used for floating type data. So this returns square root of type float. The syntax is like below.
float sqrtf(float argument)
Example
#include <cmath> #include <iomanip> #include <iostream> using namespace std; main() { float x = 144.0; float y = 180.0; cout << fixed << setprecision(6) << sqrtf(x) << endl; cout << fixed << setprecision(6) << sqrtf(y) << endl; }
Output
12.000000 13.416408
Please note that we have to put the argument, otherwise it will return error. And if the argument is negative, then also it will return NaN.
The sqrtl() Function
This function is used for long double type data. So this returns square root of type long double. This is double with more precision. When we are using integers of order 1018 then this function is helpful.
long double sqrtl(long double argument)
Example
#include <cmath> #include <iomanip> #include <iostream> using namespace std; main() { long long int x = 5000000000000000000; long long int y = 999999999999999999; cout << fixed << setprecision(12) << sqrtl(x) << endl; cout << fixed << setprecision(12) << sqrtl(y) << endl; }
Output
2236067977.499789696420 999999999.999999999476