Sqrt In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Sqrt In C++

Sqrt In C++

BLUF: Mastering Sqrt In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Sqrt In C++

C++ is renowned for its efficiency. Learn how Sqrt In C++ enables low-level control and high-performance computing in the tutorial below.

In C++, the sqrt function is employed to determine the square root of a non-negative number. It is part of the standard library and returns a value of type double. This function is included by using the <cmath> header. The declaration for this function is present in the cmath header file. It computes the square root of the given value only if it is zero or a positive number.

In mathematical terms, the function acts as:

Example

sqrt(x) = √x

When a negative value is given to the sqrt function, the outcome is indeterminate in standard real-number calculations and might lead to a domain-related issue, depending on the specific scenario.

Syntax

It has the following syntax.

Example

sqrt(x);

Here, x represents a non-negative numerical value for which we aim to calculate the square root. The input value must be a numeric data type that is suitable for double precision. It needs to be a non-negative value. Providing a negative number will lead to a domain error as the square root of a negative number is undefined in the real number set.

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.
  • C++ Sqrt Function Example

Let's consider a scenario to demonstrate the sqrt function in C++.

Example

Example

#include <iostream>

#include <cmath>

using namespace std;  //using standard namespace

int main()  //main function

{

    cout << "The square root of 0 is: " << sqrt(0) << endl;

    double num_ber1 = 8; // Finding the square root of a perfect square.

    cout << "The square root of " << num_ber1 << " is: " << sqrt(num_ber1) << endl;

    double num_ber2 = 10;  // Finding the square root of a non-perfect square.

    cout << "The square root of " << num_ber2 << " is: " << sqrt(num_ber2) << endl;

    double decimal_value = 0.25;  // Finding the square root of a decimal number.

    cout << "The square root of " << decimal_value << " is: " << sqrt(decimal_value) << endl;

    // Attempting to find the square root of a negative number.

    double negative_input = -25;

    cout << "The square root of " << negative_input << " is: " << sqrt(negative_input) << " (undefined in real numbers)" << endl;

    return 0;

}

Output:

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 instance, we've utilized the sqrt function from the library to calculate and exhibit the square roots of diverse values, encompassing zero, non-negative integers, decimal numbers, and negative numbers. This demonstration illustrates the behavior of the sqrt function with varying input scenarios.

Key Points of Sqrt Function:

There are several key points of the sqrt function to remember in C++ . Some of them are as follows:

  • The function receives a non-negative number as input and returns its square root.
  • If a negative value is supplied, the function triggers a domain error because square roots of negative values are not defined in real numbers.
  • If we call the sqrt function without any arguments, we will receive a compile-time error saying that no matching function was found.
  • Although the function is designed for double values, it also takes integers because they are automatically converted to double before processing.
  • Another Sqrt Example in C++

Let's consider a different instance to demonstrate the sqrt function in C++.

Example

Example

#include <iostream>

#include <cmath>  // Required for sqrt()

using namespace std;  //using standard namespace

int main() {  //main function

    double number = 16.0;

    double result = sqrt(number);  // using the square root function

    cout << "The square root of " << number << " is " << result << endl;

    return 0;

}

Output:

Output

The square root of 16 is 4

Explanation:

In this illustration, we determine the square root of a specified value by employing the sqrt function from the <cmath> header. Following this, we set the value to 16.0, which then calculates the square root, and subsequently exhibit the outcome via the cout statement.

Exceptions in the sqrt function in C++

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.
  • C++ Sqrt function MCQs

1) Which of the header files must be included in C++ to use the sqrt function?

  • <math.h>
  • <cmath>
  • <mathlib>
  • <stdlib.h>

2) What is the return type of the sqrt function in C++?

  • float
  • double
  • char

3) What happens if a negative number is passed to the sqrt function in C++?

  • It returns 0
  • Throws a runtime exception
  • It returns a negative number
  • It leads to a domain error

4) Which of the following options shows the proper use of the sqrt function in C++?

  • sqrt(49)
  • sqrt=
  • sqrt(25, 2)=
  • sqrt(-4)

5) What is the result of evaluating sqrt(2) in C++?

  • It is approximately 1.41.
  • It does not produce an error.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience