sqrt, sqrtl and sqrtf in C++
The cmath header defines two more inbuilt functions for calculating square root of a number (apart from sqrt which takes double as an arguement) which has an arguement of type float and long double.
-
double sqrt(double arg): It returns the square root of a number to type double
Syntax : double sqrt(double arg)
// CPP code to illustrate// the use of sqrt function.#include <cmath>#include <iomanip>#include <iostream>usingnamespacestd;intmain(){doubleval1 = 225.0;doubleval2 = 300.0;cout << fixed << setprecision(12) <<sqrt(val1) << endl;cout << fixed << setprecision(12) <<sqrt(val2) << endl;return(0);}chevron_rightfilter_noneOutput:15.000000000000 17.320508075689
Errors and exceptions associated with this function:
(Thanks to sunny6041 for contributing this section)- It is mandatory to give both the arguments otherwise it will give error no matching function for call to ‘sqrt()’ like this.
#include <iostream>#include <cmath>usingnamespacestd;intmain(){doubleanswer;answer =sqrt();cout <<"Square root of "<< a<<" is "<< answer << endl;return0;}chevron_rightfilter_noneOutput:
prog.cpp:9:16: error: no matching function for call to 'sqrt()'
- If we pass a negative value in the argument domain error occurs. and the output will be the Square root of -a is -nan.
#include <iostream>#include <cmath>usingnamespacestd;intmain(){doublea = -2 , answer;answer =sqrt(a);cout <<"Square root of "<< a<<" is "<< answer << endl;return0;}chevron_rightfilter_noneOutput:
Square root of -2 is -nan
- It is mandatory to give both the arguments otherwise it will give error no matching function for call to ‘sqrt()’ like this.
-
float sqrtf(float arg): It returns the square root of a number to type float
Syntax : float sqrtf(float arg)
// CPP code to illustrate// the use of sqrtf function.#include <cmath>#include <iomanip>#include <iostream>usingnamespacestd;intmain(){floatval1 = 225.0;floatval2 = 300.0;cout << fixed << setprecision(12) << sqrtf(val1) << endl;cout << fixed << setprecision(12) << sqrtf(val2) << endl;return(0);}chevron_rightfilter_noneOutput:15.000000000000 17.320508956909
-
long double sqrtl(long double arg): It returns the square root of a number to type long double with more precision. Advantage of using this function is that 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)
An illustration given below shows the exact difference when working with long integers with sqrt and sqrtl.
Using sqrt function.// CPP code to illustrate// the incorrectness of sqrt function.#include <cmath>#include <iomanip>#include <iostream>usingnamespacestd;intmain(){longlongintval1 = 1000000000000000000;longlongintval2 = 999999999999999999;cout << fixed << setprecision(12) <<sqrt(val1) << endl;cout << fixed << setprecision(12) <<sqrt(val2) << endl;return(0);}chevron_rightfilter_noneOutput:1000000000.000000000000 1000000000.000000000000
Output:
1000000000.000000000000 1000000000.000000000000
Using sqrtl function
// CPP code to illustrate// the correctness of sqrtl function.#include <cmath>#include <iomanip>#include <iostream>usingnamespacestd;intmain(){longlongintval1 = 1000000000000000000;longlongintval2 = 999999999999999999;cout << fixed << setprecision(12) << sqrtl(val1) << endl;cout << fixed << setprecision(12) << sqrtl(val2) << endl;return(0);}chevron_rightfilter_noneOutput:1000000000.000000000000 999999999.999999999476
Recommended Posts:
- valarray sqrt() function in C++
- sqrt() function for complex number in C++
- C++ program to compare two Strings using Operator Overloading
- not1 and not2 function templates in C++ STL with Examples
- Go vs C++
- Trie Data Structure using smart pointer and OOP in C++
- std::hash class in C++ STL
- History of C++
- What are Forward declarations in C++
- __builtin_inf() functions of GCC compiler
- Structures in C++
- Variables in C++
- Implementing Forward Iterator in BST
- Count substrings that contain all vowels | SET 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



