sqrt() in C++

Last Updated : 16 May 2026

Math sqrt() Function

In C++, the sqrt() function is defined in the <cmath> header file and is used to calculate the square root of a non-negative number. It returns the positive square root of the given value as a double-type result.

The sqrt() function supports integer and floating-point values, which are automatically converted to double before computation.

If a negative number is supplied to sqrt(), the result will be undefined in traditional real-number computation and may result in a domain error, depending on the context used.

Syntax

It has the following syntax.

Parameters

Here, x is a non-negative number whose square root that we want to compute. The provided value must be a numeric type that is compatible with double. It should be a positive or zero value. Passing a negative number will result in a domain error since the square root of a negative number is not defined within the set of real numbers.

Return Value

  • The function calculates the square root of the given value.
  • The return type is always double, regardless of whether the input is an integer or a floating-point number.

Examples of sqrt() Function

Here, we are going to discuss several examples to demonstrate the sqrt() Function.

Example 1: Calculate the Square Root of Different Types of Values Using sqrt()

This example demonstrates how the sqrt() function calculates the square root of zero, positive integers, decimal values, and negative numbers.

Output:

The square root of 0 is: 0
The square root of 8 is: 2.82843
The square root of 10 is: 3.16228
The square root of 0.25 is: 0.5
The square root of -25 is: -nan (undefined in real numbers)

Explanation

In this example, we have taken the sqrt() function from the library to compute and display the square roots of various values, including zero, positive integers, decimals, and negative numbers. It shows how the sqrt() function responds to different types of input.

Example 2: Calculate the Square Root of a Positive Number Using sqrt()

This example demonstrates how the sqrt() function calculates the square root of a positive number.

Output:

The square root of 16 is 4

Explanation:

In this example, we calculate the square root of a given number using the sqrt() function from the <cmath> library. After that, we initialize the number to 16.0, which computes its square root, and then display the result using cout.

Exceptions in the sqrt() function

When we use the sqrt() function in C++, there are a few important cases that can cause errors if not handled correctly.

  • Negative Input: The sqrt() function only accepts non-negative values. If we try to supply a negative value as an argument, the function will go outside of its valid domain. It frequently results in a domain error because the square roots of negative numbers are not specified in the set of real numbers.
  • Missing Argument: Using the sqrt() function without a value will result in a compilation error. The compiler will generate an error message stating "no matching function for call to "sqrt"".
  • It causes an error because the sqrt() function expects only one argument.