- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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++ programming
Math Class
This article demonstrates the usage of math class essentials functions sqrt(), sqrtl(), and sqrtf() to calculate the square root of double, long, and float type variables with precision respectively. The Math class of C++ offers a wide range of functions to calculate mathematical calculations including sin, cos, square root, ceil, floor, etc..It is, therefore, mandatory to import the definition of <cmath> header class library in the program in order to avail all calculative methods.
Sqrt Method
The double sqrtl () method of the Math class returns the square root of a double variable with precision. The syntax of this function is as follows;
Syntax
double sqrt(double arg)
The following c++ code constructs define a double type variable with an initialization value to calculate its square root value. Then, the math class method sqrt() accepts these values and yield the result with a precision value as follows;
Example
#include <cmath> #include <iomanip> #include <iostream> using namespace std; int main(){ double val = 225.0; cout << fixed << setprecision(5) << sqrt(val); return (0); }
Output
As seen below, the output of this program is being produced with a precision 5 as follows;
15.00000
Sqrtl Method
The long double sqrtl () method of the Math class returns the square root of a long double variable with precision. The syntax of this function is as follows;
long double sqrtl(long double arg)
An illustration is given below to calculate the square root of a long double variable supplied by the using owing to the Math.sqrtl() method as follows;
Example
#include <cmath> #include <iomanip> #include <iostream> using namespace std; int main(){ long long int val = 1000000000000000000; cout << fixed << setprecision(10) << sqrt(val); return (0); }
Output
After compilation of the program using a code editor, the calculated value of the input long type variable is seen as below;
1000000000.000000000
Sqrtf Method
The float sqrtf () method of the Math class returns the square root of a float type variable with precision. The syntax of this function is as follows;
Syntax
float sqrtf(float arg)
As per the syntax, the program supply a float type variable in the sqrtf() method in pursuit of calculating the square root as following;
Example
#include <cmath> #include <iomanip> #include <iostream> using namespace std; int main(){ float val = 300.0; cout << fixed << setprecision(5) << sqrtf(val); return (0); }
Outer
The output of the supplied float type variable is squarely rooted as below;
17.32051