Approximating pi using partial series sums
One of the powers of Julia is its flexibility in applying its type system. In this recipe, we explain how to write flexible code that can adjust to the required type, using the example of approximating π.
Getting ready
Approximation of π is a long-standing problem in mathematics. You can find many formulas for its calculation at http://mathworld.wolfram.com/PiFormulas.html.
One of the more interesting methods is the use of an infinite sum of terms
for
, ranging from zero to infinity. The denominator in each summed fraction is a double factorial (see http://mathworld.wolfram.com/DoubleFactorial.html or https://en.wikipedia.org/wiki/Double_factorial). Formally, we have the following relationship:
In this recipe, we will use this formula with different numeric types as a basis for the calculations.
Note
In the GitHub repository for this recipe, you will find the commands.txt
file that contains the presented sequence of shell and Julia commands.
Now open...