# Pi with Machin's formula (Java)

### From LiteratePrograms

# Machin's formula

A simple way to compute the mathematical constant π ≈ 3.14159 with any desired precision is due to John Machin. In 1706, he found the formula

which he used along with the Taylor series expansion of the arc cotangent function,

to calculate 100 decimals by hand. The formula is well suited for computer implementation, both to compute π with little coding effort (adding up the series term by term) and using more advanced strategies (such as binary splitting) for better speed.

## Contents |

In order to obtain *n* digits, we will use fixed-point arithmetic to compute π × 10^{n} as a Java *BigDecimal*.

## Implementation

<<Pi.java>>=import java.math.BigDecimal;import java.math.RoundingMode;publicfinalclassPi{privatestaticfinalBigDecimal TWO =newBigDecimal("2");privatestaticfinalBigDecimal FOUR =newBigDecimal("4");privatestaticfinalBigDecimal FIVE =newBigDecimal("5");privatestaticfinalBigDecimal TWO_THIRTY_NINE =newBigDecimal("239");privatePi(){}publicstaticBigDecimal pi(intnumDigits){intcalcDigits = numDigits + 10;returnFOUR.multiply((FOUR.multiply(arccot(FIVE, calcDigits))).subtract(arccot(TWO_THIRTY_NINE, calcDigits))).setScale(numDigits, RoundingMode.DOWN);}privatestaticBigDecimal arccot(BigDecimal x,intnumDigits){BigDecimal unity = BigDecimal.ONE.setScale(numDigits, RoundingMode.DOWN); BigDecimal sum = unity.divide(x, RoundingMode.DOWN); BigDecimal xpower =newBigDecimal(sum.toString()); BigDecimal term =null;booleanadd =false;for(BigDecimal n =newBigDecimal("3"); term ==null|| !term.equals(BigDecimal.ZERO); n = n.add(TWO)){xpower = xpower.divide(x.pow(2), RoundingMode.DOWN); term = xpower.divide(n, RoundingMode.DOWN); sum = add ? sum.add(term): sum.subtract(term); add = ! add;}returnsum;}}

## Applying Machin's formula

The method *pi* above uses Machin's formula to compute π using the necessary level of precision, which in turn invokes the private *arccot* method for arccot computation.

To avoid rounding errors in the result, we set the scale of the *BigDecimal* to 10 more than the requested number of digits, and later round the return value.

## High-precision arccot computation

To calculate arccot of an argument *value*, we start setting the scale of the number 1 to a value which will provide sufficient precision and avoid rounding errors (number of digits requested + 10) and use this as our *unity* value. This *unity* value is divided by *x* to obtain the first term. Note that all *BigDecimal* division uses the *DOWN* rounding mode to emulate integer division. We then repeatedly divide by *x*^{2} and a counter value that runs over 3, 5, 7, ..., to obtain each next term. The summation is stopped at the first zero term, which in this fixed-point representation corresponds to a real value less than 10^{-n}.

Example usage:

System.out.println(Pi.pi(100));

Result:

31415926535897932384626433832795028841971693993751058209749445923078164062862089 986280348253421170679

The program can be used to compute tens of thousands of digits in just a few seconds on a modern computer. (More sophisticated techniques are necessary to calculate millions or more digits in reasonable time, although in principle this program will also work.)

Download code |