My Math Forum  

Go Back   My Math Forum > Math Forums > Math

Math General Math Forum - For general math related discussion and news

LinkBack Thread Tools Display Modes
March 12th, 2018, 03:34 PM   #1
Joined: Mar 2018
From: Somwhere in the PI

Posts: 1
Thanks: 0

Pi calculation by using Machin's formula for dummy.

Hi Friends,

first of all I do apologize for my questions, but PI number calculations is beyond of my brain's possibilities.
I should start from explanation of the roots of my question:
- I'm writing small application(well to be honest I'm even not writing it but just copy&paste code and trying to understand how it works) on C# for the calculation of PI number by using John Machin's formula. I realized that the biggest problem for me is that I can't understand how mathematicians comes up with the idea of PI. So trying to find answers on my questions I red some articles and as I understood, please correct me if I wrong, that the phenomena of the PI is that we can't precisely measure length of the circle circumference with diameter equals 1, so the formulas like John Machin's was developed exactly for the purpose of calculation value of the circle circumference length with diameter equals 1. So let me switch to the formula it self:

For non-mathematicians like me, it looks like Pi/4 = 4*(Arctan(1/5)) - Arctan(1/239), which means that to be able to find Pi I have to solve right part of the Machin's equation to be able to find Pi/4; if I was wrong, I would be very appreciate for your corrections. Moving further, with my less then regular brain I really can't understand how this formula can be used to find any numbers of PI in reminder. For me, it looks like if I put this formula in code as it is it should give me what I want right away, but in fact it doesn't work this way and I can't understand why. So I find this wonderful article:
which show how to adopt Machin's formula in code to make it work. BUT!!! I can't understand how this beautiful formula:

was turned into this ugly code below. Please forgive me, I didn't meant programmers prof skills, just a number of lines in compare with original formula. The programmer who write this code is really good and prof:

public class PiJavaPort {
public static BigInteger InverseTan(int denominator, int numberOfDigitsRequired) {
int demonimatorSquared = denominator*denominator;

int degreeNeeded = GetDegreeOfPrecisionNeeded(demonimatorSquared, numberOfDigitsRequired);

BigInteger tenToNumberPowerOfDigitsRequired = GetTenToPowerOfNumberOfDigitsRequired(numberOfDigi tsRequired);

int c = 2*degreeNeeded + 1;
BigInteger s = BigInteger.Divide(tenToNumberPowerOfDigitsRequired , new BigInteger(c)); // s = (10^N)/c
for (int i = 0; i < degreeNeeded; i++) {
c = c - 2;
var temp1 = BigInteger.Divide(tenToNumberPowerOfDigitsRequired , new BigInteger(c));
var temp2 = BigInteger.Divide(s, new BigInteger(demonimatorSquared));
s = BigInteger.Subtract(temp1, temp2);
Console.WriteLine("Number of iterations=" + degreeNeeded);

// return s/denominator, which is integer part of 10^numberOfDigitsRequired times arctan(1/k)
return BigInteger.Divide(s, new BigInteger(denominator));


private static int GetDegreeOfPrecisionNeeded(int demonimatorSquared, int numberOfDigitsRequired) {
//the degree of the Taylor polynomial needed to achieve numberOfDigitsRequired
//digit accuracy of arctan(1/denominator).
int degreeNeeded = 0;

while ((Math.Log(2*degreeNeeded + 3) + (degreeNeeded + 1)*Math.Log10(demonimatorSquared))
<= numberOfDigitsRequired*Math.Log(10)) {
return degreeNeeded;

private static BigInteger GetTenToPowerOfNumberOfDigitsRequired(int numberOfDigitsRequired) {
var tenToNumberOfDigitsRequired = new BigInteger(1);

// The following loop computes 10^numberOfDigitsRequired
for (var i = 0; i < numberOfDigitsRequired; i++) {
tenToNumberOfDigitsRequired = BigInteger.Multiply(tenToNumberOfDigitsRequired, new BigInteger(10));
return tenToNumberOfDigitsRequired;

public static string Calculate(int numberOfDigitsRequired)
numberOfDigitsRequired += 8; // To be safe, compute 8 extra digits, to be dropped at end. The 8 is arbitrary

var a = BigInteger.Multiply(InverseTan(5, numberOfDigitsRequired), new BigInteger(16)); //16 x arctan(1/5)
var b = BigInteger.Multiply(InverseTan(239, numberOfDigitsRequired), new BigInteger(4)); //4 x arctan(1/239)

BigInteger pi = BigInteger.Subtract(a, b);

var piAsString = BigInteger.Divide(pi, new BigInteger(100000000)).ToString();
var piFormatted = piAsString[0]+"."+piAsString.Substring(1,numberOfDigitsRequi red-8);
return piFormatted;
I would be very very appreciate you if you can explain dummy how this code related to the Machin's formula, why do we need to calculate data like:
Why we need to write our own InverseTan method instead of just using a standard Atan? And how it works in case when we specify precision of number in reminder?

Again sorry for my questions if it sounds dummy, but I really need help of smart people to be able to understand these questions.

Looking forward to hear from you,
thank you in advance,
best regards, Maksim!

Last edited by skipjack; March 13th, 2018 at 04:46 AM.
Maksim is offline  

  My Math Forum > Math Forums > Math

calculation, dummy, formula, machin

Thread Tools
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help for big math dummy! jmk909er Elementary Math 6 December 18th, 2016 04:05 AM
Machin formula tejolson Geometry 1 August 22nd, 2014 01:21 PM
Stock Calculation Formula PLS HELP! jairbyz Applied Math 0 December 11th, 2013 05:05 PM
Question about dummy variables justusphung Calculus 4 July 5th, 2013 12:39 AM
Help with MCQ - Regression & Dummy Variable sinth Advanced Statistics 0 June 10th, 2010 04:53 AM

Copyright © 2018 My Math Forum. All rights reserved.