BinarySplittingPi.frink

Download or view BinarySplittingPi.frink in plain text format


/** This class is an example of using the BinarySplitting.frink file to
    calculate pi to arbitrary precision.  It follows the algorithm described in:

   "Fast multiprecision evaluation of series of rational numbers" by Bruno
    Hible and Thomas Papanikolaou:

    https://ginac.de/CLN/binsplit.pdf
  
    See the references in that paper for discussion of the parameters.  The
    parameters from the paper are not really optimal (and really aren't the
    Chudnovsky's parameters) this should not be used in production.  A
    highly-optimized version is found in pi.frink or pi2.frink.

    Call BinarySplittingPi.pi[digits] to calculate the specified number of
    digits.
*/


use BinarySplitting.frink
    
class BinarySplittingPi implements ParamProvider
{
   class var A = 13591409
   class var B = 545140134
   class var C = 640320
   class var C3over24 = C^3/24
   class var digitsPerIteration = 14.1816474627254776555

   class var INSTANCE = new BinarySplittingPi[]
   
   /** Call this method with the number of digits of pi you want to calculate.
   */
 
   class pi[digits] :=
   {
      // Find number of terms to calculate
      n2 = floor[digits/digitsPerIteration]
      origPrec = getPrecision[]
      try
      {
         setPrecision[digits+3]
         sqrtC = sqrt[C^3, digits+3]
         return 1./(12/sqrtC * BinarySplitting.calc[INSTANCE, 0, n2, digits])
      }
      finally
         setPrecision[origPrec]
   }

   /** Implementation of the ParamProvider interface. */
   a[n] := A + n B

   b[n] := 1

   p[n] :=
   {
      if n == 0
         return 1
      else
         return -(6n-5)(2n-1)(6n-1)
   }

   q[n] :=
   {
      if n == 0
         return 1
      else
         return n^3 C3over24
   }

}
    


Download or view BinarySplittingPi.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 19945 days, 6 hours, 53 minutes ago.