C++ std::sqrt() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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
CallWhat 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

ParameterDescription
xRequired. The number to take the root of. It should be zero or bigger. An integer is converted to double.

Return Value

Return ValueDescription
Square rootThe non-negative root of x, in the type of the argument.
NaNReturned when x is less than zero. This is a domain error.
inf / -0.0sqrt(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;
}

How It Works

  1. The loop takes each number from the array and gives it to std::sqrt().
  2. 16 and 0.25 are perfect squares, so the results are exact: 4 and 0.5. A whole value prints without a decimal point.
  3. 2 is not a perfect square, so the answer is a long decimal. cout shows six digits by default, which gives 1.41421. The root of 0 is 0.

Example 2 — Avoid the Square Root of a Negative Number

Test the input first. A negative number has no real square root, so the program prints a message instead of using NaN.

C++
#include <cmath>
#include <iostream>

int main() {
    double inputs[] = {25.0, -9.0};

    for (double x : inputs) {
        if (x < 0) {
            std::cout << x << " has no real square root\n";
        } else {
            std::cout << "sqrt(" << x << ") = " << std::sqrt(x) << '\n';
        }
    }

    double r = std::sqrt(-9.0);
    std::cout << "Raw result is NaN: " << std::isnan(r) << '\n';
    return 0;
}

How It Works

  1. The if (x < 0) test stops a bad value before it reaches sqrt(). This is the safe way to use the function.
  2. 25.0 passes the test and prints 5. -9.0 fails the test, so only the message is shown.
  3. Calling std::sqrt(-9.0) directly returns NaN. std::isnan(r) prints 1, which confirms that the result is not a number.

Example 3 — Distance Between Two Points

The distance formula is sqrt(dx * dx + dy * dy). This is the most common real use of sqrt().

C++
#include <cmath>
#include <iostream>

int main() {
    double x1 = 1.0, y1 = 2.0;
    double x2 = 4.0, y2 = 6.0;

    double dx = x2 - x1;
    double dy = y2 - y1;
    double distance = std::sqrt(dx * dx + dy * dy);

    std::cout << "dx = " << dx << ", dy = " << dy << '\n';
    std::cout << "Distance: " << distance << '\n';
    std::cout << "Origin to (1, 1): " << std::sqrt(1.0 * 1.0 + 1.0 * 1.0) << '\n';
    return 0;
}

How It Works

  1. The two points are (1, 2) and (4, 6). Moving across is 3 and moving up is 4.
  2. 3 * 3 + 4 * 4 is 25, and std::sqrt(25) is 5. This is the 3-4-5 right triangle.
  3. 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.

Next: tan()

Keep going through the <cmath> functions. Tangent of an angle in radians.

tan() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful