In this article, you will learn about how to get the value of Pi in C++ with several methods. A few of the mathematical functions in C++ are used to determine Pi(π) .
1. Making Use of Pre-Defined Constants
The M_PI constant is available in C and C++ and is frequently found in the <cmath> or <math.h> header. It stands for the number Pi. It can be used directly for Pi-based mathematical computations in your C++ program.
Example:
Let's take an example to get the value of pi using pre-defined constants in C++:
#include <iostream>
#include <cmath>
int main()
{
double pi = M_PI;
std::cout << "The value of Pi is: " << pi << std::endl;
return 0;
}
Output:
The value of Pi is: 3.14159
Explanation:
- The header files required for input/output operations (<iostream>) and mathematical functions (<cmath>) are included using the #include directives.
- The program's entrance point serves as its main function .
- double pi = MPI; line initializes a double variable pi to the value of the predefined constant MPI, which stands for the mathematical constant Pi. Usually, the <cmath> or <math.h> header file contains this constant.
- The words "The value of Pi is: " and the value of pi is printed to the standard output stream using the std::cout statement. After printing the message, a new line is added using the std::endl function.
- return 0; signifies the end of the main function and the successful execution of the program.
2. Making Use of Mathematical Functions
A numeric number between [-] and [+] is returned by the acos function , which is used to calculate the value of pi.
Acos(0.0) will produce the value for /2 when used.
An approximation of Pi can also be obtained by using different mathematical functions, such as acos(-1) or atan(1) * 4 . T hese equations use trigonometric or inverse trigonometric functions to determine the value of Pi.
Example:
Let's take an example to get the value of pi using mathematical functions in C++:
#include <iostream>
#include <cmath>
int main()
{
double pi_approx = atan(1) * 4;
std::cout << "An approximation of Pi is: " << pi_approx << std::endl;
return 0;
}
Output:
An approximation of Pi is: 3.14159
Explanation:
- The header files required for input/output operations (<iostream>) and mathematical functions (<cmath>) are included using the #include directives.
- The main function serves as the program's execution's beginning point.
- double pi_approx = atan(1) * 4; line uses the atan function to compute an approximation of Pi. The arctangent of 1, which is equal to /4 in radians, is calculated using the phrase atan(1) . An approximate value of π can be obtained by multiplying this value by 4.
- The text "An approximation of Pi is: " and pi_approx's value is printed to the standard output stream using the std::cout command. After printing the message, a new line is added using the std::endl function.
- return 0; signifies the end of the main function and the successful execution of the program.
3. Utilizing the asin function
The asin function is used to calculate the value of Π and returns a numeric value between [-Π, Π]. Given that asin(1.0) will get the result for Π/2. Consequently, to find the value of Π:
double pi = 2*asin(1.0);
The value derived from the equation above is now calculated as follows:
printf("%f\n", pi);
Example:
Let's take an example to get the value of pi using asin function in C++:
#include "bits/stdc++.h"
using namespace std;
// Function that prints the
// value of pi
void print_ValueOfPi()
{
// Find value of pi using
// asin() function
double pi = 2 * asin(1.0);
// Print value of pi
printf("The value of pi is : %f\n", pi);
}
int main()
{
// Function that prints
// the value of pi
print_ValueOfPi();
return 0;
}
Output:
The value of pi is: 3.141593
Explanation:
- #include directive in competitive programming environments, the most standard libraries can be included by using the shorthand "bits/stdc++.h" . It contains every header file from the standard library.
- Program items can be used from the standard namespace without being explicitly specified due to the using namespace std; declaration .
- It is possible to compute and print the value of Pi using the print_ValueOfPi function, which is defined.
- The formula to find Pi is double pi = 2 * asin(1.0). When the arc sine of an argument is calculated using the asin function, it returns π/2, which may be multiplied by two to find Pi.
- The program's execution entry point is known as the main function .
- The main function calls the print_ValueOfPi function to print the value of Pi.
- return 0; signifies the successful execution of the program and the end of the main function