The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

ConstantCalculus::CircleConstant - Perl extension for the calculation of circle constants.

SYNOPSIS

  # Load the Perl module.
  use ConstantCalculus::CircleConstant;

  # Set the global control output variable to 0 or 1.
  [$CONTROL_OUTPUT = 0|1];

  # Call the calculation method for Pi.
  my $pi = pi_chudnovsky($places);

  # Call the calculation algorithm for Pi directly.
  my $pi = pi_chudnovsky($PLACES[, $TERMS, $PRECISION]);

  # Call the calculation method for Tau.
  my $tau = tau_chudnovsky($places);

  # Call the calculation algorithm for Tau directly.
  my $tau = tau_chudnovsky($PLACES[, $TERMS, $PRECISION]);

DESCRIPTION

The circle constant is a mathematical constant. There are two variants of the circle constant. Most common is the use of Pi (π) for the circle constant. More uncommon is the use of Tau (τ) for the circle constant. The relation between them is τ = 2 π. There can be found other denotations for the well known name Pi. The names are Archimedes's constant or Ludolph's constant. The circle constant is used in formulas across mathematics and physics.

The circle constant is the ratio of the circumference U of a circle to its diameter d, which is π = U/d or τ = 2 U/d. It is an irrational number, which means that it cannot be expressed exactly as a ratio of two integers. Fractions such as 22/7 or 355/113 can be used to approximate the circle constant Pi, whereas 44/7 or 710/113 represent the approximation of the circle constant Tau. In consequence of this, the decimal representation of a circle constant never ends in there decimal places having a infinite number of places, nor enters a permanently repeating pattern in the decimal places. The circle constant is also a not algebraic transcendental number.

Over the centuries scientists developed formulas for approximating the circle constant Pi. Chudnovsky's formula is one of them. A algorithm based on Chudnovsky's formula can be used to calculate an approximation for Pi and also for Tau. The advantage of the Chudnovsky formula is the good convergence. In contradiction the convergence of the Leibniz formula is quit bad.

The challenge in providing an algorithm for the circle constant is that all decimal places must be correct in terms of the formula. Based on the desired decimal place number or precision, the number of places must be correct. The provided algorithm takes care of this. At the moment the result of the implemented algorithm was checkt against the known decimal places of Pi up to 10000 places.

IMPLEMENTATION

To be able to deal with large numbers pre decimal point and after the decimal point as needed, the Perl module bignum is used. The main subroutine argument is the number of places, which should be calculated for the circle constant.

If terms and precision is not given, both are estimated from the given number of places. This will result in a value of Pi, which is accurate to the requested places. If places, terms and/or precision is given, the behaviour of the algorithm can be studied with respect to terms and/or precision.

The number of iterations is calculated using the knowledge, that each iteration should result in 14 new digits after the decimal point. So the value for the calculation of the number of terms is set to 14. To make sure that reverse as less as possible digits are changed, the number of terms to calculated is uneven. So the sign of the term to add is negative after the decimal point.

To prevent rounding errors in the last digit, the precision is a factor of 14 higher than the requested number of places. The correct number of places is realised by truncating the calculated and possibly rounded value of Pi to the requested number of places.

EXAMPLES

Pi

Example 1

  # Load the Perl module.
  use ConstantCalculus::CircleConstant;

  # Declare the variable for Pi.
  my $pi = undef;

  # Set the number of places.
  my $places = 100;  

  # Calculate Pi.
  $pi = pi_chudnovsky($places);
  print $pi . "\n";

Example 2

  # Load the Perl module.
  use ConstantCalculus::CircleConstant;

  # Declare the variable for Pi.
  my $pi = undef;

  # Set the number of places.
  my $places = 100;  

  # Set the number of terms.
  my $terms = 50;  

  # Set the precision.
  my $precision = 115;  

  # Calculate Pi.
  $pi = pi_chudnovsky($places, $terms, $precision);
  print $pi . "\n";

Example 3

Load the Alias pi of the method pi_chudnovsky and set the flag for the control output to 1. Default is 0.

  # Load the Perl module and import the method pi.
  use ConstantCalculus::CircleConstant qw(pi);

  # Set the control output flag.
  $CONTROL_OUTPUT = 1;

  # Declare the variable for Pi.
  my $pi = undef;

  # Set the number of places.
  my $places = 64;  

  # Calculate Pi.
  $pi = pi($places);
  print $pi . "\n";

Tau

Example 1

  # Load the Perl module.
  use ConstantCalculus::CircleConstant;

  # Declare the variable for Tau.
  my $tau = undef;

  # Set the number of places.
  my $places = 100;  

  # Calculate Tau.
  $tau = tau_chudnovsky($places);
  print $tau . "\n";

Example 2

  # Load the Perl module.
  use ConstantCalculus::CircleConstant;

  # Declare the variable for Tau.
  my $tau = undef;

  # Set the number of places.
  my $places = 100;  

  # Set the number of terms.
  my $terms = 50;  

  # Set the precision.
  my $precision = 115;  

  # Calculate Tau.
  $tau = tau_chudnovsky($places, $terms, $precision);
  print $tau . "\n";

Example 3

Load the Alias tau of the method tau_chudnovsky and set the flag for the control output to 1. Default is 0.

  # Load the Perl module and import the method pi.
  use ConstantCalculus::CircleConstant qw(tau);

  # Set the control output flag.
  $CONTROL_OUTPUT = 1;

  # Declare the variable for Pi.
  my $tau = undef;

  # Set the number of places.
  my $places = 64;  

  # Calculate Tau.
  $tau = tau($places);
  print $tau . "\n";

MODULE METHODS

Main Methods

   pi_chudnovsky()   

   pi_chudnovsky_algorithm()

   tau_chudnovsky()   

   tau_chudnovsky_algorithm()

Other Methods

    truncate_places()

    chudnovsky_terms()

    factorial()

    pi() (Alias)

    tau() (Alias)

MODULE EXPORT

    pi_chudnovsky_algorithm

    pi_chudnovsky   

    truncate_places

    chudnovsky_terms

    factorial

    pi (Alias)

    tau (Alias)

LIMITATIONS

Limitations are not known at the moment.

BUGS

Bugs are not known yet.

NOTES

The implemented chudnovsky algorithm is used in a representation where the well known terms are optimised for calculation.

OPEN ISSUE

Further calculations with higher precision are outstanding to check the accuracy of the correctness of the last digits of the calculated circle constant.

It has to be checked, if the Perl module bignum can be used for the calculation of the required really large numbers after the decimal point.

It has also be checked, if there can be possibly a accuracy problem in using the needed square root algorithm sqrt() provided by Perl.

SEE ALSO

CPAN bignum

Perldoc bignum

Circle Constant Pi

Circle Constant Tau

Leibniz formula

Chudnovsky formula

AUTHOR

Dr. Peter Netz, <ztenretep@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2022 by Dr. Peter Netz

This library is free software; you can redistribute it and/or modify it under the same terms of The MIT License. For more details, see the full text of the license in the attached file LICENSE in the main module folder. This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.