Math-MPFR
view release on metacpan or search on metacpan
demos/euler.p view on Meta::CPAN
#################################################################################
# This script requires Math::GMPq, Math::GMPz, and Math::MPFR. #
# It calculates the euler number e (2.7182818...), correct to $ARGV[0] bits. #
# The calculated value is displayed unless $ARGV[1] is both provided and false. #
# With each iteration of the for{} loop (below) we get closer and closer to #
# the actual value of e. Furthermore, with successive iterations of the for{} #
# loop, the values alternate between "less than e" and "greater than e". #
# Hence the actual (irrational) value of e is always between the values #
# calculated by successive iterations of the for{} loop. #
# #
# Of course, the simplest and most efficient way to get the value of e, to #
# $ARGV[0] bits is simply to do: #
# Rmpfr_exp($rop, Math::MPFR->new(1), MPFR_RNDN) #
# where $rop is a $ARGV[0]-bit precision Math::MPFR object. #
# But doing it that way is a bit less interesting. #
# #
# The same for{} loop can be also used to calculate the exact probabilities of #
# "winning" at a simplistic solitaire-type card game. See demos/solitaire.p #
# in the Math::GMPz source distro. #
#################################################################################
use strict;
use warnings;
use Math::GMPz qw(:mpz);
use Math::GMPq qw(:mpq);
use Math::MPFR qw(:mpfr);
die "Usage: perl euler.pl bits [True|False]" unless @ARGV;
my $bits = shift;
Rmpfr_set_default_prec($bits);
my $display_value;
$display_value = defined($ARGV[0]) ? shift : 1;
#################################################################################
# For the sanity checks (below), set $e_big_p to e, correct to $bits+100 bits. #
# Then convert $e_big_p exactly to a rational, $e_q (a Math::GMPq object). #
#################################################################################
my $e_q = Math::GMPq->new(); #
my $e_big_p = Rmpfr_init2($bits + 100); #
Rmpfr_exp($e_big_p, Math::MPFR->new(1), MPFR_RNDN); #
Rmpfr_get_q($e_q, $e_big_p); #
#################################################################################
#################################################################
# Create some variables, and assign some initial values #
#################################################################
my $first = Math::GMPz->new(1); #
my $second = Math::GMPz->new(0); #
my $current_items = 2; #
my $factorial = Math::GMPz->new(1); #
my $e_check = Math::GMPq->new(); #
my ($e, $e_first_fr, $e_second_fr) = (Math::MPFR->new(), #
Math::MPFR->new(), #
Math::MPFR->new(), #
); #
my $chance; # becomes a MATH::GMPz object on assignment #
my $e_first = Math::GMPq->new(3); #
my $e_second = Math::GMPq->new(); #
my $count = 0; #
my $t = Math::GMPq->new(); #
my $save = Math::GMPq->new(4); #
#################################################################
#################################################################
# Set $e to a $bits-bit approximation of the euler number, #
# rounded to nearest. #
# This should exactly equal the number that we calculate. #
#################################################################
( run in 0.896 second using v1.01-cache-2.11-cpan-71847e10f99 )