Estimation of Pi using C++


It's a fantastic idea to make your blog post more engaging! It's the story behind the code that really connects with readers. Let's make this article feel more like a personal journey of discovery, where the code is a supporting character, not the main star.
Here is a revised version of your article, with a livelier tone, a focus on the narrative, and a more streamlined presentation of the code.
My Honest Attempt at Approximating Pi
Have you ever looked at a fundamental constant like Pi and wondered, "How would I even begin to calculate that myself?" That's the question that sparked this entire project! It's easy to take for granted the incredible accuracy of constants like Pi, but building my own approximation from scratch was a journey in itself.
My goal was simple: find an approximate value for Pi without using any of the famous, pre-existing series designed specifically for it. This required a lot of creative thinking and digging into the mathematical toolkit I've built up over the years. It was a true test of applying what I've learned.
The Newton-Raphson Method: A Problem-Solving Superpower
The Newton-Raphson method is a numerical method for finding the roots of an equation. It's like a powerful magnifying glass for solving problems. It's based on a simple, yet brilliant, idea: start with a guess, and then use the tangent line to find a better, more accurate guess. If you do this over and over, you'll eventually land on the root you're looking for. The formula is beautifully elegant:
x_n+1=x_n −(x_n)/f′(x_n)
The real challenge for me was finding the right equation for this method to solve. After some experimentation, I landed on a function where Pi is one of the roots:
f(x)=tan(x/4)−1=0
This was my breakthrough! But it came with a new challenge: how to calculate the values for sine and cosine without relying on pre-built functions?
Building My Own Mathematical Toolkit
To solve the puzzle, I had to create my own versions of the tools I needed.
Sine and Cosine: I decided to use the Taylor Series to approximate sine. It's an amazing concept that lets you build a function's value by summing an infinite series of terms. For my code, I used a few terms to get a reasonably accurate value. Then, with a little help from the Pythagorean identity (sin2(x)+cos2(x)=1), I could get the cosine.
Square Roots: To calculate the cosine, I needed a way to find the square root. For this, I implemented the Babylonian Method! This ancient algorithm is surprisingly effective and converges on the correct value very quickly.
This was a fantastic exercise in "low-level" programming, where I had to build my own fundamental operations rather than just calling a library.
The Code: A Glimpse into the Engine
The full code is quite a journey through these functions, but here's a look at the heart of the project—the approximatePi
function. This is where all the pieces come together to do the heavy lifting of the Newton-Raphson iterations.
C++
// A simplified look at the main approximation function
double approximatePi(double initialGuess, int maxIterations) {
double x = initialGuess;
for (int i = 0; i < maxIterations; i++) {
// Calculate our function and its derivative using my own methods
double fx = function(x);
double fpx = derivative(x);
// The magic happens here: applying the Newton-Raphson formula
x = x - fx / fpx;
}
return x;
}
The Grand Finale: Results and Reflections 🎉
After running the program with different starting values, the results were incredibly satisfying. The algorithm converged quickly on a value very close to the known value of Pi. The bound validation step I added helped ensure that I was always on the right track.
Here's an example of the output for one of the initial guesses:
Pi Approximation using Newton-Raphson Method
=============================================
Testing with initial guess: 3.0
================================================================================
Newton-Raphson Iterations for Pi Approximation:
Using equation: tan(x/4) - 1 = 0
Initial guess: 3.0
------------------------------------------------------------
Iteration 1: 3.141592653589793 (error: 1.415927e-01)
Converged after 1 iterations!
Bound Validation:
Lower bound (Pi > 1): PASS
Upper bound (Pi < 4): PASS
Results Comparison:
============================================================
Approximated Pi: 3.141592653589793
Known Pi: 3.141592653589793
Absolute Error: 0.000000e+00
Relative Error: 0.0000000000%
This project was more than just an exercise in coding; it was a deep dive into the very fabric of how our computers handle numbers. It reminded me that even the most complex problems can be broken down into smaller, manageable pieces. It’s a testament to the power of combining different mathematical tools to arrive at a solution. I hope this exploration was as interesting for you to read as it was for me to build!
More details and all the code i have written for this fun exercise are available on my GitHub Profile if you are interested, happy learning!
Subscribe to my newsletter
Read articles from Abhinay Ragam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Abhinay Ragam
Abhinay Ragam
Hey everyone! I'm Abhinay Ragam, and I'm using this blog as my personal space to tell stories about tech and math. As a student of Computer Science, I'm captivated by topics like encryption and the art of low-level design. I'm all about getting my hands dirty with code and understanding the core principles that drive it. This blog is where I'll be sharing my journey—from my latest projects to the fascinating mathematical concepts that inspire my work. I'm excited to share my passion and hopefully inspire a few of you along the way!