
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use the PI Constant in C++
The Pi is a special mathematical value, approximately 3.14159, that is often used in calculations related to circles and geometry. In C++, we need to understand how to access and use this constant in our programs.
In this article, we will show how to use the PI constant in a C++ program. The PI constant is available in the cmath header file. We will explain how to use PI to calculate values like the area or circumference of a circle.
Ways to Use Pi in C++
There are multiple ways to use the Pi constant in C++. Below are four approaches that can be used to define or access Pi in your programs:
- Using the M_PI constant from <cmath>
- Manually defining Pi
- Using the atan Function to Calculate Pi
- Using the constexpr Keyword for Pi
- Using acos() Function
Using the M_PI constant from <cmath>
If you're using a compiler that supports the <cmath> library, you can easily access Pi through the predefined constant M_PI. This constant is included in most compilers, such as GCC and Clang, and allows you to directly use the value of Pi in your programs without needing to define it yourself.
Example
In this example, we use M_PI from the <cmath> library to calculate the circumference of a circle with a given radius. We then display the value of Pi (M_PI) and the calculated circumference.
#include <iostream> #include <cmath> // For M_PI using namespace std; int main() { double radius = 7.0; double circumference = 2 * M_PI * radius; // Circumference = 2 * PI * r // Display the value of Pi std::cout << "Value of Pi is: " << M_PI << std::endl; cout << "Circumference of the circle: " << circumference << endl; return 0; }
Below is the output of the above code, which shows the value of Pi and the calculated circumference.
Value of Pi is: 3.14159 Circumference of the circle: 43.9823
Manually defining Pi
If your compiler doesn't support M_PI or if you'd like to define Pi yourself, you can manually assign its value(3.14159) directly in your code. This allows you to use Pi in calculations without relying on predefined constants.
Example
In this example, we manually define Pi as a constant with the value 3.14159 and use it to calculate the area of a circle.
#include <iostream> using namespace std; int main() { const double PI = 3.14159; // Manually assigning the value of Pi double radius = 5.0; double area = PI * radius * radius; // Area of the circle = PI * r^2 cout << "Area of the circle: " << area << endl; return 0; }
The output shows the area of a circle with a 5-unit radius, calculated using the manually defined Pi.
Area of the circle: 78.5397
Using the atan Function to Calculate Pi
Instead of using predefined constants, you can calculate Pi using the atan function from the <cmath> library. Since the arctangent of 1(atan(1)) equals Pi/4, multiplying the result by 4 gives Pi.
Example
In this example, we use atan(1), which returns Pi/4. By multiplying the result by 4, we obtain the full value of Pi. This approach works when no predefined constants are available.
#include <iostream> #include <cmath> // For atan() function int main() { // Calculate Pi using atan(1) double pi = 4 * atan(1); // atan(1) is Pi/4 std::cout << "Value of Pi is: " << pi << std::endl; return 0; }
The output shows the calculated value of Pi, derived from the atan function, by multiplying atan(1) by 4.
Value of Pi is: 3.14159
Using the constexpr Keyword for Pi
In this approach, we define Pi using the constexpr keyword, making it a constant that gets calculated during compilation. This helps improve the performance of the program.
Example
In this example, we use the constexpr keyword to define Pi as a compile-time constant. We then calculate the volume of a sphere with a radius of 4 units. The result is displayed as the volume of the sphere.
#include <iostream> using namespace std; int main() { constexpr double PI = 3.14159265358979323846; // Using constexpr for Pi double radius = 4.0; double volume = (4.0 / 3.0) * PI * radius * radius * radius; // Volume of a sphere cout << "Volume of the sphere: " << volume << endl; return 0; }
The output shows the volume of a sphere with a radius of 4 units, which is about 268.082.
Volume of the sphere: 268.083
Using acos() Function
In this approach, we use the mathematical property that Pi can be calculated by applying the inverse cosine function (acos()) to -1. Since the cosine of Pi is -1, calling acos(-1) will return the value of Pi. This method works because the acos() function gives the angle whose cosine is the provided value, and the angle whose cosine is -1 is Pi.
Example
In this example, we calculate Pi using the acos(-1) function and then use it to find the area of a circle with a radius of 6 units.
#include <iostream> #include <cmath> // For acos() using namespace std; int main() { double pi = acos(-1); // acos(-1) gives Pi double radius = 6.0; double area = pi * radius * radius; // Area = Pi * r^2 cout << "Area of the circle: " << area << endl; return 0; }
Below is the result of calculating the area of a circle with a radius of 6 units using Pi from acos(-1).
Area of the circle: 113.097
Conclusion
In this article, we looked at different ways to use the Pi constant in C++. We discussed using M_PI, defining Pi manually, calculating it with atan() and acos(), and using the constexpr keyword. Each method offers flexibility, allowing you to choose the best one for your needs.