C++

How to Use the Pi Constant in C++?

Sometimes it becomes necessary to use the pi constant in our code like we are developing a code that will calculate the radius of the circle or some other calculation we are performing that includes the pi constant value. Pi constant is the constant value that is used to perform Mathematical calculations like calculating the area of the circle or other complex Mathematical calculations. When it comes to using the pi constant in C++, there is a predefined constant provided in the cmath library which can be used to get the value of the pi constant. We can also get a user-defined Pi value, or we can create our Pi value too. In C++, it is represented as “M_PI”. It can be used by simply including the “cmath” header file. The value of PI can also be obtained by using a few built-in functions.

Let us discuss different ways to access the value of PI.

Method 1: Using the Constant Variable of the cmath Library.

In this method, we will use the constant variable that is predefined for the PI value.

Syntax:

The variable does not have any syntax. It simply has a name as our PI constant is:

M_PI

where the decimal value that it holds is “3.141592653589793”.

Example # 01:

In this example, we will try to calculate the volume of a sphere. For calculating the volume of the sphere, it is necessary to have the value of the pi constant and the radius as well. For using the pi value in the calculation, we will use the predefined variable of the cmath library “M_PI”. For the radius, we will define a variable and assign it the value, we can also take the value from the user as input. But in our case, we will assign it the value while declaring the radius variable. Let us proceed to our code. Firstly, include the header files “cmath” and the “iostream” as we are going to use the cmath libraries built-in variable so it is important to include its header file unless the compiler will display an error of undefined variable type. “iostream” is used to perform input-output operations.

Diving into our main function, we have declared a variable named “rad” which is assigned a numeric value “18”. The second float variable “vol” which will hold the return value of our calculation. The reason for creating a float value is that our pi value is the float value; while multiplying the float value with the integer value, the return type is also the float value. If you didn’t create a float variable, it will display an error of undefined integer type. In the next line, we simply printed the value of radius “rad” and then applied the formula or volume that is “volume= 4/3* M_PI * rad3. In our code, we assigned the formula of volume to the variable “vol” at the end using the cout statement and we displayed the resulting value or volume of the sphere.

#include<cmath>

#include<iostream>

using namespace std;
int main()
{
  int rad=18;
  float vol;
cout<< "The value of radius is: "<< rad <<endl;
  vol = float(4) / float(3) * M_PI * rad * rad * rad;
cout<< " The volume is : " << vol <<endl;
  return 0;
}

Let’s figure out the output as shown in the snippet below. The radius and the volume of the sphere are successfully printed without any error. We don’t have to pass the long term of the pi constant.

Method 2: Using the acos() Method.

The acos() is the built-in method that retains the values from –π to π or we can say it returns the inverse cosine of the number. But using acos() in the code returns the value for π/2.

Syntax:

acos(0.0)

The syntax for using this method is as shown above.

Example # 02:

In this example, we will use the acos() function to get the value of the pi constant and display it including header files “bits/stdc++.h” which contains every standard library. It is used when we want to save time for solving problems. Now, moving to our main function where we are going to initialize a double variable named “PI” to which we assigned “2* acos(0.0)”. In this, we multiplied 2 with the acos() function because acos() function returns the value of π/2. So, to get the value of pi, we multiplied it with “2”. Then, using the printf() statement, we displayed the resulting value that we obtained from the acos() function.

In the print statement, we passed “%f” which denotes the float value that is to be printed where the “PI” is the variable we declared for holding the return value. At the end of the code, we returned null.

#include "bits/stdc++.h"

using namespace std;
int main()
{
    double PI = 2 * acos(0.0);
printf("The value of Pi constant is: ");
printf("%f\n", PI);
    return 0;
}

As shown in the snippet below, we have successfully executed the program and displayed the value of pi.

Method 3: Using the asin() Method.

The asin() is used to calculate the arc sine of any numeric value and returns the value from –π to π. It is more similar to the acos() method that we discussed above.

Syntax:

asin(1.0)

The above given is the syntax for calling the asin() method.

Example # 03:

In this example, we will discuss another method of getting the pi value in which we will use the asin() function which is used to calculate the value of any numeric and the resulting value that is obtained is –π/2, π/2. Let us move towards our code in which after declaring our header files, we have declared a variable “PI” to which we assigned our asin() method that is multiplied by the value “2” to get the value of “PI”. Then, using the printf statement, we have displayed the value that is obtained using the asin() method. The value of “PI” is always in the decimal point so that is why we passed “%f” in the printf() statement which means the float value will be printed. Or the datatype of the variable will float.

#include "bits/stdc++.h"

using namespace std;

int main()
{
    double PI = 2 * asin(1.0);
printf("The value of Pi constant is: ");
printf("%f\n", PI);
    return 0;
}

As we can see in the screenshot that is provided below the value of “PI” is displayed without any error.

Conclusion

In this tutorial, we have studied the use of the pi constant in C++ and how it is used and how we can define the value of pi by ourselves or by getting it from the user. By implementing multiple examples, we have explained it from simple to complex, sometimes we have to make multiple calculations that involve the pi value.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.