C++

Sqrt Function in C++

The C++ library contains several functions for calculating the square root of an integer. Among them, the sqrt function is the most often utilized. The sqrt() function is a C++ library function in the cmath header “<math.h>” in previous versions that are used to calculate the square root of a specified integer. It receives a number and outputs the square root.

Apart from sqrt, the <cmath> header includes two other intrinsic functions for computing the square root of a number, each with a float or long double input. As a result, the following functions are used to calculate square roots in C++:

  • sqrt->double
  • sqrtf->float
  • sqrtl->long double

Syntax of sqrt function in c++:

In C++, the sqrt function has the following syntax:

sqrt(Datatype variable_name);

A non-negative number is passed as a parameter to the sqrt() method. Note that whenever a negative number is given as a parameter to the sqrt() method, a domain error (-nan) occurs. Lastly, sqrt() returns the square root of the number given as an argument. Here, in the article, we will understand the sqrt() function in the C++ programming language with an example of the cmath header. We have included some C++ programs below that will find the square root.

Example 1:
We must have passed a non-negative number in the sqrt function so that the compiler will not throw an exception(-nan).

It is necessary to use the cmath package in the header because the cmath library contains the sqrt function. Then, there is a main function. Within the main of the program, we have the cout statement “Square root of 16=” that will print first. After that, we have again called the cout statement where the sqrt function is used and inside the sqrt function, we have passed the value “16” as a parameter which is a non-negative number.

The sqrt function generated the square root of the number that was passed to it. In the end, the return keyword is assigned a value of “0” which returns nothing.

#include <iostream>
#include <cmath>
using namespace std;

int main() {

  cout << "Square root 16= ";
   
  cout << sqrt(16) <<"\n";

  return 0;
}

The square root of the number “16” is “4” as you can see the compiler prints the square root value “4” on the shell.

Example 2:
Now, we are assigning data type double for the sqrt function in this example of c++. The square root of a particular number is displayed in double type. For the double type, the syntax should be like this:

double sqrt(double variable_name)

Let’s begin with the program implementation which is inside our main function. Inside the main block, we have declared the two variables and assigned them double type. These variables are given a name “n1” and “n2” and initialized with the decimal integral values.

After that, the cout statement is called where the sqrt function is defined along with the setprecision method. The setprecison method fixed the decimal place to “4” as the value “4” is passed on the setprecision method. Both the variable is assigned to the sqrt function and precision is also set for both the variables which return the square root values of the double data type.

#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
 
int main()
{
    double n1 = 678.0;
    double n2 = 199.0;
 
    cout << fixed << setprecision(4) << "square root of n1: "<< sqrt(n1) <<   endl;
    cout << fixed << setprecision(4) << "square root of n2: "<< sqrt(n2) <<   endl;
 
    return (0);
}

The double type square root value is obtained from the above-specified number in double type with the fixed precision as an output.

Example 3:
For values of the floating type, the sqrtf function is utilized. So, the float type square root is returned. This is how the syntax looks:

float sqrt(float variable_name)

The first step includes the main function of the below program. Within the program’s main, we have created two variables and given the name “num1” and “num2”. These variable types are float and initialized with the decimal numbers. After variable initialization, we have called the sqrtf function in the c++ cout command.

The sqrtf function takes the “num1” and “num2” variables as an argument respectively. We have set the precision with the value “4” which returns the float value of square root with four decimal places.

#include <cmath>
#include <iomanip>
#include <iostream>
 
using namespace std;
 
int main()
{
    float num1 = 99.0;
    float num2 = 125.0;
 
    cout << fixed << setprecision(4) << "Square of num1:"<< sqrtf(num1)
         << endl;
    cout << fixed << setprecision(4) << "Square of num2:"<< sqrtf(num2)
         << endl;
 
    return (0);
}

The sqrt function returned the square root of the inputs provided as a float type. The prompt window displays the following output:

Example 4:
Here, for long double data type, the sqrtl function is utilized. As a result, the square root of the long double type is returned. With higher precision, this is doubled. This function comes in handy when working with integers of order 1018. Calculating the square root of an integer of order 1018 with the sqrt function may result in an inaccurate response due to accuracy problems, as standard functions in programming languages deal with floats/doubles. However, the sqrtl function will always yield an accurate result.

Initially, we have declared two variables “value1” and “value2” with the data type long double int. Then, initialize it with the long numeric value. In the cout statement, we have passed these specified variables as an argument in the sqrtl function with the fixed precision for the returned decimal value of the square root. This time the precision is set to the value “10”.

#include <cmath>
#include <iomanip>
#include <iostream>
 
using namespace std;
 
int main()
{
    long long int value1 = 450000000000000000;
    long long int value2 = 166000000000000000;
 
    cout << fixed << setprecision(10) <<"Square root of value1:" <<  sqrtl(value1) << endl;
    cout << fixed << setprecision(10) <<"Square root of value1:" <<        sqrtl(value2) << endl;
 
    return (0);
}

The square root value of type long double int is returned like this:

Conclusion:

In this article, we have discussed the sqrt function in detail. First, we discussed the sqrt function with a brief introduction. Then, we have explained the basic syntax, its parameter passed, and the returned value of the sqrt function. Through the examples, we have seen the working of sqrt,sqrtf, and sqrtl functions which are used for different data types. In short, the sqrt function is used for the square root value of a specific non-negative number.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content