In C++, std::sqrt() finds the square root of a number. It answers this question: which number times itself gives x? For 16 the answer is 4, and for 2 it is about 1.41421.
It works on decimals and returns a decimal, so even std::sqrt(16) gives a double and not an int. A negative number has no real square root, so std::sqrt(-1.0) returns NaN (not a number). It does not stop the program.
std::sqrt() is declared in <cmath> and is part of the group Roots and powers. These functions raise numbers to powers or take roots. Watch the domain: a negative number has no real square root, so the answer is NaN.
Syntax
Include <cmath> and call std::sqrt. Start with these lines.
C++
double a = std::sqrt(16.0); // double version
float b = std::sqrt(16.0f); // float version
Call
What it does
std::sqrt(16.0)
Passes a double and returns a double. Here the result is 4.
std::sqrt(16.0f)
Passes a float, so the float overload runs and returns a float.
std::sqrt(16)
An integer is converted to double. Returns 4 as a double.
Write std::sqrt so the C++ overloads for float, double, and long double are used.
Library declarations
C++
float sqrt(float x);
double sqrt(double x);
long double sqrt(long double x);
double sqrt(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. The number to take the root of. It should be zero or bigger. An integer is converted to double.
Return Value
Return Value
Description
Square root
The non-negative root of x, in the type of the argument.
NaN
Returned when x is less than zero. This is a domain error.
inf / -0.0
sqrt(inf) is inf, and sqrt(-0.0) is -0.0.
C++ Examples
Example 1 — Square Roots of a List of Numbers
Loop over four numbers and print each square root. Perfect squares give whole numbers.
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {16.0, 2.0, 0.25, 0.0};
for (double v : values) {
std::cout << "sqrt(" << v << ") = " << std::sqrt(v) << '\n';
}
return 0;
}
dx = 3, dy = 4
Distance: 5
Origin to (1, 1): 1.41421
How It Works
The two points are (1, 2) and (4, 6). Moving across is 3 and moving up is 4.
3 * 3 + 4 * 4 is 25, and std::sqrt(25) is 5. This is the 3-4-5 right triangle.
The corner case (0, 0) to (1, 1) gives sqrt(2), which prints as 1.41421. For a very large dx or dy, std::hypot() is safer.
Frequently Asked Questions
std::sqrt() returns the square root of a number. The square root of 16 is 4, because 4 times 4 is 16.
The square root as float, double, or long double. For a negative number the result is NaN, and std::sqrt(-0.0) is -0.0.
It returns NaN (not a number) and sets a domain error. The program keeps running. Check the input with x < 0 or the result with std::isnan().
No. It returns a double, 4.0, even for an int argument. If you need an int, cast it: static_cast<int>(std::sqrt(16)). For large values the cast can be off by one, so be careful.
Include <cmath> and write std::sqrt(). That always works and picks the C++ overloads. The global name ::sqrt often works too, but the standard does not promise it, and it may only take a double.