diff options
author | Siep Kroonenberg <siepo@cybercomm.nl> | 2011-02-17 17:57:31 +0000 |
---|---|---|
committer | Siep Kroonenberg <siepo@cybercomm.nl> | 2011-02-17 17:57:31 +0000 |
commit | 320d8694fec25ed148613684543b5a0504a046ae (patch) | |
tree | 0ddcf933d3acd3c98a387fa2bf73d0554ca6e50d /Master/tlpkg/tlperl/lib/Math | |
parent | 779e71f16ca01a6244b632b95bdb461fec163b34 (diff) |
New tlperl part XIV
git-svn-id: svn://tug.org/texlive/trunk@21436 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/tlpkg/tlperl/lib/Math')
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigFloat.pm | 4402 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigFloat/Trace.pm | 58 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigInt.pm | 5115 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigInt/Calc.pm | 2612 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigInt/CalcEmu.pm | 329 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigInt/FastCalc.pm | 125 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigInt/Trace.pm | 47 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigRat.pm | 2020 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/Complex.pm | 2093 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/Trig.pm | 768 |
10 files changed, 17569 insertions, 0 deletions
diff --git a/Master/tlpkg/tlperl/lib/Math/BigFloat.pm b/Master/tlpkg/tlperl/lib/Math/BigFloat.pm new file mode 100644 index 00000000000..27d60b3143c --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Math/BigFloat.pm @@ -0,0 +1,4402 @@ +package Math::BigFloat; + +# +# Mike grinned. 'Two down, infinity to go' - Mike Nostrus in 'Before and After' +# + +# The following hash values are internally used: +# _e : exponent (ref to $CALC object) +# _m : mantissa (ref to $CALC object) +# _es : sign of _e +# sign : +,-,+inf,-inf, or "NaN" if not a number +# _a : accuracy +# _p : precision + +$VERSION = '1.60'; +require 5.006; + +require Exporter; +@ISA = qw/Math::BigInt/; +@EXPORT_OK = qw/bpi/; + +use strict; +# $_trap_inf/$_trap_nan are internal and should never be accessed from outside +use vars qw/$AUTOLOAD $accuracy $precision $div_scale $round_mode $rnd_mode + $upgrade $downgrade $_trap_nan $_trap_inf/; +my $class = "Math::BigFloat"; + +use overload +'<=>' => sub { my $rc = $_[2] ? + ref($_[0])->bcmp($_[1],$_[0]) : + ref($_[0])->bcmp($_[0],$_[1]); + $rc = 1 unless defined $rc; + $rc <=> 0; + }, +# we need '>=' to get things like "1 >= NaN" right: +'>=' => sub { my $rc = $_[2] ? + ref($_[0])->bcmp($_[1],$_[0]) : + ref($_[0])->bcmp($_[0],$_[1]); + # if there was a NaN involved, return false + return '' unless defined $rc; + $rc >= 0; + }, +'int' => sub { $_[0]->as_number() }, # 'trunc' to bigint +; + +############################################################################## +# global constants, flags and assorted stuff + +# the following are public, but their usage is not recommended. Use the +# accessor methods instead. + +# class constants, use Class->constant_name() to access +# one of 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common' +$round_mode = 'even'; +$accuracy = undef; +$precision = undef; +$div_scale = 40; + +$upgrade = undef; +$downgrade = undef; +# the package we are using for our private parts, defaults to: +# Math::BigInt->config()->{lib} +my $MBI = 'Math::BigInt::FastCalc'; + +# are NaNs ok? (otherwise it dies when encountering an NaN) set w/ config() +$_trap_nan = 0; +# the same for infinity +$_trap_inf = 0; + +# constant for easier life +my $nan = 'NaN'; + +my $IMPORT = 0; # was import() called yet? used to make require work + +# some digits of accuracy for blog(undef,10); which we use in blog() for speed +my $LOG_10 = + '2.3025850929940456840179914546843642076011014886287729760333279009675726097'; +my $LOG_10_A = length($LOG_10)-1; +# ditto for log(2) +my $LOG_2 = + '0.6931471805599453094172321214581765680755001343602552541206800094933936220'; +my $LOG_2_A = length($LOG_2)-1; +my $HALF = '0.5'; # made into an object if nec. + +############################################################################## +# the old code had $rnd_mode, so we need to support it, too + +sub TIESCALAR { my ($class) = @_; bless \$round_mode, $class; } +sub FETCH { return $round_mode; } +sub STORE { $rnd_mode = $_[0]->round_mode($_[1]); } + +BEGIN + { + # when someone sets $rnd_mode, we catch this and check the value to see + # whether it is valid or not. + $rnd_mode = 'even'; tie $rnd_mode, 'Math::BigFloat'; + + # we need both of them in this package: + *as_int = \&as_number; + } + +############################################################################## + +{ + # valid method aliases for AUTOLOAD + my %methods = map { $_ => 1 } + qw / fadd fsub fmul fdiv fround ffround fsqrt fmod fstr fsstr fpow fnorm + fint facmp fcmp fzero fnan finf finc fdec ffac fneg + fceil ffloor frsft flsft fone flog froot fexp + /; + # valid methods that can be handed up (for AUTOLOAD) + my %hand_ups = map { $_ => 1 } + qw / is_nan is_inf is_negative is_positive is_pos is_neg + accuracy precision div_scale round_mode fabs fnot + objectify upgrade downgrade + bone binf bnan bzero + bsub + /; + + sub _method_alias { exists $methods{$_[0]||''}; } + sub _method_hand_up { exists $hand_ups{$_[0]||''}; } +} + +############################################################################## +# constructors + +sub new + { + # create a new BigFloat object from a string or another bigfloat object. + # _e: exponent + # _m: mantissa + # sign => sign (+/-), or "NaN" + + my ($class,$wanted,@r) = @_; + + # avoid numify-calls by not using || on $wanted! + return $class->bzero() if !defined $wanted; # default to 0 + return $wanted->copy() if UNIVERSAL::isa($wanted,'Math::BigFloat'); + + $class->import() if $IMPORT == 0; # make require work + + my $self = {}; bless $self, $class; + # shortcut for bigints and its subclasses + if ((ref($wanted)) && UNIVERSAL::can( $wanted, "as_number")) + { + $self->{_m} = $wanted->as_number()->{value}; # get us a bigint copy + $self->{_e} = $MBI->_zero(); + $self->{_es} = '+'; + $self->{sign} = $wanted->sign(); + return $self->bnorm(); + } + # else: got a string or something maskerading as number (with overload) + + # handle '+inf', '-inf' first + if ($wanted =~ /^[+-]?inf\z/) + { + return $downgrade->new($wanted) if $downgrade; + + $self->{sign} = $wanted; # set a default sign for bstr() + return $self->binf($wanted); + } + + # shortcut for simple forms like '12' that neither have trailing nor leading + # zeros + if ($wanted =~ /^([+-]?)([1-9][0-9]*[1-9])$/) + { + $self->{_e} = $MBI->_zero(); + $self->{_es} = '+'; + $self->{sign} = $1 || '+'; + $self->{_m} = $MBI->_new($2); + return $self->round(@r) if !$downgrade; + } + + my ($mis,$miv,$mfv,$es,$ev) = Math::BigInt::_split($wanted); + if (!ref $mis) + { + if ($_trap_nan) + { + require Carp; + Carp::croak ("$wanted is not a number initialized to $class"); + } + + return $downgrade->bnan() if $downgrade; + + $self->{_e} = $MBI->_zero(); + $self->{_es} = '+'; + $self->{_m} = $MBI->_zero(); + $self->{sign} = $nan; + } + else + { + # make integer from mantissa by adjusting exp, then convert to int + $self->{_e} = $MBI->_new($$ev); # exponent + $self->{_es} = $$es || '+'; + my $mantissa = "$$miv$$mfv"; # create mant. + $mantissa =~ s/^0+(\d)/$1/; # strip leading zeros + $self->{_m} = $MBI->_new($mantissa); # create mant. + + # 3.123E0 = 3123E-3, and 3.123E-2 => 3123E-5 + if (CORE::length($$mfv) != 0) + { + my $len = $MBI->_new( CORE::length($$mfv)); + ($self->{_e}, $self->{_es}) = + _e_sub ($self->{_e}, $len, $self->{_es}, '+'); + } + # we can only have trailing zeros on the mantissa if $$mfv eq '' + else + { + # Use a regexp to count the trailing zeros in $$miv instead of _zeros() + # because that is faster, especially when _m is not stored in base 10. + my $zeros = 0; $zeros = CORE::length($1) if $$miv =~ /[1-9](0*)$/; + if ($zeros != 0) + { + my $z = $MBI->_new($zeros); + # turn '120e2' into '12e3' + $MBI->_rsft ( $self->{_m}, $z, 10); + ($self->{_e}, $self->{_es}) = + _e_add ( $self->{_e}, $z, $self->{_es}, '+'); + } + } + $self->{sign} = $$mis; + + # for something like 0Ey, set y to 1, and -0 => +0 + # Check $$miv for being '0' and $$mfv eq '', because otherwise _m could not + # have become 0. That's faster than to call $MBI->_is_zero(). + $self->{sign} = '+', $self->{_e} = $MBI->_one() + if $$miv eq '0' and $$mfv eq ''; + + return $self->round(@r) if !$downgrade; + } + # if downgrade, inf, NaN or integers go down + + if ($downgrade && $self->{_es} eq '+') + { + if ($MBI->_is_zero( $self->{_e} )) + { + return $downgrade->new($$mis . $MBI->_str( $self->{_m} )); + } + return $downgrade->new($self->bsstr()); + } + $self->bnorm()->round(@r); # first normalize, then round + } + +sub copy + { + # if two arguments, the first one is the class to "swallow" subclasses + if (@_ > 1) + { + my $self = bless { + sign => $_[1]->{sign}, + _es => $_[1]->{_es}, + _m => $MBI->_copy($_[1]->{_m}), + _e => $MBI->_copy($_[1]->{_e}), + }, $_[0] if @_ > 1; + + $self->{_a} = $_[1]->{_a} if defined $_[1]->{_a}; + $self->{_p} = $_[1]->{_p} if defined $_[1]->{_p}; + return $self; + } + + my $self = bless { + sign => $_[0]->{sign}, + _es => $_[0]->{_es}, + _m => $MBI->_copy($_[0]->{_m}), + _e => $MBI->_copy($_[0]->{_e}), + }, ref($_[0]); + + $self->{_a} = $_[0]->{_a} if defined $_[0]->{_a}; + $self->{_p} = $_[0]->{_p} if defined $_[0]->{_p}; + $self; + } + +sub _bnan + { + # used by parent class bone() to initialize number to NaN + my $self = shift; + + if ($_trap_nan) + { + require Carp; + my $class = ref($self); + Carp::croak ("Tried to set $self to NaN in $class\::_bnan()"); + } + + $IMPORT=1; # call our import only once + $self->{_m} = $MBI->_zero(); + $self->{_e} = $MBI->_zero(); + $self->{_es} = '+'; + } + +sub _binf + { + # used by parent class bone() to initialize number to +-inf + my $self = shift; + + if ($_trap_inf) + { + require Carp; + my $class = ref($self); + Carp::croak ("Tried to set $self to +-inf in $class\::_binf()"); + } + + $IMPORT=1; # call our import only once + $self->{_m} = $MBI->_zero(); + $self->{_e} = $MBI->_zero(); + $self->{_es} = '+'; + } + +sub _bone + { + # used by parent class bone() to initialize number to 1 + my $self = shift; + $IMPORT=1; # call our import only once + $self->{_m} = $MBI->_one(); + $self->{_e} = $MBI->_zero(); + $self->{_es} = '+'; + } + +sub _bzero + { + # used by parent class bone() to initialize number to 0 + my $self = shift; + $IMPORT=1; # call our import only once + $self->{_m} = $MBI->_zero(); + $self->{_e} = $MBI->_one(); + $self->{_es} = '+'; + } + +sub isa + { + my ($self,$class) = @_; + return if $class =~ /^Math::BigInt/; # we aren't one of these + UNIVERSAL::isa($self,$class); + } + +sub config + { + # return (later set?) configuration data as hash ref + my $class = shift || 'Math::BigFloat'; + + if (@_ == 1 && ref($_[0]) ne 'HASH') + { + my $cfg = $class->SUPER::config(); + return $cfg->{$_[0]}; + } + + my $cfg = $class->SUPER::config(@_); + + # now we need only to override the ones that are different from our parent + $cfg->{class} = $class; + $cfg->{with} = $MBI; + $cfg; + } + +############################################################################## +# string conversation + +sub bstr + { + # (ref to BFLOAT or num_str ) return num_str + # Convert number from internal format to (non-scientific) string format. + # internal format is always normalized (no leading zeros, "-0" => "+0") + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN + return 'inf'; # +inf + } + + my $es = '0'; my $len = 1; my $cad = 0; my $dot = '.'; + + # $x is zero? + my $not_zero = !($x->{sign} eq '+' && $MBI->_is_zero($x->{_m})); + if ($not_zero) + { + $es = $MBI->_str($x->{_m}); + $len = CORE::length($es); + my $e = $MBI->_num($x->{_e}); + $e = -$e if $x->{_es} eq '-'; + if ($e < 0) + { + $dot = ''; + # if _e is bigger than a scalar, the following will blow your memory + if ($e <= -$len) + { + my $r = abs($e) - $len; + $es = '0.'. ('0' x $r) . $es; $cad = -($len+$r); + } + else + { + substr($es,$e,0) = '.'; $cad = $MBI->_num($x->{_e}); + $cad = -$cad if $x->{_es} eq '-'; + } + } + elsif ($e > 0) + { + # expand with zeros + $es .= '0' x $e; $len += $e; $cad = 0; + } + } # if not zero + + $es = '-'.$es if $x->{sign} eq '-'; + # if set accuracy or precision, pad with zeros on the right side + if ((defined $x->{_a}) && ($not_zero)) + { + # 123400 => 6, 0.1234 => 4, 0.001234 => 4 + my $zeros = $x->{_a} - $cad; # cad == 0 => 12340 + $zeros = $x->{_a} - $len if $cad != $len; + $es .= $dot.'0' x $zeros if $zeros > 0; + } + elsif ((($x->{_p} || 0) < 0)) + { + # 123400 => 6, 0.1234 => 4, 0.001234 => 6 + my $zeros = -$x->{_p} + $cad; + $es .= $dot.'0' x $zeros if $zeros > 0; + } + $es; + } + +sub bsstr + { + # (ref to BFLOAT or num_str ) return num_str + # Convert number from internal format to scientific string format. + # internal format is always normalized (no leading zeros, "-0E0" => "+0E0") + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN + return 'inf'; # +inf + } + my $sep = 'e'.$x->{_es}; + my $sign = $x->{sign}; $sign = '' if $sign eq '+'; + $sign . $MBI->_str($x->{_m}) . $sep . $MBI->_str($x->{_e}); + } + +sub numify + { + # Make a number from a BigFloat object + # simple return a string and let Perl's atoi()/atof() handle the rest + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + $x->bsstr(); + } + +############################################################################## +# public stuff (usually prefixed with "b") + +sub bneg + { + # (BINT or num_str) return BINT + # negate number or make a negated number from string + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x if $x->modify('bneg'); + + # for +0 dont negate (to have always normalized +0). Does nothing for 'NaN' + $x->{sign} =~ tr/+-/-+/ unless ($x->{sign} eq '+' && $MBI->_is_zero($x->{_m})); + $x; + } + +# tels 2001-08-04 +# XXX TODO this must be overwritten and return NaN for non-integer values +# band(), bior(), bxor(), too +#sub bnot +# { +# $class->SUPER::bnot($class,@_); +# } + +sub bcmp + { + # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort) + + # set up parameters + my ($self,$x,$y) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y) = objectify(2,@_); + } + + return $upgrade->bcmp($x,$y) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + # handle +-inf and NaN + return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + return 0 if ($x->{sign} eq $y->{sign}) && ($x->{sign} =~ /^[+-]inf$/); + return +1 if $x->{sign} eq '+inf'; + return -1 if $x->{sign} eq '-inf'; + return -1 if $y->{sign} eq '+inf'; + return +1; + } + + # check sign for speed first + return 1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # does also 0 <=> -y + return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # does also -x <=> 0 + + # shortcut + my $xz = $x->is_zero(); + my $yz = $y->is_zero(); + return 0 if $xz && $yz; # 0 <=> 0 + return -1 if $xz && $y->{sign} eq '+'; # 0 <=> +y + return 1 if $yz && $x->{sign} eq '+'; # +x <=> 0 + + # adjust so that exponents are equal + my $lxm = $MBI->_len($x->{_m}); + my $lym = $MBI->_len($y->{_m}); + # the numify somewhat limits our length, but makes it much faster + my ($xes,$yes) = (1,1); + $xes = -1 if $x->{_es} ne '+'; + $yes = -1 if $y->{_es} ne '+'; + my $lx = $lxm + $xes * $MBI->_num($x->{_e}); + my $ly = $lym + $yes * $MBI->_num($y->{_e}); + my $l = $lx - $ly; $l = -$l if $x->{sign} eq '-'; + return $l <=> 0 if $l != 0; + + # lengths (corrected by exponent) are equal + # so make mantissa equal length by padding with zero (shift left) + my $diff = $lxm - $lym; + my $xm = $x->{_m}; # not yet copy it + my $ym = $y->{_m}; + if ($diff > 0) + { + $ym = $MBI->_copy($y->{_m}); + $ym = $MBI->_lsft($ym, $MBI->_new($diff), 10); + } + elsif ($diff < 0) + { + $xm = $MBI->_copy($x->{_m}); + $xm = $MBI->_lsft($xm, $MBI->_new(-$diff), 10); + } + my $rc = $MBI->_acmp($xm,$ym); + $rc = -$rc if $x->{sign} eq '-'; # -124 < -123 + $rc <=> 0; + } + +sub bacmp + { + # Compares 2 values, ignoring their signs. + # Returns one of undef, <0, =0, >0. (suitable for sort) + + # set up parameters + my ($self,$x,$y) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y) = objectify(2,@_); + } + + return $upgrade->bacmp($x,$y) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + # handle +-inf and NaN's + if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/) + { + return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + return 0 if ($x->is_inf() && $y->is_inf()); + return 1 if ($x->is_inf() && !$y->is_inf()); + return -1; + } + + # shortcut + my $xz = $x->is_zero(); + my $yz = $y->is_zero(); + return 0 if $xz && $yz; # 0 <=> 0 + return -1 if $xz && !$yz; # 0 <=> +y + return 1 if $yz && !$xz; # +x <=> 0 + + # adjust so that exponents are equal + my $lxm = $MBI->_len($x->{_m}); + my $lym = $MBI->_len($y->{_m}); + my ($xes,$yes) = (1,1); + $xes = -1 if $x->{_es} ne '+'; + $yes = -1 if $y->{_es} ne '+'; + # the numify somewhat limits our length, but makes it much faster + my $lx = $lxm + $xes * $MBI->_num($x->{_e}); + my $ly = $lym + $yes * $MBI->_num($y->{_e}); + my $l = $lx - $ly; + return $l <=> 0 if $l != 0; + + # lengths (corrected by exponent) are equal + # so make mantissa equal-length by padding with zero (shift left) + my $diff = $lxm - $lym; + my $xm = $x->{_m}; # not yet copy it + my $ym = $y->{_m}; + if ($diff > 0) + { + $ym = $MBI->_copy($y->{_m}); + $ym = $MBI->_lsft($ym, $MBI->_new($diff), 10); + } + elsif ($diff < 0) + { + $xm = $MBI->_copy($x->{_m}); + $xm = $MBI->_lsft($xm, $MBI->_new(-$diff), 10); + } + $MBI->_acmp($xm,$ym); + } + +sub badd + { + # add second arg (BFLOAT or string) to first (BFLOAT) (modifies first) + # return result as BFLOAT + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('badd'); + + # inf and NaN handling + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + # NaN first + return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/)) + { + # +inf++inf or -inf+-inf => same, rest is NaN + return $x if $x->{sign} eq $y->{sign}; + return $x->bnan(); + } + # +-inf + something => +inf; something +-inf => +-inf + $x->{sign} = $y->{sign}, return $x if $y->{sign} =~ /^[+-]inf$/; + return $x; + } + + return $upgrade->badd($x,$y,@r) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + $r[3] = $y; # no push! + + # speed: no add for 0+y or x+0 + return $x->bround(@r) if $y->is_zero(); # x+0 + if ($x->is_zero()) # 0+y + { + # make copy, clobbering up x (modify in place!) + $x->{_e} = $MBI->_copy($y->{_e}); + $x->{_es} = $y->{_es}; + $x->{_m} = $MBI->_copy($y->{_m}); + $x->{sign} = $y->{sign} || $nan; + return $x->round(@r); + } + + # take lower of the two e's and adapt m1 to it to match m2 + my $e = $y->{_e}; + $e = $MBI->_zero() if !defined $e; # if no BFLOAT? + $e = $MBI->_copy($e); # make copy (didn't do it yet) + + my $es; + + ($e,$es) = _e_sub($e, $x->{_e}, $y->{_es} || '+', $x->{_es}); + + my $add = $MBI->_copy($y->{_m}); + + if ($es eq '-') # < 0 + { + $MBI->_lsft( $x->{_m}, $e, 10); + ($x->{_e},$x->{_es}) = _e_add($x->{_e}, $e, $x->{_es}, $es); + } + elsif (!$MBI->_is_zero($e)) # > 0 + { + $MBI->_lsft($add, $e, 10); + } + # else: both e are the same, so just leave them + + if ($x->{sign} eq $y->{sign}) + { + # add + $x->{_m} = $MBI->_add($x->{_m}, $add); + } + else + { + ($x->{_m}, $x->{sign}) = + _e_add($x->{_m}, $add, $x->{sign}, $y->{sign}); + } + + # delete trailing zeros, then round + $x->bnorm()->round(@r); + } + +# sub bsub is inherited from Math::BigInt! + +sub binc + { + # increment arg by one + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('binc'); + + if ($x->{_es} eq '-') + { + return $x->badd($self->bone(),@r); # digits after dot + } + + if (!$MBI->_is_zero($x->{_e})) # _e == 0 for NaN, inf, -inf + { + # 1e2 => 100, so after the shift below _m has a '0' as last digit + $x->{_m} = $MBI->_lsft($x->{_m}, $x->{_e},10); # 1e2 => 100 + $x->{_e} = $MBI->_zero(); # normalize + $x->{_es} = '+'; + # we know that the last digit of $x will be '1' or '9', depending on the + # sign + } + # now $x->{_e} == 0 + if ($x->{sign} eq '+') + { + $MBI->_inc($x->{_m}); + return $x->bnorm()->bround(@r); + } + elsif ($x->{sign} eq '-') + { + $MBI->_dec($x->{_m}); + $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # -1 +1 => -0 => +0 + return $x->bnorm()->bround(@r); + } + # inf, nan handling etc + $x->badd($self->bone(),@r); # badd() does round + } + +sub bdec + { + # decrement arg by one + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('bdec'); + + if ($x->{_es} eq '-') + { + return $x->badd($self->bone('-'),@r); # digits after dot + } + + if (!$MBI->_is_zero($x->{_e})) + { + $x->{_m} = $MBI->_lsft($x->{_m}, $x->{_e},10); # 1e2 => 100 + $x->{_e} = $MBI->_zero(); # normalize + $x->{_es} = '+'; + } + # now $x->{_e} == 0 + my $zero = $x->is_zero(); + # <= 0 + if (($x->{sign} eq '-') || $zero) + { + $MBI->_inc($x->{_m}); + $x->{sign} = '-' if $zero; # 0 => 1 => -1 + $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # -1 +1 => -0 => +0 + return $x->bnorm()->round(@r); + } + # > 0 + elsif ($x->{sign} eq '+') + { + $MBI->_dec($x->{_m}); + return $x->bnorm()->round(@r); + } + # inf, nan handling etc + $x->badd($self->bone('-'),@r); # does round + } + +sub DEBUG () { 0; } + +sub blog + { + my ($self,$x,$base,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('blog'); + + # $base > 0, $base != 1; if $base == undef default to $base == e + # $x >= 0 + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters($a,$p,$r); + + # also takes care of the "error in _find_round_parameters?" case + return $x->bnan() if $x->{sign} ne '+' || $x->is_zero(); + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # P = undef + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + return $x->bzero(@params) if $x->is_one(); + # base not defined => base == Euler's number e + if (defined $base) + { + # make object, since we don't feed it through objectify() to still get the + # case of $base == undef + $base = $self->new($base) unless ref($base); + # $base > 0; $base != 1 + return $x->bnan() if $base->is_zero() || $base->is_one() || + $base->{sign} ne '+'; + # if $x == $base, we know the result must be 1.0 + if ($x->bcmp($base) == 0) + { + $x->bone('+',@params); + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + return $x; + } + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; + local $Math::BigFloat::downgrade = undef; + + # upgrade $x if $x is not a BigFloat (handle BigInt input) + # XXX TODO: rebless! + if (!$x->isa('Math::BigFloat')) + { + $x = Math::BigFloat->new($x); + $self = ref($x); + } + + my $done = 0; + + # If the base is defined and an integer, try to calculate integer result + # first. This is very fast, and in case the real result was found, we can + # stop right here. + if (defined $base && $base->is_int() && $x->is_int()) + { + my $i = $MBI->_copy( $x->{_m} ); + $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e}); + my $int = Math::BigInt->bzero(); + $int->{value} = $i; + $int->blog($base->as_number()); + # if ($exact) + if ($base->as_number()->bpow($int) == $x) + { + # found result, return it + $x->{_m} = $int->{value}; + $x->{_e} = $MBI->_zero(); + $x->{_es} = '+'; + $x->bnorm(); + $done = 1; + } + } + + if ($done == 0) + { + # base is undef, so base should be e (Euler's number), so first calculate the + # log to base e (using reduction by 10 (and probably 2)): + $self->_log_10($x,$scale); + + # and if a different base was requested, convert it + if (defined $base) + { + $base = Math::BigFloat->new($base) unless $base->isa('Math::BigFloat'); + # not ln, but some other base (don't modify $base) + $x->bdiv( $base->copy()->blog(undef,$scale), $scale ); + } + } + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + + $x; + } + +sub _len_to_steps + { + # Given D (digits in decimal), compute N so that N! (N factorial) is + # at least D digits long. D should be at least 50. + my $d = shift; + + # two constants for the Ramanujan estimate of ln(N!) + my $lg2 = log(2 * 3.14159265) / 2; + my $lg10 = log(10); + + # D = 50 => N => 42, so L = 40 and R = 50 + my $l = 40; my $r = $d; + + # Otherwise this does not work under -Mbignum and we do not yet have "no bignum;" :( + $l = $l->numify if ref($l); + $r = $r->numify if ref($r); + $lg2 = $lg2->numify if ref($lg2); + $lg10 = $lg10->numify if ref($lg10); + + # binary search for the right value (could this be written as the reverse of lg(n!)?) + while ($r - $l > 1) + { + my $n = int(($r - $l) / 2) + $l; + my $ramanujan = + int(($n * log($n) - $n + log( $n * (1 + 4*$n*(1+2*$n)) ) / 6 + $lg2) / $lg10); + $ramanujan > $d ? $r = $n : $l = $n; + } + $l; + } + +sub bnok + { + # Calculate n over k (binomial coefficient or "choose" function) as integer. + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bnok'); + + return $x->bnan() if $x->is_nan() || $y->is_nan(); + return $x->binf() if $x->is_inf(); + + my $u = $x->as_int(); + $u->bnok($y->as_int()); + + $x->{_m} = $u->{value}; + $x->{_e} = $MBI->_zero(); + $x->{_es} = '+'; + $x->{sign} = '+'; + $x->bnorm(@r); + } + +sub bexp + { + # Calculate e ** X (Euler's number to the power of X) + my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('bexp'); + + return $x->binf() if $x->{sign} eq '+inf'; + return $x->bzero() if $x->{sign} eq '-inf'; + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters($a,$p,$r); + + # also takes care of the "error in _find_round_parameters?" case + return $x if $x->{sign} eq 'NaN'; + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # P = undef + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it's not enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + return $x->bone(@params) if $x->is_zero(); + + if (!$x->isa('Math::BigFloat')) + { + $x = Math::BigFloat->new($x); + $self = ref($x); + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; + local $Math::BigFloat::downgrade = undef; + + my $x_org = $x->copy(); + + # We use the following Taylor series: + + # x x^2 x^3 x^4 + # e = 1 + --- + --- + --- + --- ... + # 1! 2! 3! 4! + + # The difference for each term is X and N, which would result in: + # 2 copy, 2 mul, 2 add, 1 inc, 1 div operations per term + + # But it is faster to compute exp(1) and then raising it to the + # given power, esp. if $x is really big and an integer because: + + # * The numerator is always 1, making the computation faster + # * the series converges faster in the case of x == 1 + # * We can also easily check when we have reached our limit: when the + # term to be added is smaller than "1E$scale", we can stop - f.i. + # scale == 5, and we have 1/40320, then we stop since 1/40320 < 1E-5. + # * we can compute the *exact* result by simulating bigrat math: + + # 1 1 gcd(3,4) = 1 1*24 + 1*6 5 + # - + - = ---------- = -- + # 6 24 6*24 24 + + # We do not compute the gcd() here, but simple do: + # 1 1 1*24 + 1*6 30 + # - + - = --------- = -- + # 6 24 6*24 144 + + # In general: + # a c a*d + c*b and note that c is always 1 and d = (b*f) + # - + - = --------- + # b d b*d + + # This leads to: which can be reduced by b to: + # a 1 a*b*f + b a*f + 1 + # - + - = --------- = ------- + # b b*f b*b*f b*f + + # The first terms in the series are: + + # 1 1 1 1 1 1 1 1 13700 + # -- + -- + -- + -- + -- + --- + --- + ---- = ----- + # 1 1 2 6 24 120 720 5040 5040 + + # Note that we cannot simple reduce 13700/5040 to 685/252, but must keep A and B! + + if ($scale <= 75) + { + # set $x directly from a cached string form + $x->{_m} = $MBI->_new( + "27182818284590452353602874713526624977572470936999595749669676277240766303535476"); + $x->{sign} = '+'; + $x->{_es} = '-'; + $x->{_e} = $MBI->_new(79); + } + else + { + # compute A and B so that e = A / B. + + # After some terms we end up with this, so we use it as a starting point: + my $A = $MBI->_new("90933395208605785401971970164779391644753259799242"); + my $F = $MBI->_new(42); my $step = 42; + + # Compute how many steps we need to take to get $A and $B sufficiently big + my $steps = _len_to_steps($scale - 4); +# print STDERR "# Doing $steps steps for ", $scale-4, " digits\n"; + while ($step++ <= $steps) + { + # calculate $a * $f + 1 + $A = $MBI->_mul($A, $F); + $A = $MBI->_inc($A); + # increment f + $F = $MBI->_inc($F); + } + # compute $B as factorial of $steps (this is faster than doing it manually) + my $B = $MBI->_fac($MBI->_new($steps)); + +# print "A ", $MBI->_str($A), "\nB ", $MBI->_str($B), "\n"; + + # compute A/B with $scale digits in the result (truncate, not round) + $A = $MBI->_lsft( $A, $MBI->_new($scale), 10); + $A = $MBI->_div( $A, $B ); + + $x->{_m} = $A; + $x->{sign} = '+'; + $x->{_es} = '-'; + $x->{_e} = $MBI->_new($scale); + } + + # $x contains now an estimate of e, with some surplus digits, so we can round + if (!$x_org->is_one()) + { + # raise $x to the wanted power and round it in one step: + $x->bpow($x_org, @params); + } + else + { + # else just round the already computed result + delete $x->{_a}; delete $x->{_p}; + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + + $x; # return modified $x + } + +sub _log + { + # internal log function to calculate ln() based on Taylor series. + # Modifies $x in place. + my ($self,$x,$scale) = @_; + + # in case of $x == 1, result is 0 + return $x->bzero() if $x->is_one(); + + # XXX TODO: rewrite this in a similiar manner to bexp() + + # http://www.efunda.com/math/taylor_series/logarithmic.cfm?search_string=log + + # u = x-1, v = x+1 + # _ _ + # Taylor: | u 1 u^3 1 u^5 | + # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 0 + # |_ v 3 v^3 5 v^5 _| + + # This takes much more steps to calculate the result and is thus not used + # u = x-1 + # _ _ + # Taylor: | u 1 u^2 1 u^3 | + # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 1/2 + # |_ x 2 x^2 3 x^3 _| + + my ($limit,$v,$u,$below,$factor,$two,$next,$over,$f); + + $v = $x->copy(); $v->binc(); # v = x+1 + $x->bdec(); $u = $x->copy(); # u = x-1; x = x-1 + $x->bdiv($v,$scale); # first term: u/v + $below = $v->copy(); + $over = $u->copy(); + $u *= $u; $v *= $v; # u^2, v^2 + $below->bmul($v); # u^3, v^3 + $over->bmul($u); + $factor = $self->new(3); $f = $self->new(2); + + my $steps = 0 if DEBUG; + $limit = $self->new("1E-". ($scale-1)); + while (3 < 5) + { + # we calculate the next term, and add it to the last + # when the next term is below our limit, it won't affect the outcome + # anymore, so we stop + + # calculating the next term simple from over/below will result in quite + # a time hog if the input has many digits, since over and below will + # accumulate more and more digits, and the result will also have many + # digits, but in the end it is rounded to $scale digits anyway. So if we + # round $over and $below first, we save a lot of time for the division + # (not with log(1.2345), but try log (123**123) to see what I mean. This + # can introduce a rounding error if the division result would be f.i. + # 0.1234500000001 and we round it to 5 digits it would become 0.12346, but + # if we truncated $over and $below we might get 0.12345. Does this matter + # for the end result? So we give $over and $below 4 more digits to be + # on the safe side (unscientific error handling as usual... :+D + + $next = $over->copy->bround($scale+4)->bdiv( + $below->copy->bmul($factor)->bround($scale+4), + $scale); + +## old version: +## $next = $over->copy()->bdiv($below->copy()->bmul($factor),$scale); + + last if $next->bacmp($limit) <= 0; + + delete $next->{_a}; delete $next->{_p}; + $x->badd($next); + # calculate things for the next term + $over *= $u; $below *= $v; $factor->badd($f); + if (DEBUG) + { + $steps++; print "step $steps = $x\n" if $steps % 10 == 0; + } + } + print "took $steps steps\n" if DEBUG; + $x->bmul($f); # $x *= 2 + } + +sub _log_10 + { + # Internal log function based on reducing input to the range of 0.1 .. 9.99 + # and then "correcting" the result to the proper one. Modifies $x in place. + my ($self,$x,$scale) = @_; + + # Taking blog() from numbers greater than 10 takes a *very long* time, so we + # break the computation down into parts based on the observation that: + # blog(X*Y) = blog(X) + blog(Y) + # We set Y here to multiples of 10 so that $x becomes below 1 - the smaller + # $x is the faster it gets. Since 2*$x takes about 10 times as + # long, we make it faster by about a factor of 100 by dividing $x by 10. + + # The same observation is valid for numbers smaller than 0.1, e.g. computing + # log(1) is fastest, and the further away we get from 1, the longer it takes. + # So we also 'break' this down by multiplying $x with 10 and subtract the + # log(10) afterwards to get the correct result. + + # To get $x even closer to 1, we also divide by 2 and then use log(2) to + # correct for this. For instance if $x is 2.4, we use the formula: + # blog(2.4 * 2) == blog (1.2) + blog(2) + # and thus calculate only blog(1.2) and blog(2), which is faster in total + # than calculating blog(2.4). + + # In addition, the values for blog(2) and blog(10) are cached. + + # Calculate nr of digits before dot: + my $dbd = $MBI->_num($x->{_e}); + $dbd = -$dbd if $x->{_es} eq '-'; + $dbd += $MBI->_len($x->{_m}); + + # more than one digit (e.g. at least 10), but *not* exactly 10 to avoid + # infinite recursion + + my $calc = 1; # do some calculation? + + # disable the shortcut for 10, since we need log(10) and this would recurse + # infinitely deep + if ($x->{_es} eq '+' && $MBI->_is_one($x->{_e}) && $MBI->_is_one($x->{_m})) + { + $dbd = 0; # disable shortcut + # we can use the cached value in these cases + if ($scale <= $LOG_10_A) + { + $x->bzero(); $x->badd($LOG_10); # modify $x in place + $calc = 0; # no need to calc, but round + } + # if we can't use the shortcut, we continue normally + } + else + { + # disable the shortcut for 2, since we maybe have it cached + if (($MBI->_is_zero($x->{_e}) && $MBI->_is_two($x->{_m}))) + { + $dbd = 0; # disable shortcut + # we can use the cached value in these cases + if ($scale <= $LOG_2_A) + { + $x->bzero(); $x->badd($LOG_2); # modify $x in place + $calc = 0; # no need to calc, but round + } + # if we can't use the shortcut, we continue normally + } + } + + # if $x = 0.1, we know the result must be 0-log(10) + if ($calc != 0 && $x->{_es} eq '-' && $MBI->_is_one($x->{_e}) && + $MBI->_is_one($x->{_m})) + { + $dbd = 0; # disable shortcut + # we can use the cached value in these cases + if ($scale <= $LOG_10_A) + { + $x->bzero(); $x->bsub($LOG_10); + $calc = 0; # no need to calc, but round + } + } + + return if $calc == 0; # already have the result + + # default: these correction factors are undef and thus not used + my $l_10; # value of ln(10) to A of $scale + my $l_2; # value of ln(2) to A of $scale + + my $two = $self->new(2); + + # $x == 2 => 1, $x == 13 => 2, $x == 0.1 => 0, $x == 0.01 => -1 + # so don't do this shortcut for 1 or 0 + if (($dbd > 1) || ($dbd < 0)) + { + # convert our cached value to an object if not already (avoid doing this + # at import() time, since not everybody needs this) + $LOG_10 = $self->new($LOG_10,undef,undef) unless ref $LOG_10; + + #print "x = $x, dbd = $dbd, calc = $calc\n"; + # got more than one digit before the dot, or more than one zero after the + # dot, so do: + # log(123) == log(1.23) + log(10) * 2 + # log(0.0123) == log(1.23) - log(10) * 2 + + if ($scale <= $LOG_10_A) + { + # use cached value + $l_10 = $LOG_10->copy(); # copy for mul + } + else + { + # else: slower, compute and cache result + # also disable downgrade for this code path + local $Math::BigFloat::downgrade = undef; + + # shorten the time to calculate log(10) based on the following: + # log(1.25 * 8) = log(1.25) + log(8) + # = log(1.25) + log(2) + log(2) + log(2) + + # first get $l_2 (and possible compute and cache log(2)) + $LOG_2 = $self->new($LOG_2,undef,undef) unless ref $LOG_2; + if ($scale <= $LOG_2_A) + { + # use cached value + $l_2 = $LOG_2->copy(); # copy() for the mul below + } + else + { + # else: slower, compute and cache result + $l_2 = $two->copy(); $self->_log($l_2, $scale); # scale+4, actually + $LOG_2 = $l_2->copy(); # cache the result for later + # the copy() is for mul below + $LOG_2_A = $scale; + } + + # now calculate log(1.25): + $l_10 = $self->new('1.25'); $self->_log($l_10, $scale); # scale+4, actually + + # log(1.25) + log(2) + log(2) + log(2): + $l_10->badd($l_2); + $l_10->badd($l_2); + $l_10->badd($l_2); + $LOG_10 = $l_10->copy(); # cache the result for later + # the copy() is for mul below + $LOG_10_A = $scale; + } + $dbd-- if ($dbd > 1); # 20 => dbd=2, so make it dbd=1 + $l_10->bmul( $self->new($dbd)); # log(10) * (digits_before_dot-1) + my $dbd_sign = '+'; + if ($dbd < 0) + { + $dbd = -$dbd; + $dbd_sign = '-'; + } + ($x->{_e}, $x->{_es}) = + _e_sub( $x->{_e}, $MBI->_new($dbd), $x->{_es}, $dbd_sign); # 123 => 1.23 + + } + + # Now: 0.1 <= $x < 10 (and possible correction in l_10) + + ### Since $x in the range 0.5 .. 1.5 is MUCH faster, we do a repeated div + ### or mul by 2 (maximum times 3, since x < 10 and x > 0.1) + + $HALF = $self->new($HALF) unless ref($HALF); + + my $twos = 0; # default: none (0 times) + while ($x->bacmp($HALF) <= 0) # X <= 0.5 + { + $twos--; $x->bmul($two); + } + while ($x->bacmp($two) >= 0) # X >= 2 + { + $twos++; $x->bdiv($two,$scale+4); # keep all digits + } + # $twos > 0 => did mul 2, < 0 => did div 2 (but we never did both) + # So calculate correction factor based on ln(2): + if ($twos != 0) + { + $LOG_2 = $self->new($LOG_2,undef,undef) unless ref $LOG_2; + if ($scale <= $LOG_2_A) + { + # use cached value + $l_2 = $LOG_2->copy(); # copy() for the mul below + } + else + { + # else: slower, compute and cache result + # also disable downgrade for this code path + local $Math::BigFloat::downgrade = undef; + $l_2 = $two->copy(); $self->_log($l_2, $scale); # scale+4, actually + $LOG_2 = $l_2->copy(); # cache the result for later + # the copy() is for mul below + $LOG_2_A = $scale; + } + $l_2->bmul($twos); # * -2 => subtract, * 2 => add + } + + $self->_log($x,$scale); # need to do the "normal" way + $x->badd($l_10) if defined $l_10; # correct it by ln(10) + $x->badd($l_2) if defined $l_2; # and maybe by ln(2) + + # all done, $x contains now the result + $x; + } + +sub blcm + { + # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT + # does not modify arguments, but returns new object + # Lowest Common Multiplicator + + my ($self,@arg) = objectify(0,@_); + my $x = $self->new(shift @arg); + while (@arg) { $x = Math::BigInt::__lcm($x,shift @arg); } + $x; + } + +sub bgcd + { + # (BINT or num_str, BINT or num_str) return BINT + # does not modify arguments, but returns new object + + my $y = shift; + $y = __PACKAGE__->new($y) if !ref($y); + my $self = ref($y); + my $x = $y->copy()->babs(); # keep arguments + + return $x->bnan() if $x->{sign} !~ /^[+-]$/ # x NaN? + || !$x->is_int(); # only for integers now + + while (@_) + { + my $t = shift; $t = $self->new($t) if !ref($t); + $y = $t->copy()->babs(); + + return $x->bnan() if $y->{sign} !~ /^[+-]$/ # y NaN? + || !$y->is_int(); # only for integers now + + # greatest common divisor + while (! $y->is_zero()) + { + ($x,$y) = ($y->copy(), $x->copy()->bmod($y)); + } + + last if $x->is_one(); + } + $x; + } + +############################################################################## + +sub _e_add + { + # Internal helper sub to take two positive integers and their signs and + # then add them. Input ($CALC,$CALC,('+'|'-'),('+'|'-')), + # output ($CALC,('+'|'-')) + my ($x,$y,$xs,$ys) = @_; + + # if the signs are equal we can add them (-5 + -3 => -(5 + 3) => -8) + if ($xs eq $ys) + { + $x = $MBI->_add ($x, $y ); # a+b + # the sign follows $xs + return ($x, $xs); + } + + my $a = $MBI->_acmp($x,$y); + if ($a > 0) + { + $x = $MBI->_sub ($x , $y); # abs sub + } + elsif ($a == 0) + { + $x = $MBI->_zero(); # result is 0 + $xs = '+'; + } + else # a < 0 + { + $x = $MBI->_sub ( $y, $x, 1 ); # abs sub + $xs = $ys; + } + ($x,$xs); + } + +sub _e_sub + { + # Internal helper sub to take two positive integers and their signs and + # then subtract them. Input ($CALC,$CALC,('+'|'-'),('+'|'-')), + # output ($CALC,('+'|'-')) + my ($x,$y,$xs,$ys) = @_; + + # flip sign + $ys =~ tr/+-/-+/; + _e_add($x,$y,$xs,$ys); # call add (does subtract now) + } + +############################################################################### +# is_foo methods (is_negative, is_positive are inherited from BigInt) + +sub is_int + { + # return true if arg (BFLOAT or num_str) is an integer + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + (($x->{sign} =~ /^[+-]$/) && # NaN and +-inf aren't + ($x->{_es} eq '+')) ? 1 : 0; # 1e-1 => no integer + } + +sub is_zero + { + # return true if arg (BFLOAT or num_str) is zero + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + ($x->{sign} eq '+' && $MBI->_is_zero($x->{_m})) ? 1 : 0; + } + +sub is_one + { + # return true if arg (BFLOAT or num_str) is +1 or -1 if signis given + my ($self,$x,$sign) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + $sign = '+' if !defined $sign || $sign ne '-'; + + ($x->{sign} eq $sign && + $MBI->_is_zero($x->{_e}) && + $MBI->_is_one($x->{_m}) ) ? 1 : 0; + } + +sub is_odd + { + # return true if arg (BFLOAT or num_str) is odd or false if even + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + (($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't + ($MBI->_is_zero($x->{_e})) && + ($MBI->_is_odd($x->{_m}))) ? 1 : 0; + } + +sub is_even + { + # return true if arg (BINT or num_str) is even or false if odd + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + (($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't + ($x->{_es} eq '+') && # 123.45 isn't + ($MBI->_is_even($x->{_m}))) ? 1 : 0; # but 1200 is + } + +sub bmul + { + # multiply two numbers + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bmul'); + + return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) + { + return $x->bnan() if $x->is_zero() || $y->is_zero(); + # result will always be +-inf: + # +inf * +/+inf => +inf, -inf * -/-inf => +inf + # +inf * -/-inf => -inf, -inf * +/+inf => -inf + return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); + return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); + return $x->binf('-'); + } + + return $upgrade->bmul($x,$y,@r) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + # aEb * cEd = (a*c)E(b+d) + $MBI->_mul($x->{_m},$y->{_m}); + ($x->{_e}, $x->{_es}) = _e_add($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es}); + + $r[3] = $y; # no push! + + # adjust sign: + $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+'; + $x->bnorm->round(@r); + } + +sub bmuladd + { + # multiply two numbers and add the third to the result + + # set up parameters + my ($self,$x,$y,$z,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$z,@r) = objectify(3,@_); + } + + return $x if $x->modify('bmuladd'); + + return $x->bnan() if (($x->{sign} eq $nan) || + ($y->{sign} eq $nan) || + ($z->{sign} eq $nan)); + + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) + { + return $x->bnan() if $x->is_zero() || $y->is_zero(); + # result will always be +-inf: + # +inf * +/+inf => +inf, -inf * -/-inf => +inf + # +inf * -/-inf => -inf, -inf * +/+inf => -inf + return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); + return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); + return $x->binf('-'); + } + + return $upgrade->bmul($x,$y,@r) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + # aEb * cEd = (a*c)E(b+d) + $MBI->_mul($x->{_m},$y->{_m}); + ($x->{_e}, $x->{_es}) = _e_add($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es}); + + $r[3] = $y; # no push! + + # adjust sign: + $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+'; + + # z=inf handling (z=NaN handled above) + $x->{sign} = $z->{sign}, return $x if $z->{sign} =~ /^[+-]inf$/; + + # take lower of the two e's and adapt m1 to it to match m2 + my $e = $z->{_e}; + $e = $MBI->_zero() if !defined $e; # if no BFLOAT? + $e = $MBI->_copy($e); # make copy (didn't do it yet) + + my $es; + + ($e,$es) = _e_sub($e, $x->{_e}, $z->{_es} || '+', $x->{_es}); + + my $add = $MBI->_copy($z->{_m}); + + if ($es eq '-') # < 0 + { + $MBI->_lsft( $x->{_m}, $e, 10); + ($x->{_e},$x->{_es}) = _e_add($x->{_e}, $e, $x->{_es}, $es); + } + elsif (!$MBI->_is_zero($e)) # > 0 + { + $MBI->_lsft($add, $e, 10); + } + # else: both e are the same, so just leave them + + if ($x->{sign} eq $z->{sign}) + { + # add + $x->{_m} = $MBI->_add($x->{_m}, $add); + } + else + { + ($x->{_m}, $x->{sign}) = + _e_add($x->{_m}, $add, $x->{sign}, $z->{sign}); + } + + # delete trailing zeros, then round + $x->bnorm()->round(@r); + } + +sub bdiv + { + # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return + # (BFLOAT,BFLOAT) (quo,rem) or BFLOAT (only rem) + + # set up parameters + my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$a,$p,$r) = objectify(2,@_); + } + + return $x if $x->modify('bdiv'); + + return $self->_div_inf($x,$y) + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/) || $y->is_zero()); + + # x== 0 # also: or y == 1 or y == -1 + return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero(); + + # upgrade ? + return $upgrade->bdiv($upgrade->new($x),$y,$a,$p,$r) if defined $upgrade; + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my (@params,$scale); + ($x,@params) = $x->_find_round_parameters($a,$p,$r,$y); + + return $x if $x->is_nan(); # error in _find_round_parameters? + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + my $rem; $rem = $self->bzero() if wantarray; + + $y = $self->new($y) unless $y->isa('Math::BigFloat'); + + my $lx = $MBI->_len($x->{_m}); my $ly = $MBI->_len($y->{_m}); + $scale = $lx if $lx > $scale; + $scale = $ly if $ly > $scale; + my $diff = $ly - $lx; + $scale += $diff if $diff > 0; # if lx << ly, but not if ly << lx! + + # already handled inf/NaN/-inf above: + + # check that $y is not 1 nor -1 and cache the result: + my $y_not_one = !($MBI->_is_zero($y->{_e}) && $MBI->_is_one($y->{_m})); + + # flipping the sign of $y will also flip the sign of $x for the special + # case of $x->bsub($x); so we can catch it below: + my $xsign = $x->{sign}; + $y->{sign} =~ tr/+-/-+/; + + if ($xsign ne $x->{sign}) + { + # special case of $x /= $x results in 1 + $x->bone(); # "fixes" also sign of $y, since $x is $y + } + else + { + # correct $y's sign again + $y->{sign} =~ tr/+-/-+/; + # continue with normal div code: + + # make copy of $x in case of list context for later reminder calculation + if (wantarray && $y_not_one) + { + $rem = $x->copy(); + } + + $x->{sign} = $x->{sign} ne $y->sign() ? '-' : '+'; + + # check for / +-1 ( +/- 1E0) + if ($y_not_one) + { + # promote BigInts and it's subclasses (except when already a BigFloat) + $y = $self->new($y) unless $y->isa('Math::BigFloat'); + + # calculate the result to $scale digits and then round it + # a * 10 ** b / c * 10 ** d => a/c * 10 ** (b-d) + $MBI->_lsft($x->{_m},$MBI->_new($scale),10); + $MBI->_div ($x->{_m},$y->{_m}); # a/c + + # correct exponent of $x + ($x->{_e},$x->{_es}) = _e_sub($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es}); + # correct for 10**scale + ($x->{_e},$x->{_es}) = _e_sub($x->{_e}, $MBI->_new($scale), $x->{_es}, '+'); + $x->bnorm(); # remove trailing 0's + } + } # ende else $x != $y + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + delete $x->{_a}; # clear before round + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + delete $x->{_p}; # clear before round + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + + if (wantarray) + { + if ($y_not_one) + { + $rem->bmod($y,@params); # copy already done + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $rem->{_a}; delete $rem->{_p}; + } + return ($x,$rem); + } + $x; + } + +sub bmod + { + # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return reminder + + # set up parameters + my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$a,$p,$r) = objectify(2,@_); + } + + return $x if $x->modify('bmod'); + + # handle NaN, inf, -inf + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + my ($d,$re) = $self->SUPER::_div_inf($x,$y); + $x->{sign} = $re->{sign}; + $x->{_e} = $re->{_e}; + $x->{_m} = $re->{_m}; + return $x->round($a,$p,$r,$y); + } + if ($y->is_zero()) + { + return $x->bnan() if $x->is_zero(); + return $x; + } + + return $x->bzero() if $x->is_zero() + || ($x->is_int() && + # check that $y == +1 or $y == -1: + ($MBI->_is_zero($y->{_e}) && $MBI->_is_one($y->{_m}))); + + my $cmp = $x->bacmp($y); # equal or $x < $y? + return $x->bzero($a,$p) if $cmp == 0; # $x == $y => result 0 + + # only $y of the operands negative? + my $neg = 0; $neg = 1 if $x->{sign} ne $y->{sign}; + + $x->{sign} = $y->{sign}; # calc sign first + return $x->round($a,$p,$r) if $cmp < 0 && $neg == 0; # $x < $y => result $x + + my $ym = $MBI->_copy($y->{_m}); + + # 2e1 => 20 + $MBI->_lsft( $ym, $y->{_e}, 10) + if $y->{_es} eq '+' && !$MBI->_is_zero($y->{_e}); + + # if $y has digits after dot + my $shifty = 0; # correct _e of $x by this + if ($y->{_es} eq '-') # has digits after dot + { + # 123 % 2.5 => 1230 % 25 => 5 => 0.5 + $shifty = $MBI->_num($y->{_e}); # no more digits after dot + $MBI->_lsft($x->{_m}, $y->{_e}, 10);# 123 => 1230, $y->{_m} is already 25 + } + # $ym is now mantissa of $y based on exponent 0 + + my $shiftx = 0; # correct _e of $x by this + if ($x->{_es} eq '-') # has digits after dot + { + # 123.4 % 20 => 1234 % 200 + $shiftx = $MBI->_num($x->{_e}); # no more digits after dot + $MBI->_lsft($ym, $x->{_e}, 10); # 123 => 1230 + } + # 123e1 % 20 => 1230 % 20 + if ($x->{_es} eq '+' && !$MBI->_is_zero($x->{_e})) + { + $MBI->_lsft( $x->{_m}, $x->{_e},10); # es => '+' here + } + + $x->{_e} = $MBI->_new($shiftx); + $x->{_es} = '+'; + $x->{_es} = '-' if $shiftx != 0 || $shifty != 0; + $MBI->_add( $x->{_e}, $MBI->_new($shifty)) if $shifty != 0; + + # now mantissas are equalized, exponent of $x is adjusted, so calc result + + $x->{_m} = $MBI->_mod( $x->{_m}, $ym); + + $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # fix sign for -0 + $x->bnorm(); + + if ($neg != 0) # one of them negative => correct in place + { + my $r = $y - $x; + $x->{_m} = $r->{_m}; + $x->{_e} = $r->{_e}; + $x->{_es} = $r->{_es}; + $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # fix sign for -0 + $x->bnorm(); + } + + $x->round($a,$p,$r,$y); # round and return + } + +sub broot + { + # calculate $y'th root of $x + + # set up parameters + my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$a,$p,$r) = objectify(2,@_); + } + + return $x if $x->modify('broot'); + + # NaN handling: $x ** 1/0, x or y NaN, or y inf/-inf or y == 0 + return $x->bnan() if $x->{sign} !~ /^\+/ || $y->is_zero() || + $y->{sign} !~ /^\+$/; + + return $x if $x->is_zero() || $x->is_one() || $x->is_inf() || $y->is_one(); + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my (@params,$scale); + ($x,@params) = $x->_find_round_parameters($a,$p,$r); + + return $x if $x->is_nan(); # error in _find_round_parameters? + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r; # iound mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; # should be really parent class vs MBI + + # remember sign and make $x positive, since -4 ** (1/2) => -2 + my $sign = 0; $sign = 1 if $x->{sign} eq '-'; $x->{sign} = '+'; + + my $is_two = 0; + if ($y->isa('Math::BigFloat')) + { + $is_two = ($y->{sign} eq '+' && $MBI->_is_two($y->{_m}) && $MBI->_is_zero($y->{_e})); + } + else + { + $is_two = ($y == 2); + } + + # normal square root if $y == 2: + if ($is_two) + { + $x->bsqrt($scale+4); + } + elsif ($y->is_one('-')) + { + # $x ** -1 => 1/$x + my $u = $self->bone()->bdiv($x,$scale); + # copy private parts over + $x->{_m} = $u->{_m}; + $x->{_e} = $u->{_e}; + $x->{_es} = $u->{_es}; + } + else + { + # calculate the broot() as integer result first, and if it fits, return + # it rightaway (but only if $x and $y are integer): + + my $done = 0; # not yet + if ($y->is_int() && $x->is_int()) + { + my $i = $MBI->_copy( $x->{_m} ); + $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e}); + my $int = Math::BigInt->bzero(); + $int->{value} = $i; + $int->broot($y->as_number()); + # if ($exact) + if ($int->copy()->bpow($y) == $x) + { + # found result, return it + $x->{_m} = $int->{value}; + $x->{_e} = $MBI->_zero(); + $x->{_es} = '+'; + $x->bnorm(); + $done = 1; + } + } + if ($done == 0) + { + my $u = $self->bone()->bdiv($y,$scale+4); + delete $u->{_a}; delete $u->{_p}; # otherwise it conflicts + $x->bpow($u,$scale+4); # el cheapo + } + } + $x->bneg() if $sign == 1; + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + $x; + } + +sub bsqrt + { + # calculate square root + my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('bsqrt'); + + return $x->bnan() if $x->{sign} !~ /^[+]/; # NaN, -inf or < 0 + return $x if $x->{sign} eq '+inf'; # sqrt(inf) == inf + return $x->round($a,$p,$r) if $x->is_zero() || $x->is_one(); + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my (@params,$scale); + ($x,@params) = $x->_find_round_parameters($a,$p,$r); + + return $x if $x->is_nan(); # error in _find_round_parameters? + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; # should be really parent class vs MBI + + my $i = $MBI->_copy( $x->{_m} ); + $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e}); + my $xas = Math::BigInt->bzero(); + $xas->{value} = $i; + + my $gs = $xas->copy()->bsqrt(); # some guess + + if (($x->{_es} ne '-') # guess can't be accurate if there are + # digits after the dot + && ($xas->bacmp($gs * $gs) == 0)) # guess hit the nail on the head? + { + # exact result, copy result over to keep $x + $x->{_m} = $gs->{value}; $x->{_e} = $MBI->_zero(); $x->{_es} = '+'; + $x->bnorm(); + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # re-enable A and P, upgrade is taken care of by "local" + ${"$self\::accuracy"} = $ab; ${"$self\::precision"} = $pb; + return $x; + } + + # sqrt(2) = 1.4 because sqrt(2*100) = 1.4*10; so we can increase the accuracy + # of the result by multipyling the input by 100 and then divide the integer + # result of sqrt(input) by 10. Rounding afterwards returns the real result. + + # The following steps will transform 123.456 (in $x) into 123456 (in $y1) + my $y1 = $MBI->_copy($x->{_m}); + + my $length = $MBI->_len($y1); + + # Now calculate how many digits the result of sqrt(y1) would have + my $digits = int($length / 2); + + # But we need at least $scale digits, so calculate how many are missing + my $shift = $scale - $digits; + + # This happens if the input had enough digits + # (we take care of integer guesses above) + $shift = 0 if $shift < 0; + + # Multiply in steps of 100, by shifting left two times the "missing" digits + my $s2 = $shift * 2; + + # We now make sure that $y1 has the same odd or even number of digits than + # $x had. So when _e of $x is odd, we must shift $y1 by one digit left, + # because we always must multiply by steps of 100 (sqrt(100) is 10) and not + # steps of 10. The length of $x does not count, since an even or odd number + # of digits before the dot is not changed by adding an even number of digits + # after the dot (the result is still odd or even digits long). + $s2++ if $MBI->_is_odd($x->{_e}); + + $MBI->_lsft( $y1, $MBI->_new($s2), 10); + + # now take the square root and truncate to integer + $y1 = $MBI->_sqrt($y1); + + # By "shifting" $y1 right (by creating a negative _e) we calculate the final + # result, which is than later rounded to the desired scale. + + # calculate how many zeros $x had after the '.' (or before it, depending + # on sign of $dat, the result should have half as many: + my $dat = $MBI->_num($x->{_e}); + $dat = -$dat if $x->{_es} eq '-'; + $dat += $length; + + if ($dat > 0) + { + # no zeros after the dot (e.g. 1.23, 0.49 etc) + # preserve half as many digits before the dot than the input had + # (but round this "up") + $dat = int(($dat+1)/2); + } + else + { + $dat = int(($dat)/2); + } + $dat -= $MBI->_len($y1); + if ($dat < 0) + { + $dat = abs($dat); + $x->{_e} = $MBI->_new( $dat ); + $x->{_es} = '-'; + } + else + { + $x->{_e} = $MBI->_new( $dat ); + $x->{_es} = '+'; + } + $x->{_m} = $y1; + $x->bnorm(); + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + $x; + } + +sub bfac + { + # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT + # compute factorial number, modifies first argument + + # set up parameters + my ($self,$x,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + ($self,$x,@r) = objectify(1,@_) if !ref($x); + + # inf => inf + return $x if $x->modify('bfac') || $x->{sign} eq '+inf'; + + return $x->bnan() + if (($x->{sign} ne '+') || # inf, NaN, <0 etc => NaN + ($x->{_es} ne '+')); # digits after dot? + + # use BigInt's bfac() for faster calc + if (! $MBI->_is_zero($x->{_e})) + { + $MBI->_lsft($x->{_m}, $x->{_e},10); # change 12e1 to 120e0 + $x->{_e} = $MBI->_zero(); # normalize + $x->{_es} = '+'; + } + $MBI->_fac($x->{_m}); # calculate factorial + $x->bnorm()->round(@r); # norm again and round result + } + +sub _pow + { + # Calculate a power where $y is a non-integer, like 2 ** 0.3 + my ($x,$y,@r) = @_; + my $self = ref($x); + + # if $y == 0.5, it is sqrt($x) + $HALF = $self->new($HALF) unless ref($HALF); + return $x->bsqrt(@r,$y) if $y->bcmp($HALF) == 0; + + # Using: + # a ** x == e ** (x * ln a) + + # u = y * ln x + # _ _ + # Taylor: | u u^2 u^3 | + # x ** y = 1 + | --- + --- + ----- + ... | + # |_ 1 1*2 1*2*3 _| + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters(@r); + + return $x if $x->is_nan(); # error in _find_round_parameters? + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # disable P + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r[2]; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; + + my ($limit,$v,$u,$below,$factor,$next,$over); + + $u = $x->copy()->blog(undef,$scale)->bmul($y); + $v = $self->bone(); # 1 + $factor = $self->new(2); # 2 + $x->bone(); # first term: 1 + + $below = $v->copy(); + $over = $u->copy(); + + $limit = $self->new("1E-". ($scale-1)); + #my $steps = 0; + while (3 < 5) + { + # we calculate the next term, and add it to the last + # when the next term is below our limit, it won't affect the outcome + # anymore, so we stop: + $next = $over->copy()->bdiv($below,$scale); + last if $next->bacmp($limit) <= 0; + $x->badd($next); + # calculate things for the next term + $over *= $u; $below *= $factor; $factor->binc(); + + last if $x->{sign} !~ /^[-+]$/; + + #$steps++; + } + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + $x; + } + +sub bpow + { + # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT + # compute power of two numbers, second arg is used as integer + # modifies first argument + + # set up parameters + my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$a,$p,$r) = objectify(2,@_); + } + + return $x if $x->modify('bpow'); + + return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan; + return $x if $x->{sign} =~ /^[+-]inf$/; + + # cache the result of is_zero + my $y_is_zero = $y->is_zero(); + return $x->bone() if $y_is_zero; + return $x if $x->is_one() || $y->is_one(); + + my $x_is_zero = $x->is_zero(); + return $x->_pow($y,$a,$p,$r) if !$x_is_zero && !$y->is_int(); # non-integer power + + my $y1 = $y->as_number()->{value}; # make MBI part + + # if ($x == -1) + if ($x->{sign} eq '-' && $MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e})) + { + # if $x == -1 and odd/even y => +1/-1 because +-1 ^ (+-1) => +-1 + return $MBI->_is_odd($y1) ? $x : $x->babs(1); + } + if ($x_is_zero) + { + return $x if $y->{sign} eq '+'; # 0**y => 0 (if not y <= 0) + # 0 ** -y => 1 / (0 ** y) => 1 / 0! (1 / 0 => +inf) + return $x->binf(); + } + + my $new_sign = '+'; + $new_sign = $MBI->_is_odd($y1) ? '-' : '+' if $x->{sign} ne '+'; + + # calculate $x->{_m} ** $y and $x->{_e} * $y separately (faster) + $x->{_m} = $MBI->_pow( $x->{_m}, $y1); + $x->{_e} = $MBI->_mul ($x->{_e}, $y1); + + $x->{sign} = $new_sign; + $x->bnorm(); + if ($y->{sign} eq '-') + { + # modify $x in place! + my $z = $x->copy(); $x->bone(); + return scalar $x->bdiv($z,$a,$p,$r); # round in one go (might ignore y's A!) + } + $x->round($a,$p,$r,$y); + } + +sub bmodpow + { + # takes a very large number to a very large exponent in a given very + # large modulus, quickly, thanks to binary exponentation. Supports + # negative exponents. + my ($self,$num,$exp,$mod,@r) = objectify(3,@_); + + return $num if $num->modify('bmodpow'); + + # check modulus for valid values + return $num->bnan() if ($mod->{sign} ne '+' # NaN, - , -inf, +inf + || $mod->is_zero()); + + # check exponent for valid values + if ($exp->{sign} =~ /\w/) + { + # i.e., if it's NaN, +inf, or -inf... + return $num->bnan(); + } + + $num->bmodinv ($mod) if ($exp->{sign} eq '-'); + + # check num for valid values (also NaN if there was no inverse but $exp < 0) + return $num->bnan() if $num->{sign} !~ /^[+-]$/; + + # $mod is positive, sign on $exp is ignored, result also positive + + # XXX TODO: speed it up when all three numbers are integers + $num->bpow($exp)->bmod($mod); + } + +############################################################################### +# trigonometric functions + +# helper function for bpi() and batan2(), calculates arcus tanges (1/x) + +sub _atan_inv + { + # return a/b so that a/b approximates atan(1/x) to at least limit digits + my ($self, $x, $limit) = @_; + + # Taylor: x^3 x^5 x^7 x^9 + # atan = x - --- + --- - --- + --- - ... + # 3 5 7 9 + + # 1 1 1 1 + # atan 1/x = - - ------- + ------- - ------- + ... + # x x^3 * 3 x^5 * 5 x^7 * 7 + + # 1 1 1 1 + # atan 1/x = - - --------- + ---------- - ----------- + ... + # 5 3 * 125 5 * 3125 7 * 78125 + + # Subtraction/addition of a rational: + + # 5 7 5*3 +- 7*4 + # - +- - = ---------- + # 4 3 4*3 + + # Term: N N+1 + # + # a 1 a * d * c +- b + # ----- +- ------------------ = ---------------- + # b d * c b * d * c + + # since b1 = b0 * (d-2) * c + + # a 1 a * d +- b / c + # ----- +- ------------------ = ---------------- + # b d * c b * d + + # and d = d + 2 + # and c = c * x * x + + # u = d * c + # stop if length($u) > limit + # a = a * u +- b + # b = b * u + # d = d + 2 + # c = c * x * x + # sign = 1 - sign + + my $a = $MBI->_one(); + my $b = $MBI->_copy($x); + + my $x2 = $MBI->_mul( $MBI->_copy($x), $b); # x2 = x * x + my $d = $MBI->_new( 3 ); # d = 3 + my $c = $MBI->_mul( $MBI->_copy($x), $x2); # c = x ^ 3 + my $two = $MBI->_new( 2 ); + + # run the first step unconditionally + my $u = $MBI->_mul( $MBI->_copy($d), $c); + $a = $MBI->_mul($a, $u); + $a = $MBI->_sub($a, $b); + $b = $MBI->_mul($b, $u); + $d = $MBI->_add($d, $two); + $c = $MBI->_mul($c, $x2); + + # a is now a * (d-3) * c + # b is now b * (d-2) * c + + # run the second step unconditionally + $u = $MBI->_mul( $MBI->_copy($d), $c); + $a = $MBI->_mul($a, $u); + $a = $MBI->_add($a, $b); + $b = $MBI->_mul($b, $u); + $d = $MBI->_add($d, $two); + $c = $MBI->_mul($c, $x2); + + # a is now a * (d-3) * (d-5) * c * c + # b is now b * (d-2) * (d-4) * c * c + + # so we can remove c * c from both a and b to shorten the numbers involved: + $a = $MBI->_div($a, $x2); + $b = $MBI->_div($b, $x2); + $a = $MBI->_div($a, $x2); + $b = $MBI->_div($b, $x2); + +# my $step = 0; + my $sign = 0; # 0 => -, 1 => + + while (3 < 5) + { +# $step++; +# if (($i++ % 100) == 0) +# { +# print "a=",$MBI->_str($a),"\n"; +# print "b=",$MBI->_str($b),"\n"; +# } +# print "d=",$MBI->_str($d),"\n"; +# print "x2=",$MBI->_str($x2),"\n"; +# print "c=",$MBI->_str($c),"\n"; + + my $u = $MBI->_mul( $MBI->_copy($d), $c); + # use _alen() for libs like GMP where _len() would be O(N^2) + last if $MBI->_alen($u) > $limit; + my ($bc,$r) = $MBI->_div( $MBI->_copy($b), $c); + if ($MBI->_is_zero($r)) + { + # b / c is an integer, so we can remove c from all terms + # this happens almost every time: + $a = $MBI->_mul($a, $d); + $a = $MBI->_sub($a, $bc) if $sign == 0; + $a = $MBI->_add($a, $bc) if $sign == 1; + $b = $MBI->_mul($b, $d); + } + else + { + # b / c is not an integer, so we keep c in the terms + # this happens very rarely, for instance for x = 5, this happens only + # at the following steps: + # 1, 5, 14, 32, 72, 157, 340, ... + $a = $MBI->_mul($a, $u); + $a = $MBI->_sub($a, $b) if $sign == 0; + $a = $MBI->_add($a, $b) if $sign == 1; + $b = $MBI->_mul($b, $u); + } + $d = $MBI->_add($d, $two); + $c = $MBI->_mul($c, $x2); + $sign = 1 - $sign; + + } + +# print "Took $step steps for ", $MBI->_str($x),"\n"; +# print "a=",$MBI->_str($a),"\n"; print "b=",$MBI->_str($b),"\n"; + # return a/b so that a/b approximates atan(1/x) + ($a,$b); + } + +sub bpi + { + my ($self,$n) = @_; + if (@_ == 0) + { + $self = $class; + } + if (@_ == 1) + { + # called like Math::BigFloat::bpi(10); + $n = $self; $self = $class; + # called like Math::BigFloat->bpi(); + $n = undef if $n eq 'Math::BigFloat'; + } + $self = ref($self) if ref($self); + my $fallback = defined $n ? 0 : 1; + $n = 40 if !defined $n || $n < 1; + + # after 黃見利 (Hwang Chien-Lih) (1997) + # pi/4 = 183 * atan(1/239) + 32 * atan(1/1023) – 68 * atan(1/5832) + # + 12 * atan(1/110443) - 12 * atan(1/4841182) - 100 * atan(1/6826318) + + # a few more to prevent rounding errors + $n += 4; + + my ($a,$b) = $self->_atan_inv( $MBI->_new(239),$n); + my ($c,$d) = $self->_atan_inv( $MBI->_new(1023),$n); + my ($e,$f) = $self->_atan_inv( $MBI->_new(5832),$n); + my ($g,$h) = $self->_atan_inv( $MBI->_new(110443),$n); + my ($i,$j) = $self->_atan_inv( $MBI->_new(4841182),$n); + my ($k,$l) = $self->_atan_inv( $MBI->_new(6826318),$n); + + $MBI->_mul($a, $MBI->_new(732)); + $MBI->_mul($c, $MBI->_new(128)); + $MBI->_mul($e, $MBI->_new(272)); + $MBI->_mul($g, $MBI->_new(48)); + $MBI->_mul($i, $MBI->_new(48)); + $MBI->_mul($k, $MBI->_new(400)); + + my $x = $self->bone(); $x->{_m} = $a; my $x_d = $self->bone(); $x_d->{_m} = $b; + my $y = $self->bone(); $y->{_m} = $c; my $y_d = $self->bone(); $y_d->{_m} = $d; + my $z = $self->bone(); $z->{_m} = $e; my $z_d = $self->bone(); $z_d->{_m} = $f; + my $u = $self->bone(); $u->{_m} = $g; my $u_d = $self->bone(); $u_d->{_m} = $h; + my $v = $self->bone(); $v->{_m} = $i; my $v_d = $self->bone(); $v_d->{_m} = $j; + my $w = $self->bone(); $w->{_m} = $k; my $w_d = $self->bone(); $w_d->{_m} = $l; + $x->bdiv($x_d, $n); + $y->bdiv($y_d, $n); + $z->bdiv($z_d, $n); + $u->bdiv($u_d, $n); + $v->bdiv($v_d, $n); + $w->bdiv($w_d, $n); + + delete $x->{_a}; delete $y->{_a}; delete $z->{_a}; + delete $u->{_a}; delete $v->{_a}; delete $w->{_a}; + $x->badd($y)->bsub($z)->badd($u)->bsub($v)->bsub($w); + + $x->bround($n-4); + delete $x->{_a} if $fallback == 1; + $x; + } + +sub bcos + { + # Calculate a cosinus of x. + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + # Taylor: x^2 x^4 x^6 x^8 + # cos = 1 - --- + --- - --- + --- ... + # 2! 4! 6! 8! + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters(@r); + + # constant object or error in _find_round_parameters? + return $x if $x->modify('bcos') || $x->is_nan(); + + return $x->bone(@r) if $x->is_zero(); + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # disable P + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r[2]; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; + + my $last = 0; + my $over = $x * $x; # X ^ 2 + my $x2 = $over->copy(); # X ^ 2; difference between terms + my $sign = 1; # start with -= + my $below = $self->new(2); my $factorial = $self->new(3); + $x->bone(); delete $x->{_a}; delete $x->{_p}; + + my $limit = $self->new("1E-". ($scale-1)); + #my $steps = 0; + while (3 < 5) + { + # we calculate the next term, and add it to the last + # when the next term is below our limit, it won't affect the outcome + # anymore, so we stop: + my $next = $over->copy()->bdiv($below,$scale); + last if $next->bacmp($limit) <= 0; + + if ($sign == 0) + { + $x->badd($next); + } + else + { + $x->bsub($next); + } + $sign = 1-$sign; # alternate + # calculate things for the next term + $over->bmul($x2); # $x*$x + $below->bmul($factorial); $factorial->binc(); # n*(n+1) + $below->bmul($factorial); $factorial->binc(); # n*(n+1) + } + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + $x; + } + +sub bsin + { + # Calculate a sinus of x. + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + # taylor: x^3 x^5 x^7 x^9 + # sin = x - --- + --- - --- + --- ... + # 3! 5! 7! 9! + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters(@r); + + # constant object or error in _find_round_parameters? + return $x if $x->modify('bsin') || $x->is_nan(); + + return $x->bzero(@r) if $x->is_zero(); + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # disable P + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r[2]; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; + + my $last = 0; + my $over = $x * $x; # X ^ 2 + my $x2 = $over->copy(); # X ^ 2; difference between terms + $over->bmul($x); # X ^ 3 as starting value + my $sign = 1; # start with -= + my $below = $self->new(6); my $factorial = $self->new(4); + delete $x->{_a}; delete $x->{_p}; + + my $limit = $self->new("1E-". ($scale-1)); + #my $steps = 0; + while (3 < 5) + { + # we calculate the next term, and add it to the last + # when the next term is below our limit, it won't affect the outcome + # anymore, so we stop: + my $next = $over->copy()->bdiv($below,$scale); + last if $next->bacmp($limit) <= 0; + + if ($sign == 0) + { + $x->badd($next); + } + else + { + $x->bsub($next); + } + $sign = 1-$sign; # alternate + # calculate things for the next term + $over->bmul($x2); # $x*$x + $below->bmul($factorial); $factorial->binc(); # n*(n+1) + $below->bmul($factorial); $factorial->binc(); # n*(n+1) + } + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + $x; + } + +sub batan2 + { + # calculate arcus tangens of ($y/$x) + + # set up parameters + my ($self,$y,$x,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$y,$x,@r) = objectify(2,@_); + } + + return $y if $y->modify('batan2'); + + return $y->bnan() if ($y->{sign} eq $nan) || ($x->{sign} eq $nan); + + # Y X + # 0 0 result is 0 + # 0 +x result is 0 + # ? inf result is 0 + return $y->bzero(@r) if ($x->is_inf('+') && !$y->is_inf()) || ($y->is_zero() && $x->{sign} eq '+'); + + # Y X + # != 0 -inf result is +- pi + if ($x->is_inf() || $y->is_inf()) + { + # calculate PI + my $pi = $self->bpi(@r); + if ($y->is_inf()) + { + # upgrade to BigRat etc. + return $upgrade->new($y)->batan2($upgrade->new($x),@r) if defined $upgrade; + if ($x->{sign} eq '-inf') + { + # calculate 3 pi/4 + $MBI->_mul($pi->{_m}, $MBI->_new(3)); + $MBI->_div($pi->{_m}, $MBI->_new(4)); + } + elsif ($x->{sign} eq '+inf') + { + # calculate pi/4 + $MBI->_div($pi->{_m}, $MBI->_new(4)); + } + else + { + # calculate pi/2 + $MBI->_div($pi->{_m}, $MBI->_new(2)); + } + $y->{sign} = substr($y->{sign},0,1); # keep +/- + } + # modify $y in place + $y->{_m} = $pi->{_m}; + $y->{_e} = $pi->{_e}; + $y->{_es} = $pi->{_es}; + # keep the sign of $y + return $y; + } + + return $upgrade->new($y)->batan2($upgrade->new($x),@r) if defined $upgrade; + + # Y X + # 0 -x result is PI + if ($y->is_zero()) + { + # calculate PI + my $pi = $self->bpi(@r); + # modify $y in place + $y->{_m} = $pi->{_m}; + $y->{_e} = $pi->{_e}; + $y->{_es} = $pi->{_es}; + $y->{sign} = '+'; + return $y; + } + + # Y X + # +y 0 result is PI/2 + # -y 0 result is -PI/2 + if ($x->is_zero()) + { + # calculate PI/2 + my $pi = $self->bpi(@r); + # modify $y in place + $y->{_m} = $pi->{_m}; + $y->{_e} = $pi->{_e}; + $y->{_es} = $pi->{_es}; + # -y => -PI/2, +y => PI/2 + $MBI->_div($y->{_m}, $MBI->_new(2)); + return $y; + } + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($y,@params) = $y->_find_round_parameters(@r); + + # error in _find_round_parameters? + return $y if $y->is_nan(); + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # disable P + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r[2]; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # inlined is_one() && is_one('-') + if ($MBI->_is_one($y->{_m}) && $MBI->_is_zero($y->{_e})) + { + # shortcut: 1 1 result is PI/4 + # inlined is_one() && is_one('-') + if ($MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e})) + { + # 1,1 => PI/4 + my $pi_4 = $self->bpi( $scale - 3); + # modify $y in place + $y->{_m} = $pi_4->{_m}; + $y->{_e} = $pi_4->{_e}; + $y->{_es} = $pi_4->{_es}; + # 1 1 => + + # -1 1 => - + # 1 -1 => - + # -1 -1 => + + $y->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; + $MBI->_div($y->{_m}, $MBI->_new(4)); + return $y; + } + # shortcut: 1 int(X) result is _atan_inv(X) + + # is integer + if ($x->{_es} eq '+') + { + my $x1 = $MBI->_copy($x->{_m}); + $MBI->_lsft($x1, $x->{_e},10) unless $MBI->_is_zero($x->{_e}); + + my ($a,$b) = $self->_atan_inv($x1, $scale); + my $y_sign = $y->{sign}; + # calculate A/B + $y->bone(); $y->{_m} = $a; my $y_d = $self->bone(); $y_d->{_m} = $b; + $y->bdiv($y_d, @r); + $y->{sign} = $y_sign; + return $y; + } + } + + # handle all other cases + # X Y + # +x +y 0 to PI/2 + # -x +y PI/2 to PI + # +x -y 0 to -PI/2 + # -x -y -PI/2 to -PI + + my $y_sign = $y->{sign}; + + # divide $x by $y + $y->bdiv($x, $scale) unless $x->is_one(); + $y->batan(@r); + + # restore sign + $y->{sign} = $y_sign; + + $y; + } + +sub batan + { + # Calculate a arcus tangens of x. + my ($x,@r) = @_; + my $self = ref($x); + + # taylor: x^3 x^5 x^7 x^9 + # atan = x - --- + --- - --- + --- ... + # 3 5 7 9 + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters(@r); + + # constant object or error in _find_round_parameters? + return $x if $x->modify('batan') || $x->is_nan(); + + if ($x->{sign} =~ /^[+-]inf\z/) + { + # +inf result is PI/2 + # -inf result is -PI/2 + # calculate PI/2 + my $pi = $self->bpi(@r); + # modify $x in place + $x->{_m} = $pi->{_m}; + $x->{_e} = $pi->{_e}; + $x->{_es} = $pi->{_es}; + # -y => -PI/2, +y => PI/2 + $x->{sign} = substr($x->{sign},0,1); # +inf => + + $MBI->_div($x->{_m}, $MBI->_new(2)); + return $x; + } + + return $x->bzero(@r) if $x->is_zero(); + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # disable P + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r[2]; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # 1 or -1 => PI/4 + # inlined is_one() && is_one('-') + if ($MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e})) + { + my $pi = $self->bpi($scale - 3); + # modify $x in place + $x->{_m} = $pi->{_m}; + $x->{_e} = $pi->{_e}; + $x->{_es} = $pi->{_es}; + # leave the sign of $x alone (+1 => +PI/4, -1 => -PI/4) + $MBI->_div($x->{_m}, $MBI->_new(4)); + return $x; + } + + # This series is only valid if -1 < x < 1, so for other x we need to + # to calculate PI/2 - atan(1/x): + my $one = $MBI->_new(1); + my $pi = undef; + if ($x->{_es} eq '+' && ($MBI->_acmp($x->{_m},$one) >= 0)) + { + # calculate PI/2 + $pi = $self->bpi($scale - 3); + $MBI->_div($pi->{_m}, $MBI->_new(2)); + # calculate 1/$x: + my $x_copy = $x->copy(); + # modify $x in place + $x->bone(); $x->bdiv($x_copy,$scale); + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; + + my $last = 0; + my $over = $x * $x; # X ^ 2 + my $x2 = $over->copy(); # X ^ 2; difference between terms + $over->bmul($x); # X ^ 3 as starting value + my $sign = 1; # start with -= + my $below = $self->new(3); + my $two = $self->new(2); + delete $x->{_a}; delete $x->{_p}; + + my $limit = $self->new("1E-". ($scale-1)); + #my $steps = 0; + while (3 < 5) + { + # we calculate the next term, and add it to the last + # when the next term is below our limit, it won't affect the outcome + # anymore, so we stop: + my $next = $over->copy()->bdiv($below,$scale); + last if $next->bacmp($limit) <= 0; + + if ($sign == 0) + { + $x->badd($next); + } + else + { + $x->bsub($next); + } + $sign = 1-$sign; # alternate + # calculate things for the next term + $over->bmul($x2); # $x*$x + $below->badd($two); # n += 2 + } + + if (defined $pi) + { + my $x_copy = $x->copy(); + # modify $x in place + $x->{_m} = $pi->{_m}; + $x->{_e} = $pi->{_e}; + $x->{_es} = $pi->{_es}; + # PI/2 - $x + $x->bsub($x_copy); + } + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + $x; + } + +############################################################################### +# rounding functions + +sub bfround + { + # precision: round to the $Nth digit left (+$n) or right (-$n) from the '.' + # $n == 0 means round to integer + # expects and returns normalized numbers! + my $x = shift; my $self = ref($x) || $x; $x = $self->new(shift) if !ref($x); + + my ($scale,$mode) = $x->_scale_p(@_); + return $x if !defined $scale || $x->modify('bfround'); # no-op + + # never round a 0, +-inf, NaN + if ($x->is_zero()) + { + $x->{_p} = $scale if !defined $x->{_p} || $x->{_p} < $scale; # -3 < -2 + return $x; + } + return $x if $x->{sign} !~ /^[+-]$/; + + # don't round if x already has lower precision + return $x if (defined $x->{_p} && $x->{_p} < 0 && $scale < $x->{_p}); + + $x->{_p} = $scale; # remember round in any case + delete $x->{_a}; # and clear A + if ($scale < 0) + { + # round right from the '.' + + return $x if $x->{_es} eq '+'; # e >= 0 => nothing to round + + $scale = -$scale; # positive for simplicity + my $len = $MBI->_len($x->{_m}); # length of mantissa + + # the following poses a restriction on _e, but if _e is bigger than a + # scalar, you got other problems (memory etc) anyway + my $dad = -(0+ ($x->{_es}.$MBI->_num($x->{_e}))); # digits after dot + my $zad = 0; # zeros after dot + $zad = $dad - $len if (-$dad < -$len); # for 0.00..00xxx style + + # p rint "scale $scale dad $dad zad $zad len $len\n"; + # number bsstr len zad dad + # 0.123 123e-3 3 0 3 + # 0.0123 123e-4 3 1 4 + # 0.001 1e-3 1 2 3 + # 1.23 123e-2 3 0 2 + # 1.2345 12345e-4 5 0 4 + + # do not round after/right of the $dad + return $x if $scale > $dad; # 0.123, scale >= 3 => exit + + # round to zero if rounding inside the $zad, but not for last zero like: + # 0.0065, scale -2, round last '0' with following '65' (scale == zad case) + return $x->bzero() if $scale < $zad; + if ($scale == $zad) # for 0.006, scale -3 and trunc + { + $scale = -$len; + } + else + { + # adjust round-point to be inside mantissa + if ($zad != 0) + { + $scale = $scale-$zad; + } + else + { + my $dbd = $len - $dad; $dbd = 0 if $dbd < 0; # digits before dot + $scale = $dbd+$scale; + } + } + } + else + { + # round left from the '.' + + # 123 => 100 means length(123) = 3 - $scale (2) => 1 + + my $dbt = $MBI->_len($x->{_m}); + # digits before dot + my $dbd = $dbt + ($x->{_es} . $MBI->_num($x->{_e})); + # should be the same, so treat it as this + $scale = 1 if $scale == 0; + # shortcut if already integer + return $x if $scale == 1 && $dbt <= $dbd; + # maximum digits before dot + ++$dbd; + + if ($scale > $dbd) + { + # not enough digits before dot, so round to zero + return $x->bzero; + } + elsif ( $scale == $dbd ) + { + # maximum + $scale = -$dbt; + } + else + { + $scale = $dbd - $scale; + } + } + # pass sign to bround for rounding modes '+inf' and '-inf' + my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt'; + $m->bround($scale,$mode); + $x->{_m} = $m->{value}; # get our mantissa back + $x->bnorm(); + } + +sub bround + { + # accuracy: preserve $N digits, and overwrite the rest with 0's + my $x = shift; my $self = ref($x) || $x; $x = $self->new(shift) if !ref($x); + + if (($_[0] || 0) < 0) + { + require Carp; Carp::croak ('bround() needs positive accuracy'); + } + + my ($scale,$mode) = $x->_scale_a(@_); + return $x if !defined $scale || $x->modify('bround'); # no-op + + # scale is now either $x->{_a}, $accuracy, or the user parameter + # test whether $x already has lower accuracy, do nothing in this case + # but do round if the accuracy is the same, since a math operation might + # want to round a number with A=5 to 5 digits afterwards again + return $x if defined $x->{_a} && $x->{_a} < $scale; + + # scale < 0 makes no sense + # scale == 0 => keep all digits + # never round a +-inf, NaN + return $x if ($scale <= 0) || $x->{sign} !~ /^[+-]$/; + + # 1: never round a 0 + # 2: if we should keep more digits than the mantissa has, do nothing + if ($x->is_zero() || $MBI->_len($x->{_m}) <= $scale) + { + $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale; + return $x; + } + + # pass sign to bround for '+inf' and '-inf' rounding modes + my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt'; + + $m->bround($scale,$mode); # round mantissa + $x->{_m} = $m->{value}; # get our mantissa back + $x->{_a} = $scale; # remember rounding + delete $x->{_p}; # and clear P + $x->bnorm(); # del trailing zeros gen. by bround() + } + +sub bfloor + { + # return integer less or equal then $x + my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('bfloor'); + + return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf + + # if $x has digits after dot + if ($x->{_es} eq '-') + { + $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot + $x->{_e} = $MBI->_zero(); # trunc/norm + $x->{_es} = '+'; # abs e + $MBI->_inc($x->{_m}) if $x->{sign} eq '-'; # increment if negative + } + $x->round($a,$p,$r); + } + +sub bceil + { + # return integer greater or equal then $x + my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('bceil'); + return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf + + # if $x has digits after dot + if ($x->{_es} eq '-') + { + $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot + $x->{_e} = $MBI->_zero(); # trunc/norm + $x->{_es} = '+'; # abs e + $MBI->_inc($x->{_m}) if $x->{sign} eq '+'; # increment if positive + } + $x->round($a,$p,$r); + } + +sub brsft + { + # shift right by $y (divide by power of $n) + + # set up parameters + my ($self,$x,$y,$n,$a,$p,$r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$n,$a,$p,$r) = objectify(2,@_); + } + + return $x if $x->modify('brsft'); + return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf + + $n = 2 if !defined $n; $n = $self->new($n); + + # negative amount? + return $x->blsft($y->copy()->babs(),$n) if $y->{sign} =~ /^-/; + + # the following call to bdiv() will return either quo or (quo,reminder): + $x->bdiv($n->bpow($y),$a,$p,$r,$y); + } + +sub blsft + { + # shift left by $y (multiply by power of $n) + + # set up parameters + my ($self,$x,$y,$n,$a,$p,$r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$n,$a,$p,$r) = objectify(2,@_); + } + + return $x if $x->modify('blsft'); + return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf + + $n = 2 if !defined $n; $n = $self->new($n); + + # negative amount? + return $x->brsft($y->copy()->babs(),$n) if $y->{sign} =~ /^-/; + + $x->bmul($n->bpow($y),$a,$p,$r,$y); + } + +############################################################################### + +sub DESTROY + { + # going through AUTOLOAD for every DESTROY is costly, avoid it by empty sub + } + +sub AUTOLOAD + { + # make fxxx and bxxx both work by selectively mapping fxxx() to MBF::bxxx() + # or falling back to MBI::bxxx() + my $name = $AUTOLOAD; + + $name =~ s/(.*):://; # split package + my $c = $1 || $class; + no strict 'refs'; + $c->import() if $IMPORT == 0; + if (!_method_alias($name)) + { + if (!defined $name) + { + # delayed load of Carp and avoid recursion + require Carp; + Carp::croak ("$c: Can't call a method without name"); + } + if (!_method_hand_up($name)) + { + # delayed load of Carp and avoid recursion + require Carp; + Carp::croak ("Can't call $c\-\>$name, not a valid method"); + } + # try one level up, but subst. bxxx() for fxxx() since MBI only got bxxx() + $name =~ s/^f/b/; + return &{"Math::BigInt"."::$name"}(@_); + } + my $bname = $name; $bname =~ s/^f/b/; + $c .= "::$name"; + *{$c} = \&{$bname}; + &{$c}; # uses @_ + } + +sub exponent + { + # return a copy of the exponent + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + my $s = $x->{sign}; $s =~ s/^[+-]//; + return Math::BigInt->new($s); # -inf, +inf => +inf + } + Math::BigInt->new( $x->{_es} . $MBI->_str($x->{_e})); + } + +sub mantissa + { + # return a copy of the mantissa + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + my $s = $x->{sign}; $s =~ s/^[+]//; + return Math::BigInt->new($s); # -inf, +inf => +inf + } + my $m = Math::BigInt->new( $MBI->_str($x->{_m})); + $m->bneg() if $x->{sign} eq '-'; + + $m; + } + +sub parts + { + # return a copy of both the exponent and the mantissa + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + my $s = $x->{sign}; $s =~ s/^[+]//; my $se = $s; $se =~ s/^[-]//; + return ($self->new($s),$self->new($se)); # +inf => inf and -inf,+inf => inf + } + my $m = Math::BigInt->bzero(); + $m->{value} = $MBI->_copy($x->{_m}); + $m->bneg() if $x->{sign} eq '-'; + ($m, Math::BigInt->new( $x->{_es} . $MBI->_num($x->{_e}) )); + } + +############################################################################## +# private stuff (internal use only) + +sub import + { + my $self = shift; + my $l = scalar @_; + my $lib = ''; my @a; + my $lib_kind = 'try'; + $IMPORT=1; + for ( my $i = 0; $i < $l ; $i++) + { + if ( $_[$i] eq ':constant' ) + { + # This causes overlord er load to step in. 'binary' and 'integer' + # are handled by BigInt. + overload::constant float => sub { $self->new(shift); }; + } + elsif ($_[$i] eq 'upgrade') + { + # this causes upgrading + $upgrade = $_[$i+1]; # or undef to disable + $i++; + } + elsif ($_[$i] eq 'downgrade') + { + # this causes downgrading + $downgrade = $_[$i+1]; # or undef to disable + $i++; + } + elsif ($_[$i] =~ /^(lib|try|only)\z/) + { + # alternative library + $lib = $_[$i+1] || ''; # default Calc + $lib_kind = $1; # lib, try or only + $i++; + } + elsif ($_[$i] eq 'with') + { + # alternative class for our private parts() + # XXX: no longer supported + # $MBI = $_[$i+1] || 'Math::BigInt'; + $i++; + } + else + { + push @a, $_[$i]; + } + } + + $lib =~ tr/a-zA-Z0-9,://cd; # restrict to sane characters + # let use Math::BigInt lib => 'GMP'; use Math::BigFloat; still work + my $mbilib = eval { Math::BigInt->config()->{lib} }; + if ((defined $mbilib) && ($MBI eq 'Math::BigInt::Calc')) + { + # MBI already loaded + Math::BigInt->import( $lib_kind, "$lib,$mbilib", 'objectify'); + } + else + { + # MBI not loaded, or with ne "Math::BigInt::Calc" + $lib .= ",$mbilib" if defined $mbilib; + $lib =~ s/^,//; # don't leave empty + + # replacement library can handle lib statement, but also could ignore it + + # Perl < 5.6.0 dies with "out of memory!" when eval() and ':constant' is + # used in the same script, or eval inside import(). So we require MBI: + require Math::BigInt; + Math::BigInt->import( $lib_kind => $lib, 'objectify' ); + } + if ($@) + { + require Carp; Carp::croak ("Couldn't load $lib: $! $@"); + } + # find out which one was actually loaded + $MBI = Math::BigInt->config()->{lib}; + + # register us with MBI to get notified of future lib changes + Math::BigInt::_register_callback( $self, sub { $MBI = $_[0]; } ); + + $self->export_to_level(1,$self,@a); # export wanted functions + } + +sub bnorm + { + # adjust m and e so that m is smallest possible + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x if $x->{sign} !~ /^[+-]$/; # inf, nan etc + + my $zeros = $MBI->_zeros($x->{_m}); # correct for trailing zeros + if ($zeros != 0) + { + my $z = $MBI->_new($zeros); + $x->{_m} = $MBI->_rsft ($x->{_m}, $z, 10); + if ($x->{_es} eq '-') + { + if ($MBI->_acmp($x->{_e},$z) >= 0) + { + $x->{_e} = $MBI->_sub ($x->{_e}, $z); + $x->{_es} = '+' if $MBI->_is_zero($x->{_e}); + } + else + { + $x->{_e} = $MBI->_sub ( $MBI->_copy($z), $x->{_e}); + $x->{_es} = '+'; + } + } + else + { + $x->{_e} = $MBI->_add ($x->{_e}, $z); + } + } + else + { + # $x can only be 0Ey if there are no trailing zeros ('0' has 0 trailing + # zeros). So, for something like 0Ey, set y to 1, and -0 => +0 + $x->{sign} = '+', $x->{_es} = '+', $x->{_e} = $MBI->_one() + if $MBI->_is_zero($x->{_m}); + } + + $x; # MBI bnorm is no-op, so dont call it + } + +############################################################################## + +sub as_hex + { + # return number as hexadecimal string (only for integers defined) + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc + return '0x0' if $x->is_zero(); + + return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!? + + my $z = $MBI->_copy($x->{_m}); + if (! $MBI->_is_zero($x->{_e})) # > 0 + { + $MBI->_lsft( $z, $x->{_e},10); + } + $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z)); + $z->as_hex(); + } + +sub as_bin + { + # return number as binary digit string (only for integers defined) + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc + return '0b0' if $x->is_zero(); + + return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!? + + my $z = $MBI->_copy($x->{_m}); + if (! $MBI->_is_zero($x->{_e})) # > 0 + { + $MBI->_lsft( $z, $x->{_e},10); + } + $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z)); + $z->as_bin(); + } + +sub as_oct + { + # return number as octal digit string (only for integers defined) + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc + return '0' if $x->is_zero(); + + return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!? + + my $z = $MBI->_copy($x->{_m}); + if (! $MBI->_is_zero($x->{_e})) # > 0 + { + $MBI->_lsft( $z, $x->{_e},10); + } + $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z)); + $z->as_oct(); + } + +sub as_number + { + # return copy as a bigint representation of this BigFloat number + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + return $x if $x->modify('as_number'); + + if (!$x->isa('Math::BigFloat')) + { + # if the object can as_number(), use it + return $x->as_number() if $x->can('as_number'); + # otherwise, get us a float and then a number + $x = $x->can('as_float') ? $x->as_float() : $self->new(0+"$x"); + } + + my $z = $MBI->_copy($x->{_m}); + if ($x->{_es} eq '-') # < 0 + { + $MBI->_rsft( $z, $x->{_e},10); + } + elsif (! $MBI->_is_zero($x->{_e})) # > 0 + { + $MBI->_lsft( $z, $x->{_e},10); + } + $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z)); + $z; + } + +sub length + { + my $x = shift; + my $class = ref($x) || $x; + $x = $class->new(shift) unless ref($x); + + return 1 if $MBI->_is_zero($x->{_m}); + + my $len = $MBI->_len($x->{_m}); + $len += $MBI->_num($x->{_e}) if $x->{_es} eq '+'; + if (wantarray()) + { + my $t = 0; + $t = $MBI->_num($x->{_e}) if $x->{_es} eq '-'; + return ($len, $t); + } + $len; + } + +1; +__END__ + +=head1 NAME + +Math::BigFloat - Arbitrary size floating point math package + +=head1 SYNOPSIS + + use Math::BigFloat; + + # Number creation + my $x = Math::BigFloat->new($str); # defaults to 0 + my $y = $x->copy(); # make a true copy + my $nan = Math::BigFloat->bnan(); # create a NotANumber + my $zero = Math::BigFloat->bzero(); # create a +0 + my $inf = Math::BigFloat->binf(); # create a +inf + my $inf = Math::BigFloat->binf('-'); # create a -inf + my $one = Math::BigFloat->bone(); # create a +1 + my $mone = Math::BigFloat->bone('-'); # create a -1 + + my $pi = Math::BigFloat->bpi(100); # PI to 100 digits + + # the following examples compute their result to 100 digits accuracy: + my $cos = Math::BigFloat->new(1)->bcos(100); # cosinus(1) + my $sin = Math::BigFloat->new(1)->bsin(100); # sinus(1) + my $atan = Math::BigFloat->new(1)->batan(100); # arcus tangens(1) + + my $atan2 = Math::BigFloat->new( 1 )->batan2( 1 ,100); # batan(1) + my $atan2 = Math::BigFloat->new( 1 )->batan2( 8 ,100); # batan(1/8) + my $atan2 = Math::BigFloat->new( -2 )->batan2( 1 ,100); # batan(-2) + + # Testing + $x->is_zero(); # true if arg is +0 + $x->is_nan(); # true if arg is NaN + $x->is_one(); # true if arg is +1 + $x->is_one('-'); # true if arg is -1 + $x->is_odd(); # true if odd, false for even + $x->is_even(); # true if even, false for odd + $x->is_pos(); # true if >= 0 + $x->is_neg(); # true if < 0 + $x->is_inf(sign); # true if +inf, or -inf (default is '+') + + $x->bcmp($y); # compare numbers (undef,<0,=0,>0) + $x->bacmp($y); # compare absolutely (undef,<0,=0,>0) + $x->sign(); # return the sign, either +,- or NaN + $x->digit($n); # return the nth digit, counting from right + $x->digit(-$n); # return the nth digit, counting from left + + # The following all modify their first argument. If you want to preserve + # $x, use $z = $x->copy()->bXXX($y); See under L<CAVEATS> for why this is + # necessary when mixing $a = $b assignments with non-overloaded math. + + # set + $x->bzero(); # set $i to 0 + $x->bnan(); # set $i to NaN + $x->bone(); # set $x to +1 + $x->bone('-'); # set $x to -1 + $x->binf(); # set $x to inf + $x->binf('-'); # set $x to -inf + + $x->bneg(); # negation + $x->babs(); # absolute value + $x->bnorm(); # normalize (no-op) + $x->bnot(); # two's complement (bit wise not) + $x->binc(); # increment x by 1 + $x->bdec(); # decrement x by 1 + + $x->badd($y); # addition (add $y to $x) + $x->bsub($y); # subtraction (subtract $y from $x) + $x->bmul($y); # multiplication (multiply $x by $y) + $x->bdiv($y); # divide, set $x to quotient + # return (quo,rem) or quo if scalar + + $x->bmod($y); # modulus ($x % $y) + $x->bpow($y); # power of arguments ($x ** $y) + $x->bmodpow($exp,$mod); # modular exponentation (($num**$exp) % $mod)) + $x->blsft($y, $n); # left shift by $y places in base $n + $x->brsft($y, $n); # right shift by $y places in base $n + # returns (quo,rem) or quo if in scalar context + + $x->blog(); # logarithm of $x to base e (Euler's number) + $x->blog($base); # logarithm of $x to base $base (f.i. 2) + $x->bexp(); # calculate e ** $x where e is Euler's number + + $x->band($y); # bit-wise and + $x->bior($y); # bit-wise inclusive or + $x->bxor($y); # bit-wise exclusive or + $x->bnot(); # bit-wise not (two's complement) + + $x->bsqrt(); # calculate square-root + $x->broot($y); # $y'th root of $x (e.g. $y == 3 => cubic root) + $x->bfac(); # factorial of $x (1*2*3*4*..$x) + + $x->bround($N); # accuracy: preserve $N digits + $x->bfround($N); # precision: round to the $Nth digit + + $x->bfloor(); # return integer less or equal than $x + $x->bceil(); # return integer greater or equal than $x + + # The following do not modify their arguments: + + bgcd(@values); # greatest common divisor + blcm(@values); # lowest common multiplicator + + $x->bstr(); # return string + $x->bsstr(); # return string in scientific notation + + $x->as_int(); # return $x as BigInt + $x->exponent(); # return exponent as BigInt + $x->mantissa(); # return mantissa as BigInt + $x->parts(); # return (mantissa,exponent) as BigInt + + $x->length(); # number of digits (w/o sign and '.') + ($l,$f) = $x->length(); # number of digits, and length of fraction + + $x->precision(); # return P of $x (or global, if P of $x undef) + $x->precision($n); # set P of $x to $n + $x->accuracy(); # return A of $x (or global, if A of $x undef) + $x->accuracy($n); # set A $x to $n + + # these get/set the appropriate global value for all BigFloat objects + Math::BigFloat->precision(); # Precision + Math::BigFloat->accuracy(); # Accuracy + Math::BigFloat->round_mode(); # rounding mode + +=head1 DESCRIPTION + +All operators (including basic math operations) are overloaded if you +declare your big floating point numbers as + + $i = new Math::BigFloat '12_3.456_789_123_456_789E-2'; + +Operations with overloaded operators preserve the arguments, which is +exactly what you expect. + +=head2 Canonical notation + +Input to these routines are either BigFloat objects, or strings of the +following four forms: + +=over 2 + +=item * + +C</^[+-]\d+$/> + +=item * + +C</^[+-]\d+\.\d*$/> + +=item * + +C</^[+-]\d+E[+-]?\d+$/> + +=item * + +C</^[+-]\d*\.\d+E[+-]?\d+$/> + +=back + +all with optional leading and trailing zeros and/or spaces. Additionally, +numbers are allowed to have an underscore between any two digits. + +Empty strings as well as other illegal numbers results in 'NaN'. + +bnorm() on a BigFloat object is now effectively a no-op, since the numbers +are always stored in normalized form. On a string, it creates a BigFloat +object. + +=head2 Output + +Output values are BigFloat objects (normalized), except for bstr() and bsstr(). + +The string output will always have leading and trailing zeros stripped and drop +a plus sign. C<bstr()> will give you always the form with a decimal point, +while C<bsstr()> (s for scientific) gives you the scientific notation. + + Input bstr() bsstr() + '-0' '0' '0E1' + ' -123 123 123' '-123123123' '-123123123E0' + '00.0123' '0.0123' '123E-4' + '123.45E-2' '1.2345' '12345E-4' + '10E+3' '10000' '1E4' + +Some routines (C<is_odd()>, C<is_even()>, C<is_zero()>, C<is_one()>, +C<is_nan()>) return true or false, while others (C<bcmp()>, C<bacmp()>) +return either undef, <0, 0 or >0 and are suited for sort. + +Actual math is done by using the class defined with C<with => Class;> (which +defaults to BigInts) to represent the mantissa and exponent. + +The sign C</^[+-]$/> is stored separately. The string 'NaN' is used to +represent the result when input arguments are not numbers, as well as +the result of dividing by zero. + +=head2 C<mantissa()>, C<exponent()> and C<parts()> + +C<mantissa()> and C<exponent()> return the said parts of the BigFloat +as BigInts such that: + + $m = $x->mantissa(); + $e = $x->exponent(); + $y = $m * ( 10 ** $e ); + print "ok\n" if $x == $y; + +C<< ($m,$e) = $x->parts(); >> is just a shortcut giving you both of them. + +A zero is represented and returned as C<0E1>, B<not> C<0E0> (after Knuth). + +Currently the mantissa is reduced as much as possible, favouring higher +exponents over lower ones (e.g. returning 1e7 instead of 10e6 or 10000000e0). +This might change in the future, so do not depend on it. + +=head2 Accuracy vs. Precision + +See also: L<Rounding|Rounding>. + +Math::BigFloat supports both precision (rounding to a certain place before or +after the dot) and accuracy (rounding to a certain number of digits). For a +full documentation, examples and tips on these topics please see the large +section about rounding in L<Math::BigInt>. + +Since things like C<sqrt(2)> or C<1 / 3> must presented with a limited +accuracy lest a operation consumes all resources, each operation produces +no more than the requested number of digits. + +If there is no gloabl precision or accuracy set, B<and> the operation in +question was not called with a requested precision or accuracy, B<and> the +input $x has no accuracy or precision set, then a fallback parameter will +be used. For historical reasons, it is called C<div_scale> and can be accessed +via: + + $d = Math::BigFloat->div_scale(); # query + Math::BigFloat->div_scale($n); # set to $n digits + +The default value for C<div_scale> is 40. + +In case the result of one operation has more digits than specified, +it is rounded. The rounding mode taken is either the default mode, or the one +supplied to the operation after the I<scale>: + + $x = Math::BigFloat->new(2); + Math::BigFloat->accuracy(5); # 5 digits max + $y = $x->copy()->bdiv(3); # will give 0.66667 + $y = $x->copy()->bdiv(3,6); # will give 0.666667 + $y = $x->copy()->bdiv(3,6,undef,'odd'); # will give 0.666667 + Math::BigFloat->round_mode('zero'); + $y = $x->copy()->bdiv(3,6); # will also give 0.666667 + +Note that C<< Math::BigFloat->accuracy() >> and C<< Math::BigFloat->precision() >> +set the global variables, and thus B<any> newly created number will be subject +to the global rounding B<immediately>. This means that in the examples above, the +C<3> as argument to C<bdiv()> will also get an accuracy of B<5>. + +It is less confusing to either calculate the result fully, and afterwards +round it explicitly, or use the additional parameters to the math +functions like so: + + use Math::BigFloat; + $x = Math::BigFloat->new(2); + $y = $x->copy()->bdiv(3); + print $y->bround(5),"\n"; # will give 0.66667 + + or + + use Math::BigFloat; + $x = Math::BigFloat->new(2); + $y = $x->copy()->bdiv(3,5); # will give 0.66667 + print "$y\n"; + +=head2 Rounding + +=over 2 + +=item ffround ( +$scale ) + +Rounds to the $scale'th place left from the '.', counting from the dot. +The first digit is numbered 1. + +=item ffround ( -$scale ) + +Rounds to the $scale'th place right from the '.', counting from the dot. + +=item ffround ( 0 ) + +Rounds to an integer. + +=item fround ( +$scale ) + +Preserves accuracy to $scale digits from the left (aka significant digits) +and pads the rest with zeros. If the number is between 1 and -1, the +significant digits count from the first non-zero after the '.' + +=item fround ( -$scale ) and fround ( 0 ) + +These are effectively no-ops. + +=back + +All rounding functions take as a second parameter a rounding mode from one of +the following: 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common'. + +The default rounding mode is 'even'. By using +C<< Math::BigFloat->round_mode($round_mode); >> you can get and set the default +mode for subsequent rounding. The usage of C<$Math::BigFloat::$round_mode> is +no longer supported. +The second parameter to the round functions then overrides the default +temporarily. + +The C<as_number()> function returns a BigInt from a Math::BigFloat. It uses +'trunc' as rounding mode to make it equivalent to: + + $x = 2.5; + $y = int($x) + 2; + +You can override this by passing the desired rounding mode as parameter to +C<as_number()>: + + $x = Math::BigFloat->new(2.5); + $y = $x->as_number('odd'); # $y = 3 + +=head1 METHODS + +Math::BigFloat supports all methods that Math::BigInt supports, except it +calculates non-integer results when possible. Please see L<Math::BigInt> +for a full description of each method. Below are just the most important +differences: + +=head2 accuracy + + $x->accuracy(5); # local for $x + CLASS->accuracy(5); # global for all members of CLASS + # Note: This also applies to new()! + + $A = $x->accuracy(); # read out accuracy that affects $x + $A = CLASS->accuracy(); # read out global accuracy + +Set or get the global or local accuracy, aka how many significant digits the +results have. If you set a global accuracy, then this also applies to new()! + +Warning! The accuracy I<sticks>, e.g. once you created a number under the +influence of C<< CLASS->accuracy($A) >>, all results from math operations with +that number will also be rounded. + +In most cases, you should probably round the results explicitly using one of +L<round()>, L<bround()> or L<bfround()> or by passing the desired accuracy +to the math operation as additional parameter: + + my $x = Math::BigInt->new(30000); + my $y = Math::BigInt->new(7); + print scalar $x->copy()->bdiv($y, 2); # print 4300 + print scalar $x->copy()->bdiv($y)->bround(2); # print 4300 + +=head2 precision() + + $x->precision(-2); # local for $x, round at the second digit right of the dot + $x->precision(2); # ditto, round at the second digit left of the dot + + CLASS->precision(5); # Global for all members of CLASS + # This also applies to new()! + CLASS->precision(-5); # ditto + + $P = CLASS->precision(); # read out global precision + $P = $x->precision(); # read out precision that affects $x + +Note: You probably want to use L<accuracy()> instead. With L<accuracy> you +set the number of digits each result should have, with L<precision> you +set the place where to round! + +=head2 bexp() + + $x->bexp($accuracy); # calculate e ** X + +Calculates the expression C<e ** $x> where C<e> is Euler's number. + +This method was added in v1.82 of Math::BigInt (April 2007). + +=head2 bnok() + + $x->bnok($y); # x over y (binomial coefficient n over k) + +Calculates the binomial coefficient n over k, also called the "choose" +function. The result is equivalent to: + + ( n ) n! + | - | = ------- + ( k ) k!(n-k)! + +This method was added in v1.84 of Math::BigInt (April 2007). + +=head2 bpi() + + print Math::BigFloat->bpi(100), "\n"; + +Calculate PI to N digits (including the 3 before the dot). The result is +rounded according to the current rounding mode, which defaults to "even". + +This method was added in v1.87 of Math::BigInt (June 2007). + +=head2 bcos() + + my $x = Math::BigFloat->new(1); + print $x->bcos(100), "\n"; + +Calculate the cosinus of $x, modifying $x in place. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=head2 bsin() + + my $x = Math::BigFloat->new(1); + print $x->bsin(100), "\n"; + +Calculate the sinus of $x, modifying $x in place. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=head2 batan2() + + my $y = Math::BigFloat->new(2); + my $x = Math::BigFloat->new(3); + print $y->batan2($x), "\n"; + +Calculate the arcus tanges of C<$y> divided by C<$x>, modifying $y in place. +See also L<batan()>. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=head2 batan() + + my $x = Math::BigFloat->new(1); + print $x->batan(100), "\n"; + +Calculate the arcus tanges of $x, modifying $x in place. See also L<batan2()>. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=head2 bmuladd() + + $x->bmuladd($y,$z); + +Multiply $x by $y, and then add $z to the result. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=head1 Autocreating constants + +After C<use Math::BigFloat ':constant'> all the floating point constants +in the given scope are converted to C<Math::BigFloat>. This conversion +happens at compile time. + +In particular + + perl -MMath::BigFloat=:constant -e 'print 2E-100,"\n"' + +prints the value of C<2E-100>. Note that without conversion of +constants the expression 2E-100 will be calculated as normal floating point +number. + +Please note that ':constant' does not affect integer constants, nor binary +nor hexadecimal constants. Use L<bignum> or L<Math::BigInt> to get this to +work. + +=head2 Math library + +Math with the numbers is done (by default) by a module called +Math::BigInt::Calc. This is equivalent to saying: + + use Math::BigFloat lib => 'Calc'; + +You can change this by using: + + use Math::BigFloat lib => 'GMP'; + +B<Note>: General purpose packages should not be explicit about the library +to use; let the script author decide which is best. + +Note: The keyword 'lib' will warn when the requested library could not be +loaded. To suppress the warning use 'try' instead: + + use Math::BigFloat try => 'GMP'; + +If your script works with huge numbers and Calc is too slow for them, +you can also for the loading of one of these libraries and if none +of them can be used, the code will die: + + use Math::BigFloat only => 'GMP,Pari'; + +The following would first try to find Math::BigInt::Foo, then +Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: + + use Math::BigFloat lib => 'Foo,Math::BigInt::Bar'; + +See the respective low-level library documentation for further details. + +Please note that Math::BigFloat does B<not> use the denoted library itself, +but it merely passes the lib argument to Math::BigInt. So, instead of the need +to do: + + use Math::BigInt lib => 'GMP'; + use Math::BigFloat; + +you can roll it all into one line: + + use Math::BigFloat lib => 'GMP'; + +It is also possible to just require Math::BigFloat: + + require Math::BigFloat; + +This will load the necessary things (like BigInt) when they are needed, and +automatically. + +See L<Math::BigInt> for more details than you ever wanted to know about using +a different low-level library. + +=head2 Using Math::BigInt::Lite + +For backwards compatibility reasons it is still possible to +request a different storage class for use with Math::BigFloat: + + use Math::BigFloat with => 'Math::BigInt::Lite'; + +However, this request is ignored, as the current code now uses the low-level +math libary for directly storing the number parts. + +=head1 EXPORTS + +C<Math::BigFloat> exports nothing by default, but can export the C<bpi()> method: + + use Math::BigFloat qw/bpi/; + + print bpi(10), "\n"; + +=head1 BUGS + +Please see the file BUGS in the CPAN distribution Math::BigInt for known bugs. + +=head1 CAVEATS + +Do not try to be clever to insert some operations in between switching +libraries: + + require Math::BigFloat; + my $matter = Math::BigFloat->bone() + 4; # load BigInt and Calc + Math::BigFloat->import( lib => 'Pari' ); # load Pari, too + my $anti_matter = Math::BigFloat->bone()+4; # now use Pari + +This will create objects with numbers stored in two different backend libraries, +and B<VERY BAD THINGS> will happen when you use these together: + + my $flash_and_bang = $matter + $anti_matter; # Don't do this! + +=over 1 + +=item stringify, bstr() + +Both stringify and bstr() now drop the leading '+'. The old code would return +'+1.23', the new returns '1.23'. See the documentation in L<Math::BigInt> for +reasoning and details. + +=item bdiv + +The following will probably not print what you expect: + + print $c->bdiv(123.456),"\n"; + +It prints both quotient and reminder since print works in list context. Also, +bdiv() will modify $c, so be careful. You probably want to use + + print $c / 123.456,"\n"; + print scalar $c->bdiv(123.456),"\n"; # or if you want to modify $c + +instead. + +=item brsft + +The following will probably not print what you expect: + + my $c = Math::BigFloat->new('3.14159'); + print $c->brsft(3,10),"\n"; # prints 0.00314153.1415 + +It prints both quotient and remainder, since print calls C<brsft()> in list +context. Also, C<< $c->brsft() >> will modify $c, so be careful. +You probably want to use + + print scalar $c->copy()->brsft(3,10),"\n"; + # or if you really want to modify $c + print scalar $c->brsft(3,10),"\n"; + +instead. + +=item Modifying and = + +Beware of: + + $x = Math::BigFloat->new(5); + $y = $x; + +It will not do what you think, e.g. making a copy of $x. Instead it just makes +a second reference to the B<same> object and stores it in $y. Thus anything +that modifies $x will modify $y (except overloaded math operators), and vice +versa. See L<Math::BigInt> for details and how to avoid that. + +=item bpow + +C<bpow()> now modifies the first argument, unlike the old code which left +it alone and only returned the result. This is to be consistent with +C<badd()> etc. The first will modify $x, the second one won't: + + print bpow($x,$i),"\n"; # modify $x + print $x->bpow($i),"\n"; # ditto + print $x ** $i,"\n"; # leave $x alone + +=item precision() vs. accuracy() + +A common pitfall is to use L<precision()> when you want to round a result to +a certain number of digits: + + use Math::BigFloat; + + Math::BigFloat->precision(4); # does not do what you think it does + my $x = Math::BigFloat->new(12345); # rounds $x to "12000"! + print "$x\n"; # print "12000" + my $y = Math::BigFloat->new(3); # rounds $y to "0"! + print "$y\n"; # print "0" + $z = $x / $y; # 12000 / 0 => NaN! + print "$z\n"; + print $z->precision(),"\n"; # 4 + +Replacing L<precision> with L<accuracy> is probably not what you want, either: + + use Math::BigFloat; + + Math::BigFloat->accuracy(4); # enables global rounding: + my $x = Math::BigFloat->new(123456); # rounded immediately to "12350" + print "$x\n"; # print "123500" + my $y = Math::BigFloat->new(3); # rounded to "3 + print "$y\n"; # print "3" + print $z = $x->copy()->bdiv($y),"\n"; # 41170 + print $z->accuracy(),"\n"; # 4 + +What you want to use instead is: + + use Math::BigFloat; + + my $x = Math::BigFloat->new(123456); # no rounding + print "$x\n"; # print "123456" + my $y = Math::BigFloat->new(3); # no rounding + print "$y\n"; # print "3" + print $z = $x->copy()->bdiv($y,4),"\n"; # 41150 + print $z->accuracy(),"\n"; # undef + +In addition to computing what you expected, the last example also does B<not> +"taint" the result with an accuracy or precision setting, which would +influence any further operation. + +=back + +=head1 SEE ALSO + +L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well as +L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>. + +The pragmas L<bignum>, L<bigint> and L<bigrat> might also be of interest +because they solve the autoupgrading/downgrading issue, at least partly. + +The package at L<http://search.cpan.org/~tels/Math-BigInt> contains +more documentation including a full version history, testcases, empty +subclass files and benchmarks. + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 AUTHORS + +Mark Biggar, overloaded interface by Ilya Zakharevich. +Completely rewritten by Tels L<http://bloodgate.com> in 2001 - 2006, and still +at it in 2007. + +=cut diff --git a/Master/tlpkg/tlperl/lib/Math/BigFloat/Trace.pm b/Master/tlpkg/tlperl/lib/Math/BigFloat/Trace.pm new file mode 100644 index 00000000000..871b2a969e8 --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Math/BigFloat/Trace.pm @@ -0,0 +1,58 @@ +#!/usr/bin/perl -w + +package Math::BigFloat::Trace; + +require 5.005_02; +use strict; + +use Exporter; +use Math::BigFloat; +use vars qw($VERSION @ISA $PACKAGE @EXPORT_OK + $accuracy $precision $round_mode $div_scale); + +@ISA = qw(Exporter Math::BigFloat); + +$VERSION = 0.01; + +use overload; # inherit overload from BigFloat + +# Globals +$accuracy = $precision = undef; +$round_mode = 'even'; +$div_scale = 40; + +sub new +{ + my $proto = shift; + my $class = ref($proto) || $proto; + + my $value = shift; + my $a = $accuracy; $a = $_[0] if defined $_[0]; + my $p = $precision; $p = $_[1] if defined $_[1]; + my $self = Math::BigFloat->new($value,$a,$p,$round_mode); + +# remember, downgrading may return a BigInt, so don't meddle with class +# bless $self,$class; + + print "MBF new '$value' => '$self' (",ref($self),")"; + return $self; +} + +sub import + { + print "MBF import ",join(' ',@_); + my $self = shift; + + # we catch the constants, the rest goes go BigFloat + my @a = (); + foreach (@_) + { + push @a, $_ if $_ ne ':constant'; + } + overload::constant float => sub { $self->new(shift); }; + + Math::BigFloat->import(@a); # need it for subclasses +# $self->export_to_level(1,$self,@_); # need this ? + } + +1; diff --git a/Master/tlpkg/tlperl/lib/Math/BigInt.pm b/Master/tlpkg/tlperl/lib/Math/BigInt.pm new file mode 100644 index 00000000000..f97e4380798 --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Math/BigInt.pm @@ -0,0 +1,5115 @@ +package Math::BigInt; + +# +# "Mike had an infinite amount to do and a negative amount of time in which +# to do it." - Before and After +# + +# The following hash values are used: +# value: unsigned int with actual value (as a Math::BigInt::Calc or similiar) +# sign : +,-,NaN,+inf,-inf +# _a : accuracy +# _p : precision +# _f : flags, used by MBF to flag parts of a float as untouchable + +# Remember not to take shortcuts ala $xs = $x->{value}; $CALC->foo($xs); since +# underlying lib might change the reference! + +my $class = "Math::BigInt"; +use 5.006; + +$VERSION = '1.89_01'; + +@ISA = qw(Exporter); +@EXPORT_OK = qw(objectify bgcd blcm); + +# _trap_inf and _trap_nan are internal and should never be accessed from the +# outside +use vars qw/$round_mode $accuracy $precision $div_scale $rnd_mode + $upgrade $downgrade $_trap_nan $_trap_inf/; +use strict; + +# Inside overload, the first arg is always an object. If the original code had +# it reversed (like $x = 2 * $y), then the third paramater is true. +# In some cases (like add, $x = $x + 2 is the same as $x = 2 + $x) this makes +# no difference, but in some cases it does. + +# For overloaded ops with only one argument we simple use $_[0]->copy() to +# preserve the argument. + +# Thus inheritance of overload operators becomes possible and transparent for +# our subclasses without the need to repeat the entire overload section there. + +use overload +'=' => sub { $_[0]->copy(); }, + +# some shortcuts for speed (assumes that reversed order of arguments is routed +# to normal '+' and we thus can always modify first arg. If this is changed, +# this breaks and must be adjusted.) +'+=' => sub { $_[0]->badd($_[1]); }, +'-=' => sub { $_[0]->bsub($_[1]); }, +'*=' => sub { $_[0]->bmul($_[1]); }, +'/=' => sub { scalar $_[0]->bdiv($_[1]); }, +'%=' => sub { $_[0]->bmod($_[1]); }, +'^=' => sub { $_[0]->bxor($_[1]); }, +'&=' => sub { $_[0]->band($_[1]); }, +'|=' => sub { $_[0]->bior($_[1]); }, + +'**=' => sub { $_[0]->bpow($_[1]); }, +'<<=' => sub { $_[0]->blsft($_[1]); }, +'>>=' => sub { $_[0]->brsft($_[1]); }, + +# not supported by Perl yet +'..' => \&_pointpoint, + +'<=>' => sub { my $rc = $_[2] ? + ref($_[0])->bcmp($_[1],$_[0]) : + $_[0]->bcmp($_[1]); + $rc = 1 unless defined $rc; + $rc <=> 0; + }, +# we need '>=' to get things like "1 >= NaN" right: +'>=' => sub { my $rc = $_[2] ? + ref($_[0])->bcmp($_[1],$_[0]) : + $_[0]->bcmp($_[1]); + # if there was a NaN involved, return false + return '' unless defined $rc; + $rc >= 0; + }, +'cmp' => sub { + $_[2] ? + "$_[1]" cmp $_[0]->bstr() : + $_[0]->bstr() cmp "$_[1]" }, + +'cos' => sub { $_[0]->copy->bcos(); }, +'sin' => sub { $_[0]->copy->bsin(); }, +'atan2' => sub { $_[2] ? + ref($_[0])->new($_[1])->batan2($_[0]) : + $_[0]->copy()->batan2($_[1]) }, + +# are not yet overloadable +#'hex' => sub { print "hex"; $_[0]; }, +#'oct' => sub { print "oct"; $_[0]; }, + +# log(N) is log(N, e), where e is Euler's number +'log' => sub { $_[0]->copy()->blog($_[1], undef); }, +'exp' => sub { $_[0]->copy()->bexp($_[1]); }, +'int' => sub { $_[0]->copy(); }, +'neg' => sub { $_[0]->copy()->bneg(); }, +'abs' => sub { $_[0]->copy()->babs(); }, +'sqrt' => sub { $_[0]->copy()->bsqrt(); }, +'~' => sub { $_[0]->copy()->bnot(); }, + +# for subtract it's a bit tricky to not modify b: b-a => -a+b +'-' => sub { my $c = $_[0]->copy; $_[2] ? + $c->bneg()->badd( $_[1]) : + $c->bsub( $_[1]) }, +'+' => sub { $_[0]->copy()->badd($_[1]); }, +'*' => sub { $_[0]->copy()->bmul($_[1]); }, + +'/' => sub { + $_[2] ? ref($_[0])->new($_[1])->bdiv($_[0]) : $_[0]->copy->bdiv($_[1]); + }, +'%' => sub { + $_[2] ? ref($_[0])->new($_[1])->bmod($_[0]) : $_[0]->copy->bmod($_[1]); + }, +'**' => sub { + $_[2] ? ref($_[0])->new($_[1])->bpow($_[0]) : $_[0]->copy->bpow($_[1]); + }, +'<<' => sub { + $_[2] ? ref($_[0])->new($_[1])->blsft($_[0]) : $_[0]->copy->blsft($_[1]); + }, +'>>' => sub { + $_[2] ? ref($_[0])->new($_[1])->brsft($_[0]) : $_[0]->copy->brsft($_[1]); + }, +'&' => sub { + $_[2] ? ref($_[0])->new($_[1])->band($_[0]) : $_[0]->copy->band($_[1]); + }, +'|' => sub { + $_[2] ? ref($_[0])->new($_[1])->bior($_[0]) : $_[0]->copy->bior($_[1]); + }, +'^' => sub { + $_[2] ? ref($_[0])->new($_[1])->bxor($_[0]) : $_[0]->copy->bxor($_[1]); + }, + +# can modify arg of ++ and --, so avoid a copy() for speed, but don't +# use $_[0]->bone(), it would modify $_[0] to be 1! +'++' => sub { $_[0]->binc() }, +'--' => sub { $_[0]->bdec() }, + +# if overloaded, O(1) instead of O(N) and twice as fast for small numbers +'bool' => sub { + # this kludge is needed for perl prior 5.6.0 since returning 0 here fails :-/ + # v5.6.1 dumps on this: return !$_[0]->is_zero() || undef; :-( + my $t = undef; + $t = 1 if !$_[0]->is_zero(); + $t; + }, + +# the original qw() does not work with the TIESCALAR below, why? +# Order of arguments unsignificant +'""' => sub { $_[0]->bstr(); }, +'0+' => sub { $_[0]->numify(); } +; + +############################################################################## +# global constants, flags and accessory + +# These vars are public, but their direct usage is not recommended, use the +# accessor methods instead + +$round_mode = 'even'; # one of 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common' +$accuracy = undef; +$precision = undef; +$div_scale = 40; + +$upgrade = undef; # default is no upgrade +$downgrade = undef; # default is no downgrade + +# These are internally, and not to be used from the outside at all + +$_trap_nan = 0; # are NaNs ok? set w/ config() +$_trap_inf = 0; # are infs ok? set w/ config() +my $nan = 'NaN'; # constants for easier life + +my $CALC = 'Math::BigInt::FastCalc'; # module to do the low level math + # default is FastCalc.pm +my $IMPORT = 0; # was import() called yet? + # used to make require work +my %WARN; # warn only once for low-level libs +my %CAN; # cache for $CALC->can(...) +my %CALLBACKS; # callbacks to notify on lib loads +my $EMU_LIB = 'Math/BigInt/CalcEmu.pm'; # emulate low-level math + +############################################################################## +# the old code had $rnd_mode, so we need to support it, too + +$rnd_mode = 'even'; +sub TIESCALAR { my ($class) = @_; bless \$round_mode, $class; } +sub FETCH { return $round_mode; } +sub STORE { $rnd_mode = $_[0]->round_mode($_[1]); } + +BEGIN + { + # tie to enable $rnd_mode to work transparently + tie $rnd_mode, 'Math::BigInt'; + + # set up some handy alias names + *as_int = \&as_number; + *is_pos = \&is_positive; + *is_neg = \&is_negative; + } + +############################################################################## + +sub round_mode + { + no strict 'refs'; + # make Class->round_mode() work + my $self = shift; + my $class = ref($self) || $self || __PACKAGE__; + if (defined $_[0]) + { + my $m = shift; + if ($m !~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/) + { + require Carp; Carp::croak ("Unknown round mode '$m'"); + } + return ${"${class}::round_mode"} = $m; + } + ${"${class}::round_mode"}; + } + +sub upgrade + { + no strict 'refs'; + # make Class->upgrade() work + my $self = shift; + my $class = ref($self) || $self || __PACKAGE__; + # need to set new value? + if (@_ > 0) + { + return ${"${class}::upgrade"} = $_[0]; + } + ${"${class}::upgrade"}; + } + +sub downgrade + { + no strict 'refs'; + # make Class->downgrade() work + my $self = shift; + my $class = ref($self) || $self || __PACKAGE__; + # need to set new value? + if (@_ > 0) + { + return ${"${class}::downgrade"} = $_[0]; + } + ${"${class}::downgrade"}; + } + +sub div_scale + { + no strict 'refs'; + # make Class->div_scale() work + my $self = shift; + my $class = ref($self) || $self || __PACKAGE__; + if (defined $_[0]) + { + if ($_[0] < 0) + { + require Carp; Carp::croak ('div_scale must be greater than zero'); + } + ${"${class}::div_scale"} = $_[0]; + } + ${"${class}::div_scale"}; + } + +sub accuracy + { + # $x->accuracy($a); ref($x) $a + # $x->accuracy(); ref($x) + # Class->accuracy(); class + # Class->accuracy($a); class $a + + my $x = shift; + my $class = ref($x) || $x || __PACKAGE__; + + no strict 'refs'; + # need to set new value? + if (@_ > 0) + { + my $a = shift; + # convert objects to scalars to avoid deep recursion. If object doesn't + # have numify(), then hopefully it will have overloading for int() and + # boolean test without wandering into a deep recursion path... + $a = $a->numify() if ref($a) && $a->can('numify'); + + if (defined $a) + { + # also croak on non-numerical + if (!$a || $a <= 0) + { + require Carp; + Carp::croak ('Argument to accuracy must be greater than zero'); + } + if (int($a) != $a) + { + require Carp; + Carp::croak ('Argument to accuracy must be an integer'); + } + } + if (ref($x)) + { + # $object->accuracy() or fallback to global + $x->bround($a) if $a; # not for undef, 0 + $x->{_a} = $a; # set/overwrite, even if not rounded + delete $x->{_p}; # clear P + $a = ${"${class}::accuracy"} unless defined $a; # proper return value + } + else + { + ${"${class}::accuracy"} = $a; # set global A + ${"${class}::precision"} = undef; # clear global P + } + return $a; # shortcut + } + + my $a; + # $object->accuracy() or fallback to global + $a = $x->{_a} if ref($x); + # but don't return global undef, when $x's accuracy is 0! + $a = ${"${class}::accuracy"} if !defined $a; + $a; + } + +sub precision + { + # $x->precision($p); ref($x) $p + # $x->precision(); ref($x) + # Class->precision(); class + # Class->precision($p); class $p + + my $x = shift; + my $class = ref($x) || $x || __PACKAGE__; + + no strict 'refs'; + if (@_ > 0) + { + my $p = shift; + # convert objects to scalars to avoid deep recursion. If object doesn't + # have numify(), then hopefully it will have overloading for int() and + # boolean test without wandering into a deep recursion path... + $p = $p->numify() if ref($p) && $p->can('numify'); + if ((defined $p) && (int($p) != $p)) + { + require Carp; Carp::croak ('Argument to precision must be an integer'); + } + if (ref($x)) + { + # $object->precision() or fallback to global + $x->bfround($p) if $p; # not for undef, 0 + $x->{_p} = $p; # set/overwrite, even if not rounded + delete $x->{_a}; # clear A + $p = ${"${class}::precision"} unless defined $p; # proper return value + } + else + { + ${"${class}::precision"} = $p; # set global P + ${"${class}::accuracy"} = undef; # clear global A + } + return $p; # shortcut + } + + my $p; + # $object->precision() or fallback to global + $p = $x->{_p} if ref($x); + # but don't return global undef, when $x's precision is 0! + $p = ${"${class}::precision"} if !defined $p; + $p; + } + +sub config + { + # return (or set) configuration data as hash ref + my $class = shift || 'Math::BigInt'; + + no strict 'refs'; + if (@_ > 1 || (@_ == 1 && (ref($_[0]) eq 'HASH'))) + { + # try to set given options as arguments from hash + + my $args = $_[0]; + if (ref($args) ne 'HASH') + { + $args = { @_ }; + } + # these values can be "set" + my $set_args = {}; + foreach my $key ( + qw/trap_inf trap_nan + upgrade downgrade precision accuracy round_mode div_scale/ + ) + { + $set_args->{$key} = $args->{$key} if exists $args->{$key}; + delete $args->{$key}; + } + if (keys %$args > 0) + { + require Carp; + Carp::croak ("Illegal key(s) '", + join("','",keys %$args),"' passed to $class\->config()"); + } + foreach my $key (keys %$set_args) + { + if ($key =~ /^trap_(inf|nan)\z/) + { + ${"${class}::_trap_$1"} = ($set_args->{"trap_$1"} ? 1 : 0); + next; + } + # use a call instead of just setting the $variable to check argument + $class->$key($set_args->{$key}); + } + } + + # now return actual configuration + + my $cfg = { + lib => $CALC, + lib_version => ${"${CALC}::VERSION"}, + class => $class, + trap_nan => ${"${class}::_trap_nan"}, + trap_inf => ${"${class}::_trap_inf"}, + version => ${"${class}::VERSION"}, + }; + foreach my $key (qw/ + upgrade downgrade precision accuracy round_mode div_scale + /) + { + $cfg->{$key} = ${"${class}::$key"}; + }; + if (@_ == 1 && (ref($_[0]) ne 'HASH')) + { + # calls of the style config('lib') return just this value + return $cfg->{$_[0]}; + } + $cfg; + } + +sub _scale_a + { + # select accuracy parameter based on precedence, + # used by bround() and bfround(), may return undef for scale (means no op) + my ($x,$scale,$mode) = @_; + + $scale = $x->{_a} unless defined $scale; + + no strict 'refs'; + my $class = ref($x); + + $scale = ${ $class . '::accuracy' } unless defined $scale; + $mode = ${ $class . '::round_mode' } unless defined $mode; + + if (defined $scale) + { + $scale = $scale->can('numify') ? $scale->numify() : "$scale" if ref($scale); + $scale = int($scale); + } + + ($scale,$mode); + } + +sub _scale_p + { + # select precision parameter based on precedence, + # used by bround() and bfround(), may return undef for scale (means no op) + my ($x,$scale,$mode) = @_; + + $scale = $x->{_p} unless defined $scale; + + no strict 'refs'; + my $class = ref($x); + + $scale = ${ $class . '::precision' } unless defined $scale; + $mode = ${ $class . '::round_mode' } unless defined $mode; + + if (defined $scale) + { + $scale = $scale->can('numify') ? $scale->numify() : "$scale" if ref($scale); + $scale = int($scale); + } + + ($scale,$mode); + } + +############################################################################## +# constructors + +sub copy + { + # if two arguments, the first one is the class to "swallow" subclasses + if (@_ > 1) + { + my $self = bless { + sign => $_[1]->{sign}, + value => $CALC->_copy($_[1]->{value}), + }, $_[0] if @_ > 1; + + $self->{_a} = $_[1]->{_a} if defined $_[1]->{_a}; + $self->{_p} = $_[1]->{_p} if defined $_[1]->{_p}; + return $self; + } + + my $self = bless { + sign => $_[0]->{sign}, + value => $CALC->_copy($_[0]->{value}), + }, ref($_[0]); + + $self->{_a} = $_[0]->{_a} if defined $_[0]->{_a}; + $self->{_p} = $_[0]->{_p} if defined $_[0]->{_p}; + $self; + } + +sub new + { + # create a new BigInt object from a string or another BigInt object. + # see hash keys documented at top + + # the argument could be an object, so avoid ||, && etc on it, this would + # cause costly overloaded code to be called. The only allowed ops are + # ref() and defined. + + my ($class,$wanted,$a,$p,$r) = @_; + + # avoid numify-calls by not using || on $wanted! + return $class->bzero($a,$p) if !defined $wanted; # default to 0 + return $class->copy($wanted,$a,$p,$r) + if ref($wanted) && $wanted->isa($class); # MBI or subclass + + $class->import() if $IMPORT == 0; # make require work + + my $self = bless {}, $class; + + # shortcut for "normal" numbers + if ((!ref $wanted) && ($wanted =~ /^([+-]?)[1-9][0-9]*\z/)) + { + $self->{sign} = $1 || '+'; + + if ($wanted =~ /^[+-]/) + { + # remove sign without touching wanted to make it work with constants + my $t = $wanted; $t =~ s/^[+-]//; + $self->{value} = $CALC->_new($t); + } + else + { + $self->{value} = $CALC->_new($wanted); + } + no strict 'refs'; + if ( (defined $a) || (defined $p) + || (defined ${"${class}::precision"}) + || (defined ${"${class}::accuracy"}) + ) + { + $self->round($a,$p,$r) unless (@_ == 4 && !defined $a && !defined $p); + } + return $self; + } + + # handle '+inf', '-inf' first + if ($wanted =~ /^[+-]?inf\z/) + { + $self->{sign} = $wanted; # set a default sign for bstr() + return $self->binf($wanted); + } + # split str in m mantissa, e exponent, i integer, f fraction, v value, s sign + my ($mis,$miv,$mfv,$es,$ev) = _split($wanted); + if (!ref $mis) + { + if ($_trap_nan) + { + require Carp; Carp::croak("$wanted is not a number in $class"); + } + $self->{value} = $CALC->_zero(); + $self->{sign} = $nan; + return $self; + } + if (!ref $miv) + { + # _from_hex or _from_bin + $self->{value} = $mis->{value}; + $self->{sign} = $mis->{sign}; + return $self; # throw away $mis + } + # make integer from mantissa by adjusting exp, then convert to bigint + $self->{sign} = $$mis; # store sign + $self->{value} = $CALC->_zero(); # for all the NaN cases + my $e = int("$$es$$ev"); # exponent (avoid recursion) + if ($e > 0) + { + my $diff = $e - CORE::length($$mfv); + if ($diff < 0) # Not integer + { + if ($_trap_nan) + { + require Carp; Carp::croak("$wanted not an integer in $class"); + } + #print "NOI 1\n"; + return $upgrade->new($wanted,$a,$p,$r) if defined $upgrade; + $self->{sign} = $nan; + } + else # diff >= 0 + { + # adjust fraction and add it to value + #print "diff > 0 $$miv\n"; + $$miv = $$miv . ($$mfv . '0' x $diff); + } + } + else + { + if ($$mfv ne '') # e <= 0 + { + # fraction and negative/zero E => NOI + if ($_trap_nan) + { + require Carp; Carp::croak("$wanted not an integer in $class"); + } + #print "NOI 2 \$\$mfv '$$mfv'\n"; + return $upgrade->new($wanted,$a,$p,$r) if defined $upgrade; + $self->{sign} = $nan; + } + elsif ($e < 0) + { + # xE-y, and empty mfv + #print "xE-y\n"; + $e = abs($e); + if ($$miv !~ s/0{$e}$//) # can strip so many zero's? + { + if ($_trap_nan) + { + require Carp; Carp::croak("$wanted not an integer in $class"); + } + #print "NOI 3\n"; + return $upgrade->new($wanted,$a,$p,$r) if defined $upgrade; + $self->{sign} = $nan; + } + } + } + $self->{sign} = '+' if $$miv eq '0'; # normalize -0 => +0 + $self->{value} = $CALC->_new($$miv) if $self->{sign} =~ /^[+-]$/; + # if any of the globals is set, use them to round and store them inside $self + # do not round for new($x,undef,undef) since that is used by MBF to signal + # no rounding + $self->round($a,$p,$r) unless @_ == 4 && !defined $a && !defined $p; + $self; + } + +sub bnan + { + # create a bigint 'NaN', if given a BigInt, set it to 'NaN' + my $self = shift; + $self = $class if !defined $self; + if (!ref($self)) + { + my $c = $self; $self = {}; bless $self, $c; + } + no strict 'refs'; + if (${"${class}::_trap_nan"}) + { + require Carp; + Carp::croak ("Tried to set $self to NaN in $class\::bnan()"); + } + $self->import() if $IMPORT == 0; # make require work + return if $self->modify('bnan'); + if ($self->can('_bnan')) + { + # use subclass to initialize + $self->_bnan(); + } + else + { + # otherwise do our own thing + $self->{value} = $CALC->_zero(); + } + $self->{sign} = $nan; + delete $self->{_a}; delete $self->{_p}; # rounding NaN is silly + $self; + } + +sub binf + { + # create a bigint '+-inf', if given a BigInt, set it to '+-inf' + # the sign is either '+', or if given, used from there + my $self = shift; + my $sign = shift; $sign = '+' if !defined $sign || $sign !~ /^-(inf)?$/; + $self = $class if !defined $self; + if (!ref($self)) + { + my $c = $self; $self = {}; bless $self, $c; + } + no strict 'refs'; + if (${"${class}::_trap_inf"}) + { + require Carp; + Carp::croak ("Tried to set $self to +-inf in $class\::binf()"); + } + $self->import() if $IMPORT == 0; # make require work + return if $self->modify('binf'); + if ($self->can('_binf')) + { + # use subclass to initialize + $self->_binf(); + } + else + { + # otherwise do our own thing + $self->{value} = $CALC->_zero(); + } + $sign = $sign . 'inf' if $sign !~ /inf$/; # - => -inf + $self->{sign} = $sign; + ($self->{_a},$self->{_p}) = @_; # take over requested rounding + $self; + } + +sub bzero + { + # create a bigint '+0', if given a BigInt, set it to 0 + my $self = shift; + $self = __PACKAGE__ if !defined $self; + + if (!ref($self)) + { + my $c = $self; $self = {}; bless $self, $c; + } + $self->import() if $IMPORT == 0; # make require work + return if $self->modify('bzero'); + + if ($self->can('_bzero')) + { + # use subclass to initialize + $self->_bzero(); + } + else + { + # otherwise do our own thing + $self->{value} = $CALC->_zero(); + } + $self->{sign} = '+'; + if (@_ > 0) + { + if (@_ > 3) + { + # call like: $x->bzero($a,$p,$r,$y); + ($self,$self->{_a},$self->{_p}) = $self->_find_round_parameters(@_); + } + else + { + $self->{_a} = $_[0] + if ( (!defined $self->{_a}) || (defined $_[0] && $_[0] > $self->{_a})); + $self->{_p} = $_[1] + if ( (!defined $self->{_p}) || (defined $_[1] && $_[1] > $self->{_p})); + } + } + $self; + } + +sub bone + { + # create a bigint '+1' (or -1 if given sign '-'), + # if given a BigInt, set it to +1 or -1, respectively + my $self = shift; + my $sign = shift; $sign = '+' if !defined $sign || $sign ne '-'; + $self = $class if !defined $self; + + if (!ref($self)) + { + my $c = $self; $self = {}; bless $self, $c; + } + $self->import() if $IMPORT == 0; # make require work + return if $self->modify('bone'); + + if ($self->can('_bone')) + { + # use subclass to initialize + $self->_bone(); + } + else + { + # otherwise do our own thing + $self->{value} = $CALC->_one(); + } + $self->{sign} = $sign; + if (@_ > 0) + { + if (@_ > 3) + { + # call like: $x->bone($sign,$a,$p,$r,$y); + ($self,$self->{_a},$self->{_p}) = $self->_find_round_parameters(@_); + } + else + { + # call like: $x->bone($sign,$a,$p,$r); + $self->{_a} = $_[0] + if ( (!defined $self->{_a}) || (defined $_[0] && $_[0] > $self->{_a})); + $self->{_p} = $_[1] + if ( (!defined $self->{_p}) || (defined $_[1] && $_[1] > $self->{_p})); + } + } + $self; + } + +############################################################################## +# string conversation + +sub bsstr + { + # (ref to BFLOAT or num_str ) return num_str + # Convert number from internal format to scientific string format. + # internal format is always normalized (no leading zeros, "-0E0" => "+0E0") + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN + return 'inf'; # +inf + } + my ($m,$e) = $x->parts(); + #$m->bstr() . 'e+' . $e->bstr(); # e can only be positive in BigInt + # 'e+' because E can only be positive in BigInt + $m->bstr() . 'e+' . $CALC->_str($e->{value}); + } + +sub bstr + { + # make a string from bigint object + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN + return 'inf'; # +inf + } + my $es = ''; $es = $x->{sign} if $x->{sign} eq '-'; + $es.$CALC->_str($x->{value}); + } + +sub numify + { + # Make a "normal" scalar from a BigInt object + my $x = shift; $x = $class->new($x) unless ref $x; + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; + my $num = $CALC->_num($x->{value}); + return -$num if $x->{sign} eq '-'; + $num; + } + +############################################################################## +# public stuff (usually prefixed with "b") + +sub sign + { + # return the sign of the number: +/-/-inf/+inf/NaN + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + $x->{sign}; + } + +sub _find_round_parameters + { + # After any operation or when calling round(), the result is rounded by + # regarding the A & P from arguments, local parameters, or globals. + + # !!!!!!! If you change this, remember to change round(), too! !!!!!!!!!! + + # This procedure finds the round parameters, but it is for speed reasons + # duplicated in round. Otherwise, it is tested by the testsuite and used + # by fdiv(). + + # returns ($self) or ($self,$a,$p,$r) - sets $self to NaN of both A and P + # were requested/defined (locally or globally or both) + + my ($self,$a,$p,$r,@args) = @_; + # $a accuracy, if given by caller + # $p precision, if given by caller + # $r round_mode, if given by caller + # @args all 'other' arguments (0 for unary, 1 for binary ops) + + my $c = ref($self); # find out class of argument(s) + no strict 'refs'; + + # convert to normal scalar for speed and correctness in inner parts + $a = $a->can('numify') ? $a->numify() : "$a" if defined $a && ref($a); + $p = $p->can('numify') ? $p->numify() : "$p" if defined $p && ref($p); + + # now pick $a or $p, but only if we have got "arguments" + if (!defined $a) + { + foreach ($self,@args) + { + # take the defined one, or if both defined, the one that is smaller + $a = $_->{_a} if (defined $_->{_a}) && (!defined $a || $_->{_a} < $a); + } + } + if (!defined $p) + { + # even if $a is defined, take $p, to signal error for both defined + foreach ($self,@args) + { + # take the defined one, or if both defined, the one that is bigger + # -2 > -3, and 3 > 2 + $p = $_->{_p} if (defined $_->{_p}) && (!defined $p || $_->{_p} > $p); + } + } + # if still none defined, use globals (#2) + $a = ${"$c\::accuracy"} unless defined $a; + $p = ${"$c\::precision"} unless defined $p; + + # A == 0 is useless, so undef it to signal no rounding + $a = undef if defined $a && $a == 0; + + # no rounding today? + return ($self) unless defined $a || defined $p; # early out + + # set A and set P is an fatal error + return ($self->bnan()) if defined $a && defined $p; # error + + $r = ${"$c\::round_mode"} unless defined $r; + if ($r !~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/) + { + require Carp; Carp::croak ("Unknown round mode '$r'"); + } + + $a = int($a) if defined $a; + $p = int($p) if defined $p; + + ($self,$a,$p,$r); + } + +sub round + { + # Round $self according to given parameters, or given second argument's + # parameters or global defaults + + # for speed reasons, _find_round_parameters is embeded here: + + my ($self,$a,$p,$r,@args) = @_; + # $a accuracy, if given by caller + # $p precision, if given by caller + # $r round_mode, if given by caller + # @args all 'other' arguments (0 for unary, 1 for binary ops) + + my $c = ref($self); # find out class of argument(s) + no strict 'refs'; + + # now pick $a or $p, but only if we have got "arguments" + if (!defined $a) + { + foreach ($self,@args) + { + # take the defined one, or if both defined, the one that is smaller + $a = $_->{_a} if (defined $_->{_a}) && (!defined $a || $_->{_a} < $a); + } + } + if (!defined $p) + { + # even if $a is defined, take $p, to signal error for both defined + foreach ($self,@args) + { + # take the defined one, or if both defined, the one that is bigger + # -2 > -3, and 3 > 2 + $p = $_->{_p} if (defined $_->{_p}) && (!defined $p || $_->{_p} > $p); + } + } + # if still none defined, use globals (#2) + $a = ${"$c\::accuracy"} unless defined $a; + $p = ${"$c\::precision"} unless defined $p; + + # A == 0 is useless, so undef it to signal no rounding + $a = undef if defined $a && $a == 0; + + # no rounding today? + return $self unless defined $a || defined $p; # early out + + # set A and set P is an fatal error + return $self->bnan() if defined $a && defined $p; + + $r = ${"$c\::round_mode"} unless defined $r; + if ($r !~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/) + { + require Carp; Carp::croak ("Unknown round mode '$r'"); + } + + # now round, by calling either fround or ffround: + if (defined $a) + { + $self->bround(int($a),$r) if !defined $self->{_a} || $self->{_a} >= $a; + } + else # both can't be undefined due to early out + { + $self->bfround(int($p),$r) if !defined $self->{_p} || $self->{_p} <= $p; + } + # bround() or bfround() already callled bnorm() if nec. + $self; + } + +sub bnorm + { + # (numstr or BINT) return BINT + # Normalize number -- no-op here + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + $x; + } + +sub babs + { + # (BINT or num_str) return BINT + # make number absolute, or return absolute BINT from string + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x if $x->modify('babs'); + # post-normalized abs for internal use (does nothing for NaN) + $x->{sign} =~ s/^-/+/; + $x; + } + +sub bneg + { + # (BINT or num_str) return BINT + # negate number or make a negated number from string + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x if $x->modify('bneg'); + + # for +0 dont negate (to have always normalized +0). Does nothing for 'NaN' + $x->{sign} =~ tr/+-/-+/ unless ($x->{sign} eq '+' && $CALC->_is_zero($x->{value})); + $x; + } + +sub bcmp + { + # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort) + # (BINT or num_str, BINT or num_str) return cond_code + + # set up parameters + my ($self,$x,$y) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y) = objectify(2,@_); + } + + return $upgrade->bcmp($x,$y) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + # handle +-inf and NaN + return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + return 0 if $x->{sign} eq $y->{sign} && $x->{sign} =~ /^[+-]inf$/; + return +1 if $x->{sign} eq '+inf'; + return -1 if $x->{sign} eq '-inf'; + return -1 if $y->{sign} eq '+inf'; + return +1; + } + # check sign for speed first + return 1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # does also 0 <=> -y + return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # does also -x <=> 0 + + # have same sign, so compare absolute values. Don't make tests for zero here + # because it's actually slower than testin in Calc (especially w/ Pari et al) + + # post-normalized compare for internal use (honors signs) + if ($x->{sign} eq '+') + { + # $x and $y both > 0 + return $CALC->_acmp($x->{value},$y->{value}); + } + + # $x && $y both < 0 + $CALC->_acmp($y->{value},$x->{value}); # swaped acmp (lib returns 0,1,-1) + } + +sub bacmp + { + # Compares 2 values, ignoring their signs. + # Returns one of undef, <0, =0, >0. (suitable for sort) + # (BINT, BINT) return cond_code + + # set up parameters + my ($self,$x,$y) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y) = objectify(2,@_); + } + + return $upgrade->bacmp($x,$y) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + # handle +-inf and NaN + return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + return 0 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} =~ /^[+-]inf$/; + return 1 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} !~ /^[+-]inf$/; + return -1; + } + $CALC->_acmp($x->{value},$y->{value}); # lib does only 0,1,-1 + } + +sub badd + { + # add second arg (BINT or string) to first (BINT) (modifies first) + # return result as BINT + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('badd'); + return $upgrade->badd($upgrade->new($x),$upgrade->new($y),@r) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + $r[3] = $y; # no push! + # inf and NaN handling + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + # NaN first + return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/)) + { + # +inf++inf or -inf+-inf => same, rest is NaN + return $x if $x->{sign} eq $y->{sign}; + return $x->bnan(); + } + # +-inf + something => +inf + # something +-inf => +-inf + $x->{sign} = $y->{sign}, return $x if $y->{sign} =~ /^[+-]inf$/; + return $x; + } + + my ($sx, $sy) = ( $x->{sign}, $y->{sign} ); # get signs + + if ($sx eq $sy) + { + $x->{value} = $CALC->_add($x->{value},$y->{value}); # same sign, abs add + } + else + { + my $a = $CALC->_acmp ($y->{value},$x->{value}); # absolute compare + if ($a > 0) + { + $x->{value} = $CALC->_sub($y->{value},$x->{value},1); # abs sub w/ swap + $x->{sign} = $sy; + } + elsif ($a == 0) + { + # speedup, if equal, set result to 0 + $x->{value} = $CALC->_zero(); + $x->{sign} = '+'; + } + else # a < 0 + { + $x->{value} = $CALC->_sub($x->{value}, $y->{value}); # abs sub + } + } + $x->round(@r); + } + +sub bsub + { + # (BINT or num_str, BINT or num_str) return BINT + # subtract second arg from first, modify first + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bsub'); + + return $upgrade->new($x)->bsub($upgrade->new($y),@r) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + return $x->round(@r) if $y->is_zero(); + + # To correctly handle the lone special case $x->bsub($x), we note the sign + # of $x, then flip the sign from $y, and if the sign of $x did change, too, + # then we caught the special case: + my $xsign = $x->{sign}; + $y->{sign} =~ tr/+\-/-+/; # does nothing for NaN + if ($xsign ne $x->{sign}) + { + # special case of $x->bsub($x) results in 0 + return $x->bzero(@r) if $xsign =~ /^[+-]$/; + return $x->bnan(); # NaN, -inf, +inf + } + $x->badd($y,@r); # badd does not leave internal zeros + $y->{sign} =~ tr/+\-/-+/; # refix $y (does nothing for NaN) + $x; # already rounded by badd() or no round nec. + } + +sub binc + { + # increment arg by one + my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + return $x if $x->modify('binc'); + + if ($x->{sign} eq '+') + { + $x->{value} = $CALC->_inc($x->{value}); + return $x->round($a,$p,$r); + } + elsif ($x->{sign} eq '-') + { + $x->{value} = $CALC->_dec($x->{value}); + $x->{sign} = '+' if $CALC->_is_zero($x->{value}); # -1 +1 => -0 => +0 + return $x->round($a,$p,$r); + } + # inf, nan handling etc + $x->badd($self->bone(),$a,$p,$r); # badd does round + } + +sub bdec + { + # decrement arg by one + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + return $x if $x->modify('bdec'); + + if ($x->{sign} eq '-') + { + # x already < 0 + $x->{value} = $CALC->_inc($x->{value}); + } + else + { + return $x->badd($self->bone('-'),@r) unless $x->{sign} eq '+'; # inf or NaN + # >= 0 + if ($CALC->_is_zero($x->{value})) + { + # == 0 + $x->{value} = $CALC->_one(); $x->{sign} = '-'; # 0 => -1 + } + else + { + # > 0 + $x->{value} = $CALC->_dec($x->{value}); + } + } + $x->round(@r); + } + +sub blog + { + # calculate $x = $a ** $base + $b and return $a (e.g. the log() to base + # $base of $x) + + # set up parameters + my ($self,$x,$base,@r) = (undef,@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$base,@r) = objectify(1,ref($x),@_); + } + + return $x if $x->modify('blog'); + + $base = $self->new($base) if defined $base && !ref $base; + + # inf, -inf, NaN, <0 => NaN + return $x->bnan() + if $x->{sign} ne '+' || (defined $base && $base->{sign} ne '+'); + + return $upgrade->blog($upgrade->new($x),$base,@r) if + defined $upgrade; + + # fix for bug #24969: + # the default base is e (Euler's number) which is not an integer + if (!defined $base) + { + require Math::BigFloat; + my $u = Math::BigFloat->blog(Math::BigFloat->new($x))->as_int(); + # modify $x in place + $x->{value} = $u->{value}; + $x->{sign} = $u->{sign}; + return $x; + } + + my ($rc,$exact) = $CALC->_log_int($x->{value},$base->{value}); + return $x->bnan() unless defined $rc; # not possible to take log? + $x->{value} = $rc; + $x->round(@r); + } + +sub bnok + { + # Calculate n over k (binomial coefficient or "choose" function) as integer. + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bnok'); + return $x->bnan() if $x->{sign} eq 'NaN' || $y->{sign} eq 'NaN'; + return $x->binf() if $x->{sign} eq '+inf'; + + # k > n or k < 0 => 0 + my $cmp = $x->bacmp($y); + return $x->bzero() if $cmp < 0 || $y->{sign} =~ /^-/; + # k == n => 1 + return $x->bone(@r) if $cmp == 0; + + if ($CALC->can('_nok')) + { + $x->{value} = $CALC->_nok($x->{value},$y->{value}); + } + else + { + # ( 7 ) 7! 7*6*5 * 4*3*2*1 7 * 6 * 5 + # ( - ) = --------- = --------------- = --------- + # ( 3 ) 3! (7-3)! 3*2*1 * 4*3*2*1 3 * 2 * 1 + + # compute n - k + 2 (so we start with 5 in the example above) + my $z = $x - $y; + if (!$z->is_one()) + { + $z->binc(); + my $r = $z->copy(); $z->binc(); + my $d = $self->new(2); + while ($z->bacmp($x) <= 0) # f < x ? + { + $r->bmul($z); $r->bdiv($d); + $z->binc(); $d->binc(); + } + $x->{value} = $r->{value}; $x->{sign} = '+'; + } + else { $x->bone(); } + } + $x->round(@r); + } + +sub bexp + { + # Calculate e ** $x (Euler's number to the power of X), truncated to + # an integer value. + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + return $x if $x->modify('bexp'); + + # inf, -inf, NaN, <0 => NaN + return $x->bnan() if $x->{sign} eq 'NaN'; + return $x->bone() if $x->is_zero(); + return $x if $x->{sign} eq '+inf'; + return $x->bzero() if $x->{sign} eq '-inf'; + + my $u; + { + # run through Math::BigFloat unless told otherwise + require Math::BigFloat unless defined $upgrade; + local $upgrade = 'Math::BigFloat' unless defined $upgrade; + # calculate result, truncate it to integer + $u = $upgrade->bexp($upgrade->new($x),@r); + } + + if (!defined $upgrade) + { + $u = $u->as_int(); + # modify $x in place + $x->{value} = $u->{value}; + $x->round(@r); + } + else { $x = $u; } + } + +sub blcm + { + # (BINT or num_str, BINT or num_str) return BINT + # does not modify arguments, but returns new object + # Lowest Common Multiplicator + + my $y = shift; my ($x); + if (ref($y)) + { + $x = $y->copy(); + } + else + { + $x = $class->new($y); + } + my $self = ref($x); + while (@_) + { + my $y = shift; $y = $self->new($y) if !ref ($y); + $x = __lcm($x,$y); + } + $x; + } + +sub bgcd + { + # (BINT or num_str, BINT or num_str) return BINT + # does not modify arguments, but returns new object + # GCD -- Euclids algorithm, variant C (Knuth Vol 3, pg 341 ff) + + my $y = shift; + $y = $class->new($y) if !ref($y); + my $self = ref($y); + my $x = $y->copy()->babs(); # keep arguments + return $x->bnan() if $x->{sign} !~ /^[+-]$/; # x NaN? + + while (@_) + { + $y = shift; $y = $self->new($y) if !ref($y); + return $x->bnan() if $y->{sign} !~ /^[+-]$/; # y NaN? + $x->{value} = $CALC->_gcd($x->{value},$y->{value}); + last if $CALC->_is_one($x->{value}); + } + $x; + } + +sub bnot + { + # (num_str or BINT) return BINT + # represent ~x as twos-complement number + # we don't need $self, so undef instead of ref($_[0]) make it slightly faster + my ($self,$x,$a,$p,$r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + return $x if $x->modify('bnot'); + $x->binc()->bneg(); # binc already does round + } + +############################################################################## +# is_foo test routines +# we don't need $self, so undef instead of ref($_[0]) make it slightly faster + +sub is_zero + { + # return true if arg (BINT or num_str) is zero (array '+', '0') + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 0 if $x->{sign} !~ /^\+$/; # -, NaN & +-inf aren't + $CALC->_is_zero($x->{value}); + } + +sub is_nan + { + # return true if arg (BINT or num_str) is NaN + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + $x->{sign} eq $nan ? 1 : 0; + } + +sub is_inf + { + # return true if arg (BINT or num_str) is +-inf + my ($self,$x,$sign) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + if (defined $sign) + { + $sign = '[+-]inf' if $sign eq ''; # +- doesn't matter, only that's inf + $sign = "[$1]inf" if $sign =~ /^([+-])(inf)?$/; # extract '+' or '-' + return $x->{sign} =~ /^$sign$/ ? 1 : 0; + } + $x->{sign} =~ /^[+-]inf$/ ? 1 : 0; # only +-inf is infinity + } + +sub is_one + { + # return true if arg (BINT or num_str) is +1, or -1 if sign is given + my ($self,$x,$sign) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + $sign = '+' if !defined $sign || $sign ne '-'; + + return 0 if $x->{sign} ne $sign; # -1 != +1, NaN, +-inf aren't either + $CALC->_is_one($x->{value}); + } + +sub is_odd + { + # return true when arg (BINT or num_str) is odd, false for even + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't + $CALC->_is_odd($x->{value}); + } + +sub is_even + { + # return true when arg (BINT or num_str) is even, false for odd + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't + $CALC->_is_even($x->{value}); + } + +sub is_positive + { + # return true when arg (BINT or num_str) is positive (>= 0) + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 1 if $x->{sign} eq '+inf'; # +inf is positive + + # 0+ is neither positive nor negative + ($x->{sign} eq '+' && !$x->is_zero()) ? 1 : 0; + } + +sub is_negative + { + # return true when arg (BINT or num_str) is negative (< 0) + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + $x->{sign} =~ /^-/ ? 1 : 0; # -inf is negative, but NaN is not + } + +sub is_int + { + # return true when arg (BINT or num_str) is an integer + # always true for BigInt, but different for BigFloats + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + $x->{sign} =~ /^[+-]$/ ? 1 : 0; # inf/-inf/NaN aren't + } + +############################################################################### + +sub bmul + { + # multiply the first number by the second number + # (BINT or num_str, BINT or num_str) return BINT + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bmul'); + + return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) + { + return $x->bnan() if $x->is_zero() || $y->is_zero(); + # result will always be +-inf: + # +inf * +/+inf => +inf, -inf * -/-inf => +inf + # +inf * -/-inf => -inf, -inf * +/+inf => -inf + return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); + return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); + return $x->binf('-'); + } + + return $upgrade->bmul($x,$upgrade->new($y),@r) + if defined $upgrade && !$y->isa($self); + + $r[3] = $y; # no push here + + $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; # +1 * +1 or -1 * -1 => + + + $x->{value} = $CALC->_mul($x->{value},$y->{value}); # do actual math + $x->{sign} = '+' if $CALC->_is_zero($x->{value}); # no -0 + + $x->round(@r); + } + +sub bmuladd + { + # multiply two numbers and then add the third to the result + # (BINT or num_str, BINT or num_str, BINT or num_str) return BINT + + # set up parameters + my ($self,$x,$y,$z,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$z,@r) = objectify(3,@_); + } + + return $x if $x->modify('bmuladd'); + + return $x->bnan() if ($x->{sign} eq $nan) || + ($y->{sign} eq $nan) || + ($z->{sign} eq $nan); + + # inf handling of x and y + if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) + { + return $x->bnan() if $x->is_zero() || $y->is_zero(); + # result will always be +-inf: + # +inf * +/+inf => +inf, -inf * -/-inf => +inf + # +inf * -/-inf => -inf, -inf * +/+inf => -inf + return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); + return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); + return $x->binf('-'); + } + # inf handling x*y and z + if (($z->{sign} =~ /^[+-]inf$/)) + { + # something +-inf => +-inf + $x->{sign} = $z->{sign}, return $x if $z->{sign} =~ /^[+-]inf$/; + } + + return $upgrade->bmuladd($x,$upgrade->new($y),$upgrade->new($z),@r) + if defined $upgrade && (!$y->isa($self) || !$z->isa($self) || !$x->isa($self)); + + # TODO: what if $y and $z have A or P set? + $r[3] = $z; # no push here + + $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; # +1 * +1 or -1 * -1 => + + + $x->{value} = $CALC->_mul($x->{value},$y->{value}); # do actual math + $x->{sign} = '+' if $CALC->_is_zero($x->{value}); # no -0 + + my ($sx, $sz) = ( $x->{sign}, $z->{sign} ); # get signs + + if ($sx eq $sz) + { + $x->{value} = $CALC->_add($x->{value},$z->{value}); # same sign, abs add + } + else + { + my $a = $CALC->_acmp ($z->{value},$x->{value}); # absolute compare + if ($a > 0) + { + $x->{value} = $CALC->_sub($z->{value},$x->{value},1); # abs sub w/ swap + $x->{sign} = $sz; + } + elsif ($a == 0) + { + # speedup, if equal, set result to 0 + $x->{value} = $CALC->_zero(); + $x->{sign} = '+'; + } + else # a < 0 + { + $x->{value} = $CALC->_sub($x->{value}, $z->{value}); # abs sub + } + } + $x->round(@r); + } + +sub _div_inf + { + # helper function that handles +-inf cases for bdiv()/bmod() to reuse code + my ($self,$x,$y) = @_; + + # NaN if x == NaN or y == NaN or x==y==0 + return wantarray ? ($x->bnan(),$self->bnan()) : $x->bnan() + if (($x->is_nan() || $y->is_nan()) || + ($x->is_zero() && $y->is_zero())); + + # +-inf / +-inf == NaN, reminder also NaN + if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/)) + { + return wantarray ? ($x->bnan(),$self->bnan()) : $x->bnan(); + } + # x / +-inf => 0, remainder x (works even if x == 0) + if ($y->{sign} =~ /^[+-]inf$/) + { + my $t = $x->copy(); # bzero clobbers up $x + return wantarray ? ($x->bzero(),$t) : $x->bzero() + } + + # 5 / 0 => +inf, -6 / 0 => -inf + # +inf / 0 = inf, inf, and -inf / 0 => -inf, -inf + # exception: -8 / 0 has remainder -8, not 8 + # exception: -inf / 0 has remainder -inf, not inf + if ($y->is_zero()) + { + # +-inf / 0 => special case for -inf + return wantarray ? ($x,$x->copy()) : $x if $x->is_inf(); + if (!$x->is_zero() && !$x->is_inf()) + { + my $t = $x->copy(); # binf clobbers up $x + return wantarray ? + ($x->binf($x->{sign}),$t) : $x->binf($x->{sign}) + } + } + + # last case: +-inf / ordinary number + my $sign = '+inf'; + $sign = '-inf' if substr($x->{sign},0,1) ne $y->{sign}; + $x->{sign} = $sign; + return wantarray ? ($x,$self->bzero()) : $x; + } + +sub bdiv + { + # (dividend: BINT or num_str, divisor: BINT or num_str) return + # (BINT,BINT) (quo,rem) or BINT (only rem) + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bdiv'); + + return $self->_div_inf($x,$y) + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/) || $y->is_zero()); + + return $upgrade->bdiv($upgrade->new($x),$upgrade->new($y),@r) + if defined $upgrade; + + $r[3] = $y; # no push! + + # calc new sign and in case $y == +/- 1, return $x + my $xsign = $x->{sign}; # keep + $x->{sign} = ($x->{sign} ne $y->{sign} ? '-' : '+'); + + if (wantarray) + { + my $rem = $self->bzero(); + ($x->{value},$rem->{value}) = $CALC->_div($x->{value},$y->{value}); + $x->{sign} = '+' if $CALC->_is_zero($x->{value}); + $rem->{_a} = $x->{_a}; + $rem->{_p} = $x->{_p}; + $x->round(@r); + if (! $CALC->_is_zero($rem->{value})) + { + $rem->{sign} = $y->{sign}; + $rem = $y->copy()->bsub($rem) if $xsign ne $y->{sign}; # one of them '-' + } + else + { + $rem->{sign} = '+'; # dont leave -0 + } + $rem->round(@r); + return ($x,$rem); + } + + $x->{value} = $CALC->_div($x->{value},$y->{value}); + $x->{sign} = '+' if $CALC->_is_zero($x->{value}); + + $x->round(@r); + } + +############################################################################### +# modulus functions + +sub bmod + { + # modulus (or remainder) + # (BINT or num_str, BINT or num_str) return BINT + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bmod'); + $r[3] = $y; # no push! + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/) || $y->is_zero()) + { + my ($d,$r) = $self->_div_inf($x,$y); + $x->{sign} = $r->{sign}; + $x->{value} = $r->{value}; + return $x->round(@r); + } + + # calc new sign and in case $y == +/- 1, return $x + $x->{value} = $CALC->_mod($x->{value},$y->{value}); + if (!$CALC->_is_zero($x->{value})) + { + $x->{value} = $CALC->_sub($y->{value},$x->{value},1) # $y-$x + if ($x->{sign} ne $y->{sign}); + $x->{sign} = $y->{sign}; + } + else + { + $x->{sign} = '+'; # dont leave -0 + } + $x->round(@r); + } + +sub bmodinv + { + # Modular inverse. given a number which is (hopefully) relatively + # prime to the modulus, calculate its inverse using Euclid's + # alogrithm. If the number is not relatively prime to the modulus + # (i.e. their gcd is not one) then NaN is returned. + + # set up parameters + my ($self,$x,$y,@r) = (undef,@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bmodinv'); + + return $x->bnan() + if ($y->{sign} ne '+' # -, NaN, +inf, -inf + || $x->is_zero() # or num == 0 + || $x->{sign} !~ /^[+-]$/ # or num NaN, inf, -inf + ); + + # put least residue into $x if $x was negative, and thus make it positive + $x->bmod($y) if $x->{sign} eq '-'; + + my $sign; + ($x->{value},$sign) = $CALC->_modinv($x->{value},$y->{value}); + return $x->bnan() if !defined $x->{value}; # in case no GCD found + return $x if !defined $sign; # already real result + $x->{sign} = $sign; # flip/flop see below + $x->bmod($y); # calc real result + $x; + } + +sub bmodpow + { + # takes a very large number to a very large exponent in a given very + # large modulus, quickly, thanks to binary exponentation. Supports + # negative exponents. + my ($self,$num,$exp,$mod,@r) = objectify(3,@_); + + return $num if $num->modify('bmodpow'); + + # check modulus for valid values + return $num->bnan() if ($mod->{sign} ne '+' # NaN, - , -inf, +inf + || $mod->is_zero()); + + # check exponent for valid values + if ($exp->{sign} =~ /\w/) + { + # i.e., if it's NaN, +inf, or -inf... + return $num->bnan(); + } + + $num->bmodinv ($mod) if ($exp->{sign} eq '-'); + + # check num for valid values (also NaN if there was no inverse but $exp < 0) + return $num->bnan() if $num->{sign} !~ /^[+-]$/; + + # $mod is positive, sign on $exp is ignored, result also positive + $num->{value} = $CALC->_modpow($num->{value},$exp->{value},$mod->{value}); + $num; + } + +############################################################################### + +sub bfac + { + # (BINT or num_str, BINT or num_str) return BINT + # compute factorial number from $x, modify $x in place + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + return $x if $x->modify('bfac') || $x->{sign} eq '+inf'; # inf => inf + return $x->bnan() if $x->{sign} ne '+'; # NaN, <0 etc => NaN + + $x->{value} = $CALC->_fac($x->{value}); + $x->round(@r); + } + +sub bpow + { + # (BINT or num_str, BINT or num_str) return BINT + # compute power of two numbers -- stolen from Knuth Vol 2 pg 233 + # modifies first argument + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bpow'); + + return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan; + + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) + { + if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/)) + { + # +-inf ** +-inf + return $x->bnan(); + } + # +-inf ** Y + if ($x->{sign} =~ /^[+-]inf/) + { + # +inf ** 0 => NaN + return $x->bnan() if $y->is_zero(); + # -inf ** -1 => 1/inf => 0 + return $x->bzero() if $y->is_one('-') && $x->is_negative(); + + # +inf ** Y => inf + return $x if $x->{sign} eq '+inf'; + + # -inf ** Y => -inf if Y is odd + return $x if $y->is_odd(); + return $x->babs(); + } + # X ** +-inf + + # 1 ** +inf => 1 + return $x if $x->is_one(); + + # 0 ** inf => 0 + return $x if $x->is_zero() && $y->{sign} =~ /^[+]/; + + # 0 ** -inf => inf + return $x->binf() if $x->is_zero(); + + # -1 ** -inf => NaN + return $x->bnan() if $x->is_one('-') && $y->{sign} =~ /^[-]/; + + # -X ** -inf => 0 + return $x->bzero() if $x->{sign} eq '-' && $y->{sign} =~ /^[-]/; + + # -1 ** inf => NaN + return $x->bnan() if $x->{sign} eq '-'; + + # X ** inf => inf + return $x->binf() if $y->{sign} =~ /^[+]/; + # X ** -inf => 0 + return $x->bzero(); + } + + return $upgrade->bpow($upgrade->new($x),$y,@r) + if defined $upgrade && (!$y->isa($self) || $y->{sign} eq '-'); + + $r[3] = $y; # no push! + + # cases 0 ** Y, X ** 0, X ** 1, 1 ** Y are handled by Calc or Emu + + my $new_sign = '+'; + $new_sign = $y->is_odd() ? '-' : '+' if ($x->{sign} ne '+'); + + # 0 ** -7 => ( 1 / (0 ** 7)) => 1 / 0 => +inf + return $x->binf() + if $y->{sign} eq '-' && $x->{sign} eq '+' && $CALC->_is_zero($x->{value}); + # 1 ** -y => 1 / (1 ** |y|) + # so do test for negative $y after above's clause + return $x->bnan() if $y->{sign} eq '-' && !$CALC->_is_one($x->{value}); + + $x->{value} = $CALC->_pow($x->{value},$y->{value}); + $x->{sign} = $new_sign; + $x->{sign} = '+' if $CALC->_is_zero($y->{value}); + $x->round(@r); + } + +sub blsft + { + # (BINT or num_str, BINT or num_str) return BINT + # compute x << y, base n, y >= 0 + + # set up parameters + my ($self,$x,$y,$n,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$n,@r) = objectify(2,@_); + } + + return $x if $x->modify('blsft'); + return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); + return $x->round(@r) if $y->is_zero(); + + $n = 2 if !defined $n; return $x->bnan() if $n <= 0 || $y->{sign} eq '-'; + + $x->{value} = $CALC->_lsft($x->{value},$y->{value},$n); + $x->round(@r); + } + +sub brsft + { + # (BINT or num_str, BINT or num_str) return BINT + # compute x >> y, base n, y >= 0 + + # set up parameters + my ($self,$x,$y,$n,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$n,@r) = objectify(2,@_); + } + + return $x if $x->modify('brsft'); + return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); + return $x->round(@r) if $y->is_zero(); + return $x->bzero(@r) if $x->is_zero(); # 0 => 0 + + $n = 2 if !defined $n; return $x->bnan() if $n <= 0 || $y->{sign} eq '-'; + + # this only works for negative numbers when shifting in base 2 + if (($x->{sign} eq '-') && ($n == 2)) + { + return $x->round(@r) if $x->is_one('-'); # -1 => -1 + if (!$y->is_one()) + { + # although this is O(N*N) in calc (as_bin!) it is O(N) in Pari et al + # but perhaps there is a better emulation for two's complement shift... + # if $y != 1, we must simulate it by doing: + # convert to bin, flip all bits, shift, and be done + $x->binc(); # -3 => -2 + my $bin = $x->as_bin(); + $bin =~ s/^-0b//; # strip '-0b' prefix + $bin =~ tr/10/01/; # flip bits + # now shift + if ($y >= CORE::length($bin)) + { + $bin = '0'; # shifting to far right creates -1 + # 0, because later increment makes + # that 1, attached '-' makes it '-1' + # because -1 >> x == -1 ! + } + else + { + $bin =~ s/.{$y}$//; # cut off at the right side + $bin = '1' . $bin; # extend left side by one dummy '1' + $bin =~ tr/10/01/; # flip bits back + } + my $res = $self->new('0b'.$bin); # add prefix and convert back + $res->binc(); # remember to increment + $x->{value} = $res->{value}; # take over value + return $x->round(@r); # we are done now, magic, isn't? + } + # x < 0, n == 2, y == 1 + $x->bdec(); # n == 2, but $y == 1: this fixes it + } + + $x->{value} = $CALC->_rsft($x->{value},$y->{value},$n); + $x->round(@r); + } + +sub band + { + #(BINT or num_str, BINT or num_str) return BINT + # compute x & y + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('band'); + + $r[3] = $y; # no push! + + return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); + + my $sx = $x->{sign} eq '+' ? 1 : -1; + my $sy = $y->{sign} eq '+' ? 1 : -1; + + if ($sx == 1 && $sy == 1) + { + $x->{value} = $CALC->_and($x->{value},$y->{value}); + return $x->round(@r); + } + + if ($CAN{signed_and}) + { + $x->{value} = $CALC->_signed_and($x->{value},$y->{value},$sx,$sy); + return $x->round(@r); + } + + require $EMU_LIB; + __emu_band($self,$x,$y,$sx,$sy,@r); + } + +sub bior + { + #(BINT or num_str, BINT or num_str) return BINT + # compute x | y + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bior'); + $r[3] = $y; # no push! + + return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); + + my $sx = $x->{sign} eq '+' ? 1 : -1; + my $sy = $y->{sign} eq '+' ? 1 : -1; + + # the sign of X follows the sign of X, e.g. sign of Y irrelevant for bior() + + # don't use lib for negative values + if ($sx == 1 && $sy == 1) + { + $x->{value} = $CALC->_or($x->{value},$y->{value}); + return $x->round(@r); + } + + # if lib can do negative values, let it handle this + if ($CAN{signed_or}) + { + $x->{value} = $CALC->_signed_or($x->{value},$y->{value},$sx,$sy); + return $x->round(@r); + } + + require $EMU_LIB; + __emu_bior($self,$x,$y,$sx,$sy,@r); + } + +sub bxor + { + #(BINT or num_str, BINT or num_str) return BINT + # compute x ^ y + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bxor'); + $r[3] = $y; # no push! + + return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); + + my $sx = $x->{sign} eq '+' ? 1 : -1; + my $sy = $y->{sign} eq '+' ? 1 : -1; + + # don't use lib for negative values + if ($sx == 1 && $sy == 1) + { + $x->{value} = $CALC->_xor($x->{value},$y->{value}); + return $x->round(@r); + } + + # if lib can do negative values, let it handle this + if ($CAN{signed_xor}) + { + $x->{value} = $CALC->_signed_xor($x->{value},$y->{value},$sx,$sy); + return $x->round(@r); + } + + require $EMU_LIB; + __emu_bxor($self,$x,$y,$sx,$sy,@r); + } + +sub length + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + my $e = $CALC->_len($x->{value}); + wantarray ? ($e,0) : $e; + } + +sub digit + { + # return the nth decimal digit, negative values count backward, 0 is right + my ($self,$x,$n) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + $n = $n->numify() if ref($n); + $CALC->_digit($x->{value},$n||0); + } + +sub _trailing_zeros + { + # return the amount of trailing zeros in $x (as scalar) + my $x = shift; + $x = $class->new($x) unless ref $x; + + return 0 if $x->{sign} !~ /^[+-]$/; # NaN, inf, -inf etc + + $CALC->_zeros($x->{value}); # must handle odd values, 0 etc + } + +sub bsqrt + { + # calculate square root of $x + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + return $x if $x->modify('bsqrt'); + + return $x->bnan() if $x->{sign} !~ /^\+/; # -x or -inf or NaN => NaN + return $x if $x->{sign} eq '+inf'; # sqrt(+inf) == inf + + return $upgrade->bsqrt($x,@r) if defined $upgrade; + + $x->{value} = $CALC->_sqrt($x->{value}); + $x->round(@r); + } + +sub broot + { + # calculate $y'th root of $x + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + $y = $self->new(2) unless defined $y; + + # objectify is costly, so avoid it + if ((!ref($x)) || (ref($x) ne ref($y))) + { + ($self,$x,$y,@r) = objectify(2,$self || $class,@_); + } + + return $x if $x->modify('broot'); + + # NaN handling: $x ** 1/0, x or y NaN, or y inf/-inf or y == 0 + return $x->bnan() if $x->{sign} !~ /^\+/ || $y->is_zero() || + $y->{sign} !~ /^\+$/; + + return $x->round(@r) + if $x->is_zero() || $x->is_one() || $x->is_inf() || $y->is_one(); + + return $upgrade->new($x)->broot($upgrade->new($y),@r) if defined $upgrade; + + $x->{value} = $CALC->_root($x->{value},$y->{value}); + $x->round(@r); + } + +sub exponent + { + # return a copy of the exponent (here always 0, NaN or 1 for $m == 0) + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + my $s = $x->{sign}; $s =~ s/^[+-]//; # NaN, -inf,+inf => NaN or inf + return $self->new($s); + } + return $self->bone() if $x->is_zero(); + + # 12300 => 2 trailing zeros => exponent is 2 + $self->new( $CALC->_zeros($x->{value}) ); + } + +sub mantissa + { + # return the mantissa (compatible to Math::BigFloat, e.g. reduced) + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + # for NaN, +inf, -inf: keep the sign + return $self->new($x->{sign}); + } + my $m = $x->copy(); delete $m->{_p}; delete $m->{_a}; + + # that's a bit inefficient: + my $zeros = $CALC->_zeros($m->{value}); + $m->brsft($zeros,10) if $zeros != 0; + $m; + } + +sub parts + { + # return a copy of both the exponent and the mantissa + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + ($x->mantissa(),$x->exponent()); + } + +############################################################################## +# rounding functions + +sub bfround + { + # precision: round to the $Nth digit left (+$n) or right (-$n) from the '.' + # $n == 0 || $n == 1 => round to integer + my $x = shift; my $self = ref($x) || $x; $x = $self->new($x) unless ref $x; + + my ($scale,$mode) = $x->_scale_p(@_); + + return $x if !defined $scale || $x->modify('bfround'); # no-op + + # no-op for BigInts if $n <= 0 + $x->bround( $x->length()-$scale, $mode) if $scale > 0; + + delete $x->{_a}; # delete to save memory + $x->{_p} = $scale; # store new _p + $x; + } + +sub _scan_for_nonzero + { + # internal, used by bround() to scan for non-zeros after a '5' + my ($x,$pad,$xs,$len) = @_; + + return 0 if $len == 1; # "5" is trailed by invisible zeros + my $follow = $pad - 1; + return 0 if $follow > $len || $follow < 1; + + # use the string form to check whether only '0's follow or not + substr ($xs,-$follow) =~ /[^0]/ ? 1 : 0; + } + +sub fround + { + # Exists to make life easier for switch between MBF and MBI (should we + # autoload fxxx() like MBF does for bxxx()?) + my $x = shift; $x = $class->new($x) unless ref $x; + $x->bround(@_); + } + +sub bround + { + # accuracy: +$n preserve $n digits from left, + # -$n preserve $n digits from right (f.i. for 0.1234 style in MBF) + # no-op for $n == 0 + # and overwrite the rest with 0's, return normalized number + # do not return $x->bnorm(), but $x + + my $x = shift; $x = $class->new($x) unless ref $x; + my ($scale,$mode) = $x->_scale_a(@_); + return $x if !defined $scale || $x->modify('bround'); # no-op + + if ($x->is_zero() || $scale == 0) + { + $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale; # 3 > 2 + return $x; + } + return $x if $x->{sign} !~ /^[+-]$/; # inf, NaN + + # we have fewer digits than we want to scale to + my $len = $x->length(); + # convert $scale to a scalar in case it is an object (put's a limit on the + # number length, but this would already limited by memory constraints), makes + # it faster + $scale = $scale->numify() if ref ($scale); + + # scale < 0, but > -len (not >=!) + if (($scale < 0 && $scale < -$len-1) || ($scale >= $len)) + { + $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale; # 3 > 2 + return $x; + } + + # count of 0's to pad, from left (+) or right (-): 9 - +6 => 3, or |-6| => 6 + my ($pad,$digit_round,$digit_after); + $pad = $len - $scale; + $pad = abs($scale-1) if $scale < 0; + + # do not use digit(), it is very costly for binary => decimal + # getting the entire string is also costly, but we need to do it only once + my $xs = $CALC->_str($x->{value}); + my $pl = -$pad-1; + + # pad: 123: 0 => -1, at 1 => -2, at 2 => -3, at 3 => -4 + # pad+1: 123: 0 => 0, at 1 => -1, at 2 => -2, at 3 => -3 + $digit_round = '0'; $digit_round = substr($xs,$pl,1) if $pad <= $len; + $pl++; $pl ++ if $pad >= $len; + $digit_after = '0'; $digit_after = substr($xs,$pl,1) if $pad > 0; + + # in case of 01234 we round down, for 6789 up, and only in case 5 we look + # closer at the remaining digits of the original $x, remember decision + my $round_up = 1; # default round up + $round_up -- if + ($mode eq 'trunc') || # trunc by round down + ($digit_after =~ /[01234]/) || # round down anyway, + # 6789 => round up + ($digit_after eq '5') && # not 5000...0000 + ($x->_scan_for_nonzero($pad,$xs,$len) == 0) && + ( + ($mode eq 'even') && ($digit_round =~ /[24680]/) || + ($mode eq 'odd') && ($digit_round =~ /[13579]/) || + ($mode eq '+inf') && ($x->{sign} eq '-') || + ($mode eq '-inf') && ($x->{sign} eq '+') || + ($mode eq 'zero') # round down if zero, sign adjusted below + ); + my $put_back = 0; # not yet modified + + if (($pad > 0) && ($pad <= $len)) + { + substr($xs,-$pad,$pad) = '0' x $pad; # replace with '00...' + $put_back = 1; # need to put back + } + elsif ($pad > $len) + { + $x->bzero(); # round to '0' + } + + if ($round_up) # what gave test above? + { + $put_back = 1; # need to put back + $pad = $len, $xs = '0' x $pad if $scale < 0; # tlr: whack 0.51=>1.0 + + # we modify directly the string variant instead of creating a number and + # adding it, since that is faster (we already have the string) + my $c = 0; $pad ++; # for $pad == $len case + while ($pad <= $len) + { + $c = substr($xs,-$pad,1) + 1; $c = '0' if $c eq '10'; + substr($xs,-$pad,1) = $c; $pad++; + last if $c != 0; # no overflow => early out + } + $xs = '1'.$xs if $c == 0; + + } + $x->{value} = $CALC->_new($xs) if $put_back == 1; # put back, if needed + + $x->{_a} = $scale if $scale >= 0; + if ($scale < 0) + { + $x->{_a} = $len+$scale; + $x->{_a} = 0 if $scale < -$len; + } + $x; + } + +sub bfloor + { + # return integer less or equal then number; no-op since it's already integer + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + $x->round(@r); + } + +sub bceil + { + # return integer greater or equal then number; no-op since it's already int + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + $x->round(@r); + } + +sub as_number + { + # An object might be asked to return itself as bigint on certain overloaded + # operations. This does exactly this, so that sub classes can simple inherit + # it or override with their own integer conversion routine. + $_[0]->copy(); + } + +sub as_hex + { + # return as hex string, with prefixed 0x + my $x = shift; $x = $class->new($x) if !ref($x); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc + + my $s = ''; + $s = $x->{sign} if $x->{sign} eq '-'; + $s . $CALC->_as_hex($x->{value}); + } + +sub as_bin + { + # return as binary string, with prefixed 0b + my $x = shift; $x = $class->new($x) if !ref($x); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc + + my $s = ''; $s = $x->{sign} if $x->{sign} eq '-'; + return $s . $CALC->_as_bin($x->{value}); + } + +sub as_oct + { + # return as octal string, with prefixed 0 + my $x = shift; $x = $class->new($x) if !ref($x); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc + + my $s = ''; $s = $x->{sign} if $x->{sign} eq '-'; + return $s . $CALC->_as_oct($x->{value}); + } + +############################################################################## +# private stuff (internal use only) + +sub objectify + { + # check for strings, if yes, return objects instead + + # the first argument is number of args objectify() should look at it will + # return $count+1 elements, the first will be a classname. This is because + # overloaded '""' calls bstr($object,undef,undef) and this would result in + # useless objects being created and thrown away. So we cannot simple loop + # over @_. If the given count is 0, all arguments will be used. + + # If the second arg is a ref, use it as class. + # If not, try to use it as classname, unless undef, then use $class + # (aka Math::BigInt). The latter shouldn't happen,though. + + # caller: gives us: + # $x->badd(1); => ref x, scalar y + # Class->badd(1,2); => classname x (scalar), scalar x, scalar y + # Class->badd( Class->(1),2); => classname x (scalar), ref x, scalar y + # Math::BigInt::badd(1,2); => scalar x, scalar y + # In the last case we check number of arguments to turn it silently into + # $class,1,2. (We can not take '1' as class ;o) + # badd($class,1) is not supported (it should, eventually, try to add undef) + # currently it tries 'Math::BigInt' + 1, which will not work. + + # some shortcut for the common cases + # $x->unary_op(); + return (ref($_[1]),$_[1]) if (@_ == 2) && ($_[0]||0 == 1) && ref($_[1]); + + my $count = abs(shift || 0); + + my (@a,$k,$d); # resulting array, temp, and downgrade + if (ref $_[0]) + { + # okay, got object as first + $a[0] = ref $_[0]; + } + else + { + # nope, got 1,2 (Class->xxx(1) => Class,1 and not supported) + $a[0] = $class; + $a[0] = shift if $_[0] =~ /^[A-Z].*::/; # classname as first? + } + + no strict 'refs'; + # disable downgrading, because Math::BigFLoat->foo('1.0','2.0') needs floats + if (defined ${"$a[0]::downgrade"}) + { + $d = ${"$a[0]::downgrade"}; + ${"$a[0]::downgrade"} = undef; + } + + my $up = ${"$a[0]::upgrade"}; + # print STDERR "# Now in objectify, my class is today $a[0], count = $count\n"; + if ($count == 0) + { + while (@_) + { + $k = shift; + if (!ref($k)) + { + $k = $a[0]->new($k); + } + elsif (!defined $up && ref($k) ne $a[0]) + { + # foreign object, try to convert to integer + $k->can('as_number') ? $k = $k->as_number() : $k = $a[0]->new($k); + } + push @a,$k; + } + } + else + { + while ($count > 0) + { + $count--; + $k = shift; + if (!ref($k)) + { + $k = $a[0]->new($k); + } + elsif (!defined $up && ref($k) ne $a[0]) + { + # foreign object, try to convert to integer + $k->can('as_number') ? $k = $k->as_number() : $k = $a[0]->new($k); + } + push @a,$k; + } + push @a,@_; # return other params, too + } + if (! wantarray) + { + require Carp; Carp::croak ("$class objectify needs list context"); + } + ${"$a[0]::downgrade"} = $d; + @a; + } + +sub _register_callback + { + my ($class,$callback) = @_; + + if (ref($callback) ne 'CODE') + { + require Carp; + Carp::croak ("$callback is not a coderef"); + } + $CALLBACKS{$class} = $callback; + } + +sub import + { + my $self = shift; + + $IMPORT++; # remember we did import() + my @a; my $l = scalar @_; + my $warn_or_die = 0; # 0 - no warn, 1 - warn, 2 - die + for ( my $i = 0; $i < $l ; $i++ ) + { + if ($_[$i] eq ':constant') + { + # this causes overlord er load to step in + overload::constant + integer => sub { $self->new(shift) }, + binary => sub { $self->new(shift) }; + } + elsif ($_[$i] eq 'upgrade') + { + # this causes upgrading + $upgrade = $_[$i+1]; # or undef to disable + $i++; + } + elsif ($_[$i] =~ /^(lib|try|only)\z/) + { + # this causes a different low lib to take care... + $CALC = $_[$i+1] || ''; + # lib => 1 (warn on fallback), try => 0 (no warn), only => 2 (die on fallback) + $warn_or_die = 1 if $_[$i] eq 'lib'; + $warn_or_die = 2 if $_[$i] eq 'only'; + $i++; + } + else + { + push @a, $_[$i]; + } + } + # any non :constant stuff is handled by our parent, Exporter + if (@a > 0) + { + require Exporter; + + $self->SUPER::import(@a); # need it for subclasses + $self->export_to_level(1,$self,@a); # need it for MBF + } + + # try to load core math lib + my @c = split /\s*,\s*/,$CALC; + foreach (@c) + { + $_ =~ tr/a-zA-Z0-9://cd; # limit to sane characters + } + push @c, \'FastCalc', \'Calc' # if all fail, try these + if $warn_or_die < 2; # but not for "only" + $CALC = ''; # signal error + foreach my $l (@c) + { + # fallback libraries are "marked" as \'string', extract string if nec. + my $lib = $l; $lib = $$l if ref($l); + + next if ($lib || '') eq ''; + $lib = 'Math::BigInt::'.$lib if $lib !~ /^Math::BigInt/i; + $lib =~ s/\.pm$//; + if ($] < 5.006) + { + # Perl < 5.6.0 dies with "out of memory!" when eval("") and ':constant' is + # used in the same script, or eval("") inside import(). + my @parts = split /::/, $lib; # Math::BigInt => Math BigInt + my $file = pop @parts; $file .= '.pm'; # BigInt => BigInt.pm + require File::Spec; + $file = File::Spec->catfile (@parts, $file); + eval { require "$file"; $lib->import( @c ); } + } + else + { + eval "use $lib qw/@c/;"; + } + if ($@ eq '') + { + my $ok = 1; + # loaded it ok, see if the api_version() is high enough + if ($lib->can('api_version') && $lib->api_version() >= 1.0) + { + $ok = 0; + # api_version matches, check if it really provides anything we need + for my $method (qw/ + one two ten + str num + add mul div sub dec inc + acmp len digit is_one is_zero is_even is_odd + is_two is_ten + zeros new copy check + from_hex from_oct from_bin as_hex as_bin as_oct + rsft lsft xor and or + mod sqrt root fac pow modinv modpow log_int gcd + /) + { + if (!$lib->can("_$method")) + { + if (($WARN{$lib}||0) < 2) + { + require Carp; + Carp::carp ("$lib is missing method '_$method'"); + $WARN{$lib} = 1; # still warn about the lib + } + $ok++; last; + } + } + } + if ($ok == 0) + { + $CALC = $lib; + if ($warn_or_die > 0 && ref($l)) + { + require Carp; + my $msg = "Math::BigInt: couldn't load specified math lib(s), fallback to $lib"; + Carp::carp ($msg) if $warn_or_die == 1; + Carp::croak ($msg) if $warn_or_die == 2; + } + last; # found a usable one, break + } + else + { + if (($WARN{$lib}||0) < 2) + { + my $ver = eval "\$$lib\::VERSION" || 'unknown'; + require Carp; + Carp::carp ("Cannot load outdated $lib v$ver, please upgrade"); + $WARN{$lib} = 2; # never warn again + } + } + } + } + if ($CALC eq '') + { + require Carp; + if ($warn_or_die == 2) + { + Carp::croak ("Couldn't load specified math lib(s) and fallback disallowed"); + } + else + { + Carp::croak ("Couldn't load any math lib(s), not even fallback to Calc.pm"); + } + } + + # notify callbacks + foreach my $class (keys %CALLBACKS) + { + &{$CALLBACKS{$class}}($CALC); + } + + # Fill $CAN with the results of $CALC->can(...) for emulating lower math lib + # functions + + %CAN = (); + for my $method (qw/ signed_and signed_or signed_xor /) + { + $CAN{$method} = $CALC->can("_$method") ? 1 : 0; + } + + # import done + } + +sub from_hex + { + # create a bigint from a hexadecimal string + my ($self, $hs) = @_; + + my $rc = __from_hex($hs); + + return $self->bnan() unless defined $rc; + + $rc; + } + +sub from_bin + { + # create a bigint from a hexadecimal string + my ($self, $bs) = @_; + + my $rc = __from_bin($bs); + + return $self->bnan() unless defined $rc; + + $rc; + } + +sub from_oct + { + # create a bigint from a hexadecimal string + my ($self, $os) = @_; + + my $x = $self->bzero(); + + # strip underscores + $os =~ s/([0-7])_([0-7])/$1$2/g; + $os =~ s/([0-7])_([0-7])/$1$2/g; + + return $x->bnan() if $os !~ /^[\-\+]?0[0-7]+\z/; + + my $sign = '+'; $sign = '-' if $os =~ /^-/; + + $os =~ s/^[+-]//; # strip sign + $x->{value} = $CALC->_from_oct($os); + $x->{sign} = $sign unless $CALC->_is_zero($x->{value}); # no '-0' + $x; + } + +sub __from_hex + { + # internal + # convert a (ref to) big hex string to BigInt, return undef for error + my $hs = shift; + + my $x = Math::BigInt->bzero(); + + # strip underscores + $hs =~ s/([0-9a-fA-F])_([0-9a-fA-F])/$1$2/g; + $hs =~ s/([0-9a-fA-F])_([0-9a-fA-F])/$1$2/g; + + return $x->bnan() if $hs !~ /^[\-\+]?0x[0-9A-Fa-f]+$/; + + my $sign = '+'; $sign = '-' if $hs =~ /^-/; + + $hs =~ s/^[+-]//; # strip sign + $x->{value} = $CALC->_from_hex($hs); + $x->{sign} = $sign unless $CALC->_is_zero($x->{value}); # no '-0' + $x; + } + +sub __from_bin + { + # internal + # convert a (ref to) big binary string to BigInt, return undef for error + my $bs = shift; + + my $x = Math::BigInt->bzero(); + + # strip underscores + $bs =~ s/([01])_([01])/$1$2/g; + $bs =~ s/([01])_([01])/$1$2/g; + return $x->bnan() if $bs !~ /^[+-]?0b[01]+$/; + + my $sign = '+'; $sign = '-' if $bs =~ /^\-/; + $bs =~ s/^[+-]//; # strip sign + + $x->{value} = $CALC->_from_bin($bs); + $x->{sign} = $sign unless $CALC->_is_zero($x->{value}); # no '-0' + $x; + } + +sub _split + { + # input: num_str; output: undef for invalid or + # (\$mantissa_sign,\$mantissa_value,\$mantissa_fraction,\$exp_sign,\$exp_value) + # Internal, take apart a string and return the pieces. + # Strip leading/trailing whitespace, leading zeros, underscore and reject + # invalid input. + my $x = shift; + + # strip white space at front, also extranous leading zeros + $x =~ s/^\s*([-]?)0*([0-9])/$1$2/g; # will not strip ' .2' + $x =~ s/^\s+//; # but this will + $x =~ s/\s+$//g; # strip white space at end + + # shortcut, if nothing to split, return early + if ($x =~ /^[+-]?[0-9]+\z/) + { + $x =~ s/^([+-])0*([0-9])/$2/; my $sign = $1 || '+'; + return (\$sign, \$x, \'', \'', \0); + } + + # invalid starting char? + return if $x !~ /^[+-]?(\.?[0-9]|0b[0-1]|0x[0-9a-fA-F])/; + + return __from_hex($x) if $x =~ /^[\-\+]?0x/; # hex string + return __from_bin($x) if $x =~ /^[\-\+]?0b/; # binary string + + # strip underscores between digits + $x =~ s/([0-9])_([0-9])/$1$2/g; + $x =~ s/([0-9])_([0-9])/$1$2/g; # do twice for 1_2_3 + + # some possible inputs: + # 2.1234 # 0.12 # 1 # 1E1 # 2.134E1 # 434E-10 # 1.02009E-2 + # .2 # 1_2_3.4_5_6 # 1.4E1_2_3 # 1e3 # +.2 # 0e999 + + my ($m,$e,$last) = split /[Ee]/,$x; + return if defined $last; # last defined => 1e2E3 or others + $e = '0' if !defined $e || $e eq ""; + + # sign,value for exponent,mantint,mantfrac + my ($es,$ev,$mis,$miv,$mfv); + # valid exponent? + if ($e =~ /^([+-]?)0*([0-9]+)$/) # strip leading zeros + { + $es = $1; $ev = $2; + # valid mantissa? + return if $m eq '.' || $m eq ''; + my ($mi,$mf,$lastf) = split /\./,$m; + return if defined $lastf; # lastf defined => 1.2.3 or others + $mi = '0' if !defined $mi; + $mi .= '0' if $mi =~ /^[\-\+]?$/; + $mf = '0' if !defined $mf || $mf eq ''; + if ($mi =~ /^([+-]?)0*([0-9]+)$/) # strip leading zeros + { + $mis = $1||'+'; $miv = $2; + return unless ($mf =~ /^([0-9]*?)0*$/); # strip trailing zeros + $mfv = $1; + # handle the 0e999 case here + $ev = 0 if $miv eq '0' && $mfv eq ''; + return (\$mis,\$miv,\$mfv,\$es,\$ev); + } + } + return; # NaN, not a number + } + +############################################################################## +# internal calculation routines (others are in Math::BigInt::Calc etc) + +sub __lcm + { + # (BINT or num_str, BINT or num_str) return BINT + # does modify first argument + # LCM + + my ($x,$ty) = @_; + return $x->bnan() if ($x->{sign} eq $nan) || ($ty->{sign} eq $nan); + my $method = ref($x) . '::bgcd'; + no strict 'refs'; + $x * $ty / &$method($x,$ty); + } + +############################################################################### +# trigonometric functions + +sub bpi + { + # Calculate PI to N digits. Unless upgrading is in effect, returns the + # result truncated to an integer, that is, always returns '3'. + my ($self,$n) = @_; + if (@_ == 1) + { + # called like Math::BigInt::bpi(10); + $n = $self; $self = $class; + } + $self = ref($self) if ref($self); + + return $upgrade->new($n) if defined $upgrade; + + # hard-wired to "3" + $self->new(3); + } + +sub bcos + { + # Calculate cosinus(x) to N digits. Unless upgrading is in effect, returns the + # result truncated to an integer. + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + return $x if $x->modify('bcos'); + + return $x->bnan() if $x->{sign} !~ /^[+-]\z/; # -inf +inf or NaN => NaN + + return $upgrade->new($x)->bcos(@r) if defined $upgrade; + + require Math::BigFloat; + # calculate the result and truncate it to integer + my $t = Math::BigFloat->new($x)->bcos(@r)->as_int(); + + $x->bone() if $t->is_one(); + $x->bzero() if $t->is_zero(); + $x->round(@r); + } + +sub bsin + { + # Calculate sinus(x) to N digits. Unless upgrading is in effect, returns the + # result truncated to an integer. + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + return $x if $x->modify('bsin'); + + return $x->bnan() if $x->{sign} !~ /^[+-]\z/; # -inf +inf or NaN => NaN + + return $upgrade->new($x)->bsin(@r) if defined $upgrade; + + require Math::BigFloat; + # calculate the result and truncate it to integer + my $t = Math::BigFloat->new($x)->bsin(@r)->as_int(); + + $x->bone() if $t->is_one(); + $x->bzero() if $t->is_zero(); + $x->round(@r); + } + +sub batan2 + { + # calculate arcus tangens of ($y/$x) + + # set up parameters + my ($self,$y,$x,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$y,$x,@r) = objectify(2,@_); + } + + return $y if $y->modify('batan2'); + + return $y->bnan() if ($y->{sign} eq $nan) || ($x->{sign} eq $nan); + + # Y X + # != 0 -inf result is +- pi + if ($x->is_inf() || $y->is_inf()) + { + # upgrade to BigFloat etc. + return $upgrade->new($y)->batan2($upgrade->new($x),@r) if defined $upgrade; + if ($y->is_inf()) + { + if ($x->{sign} eq '-inf') + { + # calculate 3 pi/4 => 2.3.. => 2 + $y->bone( substr($y->{sign},0,1) ); + $y->bmul($self->new(2)); + } + elsif ($x->{sign} eq '+inf') + { + # calculate pi/4 => 0.7 => 0 + $y->bzero(); + } + else + { + # calculate pi/2 => 1.5 => 1 + $y->bone( substr($y->{sign},0,1) ); + } + } + else + { + if ($x->{sign} eq '+inf') + { + # calculate pi/4 => 0.7 => 0 + $y->bzero(); + } + else + { + # PI => 3.1415.. => 3 + $y->bone( substr($y->{sign},0,1) ); + $y->bmul($self->new(3)); + } + } + return $y; + } + + return $upgrade->new($y)->batan2($upgrade->new($x),@r) if defined $upgrade; + + require Math::BigFloat; + my $r = Math::BigFloat->new($y)->batan2(Math::BigFloat->new($x),@r)->as_int(); + + $x->{value} = $r->{value}; + $x->{sign} = $r->{sign}; + + $x; + } + +sub batan + { + # Calculate arcus tangens of x to N digits. Unless upgrading is in effect, returns the + # result truncated to an integer. + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + return $x if $x->modify('batan'); + + return $x->bnan() if $x->{sign} !~ /^[+-]\z/; # -inf +inf or NaN => NaN + + return $upgrade->new($x)->batan(@r) if defined $upgrade; + + # calculate the result and truncate it to integer + my $t = Math::BigFloat->new($x)->batan(@r); + + $x->{value} = $CALC->_new( $x->as_int()->bstr() ); + $x->round(@r); + } + +############################################################################### +# this method returns 0 if the object can be modified, or 1 if not. +# We use a fast constant sub() here, to avoid costly calls. Subclasses +# may override it with special code (f.i. Math::BigInt::Constant does so) + +sub modify () { 0; } + +1; +__END__ + +=pod + +=head1 NAME + +Math::BigInt - Arbitrary size integer/float math package + +=head1 SYNOPSIS + + use Math::BigInt; + + # or make it faster with huge numbers: install (optional) + # Math::BigInt::GMP and always use (it will fall back to + # pure Perl if the GMP library is not installed): + # (See also the L<MATH LIBRARY> section!) + + # will warn if Math::BigInt::GMP cannot be found + use Math::BigInt lib => 'GMP'; + + # to supress the warning use this: + # use Math::BigInt try => 'GMP'; + + # dies if GMP cannot be loaded: + # use Math::BigInt only => 'GMP'; + + my $str = '1234567890'; + my @values = (64,74,18); + my $n = 1; my $sign = '-'; + + # Number creation + my $x = Math::BigInt->new($str); # defaults to 0 + my $y = $x->copy(); # make a true copy + my $nan = Math::BigInt->bnan(); # create a NotANumber + my $zero = Math::BigInt->bzero(); # create a +0 + my $inf = Math::BigInt->binf(); # create a +inf + my $inf = Math::BigInt->binf('-'); # create a -inf + my $one = Math::BigInt->bone(); # create a +1 + my $mone = Math::BigInt->bone('-'); # create a -1 + + my $pi = Math::BigInt->bpi(); # returns '3' + # see Math::BigFloat::bpi() + + $h = Math::BigInt->new('0x123'); # from hexadecimal + $b = Math::BigInt->new('0b101'); # from binary + $o = Math::BigInt->from_oct('0101'); # from octal + + # Testing (don't modify their arguments) + # (return true if the condition is met, otherwise false) + + $x->is_zero(); # if $x is +0 + $x->is_nan(); # if $x is NaN + $x->is_one(); # if $x is +1 + $x->is_one('-'); # if $x is -1 + $x->is_odd(); # if $x is odd + $x->is_even(); # if $x is even + $x->is_pos(); # if $x >= 0 + $x->is_neg(); # if $x < 0 + $x->is_inf($sign); # if $x is +inf, or -inf (sign is default '+') + $x->is_int(); # if $x is an integer (not a float) + + # comparing and digit/sign extraction + $x->bcmp($y); # compare numbers (undef,<0,=0,>0) + $x->bacmp($y); # compare absolutely (undef,<0,=0,>0) + $x->sign(); # return the sign, either +,- or NaN + $x->digit($n); # return the nth digit, counting from right + $x->digit(-$n); # return the nth digit, counting from left + + # The following all modify their first argument. If you want to preserve + # $x, use $z = $x->copy()->bXXX($y); See under L<CAVEATS> for why this is + # necessary when mixing $a = $b assignments with non-overloaded math. + + $x->bzero(); # set $x to 0 + $x->bnan(); # set $x to NaN + $x->bone(); # set $x to +1 + $x->bone('-'); # set $x to -1 + $x->binf(); # set $x to inf + $x->binf('-'); # set $x to -inf + + $x->bneg(); # negation + $x->babs(); # absolute value + $x->bnorm(); # normalize (no-op in BigInt) + $x->bnot(); # two's complement (bit wise not) + $x->binc(); # increment $x by 1 + $x->bdec(); # decrement $x by 1 + + $x->badd($y); # addition (add $y to $x) + $x->bsub($y); # subtraction (subtract $y from $x) + $x->bmul($y); # multiplication (multiply $x by $y) + $x->bdiv($y); # divide, set $x to quotient + # return (quo,rem) or quo if scalar + + $x->bmuladd($y,$z); # $x = $x * $y + $z + + $x->bmod($y); # modulus (x % y) + $x->bmodpow($exp,$mod); # modular exponentation (($num**$exp) % $mod)) + $x->bmodinv($mod); # the inverse of $x in the given modulus $mod + + $x->bpow($y); # power of arguments (x ** y) + $x->blsft($y); # left shift in base 2 + $x->brsft($y); # right shift in base 2 + # returns (quo,rem) or quo if in scalar context + $x->blsft($y,$n); # left shift by $y places in base $n + $x->brsft($y,$n); # right shift by $y places in base $n + # returns (quo,rem) or quo if in scalar context + + $x->band($y); # bitwise and + $x->bior($y); # bitwise inclusive or + $x->bxor($y); # bitwise exclusive or + $x->bnot(); # bitwise not (two's complement) + + $x->bsqrt(); # calculate square-root + $x->broot($y); # $y'th root of $x (e.g. $y == 3 => cubic root) + $x->bfac(); # factorial of $x (1*2*3*4*..$x) + + $x->bnok($y); # x over y (binomial coefficient n over k) + + $x->blog(); # logarithm of $x to base e (Euler's number) + $x->blog($base); # logarithm of $x to base $base (f.i. 2) + $x->bexp(); # calculate e ** $x where e is Euler's number + + $x->round($A,$P,$mode); # round to accuracy or precision using mode $mode + $x->bround($n); # accuracy: preserve $n digits + $x->bfround($n); # $n > 0: round $nth digits, + # $n < 0: round to the $nth digit after the + # dot, no-op for BigInts + + # The following do not modify their arguments in BigInt (are no-ops), + # but do so in BigFloat: + + $x->bfloor(); # return integer less or equal than $x + $x->bceil(); # return integer greater or equal than $x + + # The following do not modify their arguments: + + # greatest common divisor (no OO style) + my $gcd = Math::BigInt::bgcd(@values); + # lowest common multiplicator (no OO style) + my $lcm = Math::BigInt::blcm(@values); + + $x->length(); # return number of digits in number + ($xl,$f) = $x->length(); # length of number and length of fraction part, + # latter is always 0 digits long for BigInts + + $x->exponent(); # return exponent as BigInt + $x->mantissa(); # return (signed) mantissa as BigInt + $x->parts(); # return (mantissa,exponent) as BigInt + $x->copy(); # make a true copy of $x (unlike $y = $x;) + $x->as_int(); # return as BigInt (in BigInt: same as copy()) + $x->numify(); # return as scalar (might overflow!) + + # conversation to string (do not modify their argument) + $x->bstr(); # normalized string (e.g. '3') + $x->bsstr(); # norm. string in scientific notation (e.g. '3E0') + $x->as_hex(); # as signed hexadecimal string with prefixed 0x + $x->as_bin(); # as signed binary string with prefixed 0b + $x->as_oct(); # as signed octal string with prefixed 0 + + + # precision and accuracy (see section about rounding for more) + $x->precision(); # return P of $x (or global, if P of $x undef) + $x->precision($n); # set P of $x to $n + $x->accuracy(); # return A of $x (or global, if A of $x undef) + $x->accuracy($n); # set A $x to $n + + # Global methods + Math::BigInt->precision(); # get/set global P for all BigInt objects + Math::BigInt->accuracy(); # get/set global A for all BigInt objects + Math::BigInt->round_mode(); # get/set global round mode, one of + # 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common' + Math::BigInt->config(); # return hash containing configuration + +=head1 DESCRIPTION + +All operators (including basic math operations) are overloaded if you +declare your big integers as + + $i = new Math::BigInt '123_456_789_123_456_789'; + +Operations with overloaded operators preserve the arguments which is +exactly what you expect. + +=over 2 + +=item Input + +Input values to these routines may be any string, that looks like a number +and results in an integer, including hexadecimal and binary numbers. + +Scalars holding numbers may also be passed, but note that non-integer numbers +may already have lost precision due to the conversation to float. Quote +your input if you want BigInt to see all the digits: + + $x = Math::BigInt->new(12345678890123456789); # bad + $x = Math::BigInt->new('12345678901234567890'); # good + +You can include one underscore between any two digits. + +This means integer values like 1.01E2 or even 1000E-2 are also accepted. +Non-integer values result in NaN. + +Hexadecimal (prefixed with "0x") and binary numbers (prefixed with "0b") +are accepted, too. Please note that octal numbers are not recognized +by new(), so the following will print "123": + + perl -MMath::BigInt -le 'print Math::BigInt->new("0123")' + +To convert an octal number, use from_oct(); + + perl -MMath::BigInt -le 'print Math::BigInt->from_oct("0123")' + +Currently, Math::BigInt::new() defaults to 0, while Math::BigInt::new('') +results in 'NaN'. This might change in the future, so use always the following +explicit forms to get a zero or NaN: + + $zero = Math::BigInt->bzero(); + $nan = Math::BigInt->bnan(); + +C<bnorm()> on a BigInt object is now effectively a no-op, since the numbers +are always stored in normalized form. If passed a string, creates a BigInt +object from the input. + +=item Output + +Output values are BigInt objects (normalized), except for the methods which +return a string (see L<SYNOPSIS>). + +Some routines (C<is_odd()>, C<is_even()>, C<is_zero()>, C<is_one()>, +C<is_nan()>, etc.) return true or false, while others (C<bcmp()>, C<bacmp()>) +return either undef (if NaN is involved), <0, 0 or >0 and are suited for sort. + +=back + +=head1 METHODS + +Each of the methods below (except config(), accuracy() and precision()) +accepts three additional parameters. These arguments C<$A>, C<$P> and C<$R> +are C<accuracy>, C<precision> and C<round_mode>. Please see the section about +L<ACCURACY and PRECISION> for more information. + +=head2 config() + + use Data::Dumper; + + print Dumper ( Math::BigInt->config() ); + print Math::BigInt->config()->{lib},"\n"; + +Returns a hash containing the configuration, e.g. the version number, lib +loaded etc. The following hash keys are currently filled in with the +appropriate information. + + key Description + Example + ============================================================ + lib Name of the low-level math library + Math::BigInt::Calc + lib_version Version of low-level math library (see 'lib') + 0.30 + class The class name of config() you just called + Math::BigInt + upgrade To which class math operations might be upgraded + Math::BigFloat + downgrade To which class math operations might be downgraded + undef + precision Global precision + undef + accuracy Global accuracy + undef + round_mode Global round mode + even + version version number of the class you used + 1.61 + div_scale Fallback accuracy for div + 40 + trap_nan If true, traps creation of NaN via croak() + 1 + trap_inf If true, traps creation of +inf/-inf via croak() + 1 + +The following values can be set by passing C<config()> a reference to a hash: + + trap_inf trap_nan + upgrade downgrade precision accuracy round_mode div_scale + +Example: + + $new_cfg = Math::BigInt->config( { trap_inf => 1, precision => 5 } ); + +=head2 accuracy() + + $x->accuracy(5); # local for $x + CLASS->accuracy(5); # global for all members of CLASS + # Note: This also applies to new()! + + $A = $x->accuracy(); # read out accuracy that affects $x + $A = CLASS->accuracy(); # read out global accuracy + +Set or get the global or local accuracy, aka how many significant digits the +results have. If you set a global accuracy, then this also applies to new()! + +Warning! The accuracy I<sticks>, e.g. once you created a number under the +influence of C<< CLASS->accuracy($A) >>, all results from math operations with +that number will also be rounded. + +In most cases, you should probably round the results explicitly using one of +L<round()>, L<bround()> or L<bfround()> or by passing the desired accuracy +to the math operation as additional parameter: + + my $x = Math::BigInt->new(30000); + my $y = Math::BigInt->new(7); + print scalar $x->copy()->bdiv($y, 2); # print 4300 + print scalar $x->copy()->bdiv($y)->bround(2); # print 4300 + +Please see the section about L<ACCURACY AND PRECISION> for further details. + +Value must be greater than zero. Pass an undef value to disable it: + + $x->accuracy(undef); + Math::BigInt->accuracy(undef); + +Returns the current accuracy. For C<$x->accuracy()> it will return either the +local accuracy, or if not defined, the global. This means the return value +represents the accuracy that will be in effect for $x: + + $y = Math::BigInt->new(1234567); # unrounded + print Math::BigInt->accuracy(4),"\n"; # set 4, print 4 + $x = Math::BigInt->new(123456); # $x will be automatically rounded! + print "$x $y\n"; # '123500 1234567' + print $x->accuracy(),"\n"; # will be 4 + print $y->accuracy(),"\n"; # also 4, since global is 4 + print Math::BigInt->accuracy(5),"\n"; # set to 5, print 5 + print $x->accuracy(),"\n"; # still 4 + print $y->accuracy(),"\n"; # 5, since global is 5 + +Note: Works also for subclasses like Math::BigFloat. Each class has it's own +globals separated from Math::BigInt, but it is possible to subclass +Math::BigInt and make the globals of the subclass aliases to the ones from +Math::BigInt. + +=head2 precision() + + $x->precision(-2); # local for $x, round at the second digit right of the dot + $x->precision(2); # ditto, round at the second digit left of the dot + + CLASS->precision(5); # Global for all members of CLASS + # This also applies to new()! + CLASS->precision(-5); # ditto + + $P = CLASS->precision(); # read out global precision + $P = $x->precision(); # read out precision that affects $x + +Note: You probably want to use L<accuracy()> instead. With L<accuracy> you +set the number of digits each result should have, with L<precision> you +set the place where to round! + +C<precision()> sets or gets the global or local precision, aka at which digit +before or after the dot to round all results. A set global precision also +applies to all newly created numbers! + +In Math::BigInt, passing a negative number precision has no effect since no +numbers have digits after the dot. In L<Math::BigFloat>, it will round all +results to P digits after the dot. + +Please see the section about L<ACCURACY AND PRECISION> for further details. + +Pass an undef value to disable it: + + $x->precision(undef); + Math::BigInt->precision(undef); + +Returns the current precision. For C<$x->precision()> it will return either the +local precision of $x, or if not defined, the global. This means the return +value represents the prevision that will be in effect for $x: + + $y = Math::BigInt->new(1234567); # unrounded + print Math::BigInt->precision(4),"\n"; # set 4, print 4 + $x = Math::BigInt->new(123456); # will be automatically rounded + print $x; # print "120000"! + +Note: Works also for subclasses like L<Math::BigFloat>. Each class has its +own globals separated from Math::BigInt, but it is possible to subclass +Math::BigInt and make the globals of the subclass aliases to the ones from +Math::BigInt. + +=head2 brsft() + + $x->brsft($y,$n); + +Shifts $x right by $y in base $n. Default is base 2, used are usually 10 and +2, but others work, too. + +Right shifting usually amounts to dividing $x by $n ** $y and truncating the +result: + + + $x = Math::BigInt->new(10); + $x->brsft(1); # same as $x >> 1: 5 + $x = Math::BigInt->new(1234); + $x->brsft(2,10); # result 12 + +There is one exception, and that is base 2 with negative $x: + + + $x = Math::BigInt->new(-5); + print $x->brsft(1); + +This will print -3, not -2 (as it would if you divide -5 by 2 and truncate the +result). + +=head2 new() + + $x = Math::BigInt->new($str,$A,$P,$R); + +Creates a new BigInt object from a scalar or another BigInt object. The +input is accepted as decimal, hex (with leading '0x') or binary (with leading +'0b'). + +See L<Input> for more info on accepted input formats. + +=head2 from_oct() + + $x = Math::BigInt->from_oct("0775"); # input is octal + +=head2 from_hex() + + $x = Math::BigInt->from_hex("0xcafe"); # input is hexadecimal + +=head2 from_bin() + + $x = Math::BigInt->from_oct("0x10011"); # input is binary + +=head2 bnan() + + $x = Math::BigInt->bnan(); + +Creates a new BigInt object representing NaN (Not A Number). +If used on an object, it will set it to NaN: + + $x->bnan(); + +=head2 bzero() + + $x = Math::BigInt->bzero(); + +Creates a new BigInt object representing zero. +If used on an object, it will set it to zero: + + $x->bzero(); + +=head2 binf() + + $x = Math::BigInt->binf($sign); + +Creates a new BigInt object representing infinity. The optional argument is +either '-' or '+', indicating whether you want infinity or minus infinity. +If used on an object, it will set it to infinity: + + $x->binf(); + $x->binf('-'); + +=head2 bone() + + $x = Math::BigInt->binf($sign); + +Creates a new BigInt object representing one. The optional argument is +either '-' or '+', indicating whether you want one or minus one. +If used on an object, it will set it to one: + + $x->bone(); # +1 + $x->bone('-'); # -1 + +=head2 is_one()/is_zero()/is_nan()/is_inf() + + + $x->is_zero(); # true if arg is +0 + $x->is_nan(); # true if arg is NaN + $x->is_one(); # true if arg is +1 + $x->is_one('-'); # true if arg is -1 + $x->is_inf(); # true if +inf + $x->is_inf('-'); # true if -inf (sign is default '+') + +These methods all test the BigInt for being one specific value and return +true or false depending on the input. These are faster than doing something +like: + + if ($x == 0) + +=head2 is_pos()/is_neg()/is_positive()/is_negative() + + $x->is_pos(); # true if > 0 + $x->is_neg(); # true if < 0 + +The methods return true if the argument is positive or negative, respectively. +C<NaN> is neither positive nor negative, while C<+inf> counts as positive, and +C<-inf> is negative. A C<zero> is neither positive nor negative. + +These methods are only testing the sign, and not the value. + +C<is_positive()> and C<is_negative()> are aliases to C<is_pos()> and +C<is_neg()>, respectively. C<is_positive()> and C<is_negative()> were +introduced in v1.36, while C<is_pos()> and C<is_neg()> were only introduced +in v1.68. + +=head2 is_odd()/is_even()/is_int() + + $x->is_odd(); # true if odd, false for even + $x->is_even(); # true if even, false for odd + $x->is_int(); # true if $x is an integer + +The return true when the argument satisfies the condition. C<NaN>, C<+inf>, +C<-inf> are not integers and are neither odd nor even. + +In BigInt, all numbers except C<NaN>, C<+inf> and C<-inf> are integers. + +=head2 bcmp() + + $x->bcmp($y); + +Compares $x with $y and takes the sign into account. +Returns -1, 0, 1 or undef. + +=head2 bacmp() + + $x->bacmp($y); + +Compares $x with $y while ignoring their. Returns -1, 0, 1 or undef. + +=head2 sign() + + $x->sign(); + +Return the sign, of $x, meaning either C<+>, C<->, C<-inf>, C<+inf> or NaN. + +If you want $x to have a certain sign, use one of the following methods: + + $x->babs(); # '+' + $x->babs()->bneg(); # '-' + $x->bnan(); # 'NaN' + $x->binf(); # '+inf' + $x->binf('-'); # '-inf' + +=head2 digit() + + $x->digit($n); # return the nth digit, counting from right + +If C<$n> is negative, returns the digit counting from left. + +=head2 bneg() + + $x->bneg(); + +Negate the number, e.g. change the sign between '+' and '-', or between '+inf' +and '-inf', respectively. Does nothing for NaN or zero. + +=head2 babs() + + $x->babs(); + +Set the number to its absolute value, e.g. change the sign from '-' to '+' +and from '-inf' to '+inf', respectively. Does nothing for NaN or positive +numbers. + +=head2 bnorm() + + $x->bnorm(); # normalize (no-op) + +=head2 bnot() + + $x->bnot(); + +Two's complement (bitwise not). This is equivalent to + + $x->binc()->bneg(); + +but faster. + +=head2 binc() + + $x->binc(); # increment x by 1 + +=head2 bdec() + + $x->bdec(); # decrement x by 1 + +=head2 badd() + + $x->badd($y); # addition (add $y to $x) + +=head2 bsub() + + $x->bsub($y); # subtraction (subtract $y from $x) + +=head2 bmul() + + $x->bmul($y); # multiplication (multiply $x by $y) + +=head2 bmuladd() + + $x->bmuladd($y,$z); + +Multiply $x by $y, and then add $z to the result, + +This method was added in v1.87 of Math::BigInt (June 2007). + +=head2 bdiv() + + $x->bdiv($y); # divide, set $x to quotient + # return (quo,rem) or quo if scalar + +=head2 bmod() + + $x->bmod($y); # modulus (x % y) + +=head2 bmodinv() + + num->bmodinv($mod); # modular inverse + +Returns the inverse of C<$num> in the given modulus C<$mod>. 'C<NaN>' is +returned unless C<$num> is relatively prime to C<$mod>, i.e. unless +C<bgcd($num, $mod)==1>. + +=head2 bmodpow() + + $num->bmodpow($exp,$mod); # modular exponentation + # ($num**$exp % $mod) + +Returns the value of C<$num> taken to the power C<$exp> in the modulus +C<$mod> using binary exponentation. C<bmodpow> is far superior to +writing + + $num ** $exp % $mod + +because it is much faster - it reduces internal variables into +the modulus whenever possible, so it operates on smaller numbers. + +C<bmodpow> also supports negative exponents. + + bmodpow($num, -1, $mod) + +is exactly equivalent to + + bmodinv($num, $mod) + +=head2 bpow() + + $x->bpow($y); # power of arguments (x ** y) + +=head2 blog() + + $x->blog($base, $accuracy); # logarithm of x to the base $base + +If C<$base> is not defined, Euler's number (e) is used: + + print $x->blog(undef, 100); # log(x) to 100 digits + +=head2 bexp() + + $x->bexp($accuracy); # calculate e ** X + +Calculates the expression C<e ** $x> where C<e> is Euler's number. + +This method was added in v1.82 of Math::BigInt (April 2007). + +See also L<blog()>. + +=head2 bnok() + + $x->bnok($y); # x over y (binomial coefficient n over k) + +Calculates the binomial coefficient n over k, also called the "choose" +function. The result is equivalent to: + + ( n ) n! + | - | = ------- + ( k ) k!(n-k)! + +This method was added in v1.84 of Math::BigInt (April 2007). + +=head2 bpi() + + print Math::BigInt->bpi(100), "\n"; # 3 + +Returns PI truncated to an integer, with the argument being ignored. This means +under BigInt this always returns C<3>. + +If upgrading is in effect, returns PI, rounded to N digits with the +current rounding mode: + + use Math::BigFloat; + use Math::BigInt upgrade => Math::BigFloat; + print Math::BigInt->bpi(3), "\n"; # 3.14 + print Math::BigInt->bpi(100), "\n"; # 3.1415.... + +This method was added in v1.87 of Math::BigInt (June 2007). + +=head2 bcos() + + my $x = Math::BigInt->new(1); + print $x->bcos(100), "\n"; + +Calculate the cosinus of $x, modifying $x in place. + +In BigInt, unless upgrading is in effect, the result is truncated to an +integer. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=head2 bsin() + + my $x = Math::BigInt->new(1); + print $x->bsin(100), "\n"; + +Calculate the sinus of $x, modifying $x in place. + +In BigInt, unless upgrading is in effect, the result is truncated to an +integer. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=head2 batan2() + + my $x = Math::BigInt->new(1); + my $y = Math::BigInt->new(1); + print $y->batan2($x), "\n"; + +Calculate the arcus tangens of C<$y> divided by C<$x>, modifying $y in place. + +In BigInt, unless upgrading is in effect, the result is truncated to an +integer. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=head2 batan() + + my $x = Math::BigFloat->new(0.5); + print $x->batan(100), "\n"; + +Calculate the arcus tangens of $x, modifying $x in place. + +In BigInt, unless upgrading is in effect, the result is truncated to an +integer. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=head2 blsft() + + $x->blsft($y); # left shift in base 2 + $x->blsft($y,$n); # left shift, in base $n (like 10) + +=head2 brsft() + + $x->brsft($y); # right shift in base 2 + $x->brsft($y,$n); # right shift, in base $n (like 10) + +=head2 band() + + $x->band($y); # bitwise and + +=head2 bior() + + $x->bior($y); # bitwise inclusive or + +=head2 bxor() + + $x->bxor($y); # bitwise exclusive or + +=head2 bnot() + + $x->bnot(); # bitwise not (two's complement) + +=head2 bsqrt() + + $x->bsqrt(); # calculate square-root + +=head2 broot() + + $x->broot($N); + +Calculates the N'th root of C<$x>. + +=head2 bfac() + + $x->bfac(); # factorial of $x (1*2*3*4*..$x) + +=head2 round() + + $x->round($A,$P,$round_mode); + +Round $x to accuracy C<$A> or precision C<$P> using the round mode +C<$round_mode>. + +=head2 bround() + + $x->bround($N); # accuracy: preserve $N digits + +=head2 bfround() + + $x->bfround($N); + +If N is > 0, rounds to the Nth digit from the left. If N < 0, rounds to +the Nth digit after the dot. Since BigInts are integers, the case N < 0 +is a no-op for them. + +Examples: + + Input N Result + =================================================== + 123456.123456 3 123500 + 123456.123456 2 123450 + 123456.123456 -2 123456.12 + 123456.123456 -3 123456.123 + +=head2 bfloor() + + $x->bfloor(); + +Set $x to the integer less or equal than $x. This is a no-op in BigInt, but +does change $x in BigFloat. + +=head2 bceil() + + $x->bceil(); + +Set $x to the integer greater or equal than $x. This is a no-op in BigInt, but +does change $x in BigFloat. + +=head2 bgcd() + + bgcd(@values); # greatest common divisor (no OO style) + +=head2 blcm() + + blcm(@values); # lowest common multiplicator (no OO style) + +head2 length() + + $x->length(); + ($xl,$fl) = $x->length(); + +Returns the number of digits in the decimal representation of the number. +In list context, returns the length of the integer and fraction part. For +BigInt's, the length of the fraction part will always be 0. + +=head2 exponent() + + $x->exponent(); + +Return the exponent of $x as BigInt. + +=head2 mantissa() + + $x->mantissa(); + +Return the signed mantissa of $x as BigInt. + +=head2 parts() + + $x->parts(); # return (mantissa,exponent) as BigInt + +=head2 copy() + + $x->copy(); # make a true copy of $x (unlike $y = $x;) + +=head2 as_int()/as_number() + + $x->as_int(); + +Returns $x as a BigInt (truncated towards zero). In BigInt this is the same as +C<copy()>. + +C<as_number()> is an alias to this method. C<as_number> was introduced in +v1.22, while C<as_int()> was only introduced in v1.68. + +=head2 bstr() + + $x->bstr(); + +Returns a normalized string representation of C<$x>. + +=head2 bsstr() + + $x->bsstr(); # normalized string in scientific notation + +=head2 as_hex() + + $x->as_hex(); # as signed hexadecimal string with prefixed 0x + +=head2 as_bin() + + $x->as_bin(); # as signed binary string with prefixed 0b + +=head2 as_oct() + + $x->as_oct(); # as signed octal string with prefixed 0 + +=head2 numify() + + print $x->numify(); + +This returns a normal Perl scalar from $x. It is used automatically +whenever a scalar is needed, for instance in array index operations. + +This loses precision, to avoid this use L<as_int()> instead. + +=head2 modify() + + $x->modify('bpowd'); + +This method returns 0 if the object can be modified with the given +peration, or 1 if not. + +This is used for instance by L<Math::BigInt::Constant>. + +=head2 upgrade()/downgrade() + +Set/get the class for downgrade/upgrade operations. Thuis is used +for instance by L<bignum>. The defaults are '', thus the following +operation will create a BigInt, not a BigFloat: + + my $i = Math::BigInt->new(123); + my $f = Math::BigFloat->new('123.1'); + + print $i + $f,"\n"; # print 246 + +=head2 div_scale() + +Set/get the number of digits for the default precision in divide +operations. + +=head2 round_mode() + +Set/get the current round mode. + +=head1 ACCURACY and PRECISION + +Since version v1.33, Math::BigInt and Math::BigFloat have full support for +accuracy and precision based rounding, both automatically after every +operation, as well as manually. + +This section describes the accuracy/precision handling in Math::Big* as it +used to be and as it is now, complete with an explanation of all terms and +abbreviations. + +Not yet implemented things (but with correct description) are marked with '!', +things that need to be answered are marked with '?'. + +In the next paragraph follows a short description of terms used here (because +these may differ from terms used by others people or documentation). + +During the rest of this document, the shortcuts A (for accuracy), P (for +precision), F (fallback) and R (rounding mode) will be used. + +=head2 Precision P + +A fixed number of digits before (positive) or after (negative) +the decimal point. For example, 123.45 has a precision of -2. 0 means an +integer like 123 (or 120). A precision of 2 means two digits to the left +of the decimal point are zero, so 123 with P = 1 becomes 120. Note that +numbers with zeros before the decimal point may have different precisions, +because 1200 can have p = 0, 1 or 2 (depending on what the inital value +was). It could also have p < 0, when the digits after the decimal point +are zero. + +The string output (of floating point numbers) will be padded with zeros: + + Initial value P A Result String + ------------------------------------------------------------ + 1234.01 -3 1000 1000 + 1234 -2 1200 1200 + 1234.5 -1 1230 1230 + 1234.001 1 1234 1234.0 + 1234.01 0 1234 1234 + 1234.01 2 1234.01 1234.01 + 1234.01 5 1234.01 1234.01000 + +For BigInts, no padding occurs. + +=head2 Accuracy A + +Number of significant digits. Leading zeros are not counted. A +number may have an accuracy greater than the non-zero digits +when there are zeros in it or trailing zeros. For example, 123.456 has +A of 6, 10203 has 5, 123.0506 has 7, 123.450000 has 8 and 0.000123 has 3. + +The string output (of floating point numbers) will be padded with zeros: + + Initial value P A Result String + ------------------------------------------------------------ + 1234.01 3 1230 1230 + 1234.01 6 1234.01 1234.01 + 1234.1 8 1234.1 1234.1000 + +For BigInts, no padding occurs. + +=head2 Fallback F + +When both A and P are undefined, this is used as a fallback accuracy when +dividing numbers. + +=head2 Rounding mode R + +When rounding a number, different 'styles' or 'kinds' +of rounding are possible. (Note that random rounding, as in +Math::Round, is not implemented.) + +=over 2 + +=item 'trunc' + +truncation invariably removes all digits following the +rounding place, replacing them with zeros. Thus, 987.65 rounded +to tens (P=1) becomes 980, and rounded to the fourth sigdig +becomes 987.6 (A=4). 123.456 rounded to the second place after the +decimal point (P=-2) becomes 123.46. + +All other implemented styles of rounding attempt to round to the +"nearest digit." If the digit D immediately to the right of the +rounding place (skipping the decimal point) is greater than 5, the +number is incremented at the rounding place (possibly causing a +cascade of incrementation): e.g. when rounding to units, 0.9 rounds +to 1, and -19.9 rounds to -20. If D < 5, the number is similarly +truncated at the rounding place: e.g. when rounding to units, 0.4 +rounds to 0, and -19.4 rounds to -19. + +However the results of other styles of rounding differ if the +digit immediately to the right of the rounding place (skipping the +decimal point) is 5 and if there are no digits, or no digits other +than 0, after that 5. In such cases: + +=item 'even' + +rounds the digit at the rounding place to 0, 2, 4, 6, or 8 +if it is not already. E.g., when rounding to the first sigdig, 0.45 +becomes 0.4, -0.55 becomes -0.6, but 0.4501 becomes 0.5. + +=item 'odd' + +rounds the digit at the rounding place to 1, 3, 5, 7, or 9 if +it is not already. E.g., when rounding to the first sigdig, 0.45 +becomes 0.5, -0.55 becomes -0.5, but 0.5501 becomes 0.6. + +=item '+inf' + +round to plus infinity, i.e. always round up. E.g., when +rounding to the first sigdig, 0.45 becomes 0.5, -0.55 becomes -0.5, +and 0.4501 also becomes 0.5. + +=item '-inf' + +round to minus infinity, i.e. always round down. E.g., when +rounding to the first sigdig, 0.45 becomes 0.4, -0.55 becomes -0.6, +but 0.4501 becomes 0.5. + +=item 'zero' + +round to zero, i.e. positive numbers down, negative ones up. +E.g., when rounding to the first sigdig, 0.45 becomes 0.4, -0.55 +becomes -0.5, but 0.4501 becomes 0.5. + +=item 'common' + +round up if the digit immediately to the right of the rounding place +is 5 or greater, otherwise round down. E.g., 0.15 becomes 0.2 and +0.149 becomes 0.1. + +=back + +The handling of A & P in MBI/MBF (the old core code shipped with Perl +versions <= 5.7.2) is like this: + +=over 2 + +=item Precision + + * ffround($p) is able to round to $p number of digits after the decimal + point + * otherwise P is unused + +=item Accuracy (significant digits) + + * fround($a) rounds to $a significant digits + * only fdiv() and fsqrt() take A as (optional) paramater + + other operations simply create the same number (fneg etc), or more (fmul) + of digits + + rounding/truncating is only done when explicitly calling one of fround + or ffround, and never for BigInt (not implemented) + * fsqrt() simply hands its accuracy argument over to fdiv. + * the documentation and the comment in the code indicate two different ways + on how fdiv() determines the maximum number of digits it should calculate, + and the actual code does yet another thing + POD: + max($Math::BigFloat::div_scale,length(dividend)+length(divisor)) + Comment: + result has at most max(scale, length(dividend), length(divisor)) digits + Actual code: + scale = max(scale, length(dividend)-1,length(divisor)-1); + scale += length(divisor) - length(dividend); + So for lx = 3, ly = 9, scale = 10, scale will actually be 16 (10+9-3). + Actually, the 'difference' added to the scale is calculated from the + number of "significant digits" in dividend and divisor, which is derived + by looking at the length of the mantissa. Which is wrong, since it includes + the + sign (oops) and actually gets 2 for '+100' and 4 for '+101'. Oops + again. Thus 124/3 with div_scale=1 will get you '41.3' based on the strange + assumption that 124 has 3 significant digits, while 120/7 will get you + '17', not '17.1' since 120 is thought to have 2 significant digits. + The rounding after the division then uses the remainder and $y to determine + wether it must round up or down. + ? I have no idea which is the right way. That's why I used a slightly more + ? simple scheme and tweaked the few failing testcases to match it. + +=back + +This is how it works now: + +=over 2 + +=item Setting/Accessing + + * You can set the A global via C<< Math::BigInt->accuracy() >> or + C<< Math::BigFloat->accuracy() >> or whatever class you are using. + * You can also set P globally by using C<< Math::SomeClass->precision() >> + likewise. + * Globals are classwide, and not inherited by subclasses. + * to undefine A, use C<< Math::SomeCLass->accuracy(undef); >> + * to undefine P, use C<< Math::SomeClass->precision(undef); >> + * Setting C<< Math::SomeClass->accuracy() >> clears automatically + C<< Math::SomeClass->precision() >>, and vice versa. + * To be valid, A must be > 0, P can have any value. + * If P is negative, this means round to the P'th place to the right of the + decimal point; positive values mean to the left of the decimal point. + P of 0 means round to integer. + * to find out the current global A, use C<< Math::SomeClass->accuracy() >> + * to find out the current global P, use C<< Math::SomeClass->precision() >> + * use C<< $x->accuracy() >> respective C<< $x->precision() >> for the local + setting of C<< $x >>. + * Please note that C<< $x->accuracy() >> respective C<< $x->precision() >> + return eventually defined global A or P, when C<< $x >>'s A or P is not + set. + +=item Creating numbers + + * When you create a number, you can give the desired A or P via: + $x = Math::BigInt->new($number,$A,$P); + * Only one of A or P can be defined, otherwise the result is NaN + * If no A or P is give ($x = Math::BigInt->new($number) form), then the + globals (if set) will be used. Thus changing the global defaults later on + will not change the A or P of previously created numbers (i.e., A and P of + $x will be what was in effect when $x was created) + * If given undef for A and P, B<no> rounding will occur, and the globals will + B<not> be used. This is used by subclasses to create numbers without + suffering rounding in the parent. Thus a subclass is able to have its own + globals enforced upon creation of a number by using + C<< $x = Math::BigInt->new($number,undef,undef) >>: + + use Math::BigInt::SomeSubclass; + use Math::BigInt; + + Math::BigInt->accuracy(2); + Math::BigInt::SomeSubClass->accuracy(3); + $x = Math::BigInt::SomeSubClass->new(1234); + + $x is now 1230, and not 1200. A subclass might choose to implement + this otherwise, e.g. falling back to the parent's A and P. + +=item Usage + + * If A or P are enabled/defined, they are used to round the result of each + operation according to the rules below + * Negative P is ignored in Math::BigInt, since BigInts never have digits + after the decimal point + * Math::BigFloat uses Math::BigInt internally, but setting A or P inside + Math::BigInt as globals does not tamper with the parts of a BigFloat. + A flag is used to mark all Math::BigFloat numbers as 'never round'. + +=item Precedence + + * It only makes sense that a number has only one of A or P at a time. + If you set either A or P on one object, or globally, the other one will + be automatically cleared. + * If two objects are involved in an operation, and one of them has A in + effect, and the other P, this results in an error (NaN). + * A takes precedence over P (Hint: A comes before P). + If neither of them is defined, nothing is used, i.e. the result will have + as many digits as it can (with an exception for fdiv/fsqrt) and will not + be rounded. + * There is another setting for fdiv() (and thus for fsqrt()). If neither of + A or P is defined, fdiv() will use a fallback (F) of $div_scale digits. + If either the dividend's or the divisor's mantissa has more digits than + the value of F, the higher value will be used instead of F. + This is to limit the digits (A) of the result (just consider what would + happen with unlimited A and P in the case of 1/3 :-) + * fdiv will calculate (at least) 4 more digits than required (determined by + A, P or F), and, if F is not used, round the result + (this will still fail in the case of a result like 0.12345000000001 with A + or P of 5, but this can not be helped - or can it?) + * Thus you can have the math done by on Math::Big* class in two modi: + + never round (this is the default): + This is done by setting A and P to undef. No math operation + will round the result, with fdiv() and fsqrt() as exceptions to guard + against overflows. You must explicitly call bround(), bfround() or + round() (the latter with parameters). + Note: Once you have rounded a number, the settings will 'stick' on it + and 'infect' all other numbers engaged in math operations with it, since + local settings have the highest precedence. So, to get SaferRound[tm], + use a copy() before rounding like this: + + $x = Math::BigFloat->new(12.34); + $y = Math::BigFloat->new(98.76); + $z = $x * $y; # 1218.6984 + print $x->copy()->fround(3); # 12.3 (but A is now 3!) + $z = $x * $y; # still 1218.6984, without + # copy would have been 1210! + + + round after each op: + After each single operation (except for testing like is_zero()), the + method round() is called and the result is rounded appropriately. By + setting proper values for A and P, you can have all-the-same-A or + all-the-same-P modes. For example, Math::Currency might set A to undef, + and P to -2, globally. + + ?Maybe an extra option that forbids local A & P settings would be in order, + ?so that intermediate rounding does not 'poison' further math? + +=item Overriding globals + + * you will be able to give A, P and R as an argument to all the calculation + routines; the second parameter is A, the third one is P, and the fourth is + R (shift right by one for binary operations like badd). P is used only if + the first parameter (A) is undefined. These three parameters override the + globals in the order detailed as follows, i.e. the first defined value + wins: + (local: per object, global: global default, parameter: argument to sub) + + parameter A + + parameter P + + local A (if defined on both of the operands: smaller one is taken) + + local P (if defined on both of the operands: bigger one is taken) + + global A + + global P + + global F + * fsqrt() will hand its arguments to fdiv(), as it used to, only now for two + arguments (A and P) instead of one + +=item Local settings + + * You can set A or P locally by using C<< $x->accuracy() >> or + C<< $x->precision() >> + and thus force different A and P for different objects/numbers. + * Setting A or P this way immediately rounds $x to the new value. + * C<< $x->accuracy() >> clears C<< $x->precision() >>, and vice versa. + +=item Rounding + + * the rounding routines will use the respective global or local settings. + fround()/bround() is for accuracy rounding, while ffround()/bfround() + is for precision + * the two rounding functions take as the second parameter one of the + following rounding modes (R): + 'even', 'odd', '+inf', '-inf', 'zero', 'trunc', 'common' + * you can set/get the global R by using C<< Math::SomeClass->round_mode() >> + or by setting C<< $Math::SomeClass::round_mode >> + * after each operation, C<< $result->round() >> is called, and the result may + eventually be rounded (that is, if A or P were set either locally, + globally or as parameter to the operation) + * to manually round a number, call C<< $x->round($A,$P,$round_mode); >> + this will round the number by using the appropriate rounding function + and then normalize it. + * rounding modifies the local settings of the number: + + $x = Math::BigFloat->new(123.456); + $x->accuracy(5); + $x->bround(4); + + Here 4 takes precedence over 5, so 123.5 is the result and $x->accuracy() + will be 4 from now on. + +=item Default values + + * R: 'even' + * F: 40 + * A: undef + * P: undef + +=item Remarks + + * The defaults are set up so that the new code gives the same results as + the old code (except in a few cases on fdiv): + + Both A and P are undefined and thus will not be used for rounding + after each operation. + + round() is thus a no-op, unless given extra parameters A and P + +=back + +=head1 Infinity and Not a Number + +While BigInt has extensive handling of inf and NaN, certain quirks remain. + +=over 2 + +=item oct()/hex() + +These perl routines currently (as of Perl v.5.8.6) cannot handle passed +inf. + + te@linux:~> perl -wle 'print 2 ** 3333' + inf + te@linux:~> perl -wle 'print 2 ** 3333 == 2 ** 3333' + 1 + te@linux:~> perl -wle 'print oct(2 ** 3333)' + 0 + te@linux:~> perl -wle 'print hex(2 ** 3333)' + Illegal hexadecimal digit 'i' ignored at -e line 1. + 0 + +The same problems occur if you pass them Math::BigInt->binf() objects. Since +overloading these routines is not possible, this cannot be fixed from BigInt. + +=item ==, !=, <, >, <=, >= with NaNs + +BigInt's bcmp() routine currently returns undef to signal that a NaN was +involved in a comparison. However, the overload code turns that into +either 1 or '' and thus operations like C<< NaN != NaN >> might return +wrong values. + +=item log(-inf) + +C<< log(-inf) >> is highly weird. Since log(-x)=pi*i+log(x), then +log(-inf)=pi*i+inf. However, since the imaginary part is finite, the real +infinity "overshadows" it, so the number might as well just be infinity. +However, the result is a complex number, and since BigInt/BigFloat can only +have real numbers as results, the result is NaN. + +=item exp(), cos(), sin(), atan2() + +These all might have problems handling infinity right. + +=back + +=head1 INTERNALS + +The actual numbers are stored as unsigned big integers (with seperate sign). + +You should neither care about nor depend on the internal representation; it +might change without notice. Use B<ONLY> method calls like C<< $x->sign(); >> +instead relying on the internal representation. + +=head2 MATH LIBRARY + +Math with the numbers is done (by default) by a module called +C<Math::BigInt::Calc>. This is equivalent to saying: + + use Math::BigInt try => 'Calc'; + +You can change this backend library by using: + + use Math::BigInt try => 'GMP'; + +B<Note>: General purpose packages should not be explicit about the library +to use; let the script author decide which is best. + +If your script works with huge numbers and Calc is too slow for them, +you can also for the loading of one of these libraries and if none +of them can be used, the code will die: + + use Math::BigInt only => 'GMP,Pari'; + +The following would first try to find Math::BigInt::Foo, then +Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: + + use Math::BigInt try => 'Foo,Math::BigInt::Bar'; + +The library that is loaded last will be used. Note that this can be +overwritten at any time by loading a different library, and numbers +constructed with different libraries cannot be used in math operations +together. + +=head3 What library to use? + +B<Note>: General purpose packages should not be explicit about the library +to use; let the script author decide which is best. + +L<Math::BigInt::GMP> and L<Math::BigInt::Pari> are in cases involving big +numbers much faster than Calc, however it is slower when dealing with very +small numbers (less than about 20 digits) and when converting very large +numbers to decimal (for instance for printing, rounding, calculating their +length in decimal etc). + +So please select carefully what libary you want to use. + +Different low-level libraries use different formats to store the numbers. +However, you should B<NOT> depend on the number having a specific format +internally. + +See the respective math library module documentation for further details. + +=head2 SIGN + +The sign is either '+', '-', 'NaN', '+inf' or '-inf'. + +A sign of 'NaN' is used to represent the result when input arguments are not +numbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively +minus infinity. You will get '+inf' when dividing a positive number by 0, and +'-inf' when dividing any negative number by 0. + +=head2 mantissa(), exponent() and parts() + +C<mantissa()> and C<exponent()> return the said parts of the BigInt such +that: + + $m = $x->mantissa(); + $e = $x->exponent(); + $y = $m * ( 10 ** $e ); + print "ok\n" if $x == $y; + +C<< ($m,$e) = $x->parts() >> is just a shortcut that gives you both of them +in one go. Both the returned mantissa and exponent have a sign. + +Currently, for BigInts C<$e> is always 0, except +inf and -inf, where it is +C<+inf>; and for NaN, where it is C<NaN>; and for C<$x == 0>, where it is C<1> +(to be compatible with Math::BigFloat's internal representation of a zero as +C<0E1>). + +C<$m> is currently just a copy of the original number. The relation between +C<$e> and C<$m> will stay always the same, though their real values might +change. + +=head1 EXAMPLES + + use Math::BigInt; + + sub bint { Math::BigInt->new(shift); } + + $x = Math::BigInt->bstr("1234") # string "1234" + $x = "$x"; # same as bstr() + $x = Math::BigInt->bneg("1234"); # BigInt "-1234" + $x = Math::BigInt->babs("-12345"); # BigInt "12345" + $x = Math::BigInt->bnorm("-0.00"); # BigInt "0" + $x = bint(1) + bint(2); # BigInt "3" + $x = bint(1) + "2"; # ditto (auto-BigIntify of "2") + $x = bint(1); # BigInt "1" + $x = $x + 5 / 2; # BigInt "3" + $x = $x ** 3; # BigInt "27" + $x *= 2; # BigInt "54" + $x = Math::BigInt->new(0); # BigInt "0" + $x--; # BigInt "-1" + $x = Math::BigInt->badd(4,5) # BigInt "9" + print $x->bsstr(); # 9e+0 + +Examples for rounding: + + use Math::BigFloat; + use Test; + + $x = Math::BigFloat->new(123.4567); + $y = Math::BigFloat->new(123.456789); + Math::BigFloat->accuracy(4); # no more A than 4 + + ok ($x->copy()->fround(),123.4); # even rounding + print $x->copy()->fround(),"\n"; # 123.4 + Math::BigFloat->round_mode('odd'); # round to odd + print $x->copy()->fround(),"\n"; # 123.5 + Math::BigFloat->accuracy(5); # no more A than 5 + Math::BigFloat->round_mode('odd'); # round to odd + print $x->copy()->fround(),"\n"; # 123.46 + $y = $x->copy()->fround(4),"\n"; # A = 4: 123.4 + print "$y, ",$y->accuracy(),"\n"; # 123.4, 4 + + Math::BigFloat->accuracy(undef); # A not important now + Math::BigFloat->precision(2); # P important + print $x->copy()->bnorm(),"\n"; # 123.46 + print $x->copy()->fround(),"\n"; # 123.46 + +Examples for converting: + + my $x = Math::BigInt->new('0b1'.'01' x 123); + print "bin: ",$x->as_bin()," hex:",$x->as_hex()," dec: ",$x,"\n"; + +=head1 Autocreating constants + +After C<use Math::BigInt ':constant'> all the B<integer> decimal, hexadecimal +and binary constants in the given scope are converted to C<Math::BigInt>. +This conversion happens at compile time. + +In particular, + + perl -MMath::BigInt=:constant -e 'print 2**100,"\n"' + +prints the integer value of C<2**100>. Note that without conversion of +constants the expression 2**100 will be calculated as perl scalar. + +Please note that strings and floating point constants are not affected, +so that + + use Math::BigInt qw/:constant/; + + $x = 1234567890123456789012345678901234567890 + + 123456789123456789; + $y = '1234567890123456789012345678901234567890' + + '123456789123456789'; + +do not work. You need an explicit Math::BigInt->new() around one of the +operands. You should also quote large constants to protect loss of precision: + + use Math::BigInt; + + $x = Math::BigInt->new('1234567889123456789123456789123456789'); + +Without the quotes Perl would convert the large number to a floating point +constant at compile time and then hand the result to BigInt, which results in +an truncated result or a NaN. + +This also applies to integers that look like floating point constants: + + use Math::BigInt ':constant'; + + print ref(123e2),"\n"; + print ref(123.2e2),"\n"; + +will print nothing but newlines. Use either L<bignum> or L<Math::BigFloat> +to get this to work. + +=head1 PERFORMANCE + +Using the form $x += $y; etc over $x = $x + $y is faster, since a copy of $x +must be made in the second case. For long numbers, the copy can eat up to 20% +of the work (in the case of addition/subtraction, less for +multiplication/division). If $y is very small compared to $x, the form +$x += $y is MUCH faster than $x = $x + $y since making the copy of $x takes +more time then the actual addition. + +With a technique called copy-on-write, the cost of copying with overload could +be minimized or even completely avoided. A test implementation of COW did show +performance gains for overloaded math, but introduced a performance loss due +to a constant overhead for all other operations. So Math::BigInt does currently +not COW. + +The rewritten version of this module (vs. v0.01) is slower on certain +operations, like C<new()>, C<bstr()> and C<numify()>. The reason are that it +does now more work and handles much more cases. The time spent in these +operations is usually gained in the other math operations so that code on +the average should get (much) faster. If they don't, please contact the author. + +Some operations may be slower for small numbers, but are significantly faster +for big numbers. Other operations are now constant (O(1), like C<bneg()>, +C<babs()> etc), instead of O(N) and thus nearly always take much less time. +These optimizations were done on purpose. + +If you find the Calc module to slow, try to install any of the replacement +modules and see if they help you. + +=head2 Alternative math libraries + +You can use an alternative library to drive Math::BigInt. See the section +L<MATH LIBRARY> for more information. + +For more benchmark results see L<http://bloodgate.com/perl/benchmarks.html>. + +=head2 SUBCLASSING + +=head1 Subclassing Math::BigInt + +The basic design of Math::BigInt allows simple subclasses with very little +work, as long as a few simple rules are followed: + +=over 2 + +=item * + +The public API must remain consistent, i.e. if a sub-class is overloading +addition, the sub-class must use the same name, in this case badd(). The +reason for this is that Math::BigInt is optimized to call the object methods +directly. + +=item * + +The private object hash keys like C<$x->{sign}> may not be changed, but +additional keys can be added, like C<$x->{_custom}>. + +=item * + +Accessor functions are available for all existing object hash keys and should +be used instead of directly accessing the internal hash keys. The reason for +this is that Math::BigInt itself has a pluggable interface which permits it +to support different storage methods. + +=back + +More complex sub-classes may have to replicate more of the logic internal of +Math::BigInt if they need to change more basic behaviors. A subclass that +needs to merely change the output only needs to overload C<bstr()>. + +All other object methods and overloaded functions can be directly inherited +from the parent class. + +At the very minimum, any subclass will need to provide its own C<new()> and can +store additional hash keys in the object. There are also some package globals +that must be defined, e.g.: + + # Globals + $accuracy = undef; + $precision = -2; # round to 2 decimal places + $round_mode = 'even'; + $div_scale = 40; + +Additionally, you might want to provide the following two globals to allow +auto-upgrading and auto-downgrading to work correctly: + + $upgrade = undef; + $downgrade = undef; + +This allows Math::BigInt to correctly retrieve package globals from the +subclass, like C<$SubClass::precision>. See t/Math/BigInt/Subclass.pm or +t/Math/BigFloat/SubClass.pm completely functional subclass examples. + +Don't forget to + + use overload; + +in your subclass to automatically inherit the overloading from the parent. If +you like, you can change part of the overloading, look at Math::String for an +example. + +=head1 UPGRADING + +When used like this: + + use Math::BigInt upgrade => 'Foo::Bar'; + +certain operations will 'upgrade' their calculation and thus the result to +the class Foo::Bar. Usually this is used in conjunction with Math::BigFloat: + + use Math::BigInt upgrade => 'Math::BigFloat'; + +As a shortcut, you can use the module C<bignum>: + + use bignum; + +Also good for oneliners: + + perl -Mbignum -le 'print 2 ** 255' + +This makes it possible to mix arguments of different classes (as in 2.5 + 2) +as well es preserve accuracy (as in sqrt(3)). + +Beware: This feature is not fully implemented yet. + +=head2 Auto-upgrade + +The following methods upgrade themselves unconditionally; that is if upgrade +is in effect, they will always hand up their work: + +=over 2 + +=item bsqrt() + +=item div() + +=item blog() + +=item bexp() + +=back + +Beware: This list is not complete. + +All other methods upgrade themselves only when one (or all) of their +arguments are of the class mentioned in $upgrade (This might change in later +versions to a more sophisticated scheme): + +=head1 EXPORTS + +C<Math::BigInt> exports nothing by default, but can export the following methods: + + bgcd + blcm + +=head1 CAVEATS + +Some things might not work as you expect them. Below is documented what is +known to be troublesome: + +=over 1 + +=item bstr(), bsstr() and 'cmp' + +Both C<bstr()> and C<bsstr()> as well as automated stringify via overload now +drop the leading '+'. The old code would return '+3', the new returns '3'. +This is to be consistent with Perl and to make C<cmp> (especially with +overloading) to work as you expect. It also solves problems with C<Test.pm>, +because its C<ok()> uses 'eq' internally. + +Mark Biggar said, when asked about to drop the '+' altogether, or make only +C<cmp> work: + + I agree (with the first alternative), don't add the '+' on positive + numbers. It's not as important anymore with the new internal + form for numbers. It made doing things like abs and neg easier, + but those have to be done differently now anyway. + +So, the following examples will now work all as expected: + + use Test; + BEGIN { plan tests => 1 } + use Math::BigInt; + + my $x = new Math::BigInt 3*3; + my $y = new Math::BigInt 3*3; + + ok ($x,3*3); + print "$x eq 9" if $x eq $y; + print "$x eq 9" if $x eq '9'; + print "$x eq 9" if $x eq 3*3; + +Additionally, the following still works: + + print "$x == 9" if $x == $y; + print "$x == 9" if $x == 9; + print "$x == 9" if $x == 3*3; + +There is now a C<bsstr()> method to get the string in scientific notation aka +C<1e+2> instead of C<100>. Be advised that overloaded 'eq' always uses bstr() +for comparison, but Perl will represent some numbers as 100 and others +as 1e+308. If in doubt, convert both arguments to Math::BigInt before +comparing them as strings: + + use Test; + BEGIN { plan tests => 3 } + use Math::BigInt; + + $x = Math::BigInt->new('1e56'); $y = 1e56; + ok ($x,$y); # will fail + ok ($x->bsstr(),$y); # okay + $y = Math::BigInt->new($y); + ok ($x,$y); # okay + +Alternatively, simple use C<< <=> >> for comparisons, this will get it +always right. There is not yet a way to get a number automatically represented +as a string that matches exactly the way Perl represents it. + +See also the section about L<Infinity and Not a Number> for problems in +comparing NaNs. + +=item int() + +C<int()> will return (at least for Perl v5.7.1 and up) another BigInt, not a +Perl scalar: + + $x = Math::BigInt->new(123); + $y = int($x); # BigInt 123 + $x = Math::BigFloat->new(123.45); + $y = int($x); # BigInt 123 + +In all Perl versions you can use C<as_number()> or C<as_int> for the same +effect: + + $x = Math::BigFloat->new(123.45); + $y = $x->as_number(); # BigInt 123 + $y = $x->as_int(); # ditto + +This also works for other subclasses, like Math::String. + +If you want a real Perl scalar, use C<numify()>: + + $y = $x->numify(); # 123 as scalar + +This is seldom necessary, though, because this is done automatically, like +when you access an array: + + $z = $array[$x]; # does work automatically + +=item length + +The following will probably not do what you expect: + + $c = Math::BigInt->new(123); + print $c->length(),"\n"; # prints 30 + +It prints both the number of digits in the number and in the fraction part +since print calls C<length()> in list context. Use something like: + + print scalar $c->length(),"\n"; # prints 3 + +=item bdiv + +The following will probably not do what you expect: + + print $c->bdiv(10000),"\n"; + +It prints both quotient and remainder since print calls C<bdiv()> in list +context. Also, C<bdiv()> will modify $c, so be careful. You probably want +to use + + print $c / 10000,"\n"; + print scalar $c->bdiv(10000),"\n"; # or if you want to modify $c + +instead. + +The quotient is always the greatest integer less than or equal to the +real-valued quotient of the two operands, and the remainder (when it is +nonzero) always has the same sign as the second operand; so, for +example, + + 1 / 4 => ( 0, 1) + 1 / -4 => (-1,-3) + -3 / 4 => (-1, 1) + -3 / -4 => ( 0,-3) + -11 / 2 => (-5,1) + 11 /-2 => (-5,-1) + +As a consequence, the behavior of the operator % agrees with the +behavior of Perl's built-in % operator (as documented in the perlop +manpage), and the equation + + $x == ($x / $y) * $y + ($x % $y) + +holds true for any $x and $y, which justifies calling the two return +values of bdiv() the quotient and remainder. The only exception to this rule +are when $y == 0 and $x is negative, then the remainder will also be +negative. See below under "infinity handling" for the reasoning behind this. + +Perl's 'use integer;' changes the behaviour of % and / for scalars, but will +not change BigInt's way to do things. This is because under 'use integer' Perl +will do what the underlying C thinks is right and this is different for each +system. If you need BigInt's behaving exactly like Perl's 'use integer', bug +the author to implement it ;) + +=item infinity handling + +Here are some examples that explain the reasons why certain results occur while +handling infinity: + +The following table shows the result of the division and the remainder, so that +the equation above holds true. Some "ordinary" cases are strewn in to show more +clearly the reasoning: + + A / B = C, R so that C * B + R = A + ========================================================= + 5 / 8 = 0, 5 0 * 8 + 5 = 5 + 0 / 8 = 0, 0 0 * 8 + 0 = 0 + 0 / inf = 0, 0 0 * inf + 0 = 0 + 0 /-inf = 0, 0 0 * -inf + 0 = 0 + 5 / inf = 0, 5 0 * inf + 5 = 5 + 5 /-inf = 0, 5 0 * -inf + 5 = 5 + -5/ inf = 0, -5 0 * inf + -5 = -5 + -5/-inf = 0, -5 0 * -inf + -5 = -5 + inf/ 5 = inf, 0 inf * 5 + 0 = inf + -inf/ 5 = -inf, 0 -inf * 5 + 0 = -inf + inf/ -5 = -inf, 0 -inf * -5 + 0 = inf + -inf/ -5 = inf, 0 inf * -5 + 0 = -inf + 5/ 5 = 1, 0 1 * 5 + 0 = 5 + -5/ -5 = 1, 0 1 * -5 + 0 = -5 + inf/ inf = 1, 0 1 * inf + 0 = inf + -inf/-inf = 1, 0 1 * -inf + 0 = -inf + inf/-inf = -1, 0 -1 * -inf + 0 = inf + -inf/ inf = -1, 0 1 * -inf + 0 = -inf + 8/ 0 = inf, 8 inf * 0 + 8 = 8 + inf/ 0 = inf, inf inf * 0 + inf = inf + 0/ 0 = NaN + +These cases below violate the "remainder has the sign of the second of the two +arguments", since they wouldn't match up otherwise. + + A / B = C, R so that C * B + R = A + ======================================================== + -inf/ 0 = -inf, -inf -inf * 0 + inf = -inf + -8/ 0 = -inf, -8 -inf * 0 + 8 = -8 + +=item Modifying and = + +Beware of: + + $x = Math::BigFloat->new(5); + $y = $x; + +It will not do what you think, e.g. making a copy of $x. Instead it just makes +a second reference to the B<same> object and stores it in $y. Thus anything +that modifies $x (except overloaded operators) will modify $y, and vice versa. +Or in other words, C<=> is only safe if you modify your BigInts only via +overloaded math. As soon as you use a method call it breaks: + + $x->bmul(2); + print "$x, $y\n"; # prints '10, 10' + +If you want a true copy of $x, use: + + $y = $x->copy(); + +You can also chain the calls like this, this will make first a copy and then +multiply it by 2: + + $y = $x->copy()->bmul(2); + +See also the documentation for overload.pm regarding C<=>. + +=item bpow + +C<bpow()> (and the rounding functions) now modifies the first argument and +returns it, unlike the old code which left it alone and only returned the +result. This is to be consistent with C<badd()> etc. The first three will +modify $x, the last one won't: + + print bpow($x,$i),"\n"; # modify $x + print $x->bpow($i),"\n"; # ditto + print $x **= $i,"\n"; # the same + print $x ** $i,"\n"; # leave $x alone + +The form C<$x **= $y> is faster than C<$x = $x ** $y;>, though. + +=item Overloading -$x + +The following: + + $x = -$x; + +is slower than + + $x->bneg(); + +since overload calls C<sub($x,0,1);> instead of C<neg($x)>. The first variant +needs to preserve $x since it does not know that it later will get overwritten. +This makes a copy of $x and takes O(N), but $x->bneg() is O(1). + +=item Mixing different object types + +In Perl you will get a floating point value if you do one of the following: + + $float = 5.0 + 2; + $float = 2 + 5.0; + $float = 5 / 2; + +With overloaded math, only the first two variants will result in a BigFloat: + + use Math::BigInt; + use Math::BigFloat; + + $mbf = Math::BigFloat->new(5); + $mbi2 = Math::BigInteger->new(5); + $mbi = Math::BigInteger->new(2); + + # what actually gets called: + $float = $mbf + $mbi; # $mbf->badd() + $float = $mbf / $mbi; # $mbf->bdiv() + $integer = $mbi + $mbf; # $mbi->badd() + $integer = $mbi2 / $mbi; # $mbi2->bdiv() + $integer = $mbi2 / $mbf; # $mbi2->bdiv() + +This is because math with overloaded operators follows the first (dominating) +operand, and the operation of that is called and returns thus the result. So, +Math::BigInt::bdiv() will always return a Math::BigInt, regardless whether +the result should be a Math::BigFloat or the second operant is one. + +To get a Math::BigFloat you either need to call the operation manually, +make sure the operands are already of the proper type or casted to that type +via Math::BigFloat->new(): + + $float = Math::BigFloat->new($mbi2) / $mbi; # = 2.5 + +Beware of simple "casting" the entire expression, this would only convert +the already computed result: + + $float = Math::BigFloat->new($mbi2 / $mbi); # = 2.0 thus wrong! + +Beware also of the order of more complicated expressions like: + + $integer = ($mbi2 + $mbi) / $mbf; # int / float => int + $integer = $mbi2 / Math::BigFloat->new($mbi); # ditto + +If in doubt, break the expression into simpler terms, or cast all operands +to the desired resulting type. + +Scalar values are a bit different, since: + + $float = 2 + $mbf; + $float = $mbf + 2; + +will both result in the proper type due to the way the overloaded math works. + +This section also applies to other overloaded math packages, like Math::String. + +One solution to you problem might be autoupgrading|upgrading. See the +pragmas L<bignum>, L<bigint> and L<bigrat> for an easy way to do this. + +=item bsqrt() + +C<bsqrt()> works only good if the result is a big integer, e.g. the square +root of 144 is 12, but from 12 the square root is 3, regardless of rounding +mode. The reason is that the result is always truncated to an integer. + +If you want a better approximation of the square root, then use: + + $x = Math::BigFloat->new(12); + Math::BigFloat->precision(0); + Math::BigFloat->round_mode('even'); + print $x->copy->bsqrt(),"\n"; # 4 + + Math::BigFloat->precision(2); + print $x->bsqrt(),"\n"; # 3.46 + print $x->bsqrt(3),"\n"; # 3.464 + +=item brsft() + +For negative numbers in base see also L<brsft|brsft>. + +=back + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 SEE ALSO + +L<Math::BigFloat>, L<Math::BigRat> and L<Math::Big> as well as +L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>. + +The pragmas L<bignum>, L<bigint> and L<bigrat> also might be of interest +because they solve the autoupgrading/downgrading issue, at least partly. + +The package at +L<http://search.cpan.org/search?mode=module&query=Math%3A%3ABigInt> contains +more documentation including a full version history, testcases, empty +subclass files and benchmarks. + +=head1 AUTHORS + +Original code by Mark Biggar, overloaded interface by Ilya Zakharevich. +Completely rewritten by Tels http://bloodgate.com in late 2000, 2001 - 2006 +and still at it in 2007. + +Many people contributed in one or more ways to the final beast, see the file +CREDITS for an (incomplete) list. If you miss your name, please drop me a +mail. Thank you! + +=cut diff --git a/Master/tlpkg/tlperl/lib/Math/BigInt/Calc.pm b/Master/tlpkg/tlperl/lib/Math/BigInt/Calc.pm new file mode 100644 index 00000000000..52e33d232ae --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Math/BigInt/Calc.pm @@ -0,0 +1,2612 @@ +package Math::BigInt::Calc; + +use 5.006; +use strict; +# use warnings; # dont use warnings for older Perls + +our $VERSION = '0.52'; + +# Package to store unsigned big integers in decimal and do math with them + +# Internally the numbers are stored in an array with at least 1 element, no +# leading zero parts (except the first) and in base 1eX where X is determined +# automatically at loading time to be the maximum possible value + +# todo: +# - fully remove funky $# stuff in div() (maybe - that code scares me...) + +# USE_MUL: due to problems on certain os (os390, posix-bc) "* 1e-5" is used +# instead of "/ 1e5" at some places, (marked with USE_MUL). Other platforms +# BS2000, some Crays need USE_DIV instead. +# The BEGIN block is used to determine which of the two variants gives the +# correct result. + +# Beware of things like: +# $i = $i * $y + $car; $car = int($i / $BASE); $i = $i % $BASE; +# This works on x86, but fails on ARM (SA1100, iPAQ) due to whoknows what +# reasons. So, use this instead (slower, but correct): +# $i = $i * $y + $car; $car = int($i / $BASE); $i -= $BASE * $car; + +############################################################################## +# global constants, flags and accessory + +# announce that we are compatible with MBI v1.83 and up +sub api_version () { 2; } + +# constants for easier life +my ($BASE,$BASE_LEN,$RBASE,$MAX_VAL); +my ($AND_BITS,$XOR_BITS,$OR_BITS); +my ($AND_MASK,$XOR_MASK,$OR_MASK); + +sub _base_len + { + # Set/get the BASE_LEN and assorted other, connected values. + # Used only by the testsuite, the set variant is used only by the BEGIN + # block below: + shift; + + my ($b, $int) = @_; + if (defined $b) + { + # avoid redefinitions + undef &_mul; + undef &_div; + + if ($] >= 5.008 && $int && $b > 7) + { + $BASE_LEN = $b; + *_mul = \&_mul_use_div_64; + *_div = \&_div_use_div_64; + $BASE = int("1e".$BASE_LEN); + $MAX_VAL = $BASE-1; + return $BASE_LEN unless wantarray; + return ($BASE_LEN, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN, $MAX_VAL, $BASE); + } + + # find whether we can use mul or div in mul()/div() + $BASE_LEN = $b+1; + my $caught = 0; + while (--$BASE_LEN > 5) + { + $BASE = int("1e".$BASE_LEN); + $RBASE = abs('1e-'.$BASE_LEN); # see USE_MUL + $caught = 0; + $caught += 1 if (int($BASE * $RBASE) != 1); # should be 1 + $caught += 2 if (int($BASE / $BASE) != 1); # should be 1 + last if $caught != 3; + } + $BASE = int("1e".$BASE_LEN); + $RBASE = abs('1e-'.$BASE_LEN); # see USE_MUL + $MAX_VAL = $BASE-1; + + # ($caught & 1) != 0 => cannot use MUL + # ($caught & 2) != 0 => cannot use DIV + if ($caught == 2) # 2 + { + # must USE_MUL since we cannot use DIV + *_mul = \&_mul_use_mul; + *_div = \&_div_use_mul; + } + else # 0 or 1 + { + # can USE_DIV instead + *_mul = \&_mul_use_div; + *_div = \&_div_use_div; + } + } + return $BASE_LEN unless wantarray; + return ($BASE_LEN, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN, $MAX_VAL, $BASE); + } + +sub _new + { + # (ref to string) return ref to num_array + # Convert a number from string format (without sign) to internal base + # 1ex format. Assumes normalized value as input. + my $il = length($_[1])-1; + + # < BASE_LEN due len-1 above + return [ int($_[1]) ] if $il < $BASE_LEN; # shortcut for short numbers + + # this leaves '00000' instead of int 0 and will be corrected after any op + [ reverse(unpack("a" . ($il % $BASE_LEN+1) + . ("a$BASE_LEN" x ($il / $BASE_LEN)), $_[1])) ]; + } + +BEGIN + { + # from Daniel Pfeiffer: determine largest group of digits that is precisely + # multipliable with itself plus carry + # Test now changed to expect the proper pattern, not a result off by 1 or 2 + my ($e, $num) = 3; # lowest value we will use is 3+1-1 = 3 + do + { + $num = ('9' x ++$e) + 0; + $num *= $num + 1.0; + } while ("$num" =~ /9{$e}0{$e}/); # must be a certain pattern + $e--; # last test failed, so retract one step + # the limits below brush the problems with the test above under the rug: + # the test should be able to find the proper $e automatically + $e = 5 if $^O =~ /^uts/; # UTS get's some special treatment + $e = 5 if $^O =~ /^unicos/; # unicos is also problematic (6 seems to work + # there, but we play safe) + + my $int = 0; + if ($e > 7) + { + use integer; + my $e1 = 7; + $num = 7; + do + { + $num = ('9' x ++$e1) + 0; + $num *= $num + 1; + } while ("$num" =~ /9{$e1}0{$e1}/); # must be a certain pattern + $e1--; # last test failed, so retract one step + if ($e1 > 7) + { + $int = 1; $e = $e1; + } + } + + __PACKAGE__->_base_len($e,$int); # set and store + + use integer; + # find out how many bits _and, _or and _xor can take (old default = 16) + # I don't think anybody has yet 128 bit scalars, so let's play safe. + local $^W = 0; # don't warn about 'nonportable number' + $AND_BITS = 15; $XOR_BITS = 15; $OR_BITS = 15; + + # find max bits, we will not go higher than numberofbits that fit into $BASE + # to make _and etc simpler (and faster for smaller, slower for large numbers) + my $max = 16; + while (2 ** $max < $BASE) { $max++; } + { + no integer; + $max = 16 if $] < 5.006; # older Perls might not take >16 too well + } + my ($x,$y,$z); + do { + $AND_BITS++; + $x = CORE::oct('0b' . '1' x $AND_BITS); $y = $x & $x; + $z = (2 ** $AND_BITS) - 1; + } while ($AND_BITS < $max && $x == $z && $y == $x); + $AND_BITS --; # retreat one step + do { + $XOR_BITS++; + $x = CORE::oct('0b' . '1' x $XOR_BITS); $y = $x ^ 0; + $z = (2 ** $XOR_BITS) - 1; + } while ($XOR_BITS < $max && $x == $z && $y == $x); + $XOR_BITS --; # retreat one step + do { + $OR_BITS++; + $x = CORE::oct('0b' . '1' x $OR_BITS); $y = $x | $x; + $z = (2 ** $OR_BITS) - 1; + } while ($OR_BITS < $max && $x == $z && $y == $x); + $OR_BITS --; # retreat one step + + $AND_MASK = __PACKAGE__->_new( ( 2 ** $AND_BITS )); + $XOR_MASK = __PACKAGE__->_new( ( 2 ** $XOR_BITS )); + $OR_MASK = __PACKAGE__->_new( ( 2 ** $OR_BITS )); + + # We can compute the approximate lenght no faster than the real length: + *_alen = \&_len; + } + +############################################################################### + +sub _zero + { + # create a zero + [ 0 ]; + } + +sub _one + { + # create a one + [ 1 ]; + } + +sub _two + { + # create a two (used internally for shifting) + [ 2 ]; + } + +sub _ten + { + # create a 10 (used internally for shifting) + [ 10 ]; + } + +sub _1ex + { + # create a 1Ex + my $rem = $_[1] % $BASE_LEN; # remainder + my $parts = $_[1] / $BASE_LEN; # parts + + # 000000, 000000, 100 + [ (0) x $parts, '1' . ('0' x $rem) ]; + } + +sub _copy + { + # make a true copy + [ @{$_[1]} ]; + } + +# catch and throw away +sub import { } + +############################################################################## +# convert back to string and number + +sub _str + { + # (ref to BINT) return num_str + # Convert number from internal base 100000 format to string format. + # internal format is always normalized (no leading zeros, "-0" => "+0") + my $ar = $_[1]; + + my $l = scalar @$ar; # number of parts + if ($l < 1) # should not happen + { + require Carp; + Carp::croak("$_[1] has no elements"); + } + + my $ret = ""; + # handle first one different to strip leading zeros from it (there are no + # leading zero parts in internal representation) + $l --; $ret .= int($ar->[$l]); $l--; + # Interestingly, the pre-padd method uses more time + # the old grep variant takes longer (14 vs. 10 sec) + my $z = '0' x ($BASE_LEN-1); + while ($l >= 0) + { + $ret .= substr($z.$ar->[$l],-$BASE_LEN); # fastest way I could think of + $l--; + } + $ret; + } + +sub _num + { + # Make a number (scalar int/float) from a BigInt object + my $x = $_[1]; + + return 0+$x->[0] if scalar @$x == 1; # below $BASE + my $fac = 1; + my $num = 0; + foreach (@$x) + { + $num += $fac*$_; $fac *= $BASE; + } + $num; + } + +############################################################################## +# actual math code + +sub _add + { + # (ref to int_num_array, ref to int_num_array) + # routine to add two base 1eX numbers + # stolen from Knuth Vol 2 Algorithm A pg 231 + # there are separate routines to add and sub as per Knuth pg 233 + # This routine clobbers up array x, but not y. + + my ($c,$x,$y) = @_; + + return $x if (@$y == 1) && $y->[0] == 0; # $x + 0 => $x + if ((@$x == 1) && $x->[0] == 0) # 0 + $y => $y->copy + { + # twice as slow as $x = [ @$y ], but nec. to retain $x as ref :( + @$x = @$y; return $x; + } + + # for each in Y, add Y to X and carry. If after that, something is left in + # X, foreach in X add carry to X and then return X, carry + # Trades one "$j++" for having to shift arrays + my $i; my $car = 0; my $j = 0; + for $i (@$y) + { + $x->[$j] -= $BASE if $car = (($x->[$j] += $i + $car) >= $BASE) ? 1 : 0; + $j++; + } + while ($car != 0) + { + $x->[$j] -= $BASE if $car = (($x->[$j] += $car) >= $BASE) ? 1 : 0; $j++; + } + $x; + } + +sub _inc + { + # (ref to int_num_array, ref to int_num_array) + # Add 1 to $x, modify $x in place + my ($c,$x) = @_; + + for my $i (@$x) + { + return $x if (($i += 1) < $BASE); # early out + $i = 0; # overflow, next + } + push @$x,1 if (($x->[-1] || 0) == 0); # last overflowed, so extend + $x; + } + +sub _dec + { + # (ref to int_num_array, ref to int_num_array) + # Sub 1 from $x, modify $x in place + my ($c,$x) = @_; + + my $MAX = $BASE-1; # since MAX_VAL based on BASE + for my $i (@$x) + { + last if (($i -= 1) >= 0); # early out + $i = $MAX; # underflow, next + } + pop @$x if $x->[-1] == 0 && @$x > 1; # last underflowed (but leave 0) + $x; + } + +sub _sub + { + # (ref to int_num_array, ref to int_num_array, swap) + # subtract base 1eX numbers -- stolen from Knuth Vol 2 pg 232, $x > $y + # subtract Y from X by modifying x in place + my ($c,$sx,$sy,$s) = @_; + + my $car = 0; my $i; my $j = 0; + if (!$s) + { + for $i (@$sx) + { + last unless defined $sy->[$j] || $car; + $i += $BASE if $car = (($i -= ($sy->[$j] || 0) + $car) < 0); $j++; + } + # might leave leading zeros, so fix that + return __strip_zeros($sx); + } + for $i (@$sx) + { + # we can't do an early out if $x is < than $y, since we + # need to copy the high chunks from $y. Found by Bob Mathews. + #last unless defined $sy->[$j] || $car; + $sy->[$j] += $BASE + if $car = (($sy->[$j] = $i-($sy->[$j]||0) - $car) < 0); + $j++; + } + # might leave leading zeros, so fix that + __strip_zeros($sy); + } + +sub _mul_use_mul + { + # (ref to int_num_array, ref to int_num_array) + # multiply two numbers in internal representation + # modifies first arg, second need not be different from first + my ($c,$xv,$yv) = @_; + + if (@$yv == 1) + { + # shortcut for two very short numbers (improved by Nathan Zook) + # works also if xv and yv are the same reference, and handles also $x == 0 + if (@$xv == 1) + { + if (($xv->[0] *= $yv->[0]) >= $BASE) + { + $xv->[0] = $xv->[0] - ($xv->[1] = int($xv->[0] * $RBASE)) * $BASE; + }; + return $xv; + } + # $x * 0 => 0 + if ($yv->[0] == 0) + { + @$xv = (0); + return $xv; + } + # multiply a large number a by a single element one, so speed up + my $y = $yv->[0]; my $car = 0; + foreach my $i (@$xv) + { + $i = $i * $y + $car; $car = int($i * $RBASE); $i -= $car * $BASE; + } + push @$xv, $car if $car != 0; + return $xv; + } + # shortcut for result $x == 0 => result = 0 + return $xv if ( ((@$xv == 1) && ($xv->[0] == 0)) ); + + # since multiplying $x with $x fails, make copy in this case + $yv = [@$xv] if $xv == $yv; # same references? + + my @prod = (); my ($prod,$car,$cty,$xi,$yi); + + for $xi (@$xv) + { + $car = 0; $cty = 0; + + # slow variant +# for $yi (@$yv) +# { +# $prod = $xi * $yi + ($prod[$cty] || 0) + $car; +# $prod[$cty++] = +# $prod - ($car = int($prod * RBASE)) * $BASE; # see USE_MUL +# } +# $prod[$cty] += $car if $car; # need really to check for 0? +# $xi = shift @prod; + + # faster variant + # looping through this if $xi == 0 is silly - so optimize it away! + $xi = (shift @prod || 0), next if $xi == 0; + for $yi (@$yv) + { + $prod = $xi * $yi + ($prod[$cty] || 0) + $car; +## this is actually a tad slower +## $prod = $prod[$cty]; $prod += ($car + $xi * $yi); # no ||0 here + $prod[$cty++] = + $prod - ($car = int($prod * $RBASE)) * $BASE; # see USE_MUL + } + $prod[$cty] += $car if $car; # need really to check for 0? + $xi = shift @prod || 0; # || 0 makes v5.005_3 happy + } + push @$xv, @prod; + # can't have leading zeros +# __strip_zeros($xv); + $xv; + } + +sub _mul_use_div_64 + { + # (ref to int_num_array, ref to int_num_array) + # multiply two numbers in internal representation + # modifies first arg, second need not be different from first + # works for 64 bit integer with "use integer" + my ($c,$xv,$yv) = @_; + + use integer; + if (@$yv == 1) + { + # shortcut for two small numbers, also handles $x == 0 + if (@$xv == 1) + { + # shortcut for two very short numbers (improved by Nathan Zook) + # works also if xv and yv are the same reference, and handles also $x == 0 + if (($xv->[0] *= $yv->[0]) >= $BASE) + { + $xv->[0] = + $xv->[0] - ($xv->[1] = $xv->[0] / $BASE) * $BASE; + }; + return $xv; + } + # $x * 0 => 0 + if ($yv->[0] == 0) + { + @$xv = (0); + return $xv; + } + # multiply a large number a by a single element one, so speed up + my $y = $yv->[0]; my $car = 0; + foreach my $i (@$xv) + { + #$i = $i * $y + $car; $car = $i / $BASE; $i -= $car * $BASE; + $i = $i * $y + $car; $i -= ($car = $i / $BASE) * $BASE; + } + push @$xv, $car if $car != 0; + return $xv; + } + # shortcut for result $x == 0 => result = 0 + return $xv if ( ((@$xv == 1) && ($xv->[0] == 0)) ); + + # since multiplying $x with $x fails, make copy in this case + $yv = [@$xv] if $xv == $yv; # same references? + + my @prod = (); my ($prod,$car,$cty,$xi,$yi); + for $xi (@$xv) + { + $car = 0; $cty = 0; + # looping through this if $xi == 0 is silly - so optimize it away! + $xi = (shift @prod || 0), next if $xi == 0; + for $yi (@$yv) + { + $prod = $xi * $yi + ($prod[$cty] || 0) + $car; + $prod[$cty++] = $prod - ($car = $prod / $BASE) * $BASE; + } + $prod[$cty] += $car if $car; # need really to check for 0? + $xi = shift @prod || 0; # || 0 makes v5.005_3 happy + } + push @$xv, @prod; + $xv; + } + +sub _mul_use_div + { + # (ref to int_num_array, ref to int_num_array) + # multiply two numbers in internal representation + # modifies first arg, second need not be different from first + my ($c,$xv,$yv) = @_; + + if (@$yv == 1) + { + # shortcut for two small numbers, also handles $x == 0 + if (@$xv == 1) + { + # shortcut for two very short numbers (improved by Nathan Zook) + # works also if xv and yv are the same reference, and handles also $x == 0 + if (($xv->[0] *= $yv->[0]) >= $BASE) + { + $xv->[0] = + $xv->[0] - ($xv->[1] = int($xv->[0] / $BASE)) * $BASE; + }; + return $xv; + } + # $x * 0 => 0 + if ($yv->[0] == 0) + { + @$xv = (0); + return $xv; + } + # multiply a large number a by a single element one, so speed up + my $y = $yv->[0]; my $car = 0; + foreach my $i (@$xv) + { + $i = $i * $y + $car; $car = int($i / $BASE); $i -= $car * $BASE; + # This (together with use integer;) does not work on 32-bit Perls + #$i = $i * $y + $car; $i -= ($car = $i / $BASE) * $BASE; + } + push @$xv, $car if $car != 0; + return $xv; + } + # shortcut for result $x == 0 => result = 0 + return $xv if ( ((@$xv == 1) && ($xv->[0] == 0)) ); + + # since multiplying $x with $x fails, make copy in this case + $yv = [@$xv] if $xv == $yv; # same references? + + my @prod = (); my ($prod,$car,$cty,$xi,$yi); + for $xi (@$xv) + { + $car = 0; $cty = 0; + # looping through this if $xi == 0 is silly - so optimize it away! + $xi = (shift @prod || 0), next if $xi == 0; + for $yi (@$yv) + { + $prod = $xi * $yi + ($prod[$cty] || 0) + $car; + $prod[$cty++] = $prod - ($car = int($prod / $BASE)) * $BASE; + } + $prod[$cty] += $car if $car; # need really to check for 0? + $xi = shift @prod || 0; # || 0 makes v5.005_3 happy + } + push @$xv, @prod; + # can't have leading zeros +# __strip_zeros($xv); + $xv; + } + +sub _div_use_mul + { + # ref to array, ref to array, modify first array and return remainder if + # in list context + + # see comments in _div_use_div() for more explanations + + my ($c,$x,$yorg) = @_; + + # the general div algorithmn here is about O(N*N) and thus quite slow, so + # we first check for some special cases and use shortcuts to handle them. + + # This works, because we store the numbers in a chunked format where each + # element contains 5..7 digits (depending on system). + + # if both numbers have only one element: + if (@$x == 1 && @$yorg == 1) + { + # shortcut, $yorg and $x are two small numbers + if (wantarray) + { + my $r = [ $x->[0] % $yorg->[0] ]; + $x->[0] = int($x->[0] / $yorg->[0]); + return ($x,$r); + } + else + { + $x->[0] = int($x->[0] / $yorg->[0]); + return $x; + } + } + + # if x has more than one, but y has only one element: + if (@$yorg == 1) + { + my $rem; + $rem = _mod($c,[ @$x ],$yorg) if wantarray; + + # shortcut, $y is < $BASE + my $j = scalar @$x; my $r = 0; + my $y = $yorg->[0]; my $b; + while ($j-- > 0) + { + $b = $r * $BASE + $x->[$j]; + $x->[$j] = int($b/$y); + $r = $b % $y; + } + pop @$x if @$x > 1 && $x->[-1] == 0; # splice up a leading zero + return ($x,$rem) if wantarray; + return $x; + } + + # now x and y have more than one element + + # check whether y has more elements than x, if yet, the result will be 0 + if (@$yorg > @$x) + { + my $rem; + $rem = [@$x] if wantarray; # make copy + splice (@$x,1); # keep ref to original array + $x->[0] = 0; # set to 0 + return ($x,$rem) if wantarray; # including remainder? + return $x; # only x, which is [0] now + } + # check whether the numbers have the same number of elements, in that case + # the result will fit into one element and can be computed efficiently + if (@$yorg == @$x) + { + my $rem; + # if $yorg has more digits than $x (it's leading element is longer than + # the one from $x), the result will also be 0: + if (length(int($yorg->[-1])) > length(int($x->[-1]))) + { + $rem = [@$x] if wantarray; # make copy + splice (@$x,1); # keep ref to org array + $x->[0] = 0; # set to 0 + return ($x,$rem) if wantarray; # including remainder? + return $x; + } + # now calculate $x / $yorg + if (length(int($yorg->[-1])) == length(int($x->[-1]))) + { + # same length, so make full compare + + my $a = 0; my $j = scalar @$x - 1; + # manual way (abort if unequal, good for early ne) + while ($j >= 0) + { + last if ($a = $x->[$j] - $yorg->[$j]); $j--; + } + # $a contains the result of the compare between X and Y + # a < 0: x < y, a == 0: x == y, a > 0: x > y + if ($a <= 0) + { + $rem = [ 0 ]; # a = 0 => x == y => rem 0 + $rem = [@$x] if $a != 0; # a < 0 => x < y => rem = x + splice(@$x,1); # keep single element + $x->[0] = 0; # if $a < 0 + $x->[0] = 1 if $a == 0; # $x == $y + return ($x,$rem) if wantarray; + return $x; + } + # $x >= $y, so proceed normally + } + } + + # all other cases: + + my $y = [ @$yorg ]; # always make copy to preserve + + my ($car,$bar,$prd,$dd,$xi,$yi,@q,$v2,$v1,@d,$tmp,$q,$u2,$u1,$u0); + + $car = $bar = $prd = 0; + if (($dd = int($BASE/($y->[-1]+1))) != 1) + { + for $xi (@$x) + { + $xi = $xi * $dd + $car; + $xi -= ($car = int($xi * $RBASE)) * $BASE; # see USE_MUL + } + push(@$x, $car); $car = 0; + for $yi (@$y) + { + $yi = $yi * $dd + $car; + $yi -= ($car = int($yi * $RBASE)) * $BASE; # see USE_MUL + } + } + else + { + push(@$x, 0); + } + @q = (); ($v2,$v1) = @$y[-2,-1]; + $v2 = 0 unless $v2; + while ($#$x > $#$y) + { + ($u2,$u1,$u0) = @$x[-3..-1]; + $u2 = 0 unless $u2; + #warn "oups v1 is 0, u0: $u0 $y->[-2] $y->[-1] l ",scalar @$y,"\n" + # if $v1 == 0; + $q = (($u0 == $v1) ? $MAX_VAL : int(($u0*$BASE+$u1)/$v1)); + --$q while ($v2*$q > ($u0*$BASE+$u1-$q*$v1)*$BASE+$u2); + if ($q) + { + ($car, $bar) = (0,0); + for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) + { + $prd = $q * $y->[$yi] + $car; + $prd -= ($car = int($prd * $RBASE)) * $BASE; # see USE_MUL + $x->[$xi] += $BASE if ($bar = (($x->[$xi] -= $prd + $bar) < 0)); + } + if ($x->[-1] < $car + $bar) + { + $car = 0; --$q; + for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) + { + $x->[$xi] -= $BASE + if ($car = (($x->[$xi] += $y->[$yi] + $car) >= $BASE)); + } + } + } + pop(@$x); + unshift(@q, $q); + } + if (wantarray) + { + @d = (); + if ($dd != 1) + { + $car = 0; + for $xi (reverse @$x) + { + $prd = $car * $BASE + $xi; + $car = $prd - ($tmp = int($prd / $dd)) * $dd; # see USE_MUL + unshift(@d, $tmp); + } + } + else + { + @d = @$x; + } + @$x = @q; + my $d = \@d; + __strip_zeros($x); + __strip_zeros($d); + return ($x,$d); + } + @$x = @q; + __strip_zeros($x); + $x; + } + +sub _div_use_div_64 + { + # ref to array, ref to array, modify first array and return remainder if + # in list context + # This version works on 64 bit integers + my ($c,$x,$yorg) = @_; + + use integer; + # the general div algorithmn here is about O(N*N) and thus quite slow, so + # we first check for some special cases and use shortcuts to handle them. + + # This works, because we store the numbers in a chunked format where each + # element contains 5..7 digits (depending on system). + + # if both numbers have only one element: + if (@$x == 1 && @$yorg == 1) + { + # shortcut, $yorg and $x are two small numbers + if (wantarray) + { + my $r = [ $x->[0] % $yorg->[0] ]; + $x->[0] = int($x->[0] / $yorg->[0]); + return ($x,$r); + } + else + { + $x->[0] = int($x->[0] / $yorg->[0]); + return $x; + } + } + # if x has more than one, but y has only one element: + if (@$yorg == 1) + { + my $rem; + $rem = _mod($c,[ @$x ],$yorg) if wantarray; + + # shortcut, $y is < $BASE + my $j = scalar @$x; my $r = 0; + my $y = $yorg->[0]; my $b; + while ($j-- > 0) + { + $b = $r * $BASE + $x->[$j]; + $x->[$j] = int($b/$y); + $r = $b % $y; + } + pop @$x if @$x > 1 && $x->[-1] == 0; # splice up a leading zero + return ($x,$rem) if wantarray; + return $x; + } + # now x and y have more than one element + + # check whether y has more elements than x, if yet, the result will be 0 + if (@$yorg > @$x) + { + my $rem; + $rem = [@$x] if wantarray; # make copy + splice (@$x,1); # keep ref to original array + $x->[0] = 0; # set to 0 + return ($x,$rem) if wantarray; # including remainder? + return $x; # only x, which is [0] now + } + # check whether the numbers have the same number of elements, in that case + # the result will fit into one element and can be computed efficiently + if (@$yorg == @$x) + { + my $rem; + # if $yorg has more digits than $x (it's leading element is longer than + # the one from $x), the result will also be 0: + if (length(int($yorg->[-1])) > length(int($x->[-1]))) + { + $rem = [@$x] if wantarray; # make copy + splice (@$x,1); # keep ref to org array + $x->[0] = 0; # set to 0 + return ($x,$rem) if wantarray; # including remainder? + return $x; + } + # now calculate $x / $yorg + + if (length(int($yorg->[-1])) == length(int($x->[-1]))) + { + # same length, so make full compare + + my $a = 0; my $j = scalar @$x - 1; + # manual way (abort if unequal, good for early ne) + while ($j >= 0) + { + last if ($a = $x->[$j] - $yorg->[$j]); $j--; + } + # $a contains the result of the compare between X and Y + # a < 0: x < y, a == 0: x == y, a > 0: x > y + if ($a <= 0) + { + $rem = [ 0 ]; # a = 0 => x == y => rem 0 + $rem = [@$x] if $a != 0; # a < 0 => x < y => rem = x + splice(@$x,1); # keep single element + $x->[0] = 0; # if $a < 0 + $x->[0] = 1 if $a == 0; # $x == $y + return ($x,$rem) if wantarray; # including remainder? + return $x; + } + # $x >= $y, so proceed normally + + } + } + + # all other cases: + + my $y = [ @$yorg ]; # always make copy to preserve + + my ($car,$bar,$prd,$dd,$xi,$yi,@q,$v2,$v1,@d,$tmp,$q,$u2,$u1,$u0); + + $car = $bar = $prd = 0; + if (($dd = int($BASE/($y->[-1]+1))) != 1) + { + for $xi (@$x) + { + $xi = $xi * $dd + $car; + $xi -= ($car = int($xi / $BASE)) * $BASE; + } + push(@$x, $car); $car = 0; + for $yi (@$y) + { + $yi = $yi * $dd + $car; + $yi -= ($car = int($yi / $BASE)) * $BASE; + } + } + else + { + push(@$x, 0); + } + + # @q will accumulate the final result, $q contains the current computed + # part of the final result + + @q = (); ($v2,$v1) = @$y[-2,-1]; + $v2 = 0 unless $v2; + while ($#$x > $#$y) + { + ($u2,$u1,$u0) = @$x[-3..-1]; + $u2 = 0 unless $u2; + #warn "oups v1 is 0, u0: $u0 $y->[-2] $y->[-1] l ",scalar @$y,"\n" + # if $v1 == 0; + $q = (($u0 == $v1) ? $MAX_VAL : int(($u0*$BASE+$u1)/$v1)); + --$q while ($v2*$q > ($u0*$BASE+$u1-$q*$v1)*$BASE+$u2); + if ($q) + { + ($car, $bar) = (0,0); + for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) + { + $prd = $q * $y->[$yi] + $car; + $prd -= ($car = int($prd / $BASE)) * $BASE; + $x->[$xi] += $BASE if ($bar = (($x->[$xi] -= $prd + $bar) < 0)); + } + if ($x->[-1] < $car + $bar) + { + $car = 0; --$q; + for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) + { + $x->[$xi] -= $BASE + if ($car = (($x->[$xi] += $y->[$yi] + $car) >= $BASE)); + } + } + } + pop(@$x); unshift(@q, $q); + } + if (wantarray) + { + @d = (); + if ($dd != 1) + { + $car = 0; + for $xi (reverse @$x) + { + $prd = $car * $BASE + $xi; + $car = $prd - ($tmp = int($prd / $dd)) * $dd; + unshift(@d, $tmp); + } + } + else + { + @d = @$x; + } + @$x = @q; + my $d = \@d; + __strip_zeros($x); + __strip_zeros($d); + return ($x,$d); + } + @$x = @q; + __strip_zeros($x); + $x; + } + +sub _div_use_div + { + # ref to array, ref to array, modify first array and return remainder if + # in list context + my ($c,$x,$yorg) = @_; + + # the general div algorithmn here is about O(N*N) and thus quite slow, so + # we first check for some special cases and use shortcuts to handle them. + + # This works, because we store the numbers in a chunked format where each + # element contains 5..7 digits (depending on system). + + # if both numbers have only one element: + if (@$x == 1 && @$yorg == 1) + { + # shortcut, $yorg and $x are two small numbers + if (wantarray) + { + my $r = [ $x->[0] % $yorg->[0] ]; + $x->[0] = int($x->[0] / $yorg->[0]); + return ($x,$r); + } + else + { + $x->[0] = int($x->[0] / $yorg->[0]); + return $x; + } + } + # if x has more than one, but y has only one element: + if (@$yorg == 1) + { + my $rem; + $rem = _mod($c,[ @$x ],$yorg) if wantarray; + + # shortcut, $y is < $BASE + my $j = scalar @$x; my $r = 0; + my $y = $yorg->[0]; my $b; + while ($j-- > 0) + { + $b = $r * $BASE + $x->[$j]; + $x->[$j] = int($b/$y); + $r = $b % $y; + } + pop @$x if @$x > 1 && $x->[-1] == 0; # splice up a leading zero + return ($x,$rem) if wantarray; + return $x; + } + # now x and y have more than one element + + # check whether y has more elements than x, if yet, the result will be 0 + if (@$yorg > @$x) + { + my $rem; + $rem = [@$x] if wantarray; # make copy + splice (@$x,1); # keep ref to original array + $x->[0] = 0; # set to 0 + return ($x,$rem) if wantarray; # including remainder? + return $x; # only x, which is [0] now + } + # check whether the numbers have the same number of elements, in that case + # the result will fit into one element and can be computed efficiently + if (@$yorg == @$x) + { + my $rem; + # if $yorg has more digits than $x (it's leading element is longer than + # the one from $x), the result will also be 0: + if (length(int($yorg->[-1])) > length(int($x->[-1]))) + { + $rem = [@$x] if wantarray; # make copy + splice (@$x,1); # keep ref to org array + $x->[0] = 0; # set to 0 + return ($x,$rem) if wantarray; # including remainder? + return $x; + } + # now calculate $x / $yorg + + if (length(int($yorg->[-1])) == length(int($x->[-1]))) + { + # same length, so make full compare + + my $a = 0; my $j = scalar @$x - 1; + # manual way (abort if unequal, good for early ne) + while ($j >= 0) + { + last if ($a = $x->[$j] - $yorg->[$j]); $j--; + } + # $a contains the result of the compare between X and Y + # a < 0: x < y, a == 0: x == y, a > 0: x > y + if ($a <= 0) + { + $rem = [ 0 ]; # a = 0 => x == y => rem 0 + $rem = [@$x] if $a != 0; # a < 0 => x < y => rem = x + splice(@$x,1); # keep single element + $x->[0] = 0; # if $a < 0 + $x->[0] = 1 if $a == 0; # $x == $y + return ($x,$rem) if wantarray; # including remainder? + return $x; + } + # $x >= $y, so proceed normally + + } + } + + # all other cases: + + my $y = [ @$yorg ]; # always make copy to preserve + + my ($car,$bar,$prd,$dd,$xi,$yi,@q,$v2,$v1,@d,$tmp,$q,$u2,$u1,$u0); + + $car = $bar = $prd = 0; + if (($dd = int($BASE/($y->[-1]+1))) != 1) + { + for $xi (@$x) + { + $xi = $xi * $dd + $car; + $xi -= ($car = int($xi / $BASE)) * $BASE; + } + push(@$x, $car); $car = 0; + for $yi (@$y) + { + $yi = $yi * $dd + $car; + $yi -= ($car = int($yi / $BASE)) * $BASE; + } + } + else + { + push(@$x, 0); + } + + # @q will accumulate the final result, $q contains the current computed + # part of the final result + + @q = (); ($v2,$v1) = @$y[-2,-1]; + $v2 = 0 unless $v2; + while ($#$x > $#$y) + { + ($u2,$u1,$u0) = @$x[-3..-1]; + $u2 = 0 unless $u2; + #warn "oups v1 is 0, u0: $u0 $y->[-2] $y->[-1] l ",scalar @$y,"\n" + # if $v1 == 0; + $q = (($u0 == $v1) ? $MAX_VAL : int(($u0*$BASE+$u1)/$v1)); + --$q while ($v2*$q > ($u0*$BASE+$u1-$q*$v1)*$BASE+$u2); + if ($q) + { + ($car, $bar) = (0,0); + for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) + { + $prd = $q * $y->[$yi] + $car; + $prd -= ($car = int($prd / $BASE)) * $BASE; + $x->[$xi] += $BASE if ($bar = (($x->[$xi] -= $prd + $bar) < 0)); + } + if ($x->[-1] < $car + $bar) + { + $car = 0; --$q; + for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) + { + $x->[$xi] -= $BASE + if ($car = (($x->[$xi] += $y->[$yi] + $car) >= $BASE)); + } + } + } + pop(@$x); unshift(@q, $q); + } + if (wantarray) + { + @d = (); + if ($dd != 1) + { + $car = 0; + for $xi (reverse @$x) + { + $prd = $car * $BASE + $xi; + $car = $prd - ($tmp = int($prd / $dd)) * $dd; + unshift(@d, $tmp); + } + } + else + { + @d = @$x; + } + @$x = @q; + my $d = \@d; + __strip_zeros($x); + __strip_zeros($d); + return ($x,$d); + } + @$x = @q; + __strip_zeros($x); + $x; + } + +############################################################################## +# testing + +sub _acmp + { + # internal absolute post-normalized compare (ignore signs) + # ref to array, ref to array, return <0, 0, >0 + # arrays must have at least one entry; this is not checked for + my ($c,$cx,$cy) = @_; + + # shortcut for short numbers + return (($cx->[0] <=> $cy->[0]) <=> 0) + if scalar @$cx == scalar @$cy && scalar @$cx == 1; + + # fast comp based on number of array elements (aka pseudo-length) + my $lxy = (scalar @$cx - scalar @$cy) + # or length of first element if same number of elements (aka difference 0) + || + # need int() here because sometimes the last element is '00018' vs '18' + (length(int($cx->[-1])) - length(int($cy->[-1]))); + return -1 if $lxy < 0; # already differs, ret + return 1 if $lxy > 0; # ditto + + # manual way (abort if unequal, good for early ne) + my $a; my $j = scalar @$cx; + while (--$j >= 0) + { + last if ($a = $cx->[$j] - $cy->[$j]); + } + $a <=> 0; + } + +sub _len + { + # compute number of digits in base 10 + + # int() because add/sub sometimes leaves strings (like '00005') instead of + # '5' in this place, thus causing length() to report wrong length + my $cx = $_[1]; + + (@$cx-1)*$BASE_LEN+length(int($cx->[-1])); + } + +sub _digit + { + # return the nth digit, negative values count backward + # zero is rightmost, so _digit(123,0) will give 3 + my ($c,$x,$n) = @_; + + my $len = _len('',$x); + + $n = $len+$n if $n < 0; # -1 last, -2 second-to-last + $n = abs($n); # if negative was too big + $len--; $n = $len if $n > $len; # n to big? + + my $elem = int($n / $BASE_LEN); # which array element + my $digit = $n % $BASE_LEN; # which digit in this element + $elem = '0' x $BASE_LEN . @$x[$elem]; # get element padded with 0's + substr($elem,-$digit-1,1); + } + +sub _zeros + { + # return amount of trailing zeros in decimal + # check each array elem in _m for having 0 at end as long as elem == 0 + # Upon finding a elem != 0, stop + my $x = $_[1]; + + return 0 if scalar @$x == 1 && $x->[0] == 0; + + my $zeros = 0; my $elem; + foreach my $e (@$x) + { + if ($e != 0) + { + $elem = "$e"; # preserve x + $elem =~ s/.*?(0*$)/$1/; # strip anything not zero + $zeros *= $BASE_LEN; # elems * 5 + $zeros += length($elem); # count trailing zeros + last; # early out + } + $zeros ++; # real else branch: 50% slower! + } + $zeros; + } + +############################################################################## +# _is_* routines + +sub _is_zero + { + # return true if arg is zero + (((scalar @{$_[1]} == 1) && ($_[1]->[0] == 0))) <=> 0; + } + +sub _is_even + { + # return true if arg is even + (!($_[1]->[0] & 1)) <=> 0; + } + +sub _is_odd + { + # return true if arg is even + (($_[1]->[0] & 1)) <=> 0; + } + +sub _is_one + { + # return true if arg is one + (scalar @{$_[1]} == 1) && ($_[1]->[0] == 1) <=> 0; + } + +sub _is_two + { + # return true if arg is two + (scalar @{$_[1]} == 1) && ($_[1]->[0] == 2) <=> 0; + } + +sub _is_ten + { + # return true if arg is ten + (scalar @{$_[1]} == 1) && ($_[1]->[0] == 10) <=> 0; + } + +sub __strip_zeros + { + # internal normalization function that strips leading zeros from the array + # args: ref to array + my $s = shift; + + my $cnt = scalar @$s; # get count of parts + my $i = $cnt-1; + push @$s,0 if $i < 0; # div might return empty results, so fix it + + return $s if @$s == 1; # early out + + #print "strip: cnt $cnt i $i\n"; + # '0', '3', '4', '0', '0', + # 0 1 2 3 4 + # cnt = 5, i = 4 + # i = 4 + # i = 3 + # => fcnt = cnt - i (5-2 => 3, cnt => 5-1 = 4, throw away from 4th pos) + # >= 1: skip first part (this can be zero) + while ($i > 0) { last if $s->[$i] != 0; $i--; } + $i++; splice @$s,$i if ($i < $cnt); # $i cant be 0 + $s; + } + +############################################################################### +# check routine to test internal state for corruptions + +sub _check + { + # used by the test suite + my $x = $_[1]; + + return "$x is not a reference" if !ref($x); + + # are all parts are valid? + my $i = 0; my $j = scalar @$x; my ($e,$try); + while ($i < $j) + { + $e = $x->[$i]; $e = 'undef' unless defined $e; + $try = '=~ /^[\+]?[0-9]+\$/; '."($x, $e)"; + last if $e !~ /^[+]?[0-9]+$/; + $try = '=~ /^[\+]?[0-9]+\$/; '."($x, $e) (stringify)"; + last if "$e" !~ /^[+]?[0-9]+$/; + $try = '=~ /^[\+]?[0-9]+\$/; '."($x, $e) (cat-stringify)"; + last if '' . "$e" !~ /^[+]?[0-9]+$/; + $try = ' < 0 || >= $BASE; '."($x, $e)"; + last if $e <0 || $e >= $BASE; + # this test is disabled, since new/bnorm and certain ops (like early out + # in add/sub) are allowed/expected to leave '00000' in some elements + #$try = '=~ /^00+/; '."($x, $e)"; + #last if $e =~ /^00+/; + $i++; + } + return "Illegal part '$e' at pos $i (tested: $try)" if $i < $j; + 0; + } + + +############################################################################### + +sub _mod + { + # if possible, use mod shortcut + my ($c,$x,$yo) = @_; + + # slow way since $y to big + if (scalar @$yo > 1) + { + my ($xo,$rem) = _div($c,$x,$yo); + return $rem; + } + + my $y = $yo->[0]; + # both are single element arrays + if (scalar @$x == 1) + { + $x->[0] %= $y; + return $x; + } + + # @y is a single element, but @x has more than one element + my $b = $BASE % $y; + if ($b == 0) + { + # when BASE % Y == 0 then (B * BASE) % Y == 0 + # (B * BASE) % $y + A % Y => A % Y + # so need to consider only last element: O(1) + $x->[0] %= $y; + } + elsif ($b == 1) + { + # else need to go through all elements: O(N), but loop is a bit simplified + my $r = 0; + foreach (@$x) + { + $r = ($r + $_) % $y; # not much faster, but heh... + #$r += $_ % $y; $r %= $y; + } + $r = 0 if $r == $y; + $x->[0] = $r; + } + else + { + # else need to go through all elements: O(N) + my $r = 0; my $bm = 1; + foreach (@$x) + { + $r = ($_ * $bm + $r) % $y; + $bm = ($bm * $b) % $y; + + #$r += ($_ % $y) * $bm; + #$bm *= $b; + #$bm %= $y; + #$r %= $y; + } + $r = 0 if $r == $y; + $x->[0] = $r; + } + splice (@$x,1); # keep one element of $x + $x; + } + +############################################################################## +# shifts + +sub _rsft + { + my ($c,$x,$y,$n) = @_; + + if ($n != 10) + { + $n = _new($c,$n); return _div($c,$x, _pow($c,$n,$y)); + } + + # shortcut (faster) for shifting by 10) + # multiples of $BASE_LEN + my $dst = 0; # destination + my $src = _num($c,$y); # as normal int + my $xlen = (@$x-1)*$BASE_LEN+length(int($x->[-1])); # len of x in digits + if ($src >= $xlen or ($src == $xlen and ! defined $x->[1])) + { + # 12345 67890 shifted right by more than 10 digits => 0 + splice (@$x,1); # leave only one element + $x->[0] = 0; # set to zero + return $x; + } + my $rem = $src % $BASE_LEN; # remainder to shift + $src = int($src / $BASE_LEN); # source + if ($rem == 0) + { + splice (@$x,0,$src); # even faster, 38.4 => 39.3 + } + else + { + my $len = scalar @$x - $src; # elems to go + my $vd; my $z = '0'x $BASE_LEN; + $x->[scalar @$x] = 0; # avoid || 0 test inside loop + while ($dst < $len) + { + $vd = $z.$x->[$src]; + $vd = substr($vd,-$BASE_LEN,$BASE_LEN-$rem); + $src++; + $vd = substr($z.$x->[$src],-$rem,$rem) . $vd; + $vd = substr($vd,-$BASE_LEN,$BASE_LEN) if length($vd) > $BASE_LEN; + $x->[$dst] = int($vd); + $dst++; + } + splice (@$x,$dst) if $dst > 0; # kill left-over array elems + pop @$x if $x->[-1] == 0 && @$x > 1; # kill last element if 0 + } # else rem == 0 + $x; + } + +sub _lsft + { + my ($c,$x,$y,$n) = @_; + + if ($n != 10) + { + $n = _new($c,$n); return _mul($c,$x, _pow($c,$n,$y)); + } + + # shortcut (faster) for shifting by 10) since we are in base 10eX + # multiples of $BASE_LEN: + my $src = scalar @$x; # source + my $len = _num($c,$y); # shift-len as normal int + my $rem = $len % $BASE_LEN; # remainder to shift + my $dst = $src + int($len/$BASE_LEN); # destination + my $vd; # further speedup + $x->[$src] = 0; # avoid first ||0 for speed + my $z = '0' x $BASE_LEN; + while ($src >= 0) + { + $vd = $x->[$src]; $vd = $z.$vd; + $vd = substr($vd,-$BASE_LEN+$rem,$BASE_LEN-$rem); + $vd .= $src > 0 ? substr($z.$x->[$src-1],-$BASE_LEN,$rem) : '0' x $rem; + $vd = substr($vd,-$BASE_LEN,$BASE_LEN) if length($vd) > $BASE_LEN; + $x->[$dst] = int($vd); + $dst--; $src--; + } + # set lowest parts to 0 + while ($dst >= 0) { $x->[$dst--] = 0; } + # fix spurios last zero element + splice @$x,-1 if $x->[-1] == 0; + $x; + } + +sub _pow + { + # power of $x to $y + # ref to array, ref to array, return ref to array + my ($c,$cx,$cy) = @_; + + if (scalar @$cy == 1 && $cy->[0] == 0) + { + splice (@$cx,1); $cx->[0] = 1; # y == 0 => x => 1 + return $cx; + } + if ((scalar @$cx == 1 && $cx->[0] == 1) || # x == 1 + (scalar @$cy == 1 && $cy->[0] == 1)) # or y == 1 + { + return $cx; + } + if (scalar @$cx == 1 && $cx->[0] == 0) + { + splice (@$cx,1); $cx->[0] = 0; # 0 ** y => 0 (if not y <= 0) + return $cx; + } + + my $pow2 = _one(); + + my $y_bin = _as_bin($c,$cy); $y_bin =~ s/^0b//; + my $len = length($y_bin); + while (--$len > 0) + { + _mul($c,$pow2,$cx) if substr($y_bin,$len,1) eq '1'; # is odd? + _mul($c,$cx,$cx); + } + + _mul($c,$cx,$pow2); + $cx; + } + +sub _nok + { + # n over k + # ref to array, return ref to array + my ($c,$n,$k) = @_; + + # ( 7 ) 7! 7*6*5 * 4*3*2*1 7 * 6 * 5 + # ( - ) = --------- = --------------- = --------- + # ( 3 ) 3! (7-3)! 3*2*1 * 4*3*2*1 3 * 2 * 1 + + # compute n - k + 2 (so we start with 5 in the example above) + my $x = _copy($c,$n); + + _sub($c,$n,$k); + if (!_is_one($c,$n)) + { + _inc($c,$n); + my $f = _copy($c,$n); _inc($c,$f); # n = 5, f = 6, d = 2 + my $d = _two($c); + while (_acmp($c,$f,$x) <= 0) # f < n ? + { + # n = (n * f / d) == 5 * 6 / 2 => n == 3 + $n = _mul($c,$n,$f); $n = _div($c,$n,$d); + # f = 7, d = 3 + _inc($c,$f); _inc($c,$d); + } + } + else + { + # keep ref to $n and set it to 1 + splice (@$n,1); $n->[0] = 1; + } + $n; + } + +my @factorials = ( + 1, + 1, + 2, + 2*3, + 2*3*4, + 2*3*4*5, + 2*3*4*5*6, + 2*3*4*5*6*7, +); + +sub _fac + { + # factorial of $x + # ref to array, return ref to array + my ($c,$cx) = @_; + + if ((@$cx == 1) && ($cx->[0] <= 7)) + { + $cx->[0] = $factorials[$cx->[0]]; # 0 => 1, 1 => 1, 2 => 2 etc. + return $cx; + } + + if ((@$cx == 1) && # we do this only if $x >= 12 and $x <= 7000 + ($cx->[0] >= 12 && $cx->[0] < 7000)) + { + + # Calculate (k-j) * (k-j+1) ... k .. (k+j-1) * (k + j) + # See http://blogten.blogspot.com/2007/01/calculating-n.html + # The above series can be expressed as factors: + # k * k - (j - i) * 2 + # We cache k*k, and calculate (j * j) as the sum of the first j odd integers + + # This will not work when N exceeds the storage of a Perl scalar, however, + # in this case the algorithm would be way to slow to terminate, anyway. + + # As soon as the last element of $cx is 0, we split it up and remember + # how many zeors we got so far. The reason is that n! will accumulate + # zeros at the end rather fast. + my $zero_elements = 0; + + # If n is even, set n = n -1 + my $k = _num($c,$cx); my $even = 1; + if (($k & 1) == 0) + { + $even = $k; $k --; + } + # set k to the center point + $k = ($k + 1) / 2; +# print "k $k even: $even\n"; + # now calculate k * k + my $k2 = $k * $k; + my $odd = 1; my $sum = 1; + my $i = $k - 1; + # keep reference to x + my $new_x = _new($c, $k * $even); + @$cx = @$new_x; + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } +# print STDERR "x = ", _str($c,$cx),"\n"; + my $BASE2 = int(sqrt($BASE))-1; + my $j = 1; + while ($j <= $i) + { + my $m = ($k2 - $sum); $odd += 2; $sum += $odd; $j++; + while ($j <= $i && ($m < $BASE2) && (($k2 - $sum) < $BASE2)) + { + $m *= ($k2 - $sum); + $odd += 2; $sum += $odd; $j++; +# print STDERR "\n k2 $k2 m $m sum $sum odd $odd\n"; sleep(1); + } + if ($m < $BASE) + { + _mul($c,$cx,[$m]); + } + else + { + _mul($c,$cx,$c->_new($m)); + } + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } +# print STDERR "Calculate $k2 - $sum = $m (x = ", _str($c,$cx),")\n"; + } + # multiply in the zeros again + unshift @$cx, (0) x $zero_elements; + return $cx; + } + + # go forward until $base is exceeded + # limit is either $x steps (steps == 100 means a result always too high) or + # $base. + my $steps = 100; $steps = $cx->[0] if @$cx == 1; + my $r = 2; my $cf = 3; my $step = 2; my $last = $r; + while ($r*$cf < $BASE && $step < $steps) + { + $last = $r; $r *= $cf++; $step++; + } + if ((@$cx == 1) && $step == $cx->[0]) + { + # completely done, so keep reference to $x and return + $cx->[0] = $r; + return $cx; + } + + # now we must do the left over steps + my $n; # steps still to do + if (scalar @$cx == 1) + { + $n = $cx->[0]; + } + else + { + $n = _copy($c,$cx); + } + + # Set $cx to the last result below $BASE (but keep ref to $x) + $cx->[0] = $last; splice (@$cx,1); + # As soon as the last element of $cx is 0, we split it up and remember + # how many zeors we got so far. The reason is that n! will accumulate + # zeros at the end rather fast. + my $zero_elements = 0; + + # do left-over steps fit into a scalar? + if (ref $n eq 'ARRAY') + { + # No, so use slower inc() & cmp() + # ($n is at least $BASE here) + my $base_2 = int(sqrt($BASE)) - 1; + #print STDERR "base_2: $base_2\n"; + while ($step < $base_2) + { + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } + my $b = $step * ($step + 1); $step += 2; + _mul($c,$cx,[$b]); + } + $step = [$step]; + while (_acmp($c,$step,$n) <= 0) + { + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } + _mul($c,$cx,$step); _inc($c,$step); + } + } + else + { + # Yes, so we can speed it up slightly + +# print "# left over steps $n\n"; + + my $base_4 = int(sqrt(sqrt($BASE))) - 2; + #print STDERR "base_4: $base_4\n"; + my $n4 = $n - 4; + while ($step < $n4 && $step < $base_4) + { + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } + my $b = $step * ($step + 1); $step += 2; $b *= $step * ($step + 1); $step += 2; + _mul($c,$cx,[$b]); + } + my $base_2 = int(sqrt($BASE)) - 1; + my $n2 = $n - 2; + #print STDERR "base_2: $base_2\n"; + while ($step < $n2 && $step < $base_2) + { + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } + my $b = $step * ($step + 1); $step += 2; + _mul($c,$cx,[$b]); + } + # do what's left over + while ($step <= $n) + { + _mul($c,$cx,[$step]); $step++; + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } + } + } + # multiply in the zeros again + unshift @$cx, (0) x $zero_elements; + $cx; # return result + } + +############################################################################# + +sub _log_int + { + # calculate integer log of $x to base $base + # ref to array, ref to array - return ref to array + my ($c,$x,$base) = @_; + + # X == 0 => NaN + return if (scalar @$x == 1 && $x->[0] == 0); + # BASE 0 or 1 => NaN + return if (scalar @$base == 1 && $base->[0] < 2); + my $cmp = _acmp($c,$x,$base); # X == BASE => 1 + if ($cmp == 0) + { + splice (@$x,1); $x->[0] = 1; + return ($x,1) + } + # X < BASE + if ($cmp < 0) + { + splice (@$x,1); $x->[0] = 0; + return ($x,undef); + } + + my $x_org = _copy($c,$x); # preserve x + splice(@$x,1); $x->[0] = 1; # keep ref to $x + + # Compute a guess for the result based on: + # $guess = int ( length_in_base_10(X) / ( log(base) / log(10) ) ) + my $len = _len($c,$x_org); + my $log = log($base->[-1]) / log(10); + + # for each additional element in $base, we add $BASE_LEN to the result, + # based on the observation that log($BASE,10) is BASE_LEN and + # log(x*y) == log(x) + log(y): + $log += ((scalar @$base)-1) * $BASE_LEN; + + # calculate now a guess based on the values obtained above: + my $res = int($len / $log); + + $x->[0] = $res; + my $trial = _pow ($c, _copy($c, $base), $x); + my $a = _acmp($c,$trial,$x_org); + +# print STDERR "# trial ", _str($c,$x)," was: $a (0 = exact, -1 too small, +1 too big)\n"; + + # found an exact result? + return ($x,1) if $a == 0; + + if ($a > 0) + { + # or too big + _div($c,$trial,$base); _dec($c, $x); + while (($a = _acmp($c,$trial,$x_org)) > 0) + { +# print STDERR "# big _log_int at ", _str($c,$x), "\n"; + _div($c,$trial,$base); _dec($c, $x); + } + # result is now exact (a == 0), or too small (a < 0) + return ($x, $a == 0 ? 1 : 0); + } + + # else: result was to small + _mul($c,$trial,$base); + + # did we now get the right result? + $a = _acmp($c,$trial,$x_org); + + if ($a == 0) # yes, exactly + { + _inc($c, $x); + return ($x,1); + } + return ($x,0) if $a > 0; + + # Result still too small (we should come here only if the estimate above + # was very off base): + + # Now let the normal trial run obtain the real result + # Simple loop that increments $x by 2 in each step, possible overstepping + # the real result + + my $base_mul = _mul($c, _copy($c,$base), $base); # $base * $base + + while (($a = _acmp($c,$trial,$x_org)) < 0) + { +# print STDERR "# small _log_int at ", _str($c,$x), "\n"; + _mul($c,$trial,$base_mul); _add($c, $x, [2]); + } + + my $exact = 1; + if ($a > 0) + { + # overstepped the result + _dec($c, $x); + _div($c,$trial,$base); + $a = _acmp($c,$trial,$x_org); + if ($a > 0) + { + _dec($c, $x); + } + $exact = 0 if $a != 0; # a = -1 => not exact result, a = 0 => exact + } + + ($x,$exact); # return result + } + +# for debugging: + use constant DEBUG => 0; + my $steps = 0; + sub steps { $steps }; + +sub _sqrt + { + # square-root of $x in place + # Compute a guess of the result (by rule of thumb), then improve it via + # Newton's method. + my ($c,$x) = @_; + + if (scalar @$x == 1) + { + # fits into one Perl scalar, so result can be computed directly + $x->[0] = int(sqrt($x->[0])); + return $x; + } + my $y = _copy($c,$x); + # hopefully _len/2 is < $BASE, the -1 is to always undershot the guess + # since our guess will "grow" + my $l = int((_len($c,$x)-1) / 2); + + my $lastelem = $x->[-1]; # for guess + my $elems = scalar @$x - 1; + # not enough digits, but could have more? + if ((length($lastelem) <= 3) && ($elems > 1)) + { + # right-align with zero pad + my $len = length($lastelem) & 1; + print "$lastelem => " if DEBUG; + $lastelem .= substr($x->[-2] . '0' x $BASE_LEN,0,$BASE_LEN); + # former odd => make odd again, or former even to even again + $lastelem = $lastelem / 10 if (length($lastelem) & 1) != $len; + print "$lastelem\n" if DEBUG; + } + + # construct $x (instead of _lsft($c,$x,$l,10) + my $r = $l % $BASE_LEN; # 10000 00000 00000 00000 ($BASE_LEN=5) + $l = int($l / $BASE_LEN); + print "l = $l " if DEBUG; + + splice @$x,$l; # keep ref($x), but modify it + + # we make the first part of the guess not '1000...0' but int(sqrt($lastelem)) + # that gives us: + # 14400 00000 => sqrt(14400) => guess first digits to be 120 + # 144000 000000 => sqrt(144000) => guess 379 + + print "$lastelem (elems $elems) => " if DEBUG; + $lastelem = $lastelem / 10 if ($elems & 1 == 1); # odd or even? + my $g = sqrt($lastelem); $g =~ s/\.//; # 2.345 => 2345 + $r -= 1 if $elems & 1 == 0; # 70 => 7 + + # padd with zeros if result is too short + $x->[$l--] = int(substr($g . '0' x $r,0,$r+1)); + print "now ",$x->[-1] if DEBUG; + print " would have been ", int('1' . '0' x $r),"\n" if DEBUG; + + # If @$x > 1, we could compute the second elem of the guess, too, to create + # an even better guess. Not implemented yet. Does it improve performance? + $x->[$l--] = 0 while ($l >= 0); # all other digits of guess are zero + + print "start x= ",_str($c,$x),"\n" if DEBUG; + my $two = _two(); + my $last = _zero(); + my $lastlast = _zero(); + $steps = 0 if DEBUG; + while (_acmp($c,$last,$x) != 0 && _acmp($c,$lastlast,$x) != 0) + { + $steps++ if DEBUG; + $lastlast = _copy($c,$last); + $last = _copy($c,$x); + _add($c,$x, _div($c,_copy($c,$y),$x)); + _div($c,$x, $two ); + print " x= ",_str($c,$x),"\n" if DEBUG; + } + print "\nsteps in sqrt: $steps, " if DEBUG; + _dec($c,$x) if _acmp($c,$y,_mul($c,_copy($c,$x),$x)) < 0; # overshot? + print " final ",$x->[-1],"\n" if DEBUG; + $x; + } + +sub _root + { + # take n'th root of $x in place (n >= 3) + my ($c,$x,$n) = @_; + + if (scalar @$x == 1) + { + if (scalar @$n > 1) + { + # result will always be smaller than 2 so trunc to 1 at once + $x->[0] = 1; + } + else + { + # fits into one Perl scalar, so result can be computed directly + # cannot use int() here, because it rounds wrongly (try + # (81 ** 3) ** (1/3) to see what I mean) + #$x->[0] = int( $x->[0] ** (1 / $n->[0]) ); + # round to 8 digits, then truncate result to integer + $x->[0] = int ( sprintf ("%.8f", $x->[0] ** (1 / $n->[0]) ) ); + } + return $x; + } + + # we know now that X is more than one element long + + # if $n is a power of two, we can repeatedly take sqrt($X) and find the + # proper result, because sqrt(sqrt($x)) == root($x,4) + my $b = _as_bin($c,$n); + if ($b =~ /0b1(0+)$/) + { + my $count = CORE::length($1); # 0b100 => len('00') => 2 + my $cnt = $count; # counter for loop + unshift (@$x, 0); # add one element, together with one + # more below in the loop this makes 2 + while ($cnt-- > 0) + { + # 'inflate' $X by adding one element, basically computing + # $x * $BASE * $BASE. This gives us more $BASE_LEN digits for result + # since len(sqrt($X)) approx == len($x) / 2. + unshift (@$x, 0); + # calculate sqrt($x), $x is now one element to big, again. In the next + # round we make that two, again. + _sqrt($c,$x); + } + # $x is now one element to big, so truncate result by removing it + splice (@$x,0,1); + } + else + { + # trial computation by starting with 2,4,8,16 etc until we overstep + my $step; + my $trial = _two(); + + # while still to do more than X steps + do + { + $step = _two(); + while (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) < 0) + { + _mul ($c, $step, [2]); + _add ($c, $trial, $step); + } + + # hit exactly? + if (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) == 0) + { + @$x = @$trial; # make copy while preserving ref to $x + return $x; + } + # overstepped, so go back on step + _sub($c, $trial, $step); + } while (scalar @$step > 1 || $step->[0] > 128); + + # reset step to 2 + $step = _two(); + # add two, because $trial cannot be exactly the result (otherwise we would + # alrady have found it) + _add($c, $trial, $step); + + # and now add more and more (2,4,6,8,10 etc) + while (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) < 0) + { + _add ($c, $trial, $step); + } + + # hit not exactly? (overstepped) + if (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) > 0) + { + _dec($c,$trial); + } + + # hit not exactly? (overstepped) + # 80 too small, 81 slightly too big, 82 too big + if (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) > 0) + { + _dec ($c, $trial); + } + + @$x = @$trial; # make copy while preserving ref to $x + return $x; + } + $x; + } + +############################################################################## +# binary stuff + +sub _and + { + my ($c,$x,$y) = @_; + + # the shortcut makes equal, large numbers _really_ fast, and makes only a + # very small performance drop for small numbers (e.g. something with less + # than 32 bit) Since we optimize for large numbers, this is enabled. + return $x if _acmp($c,$x,$y) == 0; # shortcut + + my $m = _one(); my ($xr,$yr); + my $mask = $AND_MASK; + + my $x1 = $x; + my $y1 = _copy($c,$y); # make copy + $x = _zero(); + my ($b,$xrr,$yrr); + use integer; + while (!_is_zero($c,$x1) && !_is_zero($c,$y1)) + { + ($x1, $xr) = _div($c,$x1,$mask); + ($y1, $yr) = _div($c,$y1,$mask); + + # make ints() from $xr, $yr + # this is when the AND_BITS are greater than $BASE and is slower for + # small (<256 bits) numbers, but faster for large numbers. Disabled + # due to KISS principle + +# $b = 1; $xrr = 0; foreach (@$xr) { $xrr += $_ * $b; $b *= $BASE; } +# $b = 1; $yrr = 0; foreach (@$yr) { $yrr += $_ * $b; $b *= $BASE; } +# _add($c,$x, _mul($c, _new( $c, ($xrr & $yrr) ), $m) ); + + # 0+ due to '&' doesn't work in strings + _add($c,$x, _mul($c, [ 0+$xr->[0] & 0+$yr->[0] ], $m) ); + _mul($c,$m,$mask); + } + $x; + } + +sub _xor + { + my ($c,$x,$y) = @_; + + return _zero() if _acmp($c,$x,$y) == 0; # shortcut (see -and) + + my $m = _one(); my ($xr,$yr); + my $mask = $XOR_MASK; + + my $x1 = $x; + my $y1 = _copy($c,$y); # make copy + $x = _zero(); + my ($b,$xrr,$yrr); + use integer; + while (!_is_zero($c,$x1) && !_is_zero($c,$y1)) + { + ($x1, $xr) = _div($c,$x1,$mask); + ($y1, $yr) = _div($c,$y1,$mask); + # make ints() from $xr, $yr (see _and()) + #$b = 1; $xrr = 0; foreach (@$xr) { $xrr += $_ * $b; $b *= $BASE; } + #$b = 1; $yrr = 0; foreach (@$yr) { $yrr += $_ * $b; $b *= $BASE; } + #_add($c,$x, _mul($c, _new( $c, ($xrr ^ $yrr) ), $m) ); + + # 0+ due to '^' doesn't work in strings + _add($c,$x, _mul($c, [ 0+$xr->[0] ^ 0+$yr->[0] ], $m) ); + _mul($c,$m,$mask); + } + # the loop stops when the shorter of the two numbers is exhausted + # the remainder of the longer one will survive bit-by-bit, so we simple + # multiply-add it in + _add($c,$x, _mul($c, $x1, $m) ) if !_is_zero($c,$x1); + _add($c,$x, _mul($c, $y1, $m) ) if !_is_zero($c,$y1); + + $x; + } + +sub _or + { + my ($c,$x,$y) = @_; + + return $x if _acmp($c,$x,$y) == 0; # shortcut (see _and) + + my $m = _one(); my ($xr,$yr); + my $mask = $OR_MASK; + + my $x1 = $x; + my $y1 = _copy($c,$y); # make copy + $x = _zero(); + my ($b,$xrr,$yrr); + use integer; + while (!_is_zero($c,$x1) && !_is_zero($c,$y1)) + { + ($x1, $xr) = _div($c,$x1,$mask); + ($y1, $yr) = _div($c,$y1,$mask); + # make ints() from $xr, $yr (see _and()) +# $b = 1; $xrr = 0; foreach (@$xr) { $xrr += $_ * $b; $b *= $BASE; } +# $b = 1; $yrr = 0; foreach (@$yr) { $yrr += $_ * $b; $b *= $BASE; } +# _add($c,$x, _mul($c, _new( $c, ($xrr | $yrr) ), $m) ); + + # 0+ due to '|' doesn't work in strings + _add($c,$x, _mul($c, [ 0+$xr->[0] | 0+$yr->[0] ], $m) ); + _mul($c,$m,$mask); + } + # the loop stops when the shorter of the two numbers is exhausted + # the remainder of the longer one will survive bit-by-bit, so we simple + # multiply-add it in + _add($c,$x, _mul($c, $x1, $m) ) if !_is_zero($c,$x1); + _add($c,$x, _mul($c, $y1, $m) ) if !_is_zero($c,$y1); + + $x; + } + +sub _as_hex + { + # convert a decimal number to hex (ref to array, return ref to string) + my ($c,$x) = @_; + + # fits into one element (handle also 0x0 case) + return sprintf("0x%x",$x->[0]) if @$x == 1; + + my $x1 = _copy($c,$x); + + my $es = ''; + my ($xr, $h, $x10000); + if ($] >= 5.006) + { + $x10000 = [ 0x10000 ]; $h = 'h4'; + } + else + { + $x10000 = [ 0x1000 ]; $h = 'h3'; + } + while (@$x1 != 1 || $x1->[0] != 0) # _is_zero() + { + ($x1, $xr) = _div($c,$x1,$x10000); + $es .= unpack($h,pack('V',$xr->[0])); + } + $es = reverse $es; + $es =~ s/^[0]+//; # strip leading zeros + '0x' . $es; # return result prepended with 0x + } + +sub _as_bin + { + # convert a decimal number to bin (ref to array, return ref to string) + my ($c,$x) = @_; + + # fits into one element (and Perl recent enough), handle also 0b0 case + # handle zero case for older Perls + if ($] <= 5.005 && @$x == 1 && $x->[0] == 0) + { + my $t = '0b0'; return $t; + } + if (@$x == 1 && $] >= 5.006) + { + my $t = sprintf("0b%b",$x->[0]); + return $t; + } + my $x1 = _copy($c,$x); + + my $es = ''; + my ($xr, $b, $x10000); + if ($] >= 5.006) + { + $x10000 = [ 0x10000 ]; $b = 'b16'; + } + else + { + $x10000 = [ 0x1000 ]; $b = 'b12'; + } + while (!(@$x1 == 1 && $x1->[0] == 0)) # _is_zero() + { + ($x1, $xr) = _div($c,$x1,$x10000); + $es .= unpack($b,pack('v',$xr->[0])); + } + $es = reverse $es; + $es =~ s/^[0]+//; # strip leading zeros + '0b' . $es; # return result prepended with 0b + } + +sub _as_oct + { + # convert a decimal number to octal (ref to array, return ref to string) + my ($c,$x) = @_; + + # fits into one element (handle also 0 case) + return sprintf("0%o",$x->[0]) if @$x == 1; + + my $x1 = _copy($c,$x); + + my $es = ''; + my $xr; + my $x1000 = [ 0100000 ]; + while (@$x1 != 1 || $x1->[0] != 0) # _is_zero() + { + ($x1, $xr) = _div($c,$x1,$x1000); + $es .= reverse sprintf("%05o", $xr->[0]); + } + $es = reverse $es; + $es =~ s/^[0]+//; # strip leading zeros + '0' . $es; # return result prepended with 0 + } + +sub _from_oct + { + # convert a octal number to decimal (string, return ref to array) + my ($c,$os) = @_; + + # for older Perls, play safe + my $m = [ 0100000 ]; + my $d = 5; # 5 digits at a time + + my $mul = _one(); + my $x = _zero(); + + my $len = int( (length($os)-1)/$d ); # $d digit parts, w/o the '0' + my $val; my $i = -$d; + while ($len >= 0) + { + $val = substr($os,$i,$d); # get oct digits + $val = CORE::oct($val); + $i -= $d; $len --; + my $adder = [ $val ]; + _add ($c, $x, _mul ($c, $adder, $mul ) ) if $val != 0; + _mul ($c, $mul, $m ) if $len >= 0; # skip last mul + } + $x; + } + +sub _from_hex + { + # convert a hex number to decimal (string, return ref to array) + my ($c,$hs) = @_; + + my $m = _new($c, 0x10000000); # 28 bit at a time (<32 bit!) + my $d = 7; # 7 digits at a time + if ($] <= 5.006) + { + # for older Perls, play safe + $m = [ 0x10000 ]; # 16 bit at a time (<32 bit!) + $d = 4; # 4 digits at a time + } + + my $mul = _one(); + my $x = _zero(); + + my $len = int( (length($hs)-2)/$d ); # $d digit parts, w/o the '0x' + my $val; my $i = -$d; + while ($len >= 0) + { + $val = substr($hs,$i,$d); # get hex digits + $val =~ s/^0x// if $len == 0; # for last part only because + $val = CORE::hex($val); # hex does not like wrong chars + $i -= $d; $len --; + my $adder = [ $val ]; + # if the resulting number was to big to fit into one element, create a + # two-element version (bug found by Mark Lakata - Thanx!) + if (CORE::length($val) > $BASE_LEN) + { + $adder = _new($c,$val); + } + _add ($c, $x, _mul ($c, $adder, $mul ) ) if $val != 0; + _mul ($c, $mul, $m ) if $len >= 0; # skip last mul + } + $x; + } + +sub _from_bin + { + # convert a hex number to decimal (string, return ref to array) + my ($c,$bs) = @_; + + # instead of converting X (8) bit at a time, it is faster to "convert" the + # number to hex, and then call _from_hex. + + my $hs = $bs; + $hs =~ s/^[+-]?0b//; # remove sign and 0b + my $l = length($hs); # bits + $hs = '0' x (8-($l % 8)) . $hs if ($l % 8) != 0; # padd left side w/ 0 + my $h = '0x' . unpack('H*', pack ('B*', $hs)); # repack as hex + + $c->_from_hex($h); + } + +############################################################################## +# special modulus functions + +sub _modinv + { + # modular inverse + my ($c,$x,$y) = @_; + + my $u = _zero($c); my $u1 = _one($c); + my $a = _copy($c,$y); my $b = _copy($c,$x); + + # Euclid's Algorithm for bgcd(), only that we calc bgcd() ($a) and the + # result ($u) at the same time. See comments in BigInt for why this works. + my $q; + ($a, $q, $b) = ($b, _div($c,$a,$b)); # step 1 + my $sign = 1; + while (!_is_zero($c,$b)) + { + my $t = _add($c, # step 2: + _mul($c,_copy($c,$u1), $q) , # t = u1 * q + $u ); # + u + $u = $u1; # u = u1, u1 = t + $u1 = $t; + $sign = -$sign; + ($a, $q, $b) = ($b, _div($c,$a,$b)); # step 1 + } + + # if the gcd is not 1, then return NaN + return (undef,undef) unless _is_one($c,$a); + + ($u1, $sign == 1 ? '+' : '-'); + } + +sub _modpow + { + # modulus of power ($x ** $y) % $z + my ($c,$num,$exp,$mod) = @_; + + # in the trivial case, + if (_is_one($c,$mod)) + { + splice @$num,0,1; $num->[0] = 0; + return $num; + } + if ((scalar @$num == 1) && (($num->[0] == 0) || ($num->[0] == 1))) + { + $num->[0] = 1; + return $num; + } + +# $num = _mod($c,$num,$mod); # this does not make it faster + + my $acc = _copy($c,$num); my $t = _one(); + + my $expbin = _as_bin($c,$exp); $expbin =~ s/^0b//; + my $len = length($expbin); + while (--$len >= 0) + { + if ( substr($expbin,$len,1) eq '1') # is_odd + { + _mul($c,$t,$acc); + $t = _mod($c,$t,$mod); + } + _mul($c,$acc,$acc); + $acc = _mod($c,$acc,$mod); + } + @$num = @$t; + $num; + } + +sub _gcd + { + # greatest common divisor + my ($c,$x,$y) = @_; + + while ( (scalar @$y != 1) || ($y->[0] != 0) ) # while ($y != 0) + { + my $t = _copy($c,$y); + $y = _mod($c, $x, $y); + $x = $t; + } + $x; + } + +############################################################################## +############################################################################## + +1; +__END__ + +=head1 NAME + +Math::BigInt::Calc - Pure Perl module to support Math::BigInt + +=head1 SYNOPSIS + +Provides support for big integer calculations. Not intended to be used by other +modules. Other modules which sport the same functions can also be used to support +Math::BigInt, like Math::BigInt::GMP or Math::BigInt::Pari. + +=head1 DESCRIPTION + +In order to allow for multiple big integer libraries, Math::BigInt was +rewritten to use library modules for core math routines. Any module which +follows the same API as this can be used instead by using the following: + + use Math::BigInt lib => 'libname'; + +'libname' is either the long name ('Math::BigInt::Pari'), or only the short +version like 'Pari'. + +=head1 STORAGE + +=head1 METHODS + +The following functions MUST be defined in order to support the use by +Math::BigInt v1.70 or later: + + api_version() return API version, 1 for v1.70, 2 for v1.83 + _new(string) return ref to new object from ref to decimal string + _zero() return a new object with value 0 + _one() return a new object with value 1 + _two() return a new object with value 2 + _ten() return a new object with value 10 + + _str(obj) return ref to a string representing the object + _num(obj) returns a Perl integer/floating point number + NOTE: because of Perl numeric notation defaults, + the _num'ified obj may lose accuracy due to + machine-dependent floating point size limitations + + _add(obj,obj) Simple addition of two objects + _mul(obj,obj) Multiplication of two objects + _div(obj,obj) Division of the 1st object by the 2nd + In list context, returns (result,remainder). + NOTE: this is integer math, so no + fractional part will be returned. + The second operand will be not be 0, so no need to + check for that. + _sub(obj,obj) Simple subtraction of 1 object from another + a third, optional parameter indicates that the params + are swapped. In this case, the first param needs to + be preserved, while you can destroy the second. + sub (x,y,1) => return x - y and keep x intact! + _dec(obj) decrement object by one (input is guaranteed to be > 0) + _inc(obj) increment object by one + + + _acmp(obj,obj) <=> operator for objects (return -1, 0 or 1) + + _len(obj) returns count of the decimal digits of the object + _digit(obj,n) returns the n'th decimal digit of object + + _is_one(obj) return true if argument is 1 + _is_two(obj) return true if argument is 2 + _is_ten(obj) return true if argument is 10 + _is_zero(obj) return true if argument is 0 + _is_even(obj) return true if argument is even (0,2,4,6..) + _is_odd(obj) return true if argument is odd (1,3,5,7..) + + _copy return a ref to a true copy of the object + + _check(obj) check whether internal representation is still intact + return 0 for ok, otherwise error message as string + + _from_hex(str) return new object from a hexadecimal string + _from_bin(str) return new object from a binary string + _from_oct(str) return new object from an octal string + + _as_hex(str) return string containing the value as + unsigned hex string, with the '0x' prepended. + Leading zeros must be stripped. + _as_bin(str) Like as_hex, only as binary string containing only + zeros and ones. Leading zeros must be stripped and a + '0b' must be prepended. + + _rsft(obj,N,B) shift object in base B by N 'digits' right + _lsft(obj,N,B) shift object in base B by N 'digits' left + + _xor(obj1,obj2) XOR (bit-wise) object 1 with object 2 + Note: XOR, AND and OR pad with zeros if size mismatches + _and(obj1,obj2) AND (bit-wise) object 1 with object 2 + _or(obj1,obj2) OR (bit-wise) object 1 with object 2 + + _mod(obj1,obj2) Return remainder of div of the 1st by the 2nd object + _sqrt(obj) return the square root of object (truncated to int) + _root(obj) return the n'th (n >= 3) root of obj (truncated to int) + _fac(obj) return factorial of object 1 (1*2*3*4..) + _pow(obj1,obj2) return object 1 to the power of object 2 + return undef for NaN + _zeros(obj) return number of trailing decimal zeros + _modinv return inverse modulus + _modpow return modulus of power ($x ** $y) % $z + _log_int(X,N) calculate integer log() of X in base N + X >= 0, N >= 0 (return undef for NaN) + returns (RESULT, EXACT) where EXACT is: + 1 : result is exactly RESULT + 0 : result was truncated to RESULT + undef : unknown whether result is exactly RESULT + _gcd(obj,obj) return Greatest Common Divisor of two objects + +The following functions are REQUIRED for an api_version of 2 or greater: + + _1ex($x) create the number 1Ex where x >= 0 + _alen(obj) returns approximate count of the decimal digits of the + object. This estimate MUST always be greater or equal + to what _len() returns. + _nok(n,k) calculate n over k (binomial coefficient) + +The following functions are optional, and can be defined if the underlying lib +has a fast way to do them. If undefined, Math::BigInt will use pure Perl (hence +slow) fallback routines to emulate these: + + _signed_or + _signed_and + _signed_xor + +Input strings come in as unsigned but with prefix (i.e. as '123', '0xabc' +or '0b1101'). + +So the library needs only to deal with unsigned big integers. Testing of input +parameter validity is done by the caller, so you need not worry about +underflow (f.i. in C<_sub()>, C<_dec()>) nor about division by zero or similar +cases. + +The first parameter can be modified, that includes the possibility that you +return a reference to a completely different object instead. Although keeping +the reference and just changing its contents is preferred over creating and +returning a different reference. + +Return values are always references to objects, strings, or true/false for +comparison routines. + +=head1 WRAP YOUR OWN + +If you want to port your own favourite c-lib for big numbers to the +Math::BigInt interface, you can take any of the already existing modules as +a rough guideline. You should really wrap up the latest BigInt and BigFloat +testsuites with your module, and replace in them any of the following: + + use Math::BigInt; + +by this: + + use Math::BigInt lib => 'yourlib'; + +This way you ensure that your library really works 100% within Math::BigInt. + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 AUTHORS + +Original math code by Mark Biggar, rewritten by Tels L<http://bloodgate.com/> +in late 2000. +Seperated from BigInt and shaped API with the help of John Peacock. + +Fixed, speed-up, streamlined and enhanced by Tels 2001 - 2007. + +=head1 SEE ALSO + +L<Math::BigInt>, L<Math::BigFloat>, +L<Math::BigInt::GMP>, L<Math::BigInt::FastCalc> and L<Math::BigInt::Pari>. + +=cut diff --git a/Master/tlpkg/tlperl/lib/Math/BigInt/CalcEmu.pm b/Master/tlpkg/tlperl/lib/Math/BigInt/CalcEmu.pm new file mode 100644 index 00000000000..5810f5db9f7 --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Math/BigInt/CalcEmu.pm @@ -0,0 +1,329 @@ +package Math::BigInt::CalcEmu; + +use 5.006002; +use strict; +# use warnings; # dont use warnings for older Perls +use vars qw/$VERSION/; + +$VERSION = '0.05'; + +package Math::BigInt; + +# See SYNOPSIS below. + +my $CALC_EMU; + +BEGIN + { + $CALC_EMU = Math::BigInt->config()->{'lib'}; + # register us with MBI to get notified of future lib changes + Math::BigInt::_register_callback( __PACKAGE__, sub { $CALC_EMU = $_[0]; } ); + } + +sub __emu_band + { + my ($self,$x,$y,$sx,$sy,@r) = @_; + + return $x->bzero(@r) if $y->is_zero() || $x->is_zero(); + + my $sign = 0; # sign of result + $sign = 1 if $sx == -1 && $sy == -1; + + my ($bx,$by); + + if ($sx == -1) # if x is negative + { + # two's complement: inc and flip all "bits" in $bx + $bx = $x->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc + $bx =~ s/-?0x//; + $bx =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + else + { + $bx = $x->as_hex(); # get binary representation + $bx =~ s/-?0x//; + $bx =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + if ($sy == -1) # if y is negative + { + # two's complement: inc and flip all "bits" in $by + $by = $y->copy()->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc + $by =~ s/-?0x//; + $by =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + else + { + $by = $y->as_hex(); # get binary representation + $by =~ s/-?0x//; + $by =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + # now we have bit-strings from X and Y, reverse them for padding + $bx = reverse $bx; + $by = reverse $by; + + # padd the shorter string + my $xx = "\x00"; $xx = "\x0f" if $sx == -1; + my $yy = "\x00"; $yy = "\x0f" if $sy == -1; + my $diff = CORE::length($bx) - CORE::length($by); + if ($diff > 0) + { + # if $yy eq "\x00", we can cut $bx, otherwise we need to padd $by + $by .= $yy x $diff; + } + elsif ($diff < 0) + { + # if $xx eq "\x00", we can cut $by, otherwise we need to padd $bx + $bx .= $xx x abs($diff); + } + + # and the strings together + my $r = $bx & $by; + + # and reverse the result again + $bx = reverse $r; + + # One of $x or $y was negative, so need to flip bits in the result. + # In both cases (one or two of them negative, or both positive) we need + # to get the characters back. + if ($sign == 1) + { + $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/0123456789abcdef/; + } + else + { + $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/fedcba9876543210/; + } + + # leading zeros will be stripped by _from_hex() + $bx = '0x' . $bx; + $x->{value} = $CALC_EMU->_from_hex( $bx ); + + # calculate sign of result + $x->{sign} = '+'; + $x->{sign} = '-' if $sign == 1 && !$x->is_zero(); + + $x->bdec() if $sign == 1; + + $x->round(@r); + } + +sub __emu_bior + { + my ($self,$x,$y,$sx,$sy,@r) = @_; + + return $x->round(@r) if $y->is_zero(); + + my $sign = 0; # sign of result + $sign = 1 if ($sx == -1) || ($sy == -1); + + my ($bx,$by); + + if ($sx == -1) # if x is negative + { + # two's complement: inc and flip all "bits" in $bx + $bx = $x->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc + $bx =~ s/-?0x//; + $bx =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + else + { + $bx = $x->as_hex(); # get binary representation + $bx =~ s/-?0x//; + $bx =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + if ($sy == -1) # if y is negative + { + # two's complement: inc and flip all "bits" in $by + $by = $y->copy()->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc + $by =~ s/-?0x//; + $by =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + else + { + $by = $y->as_hex(); # get binary representation + $by =~ s/-?0x//; + $by =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + # now we have bit-strings from X and Y, reverse them for padding + $bx = reverse $bx; + $by = reverse $by; + + # padd the shorter string + my $xx = "\x00"; $xx = "\x0f" if $sx == -1; + my $yy = "\x00"; $yy = "\x0f" if $sy == -1; + my $diff = CORE::length($bx) - CORE::length($by); + if ($diff > 0) + { + $by .= $yy x $diff; + } + elsif ($diff < 0) + { + $bx .= $xx x abs($diff); + } + + # or the strings together + my $r = $bx | $by; + + # and reverse the result again + $bx = reverse $r; + + # one of $x or $y was negative, so need to flip bits in the result + # in both cases (one or two of them negative, or both positive) we need + # to get the characters back. + if ($sign == 1) + { + $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/0123456789abcdef/; + } + else + { + $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/fedcba9876543210/; + } + + # leading zeros will be stripped by _from_hex() + $bx = '0x' . $bx; + $x->{value} = $CALC_EMU->_from_hex( $bx ); + + # calculate sign of result + $x->{sign} = '+'; + $x->{sign} = '-' if $sign == 1 && !$x->is_zero(); + + # if one of X or Y was negative, we need to decrement result + $x->bdec() if $sign == 1; + + $x->round(@r); + } + +sub __emu_bxor + { + my ($self,$x,$y,$sx,$sy,@r) = @_; + + return $x->round(@r) if $y->is_zero(); + + my $sign = 0; # sign of result + $sign = 1 if $x->{sign} ne $y->{sign}; + + my ($bx,$by); + + if ($sx == -1) # if x is negative + { + # two's complement: inc and flip all "bits" in $bx + $bx = $x->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc + $bx =~ s/-?0x//; + $bx =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + else + { + $bx = $x->as_hex(); # get binary representation + $bx =~ s/-?0x//; + $bx =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + if ($sy == -1) # if y is negative + { + # two's complement: inc and flip all "bits" in $by + $by = $y->copy()->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc + $by =~ s/-?0x//; + $by =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + else + { + $by = $y->as_hex(); # get binary representation + $by =~ s/-?0x//; + $by =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + # now we have bit-strings from X and Y, reverse them for padding + $bx = reverse $bx; + $by = reverse $by; + + # padd the shorter string + my $xx = "\x00"; $xx = "\x0f" if $sx == -1; + my $yy = "\x00"; $yy = "\x0f" if $sy == -1; + my $diff = CORE::length($bx) - CORE::length($by); + if ($diff > 0) + { + $by .= $yy x $diff; + } + elsif ($diff < 0) + { + $bx .= $xx x abs($diff); + } + + # xor the strings together + my $r = $bx ^ $by; + + # and reverse the result again + $bx = reverse $r; + + # one of $x or $y was negative, so need to flip bits in the result + # in both cases (one or two of them negative, or both positive) we need + # to get the characters back. + if ($sign == 1) + { + $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/0123456789abcdef/; + } + else + { + $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/fedcba9876543210/; + } + + # leading zeros will be stripped by _from_hex() + $bx = '0x' . $bx; + $x->{value} = $CALC_EMU->_from_hex( $bx ); + + # calculate sign of result + $x->{sign} = '+'; + $x->{sign} = '-' if $sx != $sy && !$x->is_zero(); + + $x->bdec() if $sign == 1; + + $x->round(@r); + } + +############################################################################## +############################################################################## + +1; +__END__ + +=head1 NAME + +Math::BigInt::CalcEmu - Emulate low-level math with BigInt code + +=head1 SYNOPSIS + + use Math::BigInt::CalcEmu; + +=head1 DESCRIPTION + +Contains routines that emulate low-level math functions in BigInt, e.g. +optional routines the low-level math package does not provide on its own. + +Will be loaded on demand and called automatically by BigInt. + +Stuff here is really low-priority to optimize, since it is far better to +implement the operation in the low-level math libary directly, possible even +using a call to the native lib. + +=head1 METHODS + +=head2 __emu_bxor + +=head2 __emu_band + +=head2 __emu_bior + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 AUTHORS + +(c) Tels http://bloodgate.com 2003, 2004 - based on BigInt code by +Tels from 2001-2003. + +=head1 SEE ALSO + +L<Math::BigInt>, L<Math::BigFloat>, L<Math::BigInt::BitVect>, +L<Math::BigInt::GMP> and L<Math::BigInt::Pari>. + +=cut diff --git a/Master/tlpkg/tlperl/lib/Math/BigInt/FastCalc.pm b/Master/tlpkg/tlperl/lib/Math/BigInt/FastCalc.pm new file mode 100644 index 00000000000..2b4aea58dc2 --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Math/BigInt/FastCalc.pm @@ -0,0 +1,125 @@ +package Math::BigInt::FastCalc; + +use 5.006; +use strict; +# use warnings; # dont use warnings for older Perls + +use DynaLoader; +use Math::BigInt::Calc; + +use vars qw/@ISA $VERSION $BASE $BASE_LEN/; + +@ISA = qw(DynaLoader); + +$VERSION = '0.19'; + +bootstrap Math::BigInt::FastCalc $VERSION; + +############################################################################## +# global constants, flags and accessory + +# announce that we are compatible with MBI v1.70 and up +sub api_version () { 1; } + +BEGIN + { + # use Calc to override the methods that we do not provide in XS + + for my $method (qw/ + str + add sub mul div + rsft lsft + mod modpow modinv + gcd + pow root sqrt log_int fac nok + digit check + from_hex from_bin from_oct as_hex as_bin as_oct + zeros base_len + xor or and + alen 1ex + /) + { + no strict 'refs'; + *{'Math::BigInt::FastCalc::_' . $method} = \&{'Math::BigInt::Calc::_' . $method}; + } + my ($AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN_SMALL, $MAX_VAL); + + # store BASE_LEN and BASE to later pass it to XS code + ($BASE_LEN, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN_SMALL, $MAX_VAL, $BASE) = + Math::BigInt::Calc::_base_len(); + + } + +sub import + { + _set_XS_BASE($BASE, $BASE_LEN); + } + +############################################################################## +############################################################################## + +1; +__END__ +=pod + +=head1 NAME + +Math::BigInt::FastCalc - Math::BigInt::Calc with some XS for more speed + +=head1 SYNOPSIS + +Provides support for big integer calculations. Not intended to be used by +other modules. Other modules which sport the same functions can also be used +to support Math::BigInt, like L<Math::BigInt::GMP> or L<Math::BigInt::Pari>. + +=head1 DESCRIPTION + +In order to allow for multiple big integer libraries, Math::BigInt was +rewritten to use library modules for core math routines. Any module which +follows the same API as this can be used instead by using the following: + + use Math::BigInt lib => 'libname'; + +'libname' is either the long name ('Math::BigInt::Pari'), or only the short +version like 'Pari'. To use this library: + + use Math::BigInt lib => 'FastCalc'; + +Note that from L<Math::BigInt> v1.76 onwards, FastCalc will be loaded +automatically, if possible. + +=head1 STORAGE + +FastCalc works exactly like Calc, in stores the numbers in decimal form, +chopped into parts. + +=head1 METHODS + +The following functions are now implemented in FastCalc.xs: + + _is_odd _is_even _is_one _is_zero + _is_two _is_ten + _zero _one _two _ten + _acmp _len _num + _inc _dec + __strip_zeros _copy + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 AUTHORS + +Original math code by Mark Biggar, rewritten by Tels L<http://bloodgate.com/> +in late 2000. +Seperated from BigInt and shaped API with the help of John Peacock. +Fixed, sped-up and enhanced by Tels http://bloodgate.com 2001-2003. +Further streamlining (api_version 1 etc.) by Tels 2004-2007. + +=head1 SEE ALSO + +L<Math::BigInt>, L<Math::BigFloat>, +L<Math::BigInt::GMP>, L<Math::BigInt::FastCalc> and L<Math::BigInt::Pari>. + +=cut diff --git a/Master/tlpkg/tlperl/lib/Math/BigInt/Trace.pm b/Master/tlpkg/tlperl/lib/Math/BigInt/Trace.pm new file mode 100644 index 00000000000..4733d226343 --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Math/BigInt/Trace.pm @@ -0,0 +1,47 @@ +#!/usr/bin/perl -w + +package Math::BigInt::Trace; + +require 5.005_02; +use strict; + +use Exporter; +use Math::BigInt; +use vars qw($VERSION @ISA $PACKAGE @EXPORT_OK + $accuracy $precision $round_mode $div_scale); + +@ISA = qw(Exporter Math::BigInt); + +$VERSION = 0.01; + +use overload; # inherit overload from BigInt + +# Globals +$accuracy = $precision = undef; +$round_mode = 'even'; +$div_scale = 40; + +sub new +{ + my $proto = shift; + my $class = ref($proto) || $proto; + + my $value = shift; + my $a = $accuracy; $a = $_[0] if defined $_[0]; + my $p = $precision; $p = $_[1] if defined $_[1]; + my $self = Math::BigInt->new($value,$a,$p,$round_mode); + bless $self,$class; + print "MBI new '$value' => '$self' (",ref($self),")"; + return $self; +} + +sub import + { + print "MBI import ",join(' ',@_); + my $self = shift; + Math::BigInt::import($self,@_); # need it for subclasses +# $self->export_to_level(1,$self,@_); # need this ? + @_ = (); + } + +1; diff --git a/Master/tlpkg/tlperl/lib/Math/BigRat.pm b/Master/tlpkg/tlperl/lib/Math/BigRat.pm new file mode 100644 index 00000000000..2460d1c7d4e --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Math/BigRat.pm @@ -0,0 +1,2020 @@ + +# +# "Tax the rat farms." - Lord Vetinari +# + +# The following hash values are used: +# sign : +,-,NaN,+inf,-inf +# _d : denominator +# _n : numeraotr (value = _n/_d) +# _a : accuracy +# _p : precision +# You should not look at the innards of a BigRat - use the methods for this. + +package Math::BigRat; + +# anythig older is untested, and unlikely to work +use 5.006; +use strict; + +use Math::BigFloat; +use vars qw($VERSION @ISA $upgrade $downgrade + $accuracy $precision $round_mode $div_scale $_trap_nan $_trap_inf); + +@ISA = qw(Math::BigFloat); + +$VERSION = '0.24'; +$VERSION = eval $VERSION; + +use overload; # inherit overload from Math::BigFloat + +BEGIN + { + *objectify = \&Math::BigInt::objectify; # inherit this from BigInt + *AUTOLOAD = \&Math::BigFloat::AUTOLOAD; # can't inherit AUTOLOAD + # we inherit these from BigFloat because currently it is not possible + # that MBF has a different $MBI variable than we, because MBF also uses + # Math::BigInt::config->('lib'); (there is always only one library loaded) + *_e_add = \&Math::BigFloat::_e_add; + *_e_sub = \&Math::BigFloat::_e_sub; + *as_int = \&as_number; + *is_pos = \&is_positive; + *is_neg = \&is_negative; + } + +############################################################################## +# Global constants and flags. Access these only via the accessor methods! + +$accuracy = $precision = undef; +$round_mode = 'even'; +$div_scale = 40; +$upgrade = undef; +$downgrade = undef; + +# These are internally, and not to be used from the outside at all! + +$_trap_nan = 0; # are NaNs ok? set w/ config() +$_trap_inf = 0; # are infs ok? set w/ config() + +# the package we are using for our private parts, defaults to: +# Math::BigInt->config()->{lib} +my $MBI = 'Math::BigInt::Calc'; + +my $nan = 'NaN'; +my $class = 'Math::BigRat'; + +sub isa + { + return 0 if $_[1] =~ /^Math::Big(Int|Float)/; # we aren't + UNIVERSAL::isa(@_); + } + +############################################################################## + +sub _new_from_float + { + # turn a single float input into a rational number (like '0.1') + my ($self,$f) = @_; + + return $self->bnan() if $f->is_nan(); + return $self->binf($f->{sign}) if $f->{sign} =~ /^[+-]inf$/; + + $self->{_n} = $MBI->_copy( $f->{_m} ); # mantissa + $self->{_d} = $MBI->_one(); + $self->{sign} = $f->{sign} || '+'; + if ($f->{_es} eq '-') + { + # something like Math::BigRat->new('0.1'); + # 1 / 1 => 1/10 + $MBI->_lsft ( $self->{_d}, $f->{_e} ,10); + } + else + { + # something like Math::BigRat->new('10'); + # 1 / 1 => 10/1 + $MBI->_lsft ( $self->{_n}, $f->{_e} ,10) unless + $MBI->_is_zero($f->{_e}); + } + $self; + } + +sub new + { + # create a Math::BigRat + my $class = shift; + + my ($n,$d) = @_; + + my $self = { }; bless $self,$class; + + # input like (BigInt) or (BigFloat): + if ((!defined $d) && (ref $n) && (!$n->isa('Math::BigRat'))) + { + if ($n->isa('Math::BigFloat')) + { + $self->_new_from_float($n); + } + if ($n->isa('Math::BigInt')) + { + # TODO: trap NaN, inf + $self->{_n} = $MBI->_copy($n->{value}); # "mantissa" = N + $self->{_d} = $MBI->_one(); # d => 1 + $self->{sign} = $n->{sign}; + } + if ($n->isa('Math::BigInt::Lite')) + { + # TODO: trap NaN, inf + $self->{sign} = '+'; $self->{sign} = '-' if $$n < 0; + $self->{_n} = $MBI->_new(abs($$n)); # "mantissa" = N + $self->{_d} = $MBI->_one(); # d => 1 + } + return $self->bnorm(); # normalize (120/1 => 12/10) + } + + # input like (BigInt,BigInt) or (BigLite,BigLite): + if (ref($d) && ref($n)) + { + # do N first (for $self->{sign}): + if ($n->isa('Math::BigInt')) + { + # TODO: trap NaN, inf + $self->{_n} = $MBI->_copy($n->{value}); # "mantissa" = N + $self->{sign} = $n->{sign}; + } + elsif ($n->isa('Math::BigInt::Lite')) + { + # TODO: trap NaN, inf + $self->{sign} = '+'; $self->{sign} = '-' if $$n < 0; + $self->{_n} = $MBI->_new(abs($$n)); # "mantissa" = $n + } + else + { + require Carp; + Carp::croak(ref($n) . " is not a recognized object format for Math::BigRat->new"); + } + # now D: + if ($d->isa('Math::BigInt')) + { + # TODO: trap NaN, inf + $self->{_d} = $MBI->_copy($d->{value}); # "mantissa" = D + # +/+ or -/- => +, +/- or -/+ => - + $self->{sign} = $d->{sign} ne $self->{sign} ? '-' : '+'; + } + elsif ($d->isa('Math::BigInt::Lite')) + { + # TODO: trap NaN, inf + $self->{_d} = $MBI->_new(abs($$d)); # "mantissa" = D + my $ds = '+'; $ds = '-' if $$d < 0; + # +/+ or -/- => +, +/- or -/+ => - + $self->{sign} = $ds ne $self->{sign} ? '-' : '+'; + } + else + { + require Carp; + Carp::croak(ref($d) . " is not a recognized object format for Math::BigRat->new"); + } + return $self->bnorm(); # normalize (120/1 => 12/10) + } + return $n->copy() if ref $n; # already a BigRat + + if (!defined $n) + { + $self->{_n} = $MBI->_zero(); # undef => 0 + $self->{_d} = $MBI->_one(); + $self->{sign} = '+'; + return $self; + } + + # string input with / delimiter + if ($n =~ /\s*\/\s*/) + { + return $class->bnan() if $n =~ /\/.*\//; # 1/2/3 isn't valid + return $class->bnan() if $n =~ /\/\s*$/; # 1/ isn't valid + ($n,$d) = split (/\//,$n); + # try as BigFloats first + if (($n =~ /[\.eE]/) || ($d =~ /[\.eE]/)) + { + local $Math::BigFloat::accuracy = undef; + local $Math::BigFloat::precision = undef; + + # one of them looks like a float + my $nf = Math::BigFloat->new($n,undef,undef); + $self->{sign} = '+'; + return $self->bnan() if $nf->is_nan(); + + $self->{_n} = $MBI->_copy( $nf->{_m} ); # get mantissa + + # now correct $self->{_n} due to $n + my $f = Math::BigFloat->new($d,undef,undef); + return $self->bnan() if $f->is_nan(); + $self->{_d} = $MBI->_copy( $f->{_m} ); + + # calculate the difference between nE and dE + my $diff_e = $nf->exponent()->bsub( $f->exponent); + if ($diff_e->is_negative()) + { + # < 0: mul d with it + $MBI->_lsft( $self->{_d}, $MBI->_new( $diff_e->babs()), 10); + } + elsif (!$diff_e->is_zero()) + { + # > 0: mul n with it + $MBI->_lsft( $self->{_n}, $MBI->_new( $diff_e), 10); + } + } + else + { + # both d and n look like (big)ints + + $self->{sign} = '+'; # no sign => '+' + $self->{_n} = undef; + $self->{_d} = undef; + if ($n =~ /^([+-]?)0*([0-9]+)\z/) # first part ok? + { + $self->{sign} = $1 || '+'; # no sign => '+' + $self->{_n} = $MBI->_new($2 || 0); + } + + if ($d =~ /^([+-]?)0*([0-9]+)\z/) # second part ok? + { + $self->{sign} =~ tr/+-/-+/ if ($1 || '') eq '-'; # negate if second part neg. + $self->{_d} = $MBI->_new($2 || 0); + } + + if (!defined $self->{_n} || !defined $self->{_d}) + { + $d = Math::BigInt->new($d,undef,undef) unless ref $d; + $n = Math::BigInt->new($n,undef,undef) unless ref $n; + + if ($n->{sign} =~ /^[+-]$/ && $d->{sign} =~ /^[+-]$/) + { + # both parts are ok as integers (wierd things like ' 1e0' + $self->{_n} = $MBI->_copy($n->{value}); + $self->{_d} = $MBI->_copy($d->{value}); + $self->{sign} = $n->{sign}; + $self->{sign} =~ tr/+-/-+/ if $d->{sign} eq '-'; # -1/-2 => 1/2 + return $self->bnorm(); + } + + $self->{sign} = '+'; # a default sign + return $self->bnan() if $n->is_nan() || $d->is_nan(); + + # handle inf cases: + if ($n->is_inf() || $d->is_inf()) + { + if ($n->is_inf()) + { + return $self->bnan() if $d->is_inf(); # both are inf => NaN + my $s = '+'; # '+inf/+123' or '-inf/-123' + $s = '-' if substr($n->{sign},0,1) ne $d->{sign}; + # +-inf/123 => +-inf + return $self->binf($s); + } + # 123/inf => 0 + return $self->bzero(); + } + } + } + + return $self->bnorm(); + } + + # simple string input + if (($n =~ /[\.eE]/) && $n !~ /^0x/) + { + # looks like a float, quacks like a float, so probably is a float + $self->{sign} = 'NaN'; + local $Math::BigFloat::accuracy = undef; + local $Math::BigFloat::precision = undef; + $self->_new_from_float(Math::BigFloat->new($n,undef,undef)); + } + else + { + # for simple forms, use $MBI directly + if ($n =~ /^([+-]?)0*([0-9]+)\z/) + { + $self->{sign} = $1 || '+'; + $self->{_n} = $MBI->_new($2 || 0); + $self->{_d} = $MBI->_one(); + } + else + { + my $n = Math::BigInt->new($n,undef,undef); + $self->{_n} = $MBI->_copy($n->{value}); + $self->{_d} = $MBI->_one(); + $self->{sign} = $n->{sign}; + return $self->bnan() if $self->{sign} eq 'NaN'; + return $self->binf($self->{sign}) if $self->{sign} =~ /^[+-]inf$/; + } + } + $self->bnorm(); + } + +sub copy + { + # if two arguments, the first one is the class to "swallow" subclasses + my ($c,$x) = @_; + + if (scalar @_ == 1) + { + $x = $_[0]; + $c = ref($x); + } + return unless ref($x); # only for objects + + my $self = bless {}, $c; + + $self->{sign} = $x->{sign}; + $self->{_d} = $MBI->_copy($x->{_d}); + $self->{_n} = $MBI->_copy($x->{_n}); + $self->{_a} = $x->{_a} if defined $x->{_a}; + $self->{_p} = $x->{_p} if defined $x->{_p}; + $self; + } + +############################################################################## + +sub config + { + # return (later set?) configuration data as hash ref + my $class = shift || 'Math::BigRat'; + + if (@_ == 1 && ref($_[0]) ne 'HASH') + { + my $cfg = $class->SUPER::config(); + return $cfg->{$_[0]}; + } + + my $cfg = $class->SUPER::config(@_); + + # now we need only to override the ones that are different from our parent + $cfg->{class} = $class; + $cfg->{with} = $MBI; + $cfg; + } + +############################################################################## + +sub bstr + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) # inf, NaN etc + { + my $s = $x->{sign}; $s =~ s/^\+//; # +inf => inf + return $s; + } + + my $s = ''; $s = $x->{sign} if $x->{sign} ne '+'; # '+3/2' => '3/2' + + return $s . $MBI->_str($x->{_n}) if $MBI->_is_one($x->{_d}); + $s . $MBI->_str($x->{_n}) . '/' . $MBI->_str($x->{_d}); + } + +sub bsstr + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) # inf, NaN etc + { + my $s = $x->{sign}; $s =~ s/^\+//; # +inf => inf + return $s; + } + + my $s = ''; $s = $x->{sign} if $x->{sign} ne '+'; # +3 vs 3 + $s . $MBI->_str($x->{_n}) . '/' . $MBI->_str($x->{_d}); + } + +sub bnorm + { + # reduce the number to the shortest form + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + # Both parts must be objects of whatever we are using today. + if ( my $c = $MBI->_check($x->{_n}) ) + { + require Carp; Carp::croak ("n did not pass the self-check ($c) in bnorm()"); + } + if ( my $c = $MBI->_check($x->{_d}) ) + { + require Carp; Carp::croak ("d did not pass the self-check ($c) in bnorm()"); + } + + # no normalize for NaN, inf etc. + return $x if $x->{sign} !~ /^[+-]$/; + + # normalize zeros to 0/1 + if ($MBI->_is_zero($x->{_n})) + { + $x->{sign} = '+'; # never leave a -0 + $x->{_d} = $MBI->_one() unless $MBI->_is_one($x->{_d}); + return $x; + } + + return $x if $MBI->_is_one($x->{_d}); # no need to reduce + + # reduce other numbers + my $gcd = $MBI->_copy($x->{_n}); + $gcd = $MBI->_gcd($gcd,$x->{_d}); + + if (!$MBI->_is_one($gcd)) + { + $x->{_n} = $MBI->_div($x->{_n},$gcd); + $x->{_d} = $MBI->_div($x->{_d},$gcd); + } + $x; + } + +############################################################################## +# sign manipulation + +sub bneg + { + # (BRAT or num_str) return BRAT + # negate number or make a negated number from string + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x if $x->modify('bneg'); + + # for +0 dont negate (to have always normalized +0). Does nothing for 'NaN' + $x->{sign} =~ tr/+-/-+/ unless ($x->{sign} eq '+' && $MBI->_is_zero($x->{_n})); + $x; + } + +############################################################################## +# special values + +sub _bnan + { + # used by parent class bnan() to initialize number to NaN + my $self = shift; + + if ($_trap_nan) + { + require Carp; + my $class = ref($self); + # "$self" below will stringify the object, this blows up if $self is a + # partial object (happens under trap_nan), so fix it beforehand + $self->{_d} = $MBI->_zero() unless defined $self->{_d}; + $self->{_n} = $MBI->_zero() unless defined $self->{_n}; + Carp::croak ("Tried to set $self to NaN in $class\::_bnan()"); + } + $self->{_n} = $MBI->_zero(); + $self->{_d} = $MBI->_zero(); + } + +sub _binf + { + # used by parent class bone() to initialize number to +inf/-inf + my $self = shift; + + if ($_trap_inf) + { + require Carp; + my $class = ref($self); + # "$self" below will stringify the object, this blows up if $self is a + # partial object (happens under trap_nan), so fix it beforehand + $self->{_d} = $MBI->_zero() unless defined $self->{_d}; + $self->{_n} = $MBI->_zero() unless defined $self->{_n}; + Carp::croak ("Tried to set $self to inf in $class\::_binf()"); + } + $self->{_n} = $MBI->_zero(); + $self->{_d} = $MBI->_zero(); + } + +sub _bone + { + # used by parent class bone() to initialize number to +1/-1 + my $self = shift; + $self->{_n} = $MBI->_one(); + $self->{_d} = $MBI->_one(); + } + +sub _bzero + { + # used by parent class bzero() to initialize number to 0 + my $self = shift; + $self->{_n} = $MBI->_zero(); + $self->{_d} = $MBI->_one(); + } + +############################################################################## +# mul/add/div etc + +sub badd + { + # add two rational numbers + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + # +inf + +inf => +inf, -inf + -inf => -inf + return $x->binf(substr($x->{sign},0,1)) + if $x->{sign} eq $y->{sign} && $x->{sign} =~ /^[+-]inf$/; + + # +inf + -inf or -inf + +inf => NaN + return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); + + # 1 1 gcd(3,4) = 1 1*3 + 1*4 7 + # - + - = --------- = -- + # 4 3 4*3 12 + + # we do not compute the gcd() here, but simple do: + # 5 7 5*3 + 7*4 43 + # - + - = --------- = -- + # 4 3 4*3 12 + + # and bnorm() will then take care of the rest + + # 5 * 3 + $x->{_n} = $MBI->_mul( $x->{_n}, $y->{_d}); + + # 7 * 4 + my $m = $MBI->_mul( $MBI->_copy( $y->{_n} ), $x->{_d} ); + + # 5 * 3 + 7 * 4 + ($x->{_n}, $x->{sign}) = _e_add( $x->{_n}, $m, $x->{sign}, $y->{sign}); + + # 4 * 3 + $x->{_d} = $MBI->_mul( $x->{_d}, $y->{_d}); + + # normalize result, and possible round + $x->bnorm()->round(@r); + } + +sub bsub + { + # subtract two rational numbers + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + # flip sign of $x, call badd(), then flip sign of result + $x->{sign} =~ tr/+-/-+/ + unless $x->{sign} eq '+' && $MBI->_is_zero($x->{_n}); # not -0 + $x->badd($y,@r); # does norm and round + $x->{sign} =~ tr/+-/-+/ + unless $x->{sign} eq '+' && $MBI->_is_zero($x->{_n}); # not -0 + $x; + } + +sub bmul + { + # multiply two rational numbers + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x->bnan() if ($x->{sign} eq 'NaN' || $y->{sign} eq 'NaN'); + + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) + { + return $x->bnan() if $x->is_zero() || $y->is_zero(); + # result will always be +-inf: + # +inf * +/+inf => +inf, -inf * -/-inf => +inf + # +inf * -/-inf => -inf, -inf * +/+inf => -inf + return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); + return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); + return $x->binf('-'); + } + + # x== 0 # also: or y == 1 or y == -1 + return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero(); + + # XXX TODO: + # According to Knuth, this can be optimized by doing gcd twice (for d and n) + # and reducing in one step. This would save us the bnorm() at the end. + + # 1 2 1 * 2 2 1 + # - * - = ----- = - = - + # 4 3 4 * 3 12 6 + + $x->{_n} = $MBI->_mul( $x->{_n}, $y->{_n}); + $x->{_d} = $MBI->_mul( $x->{_d}, $y->{_d}); + + # compute new sign + $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; + + $x->bnorm()->round(@r); + } + +sub bdiv + { + # (dividend: BRAT or num_str, divisor: BRAT or num_str) return + # (BRAT,BRAT) (quo,rem) or BRAT (only rem) + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $self->_div_inf($x,$y) + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/) || $y->is_zero()); + + # x== 0 # also: or y == 1 or y == -1 + return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero(); + + # XXX TODO: list context, upgrade + # According to Knuth, this can be optimized by doing gcd twice (for d and n) + # and reducing in one step. This would save us the bnorm() at the end. + + # 1 1 1 3 + # - / - == - * - + # 4 3 4 1 + + $x->{_n} = $MBI->_mul( $x->{_n}, $y->{_d}); + $x->{_d} = $MBI->_mul( $x->{_d}, $y->{_n}); + + # compute new sign + $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; + + $x->bnorm()->round(@r); + $x; + } + +sub bmod + { + # compute "remainder" (in Perl way) of $x / $y + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $self->_div_inf($x,$y) + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/) || $y->is_zero()); + + return $x if $x->is_zero(); # 0 / 7 = 0, mod 0 + + # compute $x - $y * floor($x/$y), keeping the sign of $x + + # copy x to u, make it positive and then do a normal division ($u/$y) + my $u = bless { sign => '+' }, $self; + $u->{_n} = $MBI->_mul( $MBI->_copy($x->{_n}), $y->{_d} ); + $u->{_d} = $MBI->_mul( $MBI->_copy($x->{_d}), $y->{_n} ); + + # compute floor(u) + if (! $MBI->_is_one($u->{_d})) + { + $u->{_n} = $MBI->_div($u->{_n},$u->{_d}); # 22/7 => 3/1 w/ truncate + # no need to set $u->{_d} to 1, since below we set it to $y->{_d} anyway + } + + # now compute $y * $u + $u->{_d} = $MBI->_copy($y->{_d}); # 1 * $y->{_d}, see floor above + $u->{_n} = $MBI->_mul($u->{_n},$y->{_n}); + + my $xsign = $x->{sign}; $x->{sign} = '+'; # remember sign and make x positive + # compute $x - $u + $x->bsub($u); + $x->{sign} = $xsign; # put sign back + + $x->bnorm()->round(@r); + } + +############################################################################## +# bdec/binc + +sub bdec + { + # decrement value (subtract 1) + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->{sign} !~ /^[+-]$/; # NaN, inf, -inf + + if ($x->{sign} eq '-') + { + $x->{_n} = $MBI->_add( $x->{_n}, $x->{_d}); # -5/2 => -7/2 + } + else + { + if ($MBI->_acmp($x->{_n},$x->{_d}) < 0) # n < d? + { + # 1/3 -- => -2/3 + $x->{_n} = $MBI->_sub( $MBI->_copy($x->{_d}), $x->{_n}); + $x->{sign} = '-'; + } + else + { + $x->{_n} = $MBI->_sub($x->{_n}, $x->{_d}); # 5/2 => 3/2 + } + } + $x->bnorm()->round(@r); + } + +sub binc + { + # increment value (add 1) + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->{sign} !~ /^[+-]$/; # NaN, inf, -inf + + if ($x->{sign} eq '-') + { + if ($MBI->_acmp($x->{_n},$x->{_d}) < 0) + { + # -1/3 ++ => 2/3 (overflow at 0) + $x->{_n} = $MBI->_sub( $MBI->_copy($x->{_d}), $x->{_n}); + $x->{sign} = '+'; + } + else + { + $x->{_n} = $MBI->_sub($x->{_n}, $x->{_d}); # -5/2 => -3/2 + } + } + else + { + $x->{_n} = $MBI->_add($x->{_n},$x->{_d}); # 5/2 => 7/2 + } + $x->bnorm()->round(@r); + } + +############################################################################## +# is_foo methods (the rest is inherited) + +sub is_int + { + # return true if arg (BRAT or num_str) is an integer + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN and +-inf aren't + $MBI->_is_one($x->{_d}); # x/y && y != 1 => no integer + 0; + } + +sub is_zero + { + # return true if arg (BRAT or num_str) is zero + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 1 if $x->{sign} eq '+' && $MBI->_is_zero($x->{_n}); + 0; + } + +sub is_one + { + # return true if arg (BRAT or num_str) is +1 or -1 if signis given + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + my $sign = $_[2] || ''; $sign = '+' if $sign ne '-'; + return 1 + if ($x->{sign} eq $sign && $MBI->_is_one($x->{_n}) && $MBI->_is_one($x->{_d})); + 0; + } + +sub is_odd + { + # return true if arg (BFLOAT or num_str) is odd or false if even + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't + ($MBI->_is_one($x->{_d}) && $MBI->_is_odd($x->{_n})); # x/2 is not, but 3/1 + 0; + } + +sub is_even + { + # return true if arg (BINT or num_str) is even or false if odd + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't + return 1 if ($MBI->_is_one($x->{_d}) # x/3 is never + && $MBI->_is_even($x->{_n})); # but 4/1 is + 0; + } + +############################################################################## +# parts() and friends + +sub numerator + { + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + # NaN, inf, -inf + return Math::BigInt->new($x->{sign}) if ($x->{sign} !~ /^[+-]$/); + + my $n = Math::BigInt->new($MBI->_str($x->{_n})); $n->{sign} = $x->{sign}; + $n; + } + +sub denominator + { + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + # NaN + return Math::BigInt->new($x->{sign}) if $x->{sign} eq 'NaN'; + # inf, -inf + return Math::BigInt->bone() if $x->{sign} !~ /^[+-]$/; + + Math::BigInt->new($MBI->_str($x->{_d})); + } + +sub parts + { + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + my $c = 'Math::BigInt'; + + return ($c->bnan(),$c->bnan()) if $x->{sign} eq 'NaN'; + return ($c->binf(),$c->binf()) if $x->{sign} eq '+inf'; + return ($c->binf('-'),$c->binf()) if $x->{sign} eq '-inf'; + + my $n = $c->new( $MBI->_str($x->{_n})); + $n->{sign} = $x->{sign}; + my $d = $c->new( $MBI->_str($x->{_d})); + ($n,$d); + } + +sub length + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $nan unless $x->is_int(); + $MBI->_len($x->{_n}); # length(-123/1) => length(123) + } + +sub digit + { + my ($self,$x,$n) = ref($_[0]) ? (undef,$_[0],$_[1]) : objectify(1,@_); + + return $nan unless $x->is_int(); + $MBI->_digit($x->{_n},$n || 0); # digit(-123/1,2) => digit(123,2) + } + +############################################################################## +# special calc routines + +sub bceil + { + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + return $x if $x->{sign} !~ /^[+-]$/ || # not for NaN, inf + $MBI->_is_one($x->{_d}); # 22/1 => 22, 0/1 => 0 + + $x->{_n} = $MBI->_div($x->{_n},$x->{_d}); # 22/7 => 3/1 w/ truncate + $x->{_d} = $MBI->_one(); # d => 1 + $x->{_n} = $MBI->_inc($x->{_n}) + if $x->{sign} eq '+'; # +22/7 => 4/1 + $x->{sign} = '+' if $MBI->_is_zero($x->{_n}); # -0 => 0 + $x; + } + +sub bfloor + { + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + return $x if $x->{sign} !~ /^[+-]$/ || # not for NaN, inf + $MBI->_is_one($x->{_d}); # 22/1 => 22, 0/1 => 0 + + $x->{_n} = $MBI->_div($x->{_n},$x->{_d}); # 22/7 => 3/1 w/ truncate + $x->{_d} = $MBI->_one(); # d => 1 + $x->{_n} = $MBI->_inc($x->{_n}) + if $x->{sign} eq '-'; # -22/7 => -4/1 + $x; + } + +sub bfac + { + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + # if $x is not an integer + if (($x->{sign} ne '+') || (!$MBI->_is_one($x->{_d}))) + { + return $x->bnan(); + } + + $x->{_n} = $MBI->_fac($x->{_n}); + # since _d is 1, we don't need to reduce/norm the result + $x->round(@r); + } + +sub bpow + { + # power ($x ** $y) + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->{sign} =~ /^[+-]inf$/; # -inf/+inf ** x + return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan; + return $x->bone(@r) if $y->is_zero(); + return $x->round(@r) if $x->is_one() || $y->is_one(); + + if ($x->{sign} eq '-' && $MBI->_is_one($x->{_n}) && $MBI->_is_one($x->{_d})) + { + # if $x == -1 and odd/even y => +1/-1 + return $y->is_odd() ? $x->round(@r) : $x->babs()->round(@r); + # my Casio FX-5500L has a bug here: -1 ** 2 is -1, but -1 * -1 is 1; + } + # 1 ** -y => 1 / (1 ** |y|) + # so do test for negative $y after above's clause + + return $x->round(@r) if $x->is_zero(); # 0**y => 0 (if not y <= 0) + + # shortcut if y == 1/N (is then sqrt() respective broot()) + if ($MBI->_is_one($y->{_n})) + { + return $x->bsqrt(@r) if $MBI->_is_two($y->{_d}); # 1/2 => sqrt + return $x->broot($MBI->_str($y->{_d}),@r); # 1/N => root(N) + } + + # shortcut y/1 (and/or x/1) + if ($MBI->_is_one($y->{_d})) + { + # shortcut for x/1 and y/1 + if ($MBI->_is_one($x->{_d})) + { + $x->{_n} = $MBI->_pow($x->{_n},$y->{_n}); # x/1 ** y/1 => (x ** y)/1 + if ($y->{sign} eq '-') + { + # 0.2 ** -3 => 1/(0.2 ** 3) + ($x->{_n},$x->{_d}) = ($x->{_d},$x->{_n}); # swap + } + # correct sign; + ** + => + + if ($x->{sign} eq '-') + { + # - * - => +, - * - * - => - + $x->{sign} = '+' if $MBI->_is_even($y->{_n}); + } + return $x->round(@r); + } + # x/z ** y/1 + $x->{_n} = $MBI->_pow($x->{_n},$y->{_n}); # 5/2 ** y/1 => 5 ** y / 2 ** y + $x->{_d} = $MBI->_pow($x->{_d},$y->{_n}); + if ($y->{sign} eq '-') + { + # 0.2 ** -3 => 1/(0.2 ** 3) + ($x->{_n},$x->{_d}) = ($x->{_d},$x->{_n}); # swap + } + # correct sign; + ** + => + + if ($x->{sign} eq '-') + { + # - * - => +, - * - * - => - + $x->{sign} = '+' if $MBI->_is_even($y->{_n}); + } + return $x->round(@r); + } + +# print STDERR "# $x $y\n"; + + # otherwise: + + # n/d n ______________ + # a/b = -\/ (a/b) ** d + + # (a/b) ** n == (a ** n) / (b ** n) + $MBI->_pow($x->{_n}, $y->{_n} ); + $MBI->_pow($x->{_d}, $y->{_n} ); + + return $x->broot($MBI->_str($y->{_d}),@r); # n/d => root(n) + } + +sub blog + { + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,$class,@_); + } + + # blog(1,Y) => 0 + return $x->bzero() if $x->is_one() && $y->{sign} eq '+'; + + # $x <= 0 => NaN + return $x->bnan() if $x->is_zero() || $x->{sign} ne '+' || $y->{sign} ne '+'; + + if ($x->is_int() && $y->is_int()) + { + return $self->new($x->as_number()->blog($y->as_number(),@r)); + } + + # do it with floats + $x->_new_from_float( $x->_as_float()->blog(Math::BigFloat->new("$y"),@r) ); + } + +sub bexp + { + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,$class,@_); + } + + return $x->binf(@r) if $x->{sign} eq '+inf'; + return $x->bzero(@r) if $x->{sign} eq '-inf'; + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters(@r); + + # also takes care of the "error in _find_round_parameters?" case + return $x if $x->{sign} eq 'NaN'; + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # P = undef + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r[2]; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it's not enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + return $x->bone(@params) if $x->is_zero(); + + # See the comments in Math::BigFloat on how this algorithm works. + # Basically we calculate A and B (where B is faculty(N)) so that A/B = e + + my $x_org = $x->copy(); + if ($scale <= 75) + { + # set $x directly from a cached string form + $x->{_n} = $MBI->_new("90933395208605785401971970164779391644753259799242"); + $x->{_d} = $MBI->_new("33452526613163807108170062053440751665152000000000"); + $x->{sign} = '+'; + } + else + { + # compute A and B so that e = A / B. + + # After some terms we end up with this, so we use it as a starting point: + my $A = $MBI->_new("90933395208605785401971970164779391644753259799242"); + my $F = $MBI->_new(42); my $step = 42; + + # Compute how many steps we need to take to get $A and $B sufficiently big + my $steps = Math::BigFloat::_len_to_steps($scale - 4); +# print STDERR "# Doing $steps steps for ", $scale-4, " digits\n"; + while ($step++ <= $steps) + { + # calculate $a * $f + 1 + $A = $MBI->_mul($A, $F); + $A = $MBI->_inc($A); + # increment f + $F = $MBI->_inc($F); + } + # compute $B as factorial of $steps (this is faster than doing it manually) + my $B = $MBI->_fac($MBI->_new($steps)); + +# print "A ", $MBI->_str($A), "\nB ", $MBI->_str($B), "\n"; + + $x->{_n} = $A; + $x->{_d} = $B; + $x->{sign} = '+'; + } + + # $x contains now an estimate of e, with some surplus digits, so we can round + if (!$x_org->is_one()) + { + # raise $x to the wanted power and round it in one step: + $x->bpow($x_org, @params); + } + else + { + # else just round the already computed result + delete $x->{_a}; delete $x->{_p}; + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + + $x; + } + +sub bnok + { + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,$class,@_); + } + + # do it with floats + $x->_new_from_float( $x->_as_float()->bnok(Math::BigFloat->new("$y"),@r) ); + } + +sub _float_from_part + { + my $x = shift; + + my $f = Math::BigFloat->bzero(); + $f->{_m} = $MBI->_copy($x); + $f->{_e} = $MBI->_zero(); + + $f; + } + +sub _as_float + { + my $x = shift; + + local $Math::BigFloat::upgrade = undef; + local $Math::BigFloat::accuracy = undef; + local $Math::BigFloat::precision = undef; + # 22/7 => 3.142857143.. + + my $a = $x->accuracy() || 0; + if ($a != 0 || !$MBI->_is_one($x->{_d})) + { + # n/d + return scalar Math::BigFloat->new($x->{sign} . $MBI->_str($x->{_n}))->bdiv( $MBI->_str($x->{_d}), $x->accuracy()); + } + # just n + Math::BigFloat->new($x->{sign} . $MBI->_str($x->{_n})); + } + +sub broot + { + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + if ($x->is_int() && $y->is_int()) + { + return $self->new($x->as_number()->broot($y->as_number(),@r)); + } + + # do it with floats + $x->_new_from_float( $x->_as_float()->broot($y->_as_float(),@r) )->bnorm()->bround(@r); + } + +sub bmodpow + { + # set up parameters + my ($self,$x,$y,$m,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$m,@r) = objectify(3,@_); + } + + # $x or $y or $m are NaN or +-inf => NaN + return $x->bnan() + if $x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/ || + $m->{sign} !~ /^[+-]$/; + + if ($x->is_int() && $y->is_int() && $m->is_int()) + { + return $self->new($x->as_number()->bmodpow($y->as_number(),$m,@r)); + } + + warn ("bmodpow() not fully implemented"); + $x->bnan(); + } + +sub bmodinv + { + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + # $x or $y are NaN or +-inf => NaN + return $x->bnan() + if $x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/; + + if ($x->is_int() && $y->is_int()) + { + return $self->new($x->as_number()->bmodinv($y->as_number(),@r)); + } + + warn ("bmodinv() not fully implemented"); + $x->bnan(); + } + +sub bsqrt + { + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x->bnan() if $x->{sign} !~ /^[+]/; # NaN, -inf or < 0 + return $x if $x->{sign} eq '+inf'; # sqrt(inf) == inf + return $x->round(@r) if $x->is_zero() || $x->is_one(); + + local $Math::BigFloat::upgrade = undef; + local $Math::BigFloat::downgrade = undef; + local $Math::BigFloat::precision = undef; + local $Math::BigFloat::accuracy = undef; + local $Math::BigInt::upgrade = undef; + local $Math::BigInt::precision = undef; + local $Math::BigInt::accuracy = undef; + + $x->{_n} = _float_from_part( $x->{_n} )->bsqrt(); + $x->{_d} = _float_from_part( $x->{_d} )->bsqrt(); + + # XXX TODO: we probably can optimze this: + + # if sqrt(D) was not integer + if ($x->{_d}->{_es} ne '+') + { + $x->{_n}->blsft($x->{_d}->exponent()->babs(),10); # 7.1/4.51 => 7.1/45.1 + $x->{_d} = $MBI->_copy( $x->{_d}->{_m} ); # 7.1/45.1 => 71/45.1 + } + # if sqrt(N) was not integer + if ($x->{_n}->{_es} ne '+') + { + $x->{_d}->blsft($x->{_n}->exponent()->babs(),10); # 71/45.1 => 710/45.1 + $x->{_n} = $MBI->_copy( $x->{_n}->{_m} ); # 710/45.1 => 710/451 + } + + # convert parts to $MBI again + $x->{_n} = $MBI->_lsft( $MBI->_copy( $x->{_n}->{_m} ), $x->{_n}->{_e}, 10) + if ref($x->{_n}) ne $MBI && ref($x->{_n}) ne 'ARRAY'; + $x->{_d} = $MBI->_lsft( $MBI->_copy( $x->{_d}->{_m} ), $x->{_d}->{_e}, 10) + if ref($x->{_d}) ne $MBI && ref($x->{_d}) ne 'ARRAY'; + + $x->bnorm()->round(@r); + } + +sub blsft + { + my ($self,$x,$y,$b,@r) = objectify(3,@_); + + $b = 2 unless defined $b; + $b = $self->new($b) unless ref ($b); + $x->bmul( $b->copy()->bpow($y), @r); + $x; + } + +sub brsft + { + my ($self,$x,$y,$b,@r) = objectify(3,@_); + + $b = 2 unless defined $b; + $b = $self->new($b) unless ref ($b); + $x->bdiv( $b->copy()->bpow($y), @r); + $x; + } + +############################################################################## +# round + +sub round + { + $_[0]; + } + +sub bround + { + $_[0]; + } + +sub bfround + { + $_[0]; + } + +############################################################################## +# comparing + +sub bcmp + { + # compare two signed numbers + + # set up parameters + my ($self,$x,$y) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y) = objectify(2,@_); + } + + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + # handle +-inf and NaN + return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + return 0 if $x->{sign} eq $y->{sign} && $x->{sign} =~ /^[+-]inf$/; + return +1 if $x->{sign} eq '+inf'; + return -1 if $x->{sign} eq '-inf'; + return -1 if $y->{sign} eq '+inf'; + return +1; + } + # check sign for speed first + return 1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # does also 0 <=> -y + return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # does also -x <=> 0 + + # shortcut + my $xz = $MBI->_is_zero($x->{_n}); + my $yz = $MBI->_is_zero($y->{_n}); + return 0 if $xz && $yz; # 0 <=> 0 + return -1 if $xz && $y->{sign} eq '+'; # 0 <=> +y + return 1 if $yz && $x->{sign} eq '+'; # +x <=> 0 + + my $t = $MBI->_mul( $MBI->_copy($x->{_n}), $y->{_d}); + my $u = $MBI->_mul( $MBI->_copy($y->{_n}), $x->{_d}); + + my $cmp = $MBI->_acmp($t,$u); # signs are equal + $cmp = -$cmp if $x->{sign} eq '-'; # both are '-' => reverse + $cmp; + } + +sub bacmp + { + # compare two numbers (as unsigned) + + # set up parameters + my ($self,$x,$y) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y) = objectify(2,$class,@_); + } + + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + # handle +-inf and NaN + return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + return 0 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} =~ /^[+-]inf$/; + return 1 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} !~ /^[+-]inf$/; + return -1; + } + + my $t = $MBI->_mul( $MBI->_copy($x->{_n}), $y->{_d}); + my $u = $MBI->_mul( $MBI->_copy($y->{_n}), $x->{_d}); + $MBI->_acmp($t,$u); # ignore signs + } + +############################################################################## +# output conversation + +sub numify + { + # convert 17/8 => float (aka 2.125) + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, NaN, etc + + # N/1 => N + my $neg = ''; $neg = '-' if $x->{sign} eq '-'; + return $neg . $MBI->_num($x->{_n}) if $MBI->_is_one($x->{_d}); + + $x->_as_float()->numify() + 0.0; + } + +sub as_number + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + # NaN, inf etc + return Math::BigInt->new($x->{sign}) if $x->{sign} !~ /^[+-]$/; + + my $u = Math::BigInt->bzero(); + $u->{sign} = $x->{sign}; + $u->{value} = $MBI->_div( $MBI->_copy($x->{_n}), $x->{_d}); # 22/7 => 3 + $u; + } + +sub as_float + { + # return N/D as Math::BigFloat + + # set up parameters + my ($self,$x,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + ($self,$x,@r) = objectify(1,$class,@_) unless ref $_[0]; + + # NaN, inf etc + return Math::BigFloat->new($x->{sign}) if $x->{sign} !~ /^[+-]$/; + + my $u = Math::BigFloat->bzero(); + $u->{sign} = $x->{sign}; + # n + $u->{_m} = $MBI->_copy($x->{_n}); + $u->{_e} = $MBI->_zero(); + $u->bdiv( $MBI->_str($x->{_d}), @r); + # return $u + $u; + } + +sub as_bin + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x unless $x->is_int(); + + my $s = $x->{sign}; $s = '' if $s eq '+'; + $s . $MBI->_as_bin($x->{_n}); + } + +sub as_hex + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x unless $x->is_int(); + + my $s = $x->{sign}; $s = '' if $s eq '+'; + $s . $MBI->_as_hex($x->{_n}); + } + +sub as_oct + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x unless $x->is_int(); + + my $s = $x->{sign}; $s = '' if $s eq '+'; + $s . $MBI->_as_oct($x->{_n}); + } + +############################################################################## + +sub from_hex + { + my $class = shift; + + $class->new(@_); + } + +sub from_bin + { + my $class = shift; + + $class->new(@_); + } + +sub from_oct + { + my $class = shift; + + my @parts; + for my $c (@_) + { + push @parts, Math::BigInt->from_oct($c); + } + $class->new ( @parts ); + } + +############################################################################## +# import + +sub import + { + my $self = shift; + my $l = scalar @_; + my $lib = ''; my @a; + my $try = 'try'; + + for ( my $i = 0; $i < $l ; $i++) + { + if ( $_[$i] eq ':constant' ) + { + # this rest causes overlord er load to step in + overload::constant float => sub { $self->new(shift); }; + } +# elsif ($_[$i] eq 'upgrade') +# { +# # this causes upgrading +# $upgrade = $_[$i+1]; # or undef to disable +# $i++; +# } + elsif ($_[$i] eq 'downgrade') + { + # this causes downgrading + $downgrade = $_[$i+1]; # or undef to disable + $i++; + } + elsif ($_[$i] =~ /^(lib|try|only)\z/) + { + $lib = $_[$i+1] || ''; # default Calc + $try = $1; # lib, try or only + $i++; + } + elsif ($_[$i] eq 'with') + { + # this argument is no longer used + #$MBI = $_[$i+1] || 'Math::BigInt::Calc'; # default Math::BigInt::Calc + $i++; + } + else + { + push @a, $_[$i]; + } + } + require Math::BigInt; + + # let use Math::BigInt lib => 'GMP'; use Math::BigRat; still have GMP + if ($lib ne '') + { + my @c = split /\s*,\s*/, $lib; + foreach (@c) + { + $_ =~ tr/a-zA-Z0-9://cd; # limit to sane characters + } + $lib = join(",", @c); + } + my @import = ('objectify'); + push @import, $try => $lib if $lib ne ''; + + # MBI already loaded, so feed it our lib arguments + Math::BigInt->import( @import ); + + $MBI = Math::BigFloat->config()->{lib}; + + # register us with MBI to get notified of future lib changes + Math::BigInt::_register_callback( $self, sub { $MBI = $_[0]; } ); + + # any non :constant stuff is handled by our parent, Exporter (loaded + # by Math::BigFloat, even if @_ is empty, to give it a chance + $self->SUPER::import(@a); # for subclasses + $self->export_to_level(1,$self,@a); # need this, too + } + +1; + +__END__ + +=head1 NAME + +Math::BigRat - Arbitrary big rational numbers + +=head1 SYNOPSIS + + use Math::BigRat; + + my $x = Math::BigRat->new('3/7'); $x += '5/9'; + + print $x->bstr(),"\n"; + print $x ** 2,"\n"; + + my $y = Math::BigRat->new('inf'); + print "$y ", ($y->is_inf ? 'is' : 'is not') , " infinity\n"; + + my $z = Math::BigRat->new(144); $z->bsqrt(); + +=head1 DESCRIPTION + +Math::BigRat complements Math::BigInt and Math::BigFloat by providing support +for arbitrary big rational numbers. + +=head2 MATH LIBRARY + +You can change the underlying module that does the low-level +math operations by using: + + use Math::BigRat try => 'GMP'; + +Note: This needs Math::BigInt::GMP installed. + +The following would first try to find Math::BigInt::Foo, then +Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: + + use Math::BigRat try => 'Foo,Math::BigInt::Bar'; + +If you want to get warned when the fallback occurs, replace "try" with +"lib": + + use Math::BigRat lib => 'Foo,Math::BigInt::Bar'; + +If you want the code to die instead, replace "try" with +"only": + + use Math::BigRat only => 'Foo,Math::BigInt::Bar'; + +=head1 METHODS + +Any methods not listed here are derived from Math::BigFloat (or +Math::BigInt), so make sure you check these two modules for further +information. + +=head2 new() + + $x = Math::BigRat->new('1/3'); + +Create a new Math::BigRat object. Input can come in various forms: + + $x = Math::BigRat->new(123); # scalars + $x = Math::BigRat->new('inf'); # infinity + $x = Math::BigRat->new('123.3'); # float + $x = Math::BigRat->new('1/3'); # simple string + $x = Math::BigRat->new('1 / 3'); # spaced + $x = Math::BigRat->new('1 / 0.1'); # w/ floats + $x = Math::BigRat->new(Math::BigInt->new(3)); # BigInt + $x = Math::BigRat->new(Math::BigFloat->new('3.1')); # BigFloat + $x = Math::BigRat->new(Math::BigInt::Lite->new('2')); # BigLite + + # You can also give D and N as different objects: + $x = Math::BigRat->new( + Math::BigInt->new(-123), + Math::BigInt->new(7), + ); # => -123/7 + +=head2 numerator() + + $n = $x->numerator(); + +Returns a copy of the numerator (the part above the line) as signed BigInt. + +=head2 denominator() + + $d = $x->denominator(); + +Returns a copy of the denominator (the part under the line) as positive BigInt. + +=head2 parts() + + ($n,$d) = $x->parts(); + +Return a list consisting of (signed) numerator and (unsigned) denominator as +BigInts. + +=head2 numify() + + my $y = $x->numify(); + +Returns the object as a scalar. This will lose some data if the object +cannot be represented by a normal Perl scalar (integer or float), so +use L<as_int()> or L<as_float()> instead. + +This routine is automatically used whenever a scalar is required: + + my $x = Math::BigRat->new('3/1'); + @array = (0,1,2,3); + $y = $array[$x]; # set $y to 3 + +=head2 as_int()/as_number() + + $x = Math::BigRat->new('13/7'); + print $x->as_int(),"\n"; # '1' + +Returns a copy of the object as BigInt, truncated to an integer. + +C<as_number()> is an alias for C<as_int()>. + +=head2 as_float() + + $x = Math::BigRat->new('13/7'); + print $x->as_float(),"\n"; # '1' + + $x = Math::BigRat->new('2/3'); + print $x->as_float(5),"\n"; # '0.66667' + +Returns a copy of the object as BigFloat, preserving the +accuracy as wanted, or the default of 40 digits. + +This method was added in v0.22 of Math::BigRat (April 2008). + +=head2 as_hex() + + $x = Math::BigRat->new('13'); + print $x->as_hex(),"\n"; # '0xd' + +Returns the BigRat as hexadecimal string. Works only for integers. + +=head2 as_bin() + + $x = Math::BigRat->new('13'); + print $x->as_bin(),"\n"; # '0x1101' + +Returns the BigRat as binary string. Works only for integers. + +=head2 as_oct() + + $x = Math::BigRat->new('13'); + print $x->as_oct(),"\n"; # '015' + +Returns the BigRat as octal string. Works only for integers. + +=head2 from_hex()/from_bin()/from_oct() + + my $h = Math::BigRat->from_hex('0x10'); + my $b = Math::BigRat->from_bin('0b10000000'); + my $o = Math::BigRat->from_oct('020'); + +Create a BigRat from an hexadecimal, binary or octal number +in string form. + +=head2 length() + + $len = $x->length(); + +Return the length of $x in digitis for integer values. + +=head2 digit() + + print Math::BigRat->new('123/1')->digit(1); # 1 + print Math::BigRat->new('123/1')->digit(-1); # 3 + +Return the N'ths digit from X when X is an integer value. + +=head2 bnorm() + + $x->bnorm(); + +Reduce the number to the shortest form. This routine is called +automatically whenever it is needed. + +=head2 bfac() + + $x->bfac(); + +Calculates the factorial of $x. For instance: + + print Math::BigRat->new('3/1')->bfac(),"\n"; # 1*2*3 + print Math::BigRat->new('5/1')->bfac(),"\n"; # 1*2*3*4*5 + +Works currently only for integers. + +=head2 bround()/round()/bfround() + +Are not yet implemented. + +=head2 bmod() + + use Math::BigRat; + my $x = Math::BigRat->new('7/4'); + my $y = Math::BigRat->new('4/3'); + print $x->bmod($y); + +Set $x to the remainder of the division of $x by $y. + +=head2 bneg() + + $x->bneg(); + +Used to negate the object in-place. + +=head2 is_one() + + print "$x is 1\n" if $x->is_one(); + +Return true if $x is exactly one, otherwise false. + +=head2 is_zero() + + print "$x is 0\n" if $x->is_zero(); + +Return true if $x is exactly zero, otherwise false. + +=head2 is_pos()/is_positive() + + print "$x is >= 0\n" if $x->is_positive(); + +Return true if $x is positive (greater than or equal to zero), otherwise +false. Please note that '+inf' is also positive, while 'NaN' and '-inf' aren't. + +C<is_positive()> is an alias for C<is_pos()>. + +=head2 is_neg()/is_negative() + + print "$x is < 0\n" if $x->is_negative(); + +Return true if $x is negative (smaller than zero), otherwise false. Please +note that '-inf' is also negative, while 'NaN' and '+inf' aren't. + +C<is_negative()> is an alias for C<is_neg()>. + +=head2 is_int() + + print "$x is an integer\n" if $x->is_int(); + +Return true if $x has a denominator of 1 (e.g. no fraction parts), otherwise +false. Please note that '-inf', 'inf' and 'NaN' aren't integer. + +=head2 is_odd() + + print "$x is odd\n" if $x->is_odd(); + +Return true if $x is odd, otherwise false. + +=head2 is_even() + + print "$x is even\n" if $x->is_even(); + +Return true if $x is even, otherwise false. + +=head2 bceil() + + $x->bceil(); + +Set $x to the next bigger integer value (e.g. truncate the number to integer +and then increment it by one). + +=head2 bfloor() + + $x->bfloor(); + +Truncate $x to an integer value. + +=head2 bsqrt() + + $x->bsqrt(); + +Calculate the square root of $x. + +=head2 broot() + + $x->broot($n); + +Calculate the N'th root of $x. + +=head2 badd()/bmul()/bsub()/bdiv()/bdec()/binc() + +Please see the documentation in L<Math::BigInt>. + +=head2 copy() + + my $z = $x->copy(); + +Makes a deep copy of the object. + +Please see the documentation in L<Math::BigInt> for further details. + +=head2 bstr()/bsstr() + + my $x = Math::BigInt->new('8/4'); + print $x->bstr(),"\n"; # prints 1/2 + print $x->bsstr(),"\n"; # prints 1/2 + +Return a string representating this object. + +=head2 bacmp()/bcmp() + +Used to compare numbers. + +Please see the documentation in L<Math::BigInt> for further details. + +=head2 blsft()/brsft() + +Used to shift numbers left/right. + +Please see the documentation in L<Math::BigInt> for further details. + +=head2 bpow() + + $x->bpow($y); + +Compute $x ** $y. + +Please see the documentation in L<Math::BigInt> for further details. + +=head2 bexp() + + $x->bexp($accuracy); # calculate e ** X + +Calculates two integers A and B so that A/B is equal to C<e ** $x>, where C<e> is +Euler's number. + +This method was added in v0.20 of Math::BigRat (May 2007). + +See also L<blog()>. + +=head2 bnok() + + $x->bnok($y); # x over y (binomial coefficient n over k) + +Calculates the binomial coefficient n over k, also called the "choose" +function. The result is equivalent to: + + ( n ) n! + | - | = ------- + ( k ) k!(n-k)! + +This method was added in v0.20 of Math::BigRat (May 2007). + +=head2 config() + + use Data::Dumper; + + print Dumper ( Math::BigRat->config() ); + print Math::BigRat->config()->{lib},"\n"; + +Returns a hash containing the configuration, e.g. the version number, lib +loaded etc. The following hash keys are currently filled in with the +appropriate information. + + key RO/RW Description + Example + ============================================================ + lib RO Name of the Math library + Math::BigInt::Calc + lib_version RO Version of 'lib' + 0.30 + class RO The class of config you just called + Math::BigRat + version RO version number of the class you used + 0.10 + upgrade RW To which class numbers are upgraded + undef + downgrade RW To which class numbers are downgraded + undef + precision RW Global precision + undef + accuracy RW Global accuracy + undef + round_mode RW Global round mode + even + div_scale RW Fallback accuracy for div + 40 + trap_nan RW Trap creation of NaN (undef = no) + undef + trap_inf RW Trap creation of +inf/-inf (undef = no) + undef + +By passing a reference to a hash you may set the configuration values. This +works only for values that a marked with a C<RW> above, anything else is +read-only. + +=head2 objectify() + +This is an internal routine that turns scalars into objects. + +=head1 BUGS + +Some things are not yet implemented, or only implemented half-way: + +=over 2 + +=item inf handling (partial) + +=item NaN handling (partial) + +=item rounding (not implemented except for bceil/bfloor) + +=item $x ** $y where $y is not an integer + +=item bmod(), blog(), bmodinv() and bmodpow() (partial) + +=back + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 SEE ALSO + +L<Math::BigFloat> and L<Math::Big> as well as L<Math::BigInt::BitVect>, +L<Math::BigInt::Pari> and L<Math::BigInt::GMP>. + +See L<http://search.cpan.org/search?dist=bignum> for a way to use +Math::BigRat. + +The package at L<http://search.cpan.org/search?dist=Math%3A%3ABigRat> +may contain more documentation and examples as well as testcases. + +=head1 AUTHORS + +(C) by Tels L<http://bloodgate.com/> 2001 - 2009. + +Currently maintained by Jonathan "Duke" Leto <jonathan@leto.net> L<http://leto.net> + +=cut diff --git a/Master/tlpkg/tlperl/lib/Math/Complex.pm b/Master/tlpkg/tlperl/lib/Math/Complex.pm new file mode 100644 index 00000000000..8475a2b5d09 --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Math/Complex.pm @@ -0,0 +1,2093 @@ +# +# Complex numbers and associated mathematical functions +# -- Raphael Manfredi Since Sep 1996 +# -- Jarkko Hietaniemi Since Mar 1997 +# -- Daniel S. Lewart Since Sep 1997 +# + +package Math::Complex; + +use strict; + +use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $Inf $ExpInf); + +$VERSION = 1.56; + +use Config; + +BEGIN { + my %DBL_MAX = + ( + 4 => '1.70141183460469229e+38', + 8 => '1.7976931348623157e+308', + # AFAICT the 10, 12, and 16-byte long doubles + # all have the same maximum. + 10 => '1.1897314953572317650857593266280070162E+4932', + 12 => '1.1897314953572317650857593266280070162E+4932', + 16 => '1.1897314953572317650857593266280070162E+4932', + ); + my $nvsize = $Config{nvsize} || + ($Config{uselongdouble} && $Config{longdblsize}) || + $Config{doublesize}; + die "Math::Complex: Could not figure out nvsize\n" + unless defined $nvsize; + die "Math::Complex: Cannot not figure out max nv (nvsize = $nvsize)\n" + unless defined $DBL_MAX{$nvsize}; + my $DBL_MAX = eval $DBL_MAX{$nvsize}; + die "Math::Complex: Could not figure out max nv (nvsize = $nvsize)\n" + unless defined $DBL_MAX; + my $BIGGER_THAN_THIS = 1e30; # Must find something bigger than this. + if ($^O eq 'unicosmk') { + $Inf = $DBL_MAX; + } else { + local $SIG{FPE} = { }; + local $!; + # We do want an arithmetic overflow, Inf INF inf Infinity. + for my $t ( + 'exp(99999)', # Enough even with 128-bit long doubles. + 'inf', + 'Inf', + 'INF', + 'infinity', + 'Infinity', + 'INFINITY', + '1e99999', + ) { + local $^W = 0; + my $i = eval "$t+1.0"; + if (defined $i && $i > $BIGGER_THAN_THIS) { + $Inf = $i; + last; + } + } + $Inf = $DBL_MAX unless defined $Inf; # Oh well, close enough. + die "Math::Complex: Could not get Infinity" + unless $Inf > $BIGGER_THAN_THIS; + $ExpInf = exp(99999); + } + # print "# On this machine, Inf = '$Inf'\n"; +} + +use Scalar::Util qw(set_prototype); + +use warnings; +no warnings 'syntax'; # To avoid the (_) warnings. + +BEGIN { + # For certain functions that we override, in 5.10 or better + # we can set a smarter prototype that will handle the lexical $_ + # (also a 5.10+ feature). + if ($] >= 5.010000) { + set_prototype \&abs, '_'; + set_prototype \&cos, '_'; + set_prototype \&exp, '_'; + set_prototype \&log, '_'; + set_prototype \&sin, '_'; + set_prototype \&sqrt, '_'; + } +} + +my $i; +my %LOGN; + +# Regular expression for floating point numbers. +# These days we could use Scalar::Util::lln(), I guess. +my $gre = qr'\s*([\+\-]?(?:(?:(?:\d+(?:_\d+)*(?:\.\d*(?:_\d+)*)?|\.\d+(?:_\d+)*)(?:[eE][\+\-]?\d+(?:_\d+)*)?))|inf)'i; + +require Exporter; + +@ISA = qw(Exporter); + +my @trig = qw( + pi + tan + csc cosec sec cot cotan + asin acos atan + acsc acosec asec acot acotan + sinh cosh tanh + csch cosech sech coth cotanh + asinh acosh atanh + acsch acosech asech acoth acotanh + ); + +@EXPORT = (qw( + i Re Im rho theta arg + sqrt log ln + log10 logn cbrt root + cplx cplxe + atan2 + ), + @trig); + +my @pi = qw(pi pi2 pi4 pip2 pip4 Inf); + +@EXPORT_OK = @pi; + +%EXPORT_TAGS = ( + 'trig' => [@trig], + 'pi' => [@pi], +); + +use overload + '+' => \&_plus, + '-' => \&_minus, + '*' => \&_multiply, + '/' => \&_divide, + '**' => \&_power, + '==' => \&_numeq, + '<=>' => \&_spaceship, + 'neg' => \&_negate, + '~' => \&_conjugate, + 'abs' => \&abs, + 'sqrt' => \&sqrt, + 'exp' => \&exp, + 'log' => \&log, + 'sin' => \&sin, + 'cos' => \&cos, + 'tan' => \&tan, + 'atan2' => \&atan2, + '""' => \&_stringify; + +# +# Package "privates" +# + +my %DISPLAY_FORMAT = ('style' => 'cartesian', + 'polar_pretty_print' => 1); +my $eps = 1e-14; # Epsilon + +# +# Object attributes (internal): +# cartesian [real, imaginary] -- cartesian form +# polar [rho, theta] -- polar form +# c_dirty cartesian form not up-to-date +# p_dirty polar form not up-to-date +# display display format (package's global when not set) +# + +# Die on bad *make() arguments. + +sub _cannot_make { + die "@{[(caller(1))[3]]}: Cannot take $_[0] of '$_[1]'.\n"; +} + +sub _make { + my $arg = shift; + my ($p, $q); + + if ($arg =~ /^$gre$/) { + ($p, $q) = ($1, 0); + } elsif ($arg =~ /^(?:$gre)?$gre\s*i\s*$/) { + ($p, $q) = ($1 || 0, $2); + } elsif ($arg =~ /^\s*\(\s*$gre\s*(?:,\s*$gre\s*)?\)\s*$/) { + ($p, $q) = ($1, $2 || 0); + } + + if (defined $p) { + $p =~ s/^\+//; + $p =~ s/^(-?)inf$/"${1}9**9**9"/e; + $q =~ s/^\+//; + $q =~ s/^(-?)inf$/"${1}9**9**9"/e; + } + + return ($p, $q); +} + +sub _emake { + my $arg = shift; + my ($p, $q); + + if ($arg =~ /^\s*\[\s*$gre\s*(?:,\s*$gre\s*)?\]\s*$/) { + ($p, $q) = ($1, $2 || 0); + } elsif ($arg =~ m!^\s*\[\s*$gre\s*(?:,\s*([-+]?\d*\s*)?pi(?:/\s*(\d+))?\s*)?\]\s*$!) { + ($p, $q) = ($1, ($2 eq '-' ? -1 : ($2 || 1)) * pi() / ($3 || 1)); + } elsif ($arg =~ /^\s*\[\s*$gre\s*\]\s*$/) { + ($p, $q) = ($1, 0); + } elsif ($arg =~ /^\s*$gre\s*$/) { + ($p, $q) = ($1, 0); + } + + if (defined $p) { + $p =~ s/^\+//; + $q =~ s/^\+//; + $p =~ s/^(-?)inf$/"${1}9**9**9"/e; + $q =~ s/^(-?)inf$/"${1}9**9**9"/e; + } + + return ($p, $q); +} + +# +# ->make +# +# Create a new complex number (cartesian form) +# +sub make { + my $self = bless {}, shift; + my ($re, $im); + if (@_ == 0) { + ($re, $im) = (0, 0); + } elsif (@_ == 1) { + return (ref $self)->emake($_[0]) + if ($_[0] =~ /^\s*\[/); + ($re, $im) = _make($_[0]); + } elsif (@_ == 2) { + ($re, $im) = @_; + } + if (defined $re) { + _cannot_make("real part", $re) unless $re =~ /^$gre$/; + } + $im ||= 0; + _cannot_make("imaginary part", $im) unless $im =~ /^$gre$/; + $self->_set_cartesian([$re, $im ]); + $self->display_format('cartesian'); + + return $self; +} + +# +# ->emake +# +# Create a new complex number (exponential form) +# +sub emake { + my $self = bless {}, shift; + my ($rho, $theta); + if (@_ == 0) { + ($rho, $theta) = (0, 0); + } elsif (@_ == 1) { + return (ref $self)->make($_[0]) + if ($_[0] =~ /^\s*\(/ || $_[0] =~ /i\s*$/); + ($rho, $theta) = _emake($_[0]); + } elsif (@_ == 2) { + ($rho, $theta) = @_; + } + if (defined $rho && defined $theta) { + if ($rho < 0) { + $rho = -$rho; + $theta = ($theta <= 0) ? $theta + pi() : $theta - pi(); + } + } + if (defined $rho) { + _cannot_make("rho", $rho) unless $rho =~ /^$gre$/; + } + $theta ||= 0; + _cannot_make("theta", $theta) unless $theta =~ /^$gre$/; + $self->_set_polar([$rho, $theta]); + $self->display_format('polar'); + + return $self; +} + +sub new { &make } # For backward compatibility only. + +# +# cplx +# +# Creates a complex number from a (re, im) tuple. +# This avoids the burden of writing Math::Complex->make(re, im). +# +sub cplx { + return __PACKAGE__->make(@_); +} + +# +# cplxe +# +# Creates a complex number from a (rho, theta) tuple. +# This avoids the burden of writing Math::Complex->emake(rho, theta). +# +sub cplxe { + return __PACKAGE__->emake(@_); +} + +# +# pi +# +# The number defined as pi = 180 degrees +# +sub pi () { 4 * CORE::atan2(1, 1) } + +# +# pi2 +# +# The full circle +# +sub pi2 () { 2 * pi } + +# +# pi4 +# +# The full circle twice. +# +sub pi4 () { 4 * pi } + +# +# pip2 +# +# The quarter circle +# +sub pip2 () { pi / 2 } + +# +# pip4 +# +# The eighth circle. +# +sub pip4 () { pi / 4 } + +# +# _uplog10 +# +# Used in log10(). +# +sub _uplog10 () { 1 / CORE::log(10) } + +# +# i +# +# The number defined as i*i = -1; +# +sub i () { + return $i if ($i); + $i = bless {}; + $i->{'cartesian'} = [0, 1]; + $i->{'polar'} = [1, pip2]; + $i->{c_dirty} = 0; + $i->{p_dirty} = 0; + return $i; +} + +# +# _ip2 +# +# Half of i. +# +sub _ip2 () { i / 2 } + +# +# Attribute access/set routines +# + +sub _cartesian {$_[0]->{c_dirty} ? + $_[0]->_update_cartesian : $_[0]->{'cartesian'}} +sub _polar {$_[0]->{p_dirty} ? + $_[0]->_update_polar : $_[0]->{'polar'}} + +sub _set_cartesian { $_[0]->{p_dirty}++; $_[0]->{c_dirty} = 0; + $_[0]->{'cartesian'} = $_[1] } +sub _set_polar { $_[0]->{c_dirty}++; $_[0]->{p_dirty} = 0; + $_[0]->{'polar'} = $_[1] } + +# +# ->_update_cartesian +# +# Recompute and return the cartesian form, given accurate polar form. +# +sub _update_cartesian { + my $self = shift; + my ($r, $t) = @{$self->{'polar'}}; + $self->{c_dirty} = 0; + return $self->{'cartesian'} = [$r * CORE::cos($t), $r * CORE::sin($t)]; +} + +# +# +# ->_update_polar +# +# Recompute and return the polar form, given accurate cartesian form. +# +sub _update_polar { + my $self = shift; + my ($x, $y) = @{$self->{'cartesian'}}; + $self->{p_dirty} = 0; + return $self->{'polar'} = [0, 0] if $x == 0 && $y == 0; + return $self->{'polar'} = [CORE::sqrt($x*$x + $y*$y), + CORE::atan2($y, $x)]; +} + +# +# (_plus) +# +# Computes z1+z2. +# +sub _plus { + my ($z1, $z2, $regular) = @_; + my ($re1, $im1) = @{$z1->_cartesian}; + $z2 = cplx($z2) unless ref $z2; + my ($re2, $im2) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0); + unless (defined $regular) { + $z1->_set_cartesian([$re1 + $re2, $im1 + $im2]); + return $z1; + } + return (ref $z1)->make($re1 + $re2, $im1 + $im2); +} + +# +# (_minus) +# +# Computes z1-z2. +# +sub _minus { + my ($z1, $z2, $inverted) = @_; + my ($re1, $im1) = @{$z1->_cartesian}; + $z2 = cplx($z2) unless ref $z2; + my ($re2, $im2) = @{$z2->_cartesian}; + unless (defined $inverted) { + $z1->_set_cartesian([$re1 - $re2, $im1 - $im2]); + return $z1; + } + return $inverted ? + (ref $z1)->make($re2 - $re1, $im2 - $im1) : + (ref $z1)->make($re1 - $re2, $im1 - $im2); + +} + +# +# (_multiply) +# +# Computes z1*z2. +# +sub _multiply { + my ($z1, $z2, $regular) = @_; + if ($z1->{p_dirty} == 0 and ref $z2 and $z2->{p_dirty} == 0) { + # if both polar better use polar to avoid rounding errors + my ($r1, $t1) = @{$z1->_polar}; + my ($r2, $t2) = @{$z2->_polar}; + my $t = $t1 + $t2; + if ($t > pi()) { $t -= pi2 } + elsif ($t <= -pi()) { $t += pi2 } + unless (defined $regular) { + $z1->_set_polar([$r1 * $r2, $t]); + return $z1; + } + return (ref $z1)->emake($r1 * $r2, $t); + } else { + my ($x1, $y1) = @{$z1->_cartesian}; + if (ref $z2) { + my ($x2, $y2) = @{$z2->_cartesian}; + return (ref $z1)->make($x1*$x2-$y1*$y2, $x1*$y2+$y1*$x2); + } else { + return (ref $z1)->make($x1*$z2, $y1*$z2); + } + } +} + +# +# _divbyzero +# +# Die on division by zero. +# +sub _divbyzero { + my $mess = "$_[0]: Division by zero.\n"; + + if (defined $_[1]) { + $mess .= "(Because in the definition of $_[0], the divisor "; + $mess .= "$_[1] " unless ("$_[1]" eq '0'); + $mess .= "is 0)\n"; + } + + my @up = caller(1); + + $mess .= "Died at $up[1] line $up[2].\n"; + + die $mess; +} + +# +# (_divide) +# +# Computes z1/z2. +# +sub _divide { + my ($z1, $z2, $inverted) = @_; + if ($z1->{p_dirty} == 0 and ref $z2 and $z2->{p_dirty} == 0) { + # if both polar better use polar to avoid rounding errors + my ($r1, $t1) = @{$z1->_polar}; + my ($r2, $t2) = @{$z2->_polar}; + my $t; + if ($inverted) { + _divbyzero "$z2/0" if ($r1 == 0); + $t = $t2 - $t1; + if ($t > pi()) { $t -= pi2 } + elsif ($t <= -pi()) { $t += pi2 } + return (ref $z1)->emake($r2 / $r1, $t); + } else { + _divbyzero "$z1/0" if ($r2 == 0); + $t = $t1 - $t2; + if ($t > pi()) { $t -= pi2 } + elsif ($t <= -pi()) { $t += pi2 } + return (ref $z1)->emake($r1 / $r2, $t); + } + } else { + my ($d, $x2, $y2); + if ($inverted) { + ($x2, $y2) = @{$z1->_cartesian}; + $d = $x2*$x2 + $y2*$y2; + _divbyzero "$z2/0" if $d == 0; + return (ref $z1)->make(($x2*$z2)/$d, -($y2*$z2)/$d); + } else { + my ($x1, $y1) = @{$z1->_cartesian}; + if (ref $z2) { + ($x2, $y2) = @{$z2->_cartesian}; + $d = $x2*$x2 + $y2*$y2; + _divbyzero "$z1/0" if $d == 0; + my $u = ($x1*$x2 + $y1*$y2)/$d; + my $v = ($y1*$x2 - $x1*$y2)/$d; + return (ref $z1)->make($u, $v); + } else { + _divbyzero "$z1/0" if $z2 == 0; + return (ref $z1)->make($x1/$z2, $y1/$z2); + } + } + } +} + +# +# (_power) +# +# Computes z1**z2 = exp(z2 * log z1)). +# +sub _power { + my ($z1, $z2, $inverted) = @_; + if ($inverted) { + return 1 if $z1 == 0 || $z2 == 1; + return 0 if $z2 == 0 && Re($z1) > 0; + } else { + return 1 if $z2 == 0 || $z1 == 1; + return 0 if $z1 == 0 && Re($z2) > 0; + } + my $w = $inverted ? &exp($z1 * &log($z2)) + : &exp($z2 * &log($z1)); + # If both arguments cartesian, return cartesian, else polar. + return $z1->{c_dirty} == 0 && + (not ref $z2 or $z2->{c_dirty} == 0) ? + cplx(@{$w->_cartesian}) : $w; +} + +# +# (_spaceship) +# +# Computes z1 <=> z2. +# Sorts on the real part first, then on the imaginary part. Thus 2-4i < 3+8i. +# +sub _spaceship { + my ($z1, $z2, $inverted) = @_; + my ($re1, $im1) = ref $z1 ? @{$z1->_cartesian} : ($z1, 0); + my ($re2, $im2) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0); + my $sgn = $inverted ? -1 : 1; + return $sgn * ($re1 <=> $re2) if $re1 != $re2; + return $sgn * ($im1 <=> $im2); +} + +# +# (_numeq) +# +# Computes z1 == z2. +# +# (Required in addition to _spaceship() because of NaNs.) +sub _numeq { + my ($z1, $z2, $inverted) = @_; + my ($re1, $im1) = ref $z1 ? @{$z1->_cartesian} : ($z1, 0); + my ($re2, $im2) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0); + return $re1 == $re2 && $im1 == $im2 ? 1 : 0; +} + +# +# (_negate) +# +# Computes -z. +# +sub _negate { + my ($z) = @_; + if ($z->{c_dirty}) { + my ($r, $t) = @{$z->_polar}; + $t = ($t <= 0) ? $t + pi : $t - pi; + return (ref $z)->emake($r, $t); + } + my ($re, $im) = @{$z->_cartesian}; + return (ref $z)->make(-$re, -$im); +} + +# +# (_conjugate) +# +# Compute complex's _conjugate. +# +sub _conjugate { + my ($z) = @_; + if ($z->{c_dirty}) { + my ($r, $t) = @{$z->_polar}; + return (ref $z)->emake($r, -$t); + } + my ($re, $im) = @{$z->_cartesian}; + return (ref $z)->make($re, -$im); +} + +# +# (abs) +# +# Compute or set complex's norm (rho). +# +sub abs { + my ($z, $rho) = @_ ? @_ : $_; + unless (ref $z) { + if (@_ == 2) { + $_[0] = $_[1]; + } else { + return CORE::abs($z); + } + } + if (defined $rho) { + $z->{'polar'} = [ $rho, ${$z->_polar}[1] ]; + $z->{p_dirty} = 0; + $z->{c_dirty} = 1; + return $rho; + } else { + return ${$z->_polar}[0]; + } +} + +sub _theta { + my $theta = $_[0]; + + if ($$theta > pi()) { $$theta -= pi2 } + elsif ($$theta <= -pi()) { $$theta += pi2 } +} + +# +# arg +# +# Compute or set complex's argument (theta). +# +sub arg { + my ($z, $theta) = @_; + return $z unless ref $z; + if (defined $theta) { + _theta(\$theta); + $z->{'polar'} = [ ${$z->_polar}[0], $theta ]; + $z->{p_dirty} = 0; + $z->{c_dirty} = 1; + } else { + $theta = ${$z->_polar}[1]; + _theta(\$theta); + } + return $theta; +} + +# +# (sqrt) +# +# Compute sqrt(z). +# +# It is quite tempting to use wantarray here so that in list context +# sqrt() would return the two solutions. This, however, would +# break things like +# +# print "sqrt(z) = ", sqrt($z), "\n"; +# +# The two values would be printed side by side without no intervening +# whitespace, quite confusing. +# Therefore if you want the two solutions use the root(). +# +sub sqrt { + my ($z) = @_ ? $_[0] : $_; + my ($re, $im) = ref $z ? @{$z->_cartesian} : ($z, 0); + return $re < 0 ? cplx(0, CORE::sqrt(-$re)) : CORE::sqrt($re) + if $im == 0; + my ($r, $t) = @{$z->_polar}; + return (ref $z)->emake(CORE::sqrt($r), $t/2); +} + +# +# cbrt +# +# Compute cbrt(z) (cubic root). +# +# Why are we not returning three values? The same answer as for sqrt(). +# +sub cbrt { + my ($z) = @_; + return $z < 0 ? + -CORE::exp(CORE::log(-$z)/3) : + ($z > 0 ? CORE::exp(CORE::log($z)/3): 0) + unless ref $z; + my ($r, $t) = @{$z->_polar}; + return 0 if $r == 0; + return (ref $z)->emake(CORE::exp(CORE::log($r)/3), $t/3); +} + +# +# _rootbad +# +# Die on bad root. +# +sub _rootbad { + my $mess = "Root '$_[0]' illegal, root rank must be positive integer.\n"; + + my @up = caller(1); + + $mess .= "Died at $up[1] line $up[2].\n"; + + die $mess; +} + +# +# root +# +# Computes all nth root for z, returning an array whose size is n. +# `n' must be a positive integer. +# +# The roots are given by (for k = 0..n-1): +# +# z^(1/n) = r^(1/n) (cos ((t+2 k pi)/n) + i sin ((t+2 k pi)/n)) +# +sub root { + my ($z, $n, $k) = @_; + _rootbad($n) if ($n < 1 or int($n) != $n); + my ($r, $t) = ref $z ? + @{$z->_polar} : (CORE::abs($z), $z >= 0 ? 0 : pi); + my $theta_inc = pi2 / $n; + my $rho = $r ** (1/$n); + my $cartesian = ref $z && $z->{c_dirty} == 0; + if (@_ == 2) { + my @root; + for (my $i = 0, my $theta = $t / $n; + $i < $n; + $i++, $theta += $theta_inc) { + my $w = cplxe($rho, $theta); + # Yes, $cartesian is loop invariant. + push @root, $cartesian ? cplx(@{$w->_cartesian}) : $w; + } + return @root; + } elsif (@_ == 3) { + my $w = cplxe($rho, $t / $n + $k * $theta_inc); + return $cartesian ? cplx(@{$w->_cartesian}) : $w; + } +} + +# +# Re +# +# Return or set Re(z). +# +sub Re { + my ($z, $Re) = @_; + return $z unless ref $z; + if (defined $Re) { + $z->{'cartesian'} = [ $Re, ${$z->_cartesian}[1] ]; + $z->{c_dirty} = 0; + $z->{p_dirty} = 1; + } else { + return ${$z->_cartesian}[0]; + } +} + +# +# Im +# +# Return or set Im(z). +# +sub Im { + my ($z, $Im) = @_; + return 0 unless ref $z; + if (defined $Im) { + $z->{'cartesian'} = [ ${$z->_cartesian}[0], $Im ]; + $z->{c_dirty} = 0; + $z->{p_dirty} = 1; + } else { + return ${$z->_cartesian}[1]; + } +} + +# +# rho +# +# Return or set rho(w). +# +sub rho { + Math::Complex::abs(@_); +} + +# +# theta +# +# Return or set theta(w). +# +sub theta { + Math::Complex::arg(@_); +} + +# +# (exp) +# +# Computes exp(z). +# +sub exp { + my ($z) = @_ ? @_ : $_; + return CORE::exp($z) unless ref $z; + my ($x, $y) = @{$z->_cartesian}; + return (ref $z)->emake(CORE::exp($x), $y); +} + +# +# _logofzero +# +# Die on logarithm of zero. +# +sub _logofzero { + my $mess = "$_[0]: Logarithm of zero.\n"; + + if (defined $_[1]) { + $mess .= "(Because in the definition of $_[0], the argument "; + $mess .= "$_[1] " unless ($_[1] eq '0'); + $mess .= "is 0)\n"; + } + + my @up = caller(1); + + $mess .= "Died at $up[1] line $up[2].\n"; + + die $mess; +} + +# +# (log) +# +# Compute log(z). +# +sub log { + my ($z) = @_ ? @_ : $_; + unless (ref $z) { + _logofzero("log") if $z == 0; + return $z > 0 ? CORE::log($z) : cplx(CORE::log(-$z), pi); + } + my ($r, $t) = @{$z->_polar}; + _logofzero("log") if $r == 0; + if ($t > pi()) { $t -= pi2 } + elsif ($t <= -pi()) { $t += pi2 } + return (ref $z)->make(CORE::log($r), $t); +} + +# +# ln +# +# Alias for log(). +# +sub ln { Math::Complex::log(@_) } + +# +# log10 +# +# Compute log10(z). +# + +sub log10 { + return Math::Complex::log($_[0]) * _uplog10; +} + +# +# logn +# +# Compute logn(z,n) = log(z) / log(n) +# +sub logn { + my ($z, $n) = @_; + $z = cplx($z, 0) unless ref $z; + my $logn = $LOGN{$n}; + $logn = $LOGN{$n} = CORE::log($n) unless defined $logn; # Cache log(n) + return &log($z) / $logn; +} + +# +# (cos) +# +# Compute cos(z) = (exp(iz) + exp(-iz))/2. +# +sub cos { + my ($z) = @_ ? @_ : $_; + return CORE::cos($z) unless ref $z; + my ($x, $y) = @{$z->_cartesian}; + my $ey = CORE::exp($y); + my $sx = CORE::sin($x); + my $cx = CORE::cos($x); + my $ey_1 = $ey ? 1 / $ey : Inf(); + return (ref $z)->make($cx * ($ey + $ey_1)/2, + $sx * ($ey_1 - $ey)/2); +} + +# +# (sin) +# +# Compute sin(z) = (exp(iz) - exp(-iz))/2. +# +sub sin { + my ($z) = @_ ? @_ : $_; + return CORE::sin($z) unless ref $z; + my ($x, $y) = @{$z->_cartesian}; + my $ey = CORE::exp($y); + my $sx = CORE::sin($x); + my $cx = CORE::cos($x); + my $ey_1 = $ey ? 1 / $ey : Inf(); + return (ref $z)->make($sx * ($ey + $ey_1)/2, + $cx * ($ey - $ey_1)/2); +} + +# +# tan +# +# Compute tan(z) = sin(z) / cos(z). +# +sub tan { + my ($z) = @_; + my $cz = &cos($z); + _divbyzero "tan($z)", "cos($z)" if $cz == 0; + return &sin($z) / $cz; +} + +# +# sec +# +# Computes the secant sec(z) = 1 / cos(z). +# +sub sec { + my ($z) = @_; + my $cz = &cos($z); + _divbyzero "sec($z)", "cos($z)" if ($cz == 0); + return 1 / $cz; +} + +# +# csc +# +# Computes the cosecant csc(z) = 1 / sin(z). +# +sub csc { + my ($z) = @_; + my $sz = &sin($z); + _divbyzero "csc($z)", "sin($z)" if ($sz == 0); + return 1 / $sz; +} + +# +# cosec +# +# Alias for csc(). +# +sub cosec { Math::Complex::csc(@_) } + +# +# cot +# +# Computes cot(z) = cos(z) / sin(z). +# +sub cot { + my ($z) = @_; + my $sz = &sin($z); + _divbyzero "cot($z)", "sin($z)" if ($sz == 0); + return &cos($z) / $sz; +} + +# +# cotan +# +# Alias for cot(). +# +sub cotan { Math::Complex::cot(@_) } + +# +# acos +# +# Computes the arc cosine acos(z) = -i log(z + sqrt(z*z-1)). +# +sub acos { + my $z = $_[0]; + return CORE::atan2(CORE::sqrt(1-$z*$z), $z) + if (! ref $z) && CORE::abs($z) <= 1; + $z = cplx($z, 0) unless ref $z; + my ($x, $y) = @{$z->_cartesian}; + return 0 if $x == 1 && $y == 0; + my $t1 = CORE::sqrt(($x+1)*($x+1) + $y*$y); + my $t2 = CORE::sqrt(($x-1)*($x-1) + $y*$y); + my $alpha = ($t1 + $t2)/2; + my $beta = ($t1 - $t2)/2; + $alpha = 1 if $alpha < 1; + if ($beta > 1) { $beta = 1 } + elsif ($beta < -1) { $beta = -1 } + my $u = CORE::atan2(CORE::sqrt(1-$beta*$beta), $beta); + my $v = CORE::log($alpha + CORE::sqrt($alpha*$alpha-1)); + $v = -$v if $y > 0 || ($y == 0 && $x < -1); + return (ref $z)->make($u, $v); +} + +# +# asin +# +# Computes the arc sine asin(z) = -i log(iz + sqrt(1-z*z)). +# +sub asin { + my $z = $_[0]; + return CORE::atan2($z, CORE::sqrt(1-$z*$z)) + if (! ref $z) && CORE::abs($z) <= 1; + $z = cplx($z, 0) unless ref $z; + my ($x, $y) = @{$z->_cartesian}; + return 0 if $x == 0 && $y == 0; + my $t1 = CORE::sqrt(($x+1)*($x+1) + $y*$y); + my $t2 = CORE::sqrt(($x-1)*($x-1) + $y*$y); + my $alpha = ($t1 + $t2)/2; + my $beta = ($t1 - $t2)/2; + $alpha = 1 if $alpha < 1; + if ($beta > 1) { $beta = 1 } + elsif ($beta < -1) { $beta = -1 } + my $u = CORE::atan2($beta, CORE::sqrt(1-$beta*$beta)); + my $v = -CORE::log($alpha + CORE::sqrt($alpha*$alpha-1)); + $v = -$v if $y > 0 || ($y == 0 && $x < -1); + return (ref $z)->make($u, $v); +} + +# +# atan +# +# Computes the arc tangent atan(z) = i/2 log((i+z) / (i-z)). +# +sub atan { + my ($z) = @_; + return CORE::atan2($z, 1) unless ref $z; + my ($x, $y) = ref $z ? @{$z->_cartesian} : ($z, 0); + return 0 if $x == 0 && $y == 0; + _divbyzero "atan(i)" if ( $z == i); + _logofzero "atan(-i)" if (-$z == i); # -i is a bad file test... + my $log = &log((i + $z) / (i - $z)); + return _ip2 * $log; +} + +# +# asec +# +# Computes the arc secant asec(z) = acos(1 / z). +# +sub asec { + my ($z) = @_; + _divbyzero "asec($z)", $z if ($z == 0); + return acos(1 / $z); +} + +# +# acsc +# +# Computes the arc cosecant acsc(z) = asin(1 / z). +# +sub acsc { + my ($z) = @_; + _divbyzero "acsc($z)", $z if ($z == 0); + return asin(1 / $z); +} + +# +# acosec +# +# Alias for acsc(). +# +sub acosec { Math::Complex::acsc(@_) } + +# +# acot +# +# Computes the arc cotangent acot(z) = atan(1 / z) +# +sub acot { + my ($z) = @_; + _divbyzero "acot(0)" if $z == 0; + return ($z >= 0) ? CORE::atan2(1, $z) : CORE::atan2(-1, -$z) + unless ref $z; + _divbyzero "acot(i)" if ($z - i == 0); + _logofzero "acot(-i)" if ($z + i == 0); + return atan(1 / $z); +} + +# +# acotan +# +# Alias for acot(). +# +sub acotan { Math::Complex::acot(@_) } + +# +# cosh +# +# Computes the hyperbolic cosine cosh(z) = (exp(z) + exp(-z))/2. +# +sub cosh { + my ($z) = @_; + my $ex; + unless (ref $z) { + $ex = CORE::exp($z); + return $ex ? ($ex == $ExpInf ? Inf() : ($ex + 1/$ex)/2) : Inf(); + } + my ($x, $y) = @{$z->_cartesian}; + $ex = CORE::exp($x); + my $ex_1 = $ex ? 1 / $ex : Inf(); + return (ref $z)->make(CORE::cos($y) * ($ex + $ex_1)/2, + CORE::sin($y) * ($ex - $ex_1)/2); +} + +# +# sinh +# +# Computes the hyperbolic sine sinh(z) = (exp(z) - exp(-z))/2. +# +sub sinh { + my ($z) = @_; + my $ex; + unless (ref $z) { + return 0 if $z == 0; + $ex = CORE::exp($z); + return $ex ? ($ex == $ExpInf ? Inf() : ($ex - 1/$ex)/2) : -Inf(); + } + my ($x, $y) = @{$z->_cartesian}; + my $cy = CORE::cos($y); + my $sy = CORE::sin($y); + $ex = CORE::exp($x); + my $ex_1 = $ex ? 1 / $ex : Inf(); + return (ref $z)->make(CORE::cos($y) * ($ex - $ex_1)/2, + CORE::sin($y) * ($ex + $ex_1)/2); +} + +# +# tanh +# +# Computes the hyperbolic tangent tanh(z) = sinh(z) / cosh(z). +# +sub tanh { + my ($z) = @_; + my $cz = cosh($z); + _divbyzero "tanh($z)", "cosh($z)" if ($cz == 0); + my $sz = sinh($z); + return 1 if $cz == $sz; + return -1 if $cz == -$sz; + return $sz / $cz; +} + +# +# sech +# +# Computes the hyperbolic secant sech(z) = 1 / cosh(z). +# +sub sech { + my ($z) = @_; + my $cz = cosh($z); + _divbyzero "sech($z)", "cosh($z)" if ($cz == 0); + return 1 / $cz; +} + +# +# csch +# +# Computes the hyperbolic cosecant csch(z) = 1 / sinh(z). +# +sub csch { + my ($z) = @_; + my $sz = sinh($z); + _divbyzero "csch($z)", "sinh($z)" if ($sz == 0); + return 1 / $sz; +} + +# +# cosech +# +# Alias for csch(). +# +sub cosech { Math::Complex::csch(@_) } + +# +# coth +# +# Computes the hyperbolic cotangent coth(z) = cosh(z) / sinh(z). +# +sub coth { + my ($z) = @_; + my $sz = sinh($z); + _divbyzero "coth($z)", "sinh($z)" if $sz == 0; + my $cz = cosh($z); + return 1 if $cz == $sz; + return -1 if $cz == -$sz; + return $cz / $sz; +} + +# +# cotanh +# +# Alias for coth(). +# +sub cotanh { Math::Complex::coth(@_) } + +# +# acosh +# +# Computes the area/inverse hyperbolic cosine acosh(z) = log(z + sqrt(z*z-1)). +# +sub acosh { + my ($z) = @_; + unless (ref $z) { + $z = cplx($z, 0); + } + my ($re, $im) = @{$z->_cartesian}; + if ($im == 0) { + return CORE::log($re + CORE::sqrt($re*$re - 1)) + if $re >= 1; + return cplx(0, CORE::atan2(CORE::sqrt(1 - $re*$re), $re)) + if CORE::abs($re) < 1; + } + my $t = &sqrt($z * $z - 1) + $z; + # Try Taylor if looking bad (this usually means that + # $z was large negative, therefore the sqrt is really + # close to abs(z), summing that with z...) + $t = 1/(2 * $z) - 1/(8 * $z**3) + 1/(16 * $z**5) - 5/(128 * $z**7) + if $t == 0; + my $u = &log($t); + $u->Im(-$u->Im) if $re < 0 && $im == 0; + return $re < 0 ? -$u : $u; +} + +# +# asinh +# +# Computes the area/inverse hyperbolic sine asinh(z) = log(z + sqrt(z*z+1)) +# +sub asinh { + my ($z) = @_; + unless (ref $z) { + my $t = $z + CORE::sqrt($z*$z + 1); + return CORE::log($t) if $t; + } + my $t = &sqrt($z * $z + 1) + $z; + # Try Taylor if looking bad (this usually means that + # $z was large negative, therefore the sqrt is really + # close to abs(z), summing that with z...) + $t = 1/(2 * $z) - 1/(8 * $z**3) + 1/(16 * $z**5) - 5/(128 * $z**7) + if $t == 0; + return &log($t); +} + +# +# atanh +# +# Computes the area/inverse hyperbolic tangent atanh(z) = 1/2 log((1+z) / (1-z)). +# +sub atanh { + my ($z) = @_; + unless (ref $z) { + return CORE::log((1 + $z)/(1 - $z))/2 if CORE::abs($z) < 1; + $z = cplx($z, 0); + } + _divbyzero 'atanh(1)', "1 - $z" if (1 - $z == 0); + _logofzero 'atanh(-1)' if (1 + $z == 0); + return 0.5 * &log((1 + $z) / (1 - $z)); +} + +# +# asech +# +# Computes the area/inverse hyperbolic secant asech(z) = acosh(1 / z). +# +sub asech { + my ($z) = @_; + _divbyzero 'asech(0)', "$z" if ($z == 0); + return acosh(1 / $z); +} + +# +# acsch +# +# Computes the area/inverse hyperbolic cosecant acsch(z) = asinh(1 / z). +# +sub acsch { + my ($z) = @_; + _divbyzero 'acsch(0)', $z if ($z == 0); + return asinh(1 / $z); +} + +# +# acosech +# +# Alias for acosh(). +# +sub acosech { Math::Complex::acsch(@_) } + +# +# acoth +# +# Computes the area/inverse hyperbolic cotangent acoth(z) = 1/2 log((1+z) / (z-1)). +# +sub acoth { + my ($z) = @_; + _divbyzero 'acoth(0)' if ($z == 0); + unless (ref $z) { + return CORE::log(($z + 1)/($z - 1))/2 if CORE::abs($z) > 1; + $z = cplx($z, 0); + } + _divbyzero 'acoth(1)', "$z - 1" if ($z - 1 == 0); + _logofzero 'acoth(-1)', "1 + $z" if (1 + $z == 0); + return &log((1 + $z) / ($z - 1)) / 2; +} + +# +# acotanh +# +# Alias for acot(). +# +sub acotanh { Math::Complex::acoth(@_) } + +# +# (atan2) +# +# Compute atan(z1/z2), minding the right quadrant. +# +sub atan2 { + my ($z1, $z2, $inverted) = @_; + my ($re1, $im1, $re2, $im2); + if ($inverted) { + ($re1, $im1) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0); + ($re2, $im2) = ref $z1 ? @{$z1->_cartesian} : ($z1, 0); + } else { + ($re1, $im1) = ref $z1 ? @{$z1->_cartesian} : ($z1, 0); + ($re2, $im2) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0); + } + if ($im1 || $im2) { + # In MATLAB the imaginary parts are ignored. + # warn "atan2: Imaginary parts ignored"; + # http://documents.wolfram.com/mathematica/functions/ArcTan + # NOTE: Mathematica ArcTan[x,y] while atan2(y,x) + my $s = $z1 * $z1 + $z2 * $z2; + _divbyzero("atan2") if $s == 0; + my $i = &i; + my $r = $z2 + $z1 * $i; + return -$i * &log($r / &sqrt( $s )); + } + return CORE::atan2($re1, $re2); +} + +# +# display_format +# ->display_format +# +# Set (get if no argument) the display format for all complex numbers that +# don't happen to have overridden it via ->display_format +# +# When called as an object method, this actually sets the display format for +# the current object. +# +# Valid object formats are 'c' and 'p' for cartesian and polar. The first +# letter is used actually, so the type can be fully spelled out for clarity. +# +sub display_format { + my $self = shift; + my %display_format = %DISPLAY_FORMAT; + + if (ref $self) { # Called as an object method + if (exists $self->{display_format}) { + my %obj = %{$self->{display_format}}; + @display_format{keys %obj} = values %obj; + } + } + if (@_ == 1) { + $display_format{style} = shift; + } else { + my %new = @_; + @display_format{keys %new} = values %new; + } + + if (ref $self) { # Called as an object method + $self->{display_format} = { %display_format }; + return + wantarray ? + %{$self->{display_format}} : + $self->{display_format}->{style}; + } + + # Called as a class method + %DISPLAY_FORMAT = %display_format; + return + wantarray ? + %DISPLAY_FORMAT : + $DISPLAY_FORMAT{style}; +} + +# +# (_stringify) +# +# Show nicely formatted complex number under its cartesian or polar form, +# depending on the current display format: +# +# . If a specific display format has been recorded for this object, use it. +# . Otherwise, use the generic current default for all complex numbers, +# which is a package global variable. +# +sub _stringify { + my ($z) = shift; + + my $style = $z->display_format; + + $style = $DISPLAY_FORMAT{style} unless defined $style; + + return $z->_stringify_polar if $style =~ /^p/i; + return $z->_stringify_cartesian; +} + +# +# ->_stringify_cartesian +# +# Stringify as a cartesian representation 'a+bi'. +# +sub _stringify_cartesian { + my $z = shift; + my ($x, $y) = @{$z->_cartesian}; + my ($re, $im); + + my %format = $z->display_format; + my $format = $format{format}; + + if ($x) { + if ($x =~ /^NaN[QS]?$/i) { + $re = $x; + } else { + if ($x =~ /^-?\Q$Inf\E$/oi) { + $re = $x; + } else { + $re = defined $format ? sprintf($format, $x) : $x; + } + } + } else { + undef $re; + } + + if ($y) { + if ($y =~ /^(NaN[QS]?)$/i) { + $im = $y; + } else { + if ($y =~ /^-?\Q$Inf\E$/oi) { + $im = $y; + } else { + $im = + defined $format ? + sprintf($format, $y) : + ($y == 1 ? "" : ($y == -1 ? "-" : $y)); + } + } + $im .= "i"; + } else { + undef $im; + } + + my $str = $re; + + if (defined $im) { + if ($y < 0) { + $str .= $im; + } elsif ($y > 0 || $im =~ /^NaN[QS]?i$/i) { + $str .= "+" if defined $re; + $str .= $im; + } + } elsif (!defined $re) { + $str = "0"; + } + + return $str; +} + + +# +# ->_stringify_polar +# +# Stringify as a polar representation '[r,t]'. +# +sub _stringify_polar { + my $z = shift; + my ($r, $t) = @{$z->_polar}; + my $theta; + + my %format = $z->display_format; + my $format = $format{format}; + + if ($t =~ /^NaN[QS]?$/i || $t =~ /^-?\Q$Inf\E$/oi) { + $theta = $t; + } elsif ($t == pi) { + $theta = "pi"; + } elsif ($r == 0 || $t == 0) { + $theta = defined $format ? sprintf($format, $t) : $t; + } + + return "[$r,$theta]" if defined $theta; + + # + # Try to identify pi/n and friends. + # + + $t -= int(CORE::abs($t) / pi2) * pi2; + + if ($format{polar_pretty_print} && $t) { + my ($a, $b); + for $a (2..9) { + $b = $t * $a / pi; + if ($b =~ /^-?\d+$/) { + $b = $b < 0 ? "-" : "" if CORE::abs($b) == 1; + $theta = "${b}pi/$a"; + last; + } + } + } + + if (defined $format) { + $r = sprintf($format, $r); + $theta = sprintf($format, $theta) unless defined $theta; + } else { + $theta = $t unless defined $theta; + } + + return "[$r,$theta]"; +} + +sub Inf { + return $Inf; +} + +1; +__END__ + +=pod + +=head1 NAME + +Math::Complex - complex numbers and associated mathematical functions + +=head1 SYNOPSIS + + use Math::Complex; + + $z = Math::Complex->make(5, 6); + $t = 4 - 3*i + $z; + $j = cplxe(1, 2*pi/3); + +=head1 DESCRIPTION + +This package lets you create and manipulate complex numbers. By default, +I<Perl> limits itself to real numbers, but an extra C<use> statement brings +full complex support, along with a full set of mathematical functions +typically associated with and/or extended to complex numbers. + +If you wonder what complex numbers are, they were invented to be able to solve +the following equation: + + x*x = -1 + +and by definition, the solution is noted I<i> (engineers use I<j> instead since +I<i> usually denotes an intensity, but the name does not matter). The number +I<i> is a pure I<imaginary> number. + +The arithmetics with pure imaginary numbers works just like you would expect +it with real numbers... you just have to remember that + + i*i = -1 + +so you have: + + 5i + 7i = i * (5 + 7) = 12i + 4i - 3i = i * (4 - 3) = i + 4i * 2i = -8 + 6i / 2i = 3 + 1 / i = -i + +Complex numbers are numbers that have both a real part and an imaginary +part, and are usually noted: + + a + bi + +where C<a> is the I<real> part and C<b> is the I<imaginary> part. The +arithmetic with complex numbers is straightforward. You have to +keep track of the real and the imaginary parts, but otherwise the +rules used for real numbers just apply: + + (4 + 3i) + (5 - 2i) = (4 + 5) + i(3 - 2) = 9 + i + (2 + i) * (4 - i) = 2*4 + 4i -2i -i*i = 8 + 2i + 1 = 9 + 2i + +A graphical representation of complex numbers is possible in a plane +(also called the I<complex plane>, but it's really a 2D plane). +The number + + z = a + bi + +is the point whose coordinates are (a, b). Actually, it would +be the vector originating from (0, 0) to (a, b). It follows that the addition +of two complex numbers is a vectorial addition. + +Since there is a bijection between a point in the 2D plane and a complex +number (i.e. the mapping is unique and reciprocal), a complex number +can also be uniquely identified with polar coordinates: + + [rho, theta] + +where C<rho> is the distance to the origin, and C<theta> the angle between +the vector and the I<x> axis. There is a notation for this using the +exponential form, which is: + + rho * exp(i * theta) + +where I<i> is the famous imaginary number introduced above. Conversion +between this form and the cartesian form C<a + bi> is immediate: + + a = rho * cos(theta) + b = rho * sin(theta) + +which is also expressed by this formula: + + z = rho * exp(i * theta) = rho * (cos theta + i * sin theta) + +In other words, it's the projection of the vector onto the I<x> and I<y> +axes. Mathematicians call I<rho> the I<norm> or I<modulus> and I<theta> +the I<argument> of the complex number. The I<norm> of C<z> is +marked here as C<abs(z)>. + +The polar notation (also known as the trigonometric representation) is +much more handy for performing multiplications and divisions of +complex numbers, whilst the cartesian notation is better suited for +additions and subtractions. Real numbers are on the I<x> axis, and +therefore I<y> or I<theta> is zero or I<pi>. + +All the common operations that can be performed on a real number have +been defined to work on complex numbers as well, and are merely +I<extensions> of the operations defined on real numbers. This means +they keep their natural meaning when there is no imaginary part, provided +the number is within their definition set. + +For instance, the C<sqrt> routine which computes the square root of +its argument is only defined for non-negative real numbers and yields a +non-negative real number (it is an application from B<R+> to B<R+>). +If we allow it to return a complex number, then it can be extended to +negative real numbers to become an application from B<R> to B<C> (the +set of complex numbers): + + sqrt(x) = x >= 0 ? sqrt(x) : sqrt(-x)*i + +It can also be extended to be an application from B<C> to B<C>, +whilst its restriction to B<R> behaves as defined above by using +the following definition: + + sqrt(z = [r,t]) = sqrt(r) * exp(i * t/2) + +Indeed, a negative real number can be noted C<[x,pi]> (the modulus +I<x> is always non-negative, so C<[x,pi]> is really C<-x>, a negative +number) and the above definition states that + + sqrt([x,pi]) = sqrt(x) * exp(i*pi/2) = [sqrt(x),pi/2] = sqrt(x)*i + +which is exactly what we had defined for negative real numbers above. +The C<sqrt> returns only one of the solutions: if you want the both, +use the C<root> function. + +All the common mathematical functions defined on real numbers that +are extended to complex numbers share that same property of working +I<as usual> when the imaginary part is zero (otherwise, it would not +be called an extension, would it?). + +A I<new> operation possible on a complex number that is +the identity for real numbers is called the I<conjugate>, and is noted +with a horizontal bar above the number, or C<~z> here. + + z = a + bi + ~z = a - bi + +Simple... Now look: + + z * ~z = (a + bi) * (a - bi) = a*a + b*b + +We saw that the norm of C<z> was noted C<abs(z)> and was defined as the +distance to the origin, also known as: + + rho = abs(z) = sqrt(a*a + b*b) + +so + + z * ~z = abs(z) ** 2 + +If z is a pure real number (i.e. C<b == 0>), then the above yields: + + a * a = abs(a) ** 2 + +which is true (C<abs> has the regular meaning for real number, i.e. stands +for the absolute value). This example explains why the norm of C<z> is +noted C<abs(z)>: it extends the C<abs> function to complex numbers, yet +is the regular C<abs> we know when the complex number actually has no +imaginary part... This justifies I<a posteriori> our use of the C<abs> +notation for the norm. + +=head1 OPERATIONS + +Given the following notations: + + z1 = a + bi = r1 * exp(i * t1) + z2 = c + di = r2 * exp(i * t2) + z = <any complex or real number> + +the following (overloaded) operations are supported on complex numbers: + + z1 + z2 = (a + c) + i(b + d) + z1 - z2 = (a - c) + i(b - d) + z1 * z2 = (r1 * r2) * exp(i * (t1 + t2)) + z1 / z2 = (r1 / r2) * exp(i * (t1 - t2)) + z1 ** z2 = exp(z2 * log z1) + ~z = a - bi + abs(z) = r1 = sqrt(a*a + b*b) + sqrt(z) = sqrt(r1) * exp(i * t/2) + exp(z) = exp(a) * exp(i * b) + log(z) = log(r1) + i*t + sin(z) = 1/2i (exp(i * z1) - exp(-i * z)) + cos(z) = 1/2 (exp(i * z1) + exp(-i * z)) + atan2(y, x) = atan(y / x) # Minding the right quadrant, note the order. + +The definition used for complex arguments of atan2() is + + -i log((x + iy)/sqrt(x*x+y*y)) + +Note that atan2(0, 0) is not well-defined. + +The following extra operations are supported on both real and complex +numbers: + + Re(z) = a + Im(z) = b + arg(z) = t + abs(z) = r + + cbrt(z) = z ** (1/3) + log10(z) = log(z) / log(10) + logn(z, n) = log(z) / log(n) + + tan(z) = sin(z) / cos(z) + + csc(z) = 1 / sin(z) + sec(z) = 1 / cos(z) + cot(z) = 1 / tan(z) + + asin(z) = -i * log(i*z + sqrt(1-z*z)) + acos(z) = -i * log(z + i*sqrt(1-z*z)) + atan(z) = i/2 * log((i+z) / (i-z)) + + acsc(z) = asin(1 / z) + asec(z) = acos(1 / z) + acot(z) = atan(1 / z) = -i/2 * log((i+z) / (z-i)) + + sinh(z) = 1/2 (exp(z) - exp(-z)) + cosh(z) = 1/2 (exp(z) + exp(-z)) + tanh(z) = sinh(z) / cosh(z) = (exp(z) - exp(-z)) / (exp(z) + exp(-z)) + + csch(z) = 1 / sinh(z) + sech(z) = 1 / cosh(z) + coth(z) = 1 / tanh(z) + + asinh(z) = log(z + sqrt(z*z+1)) + acosh(z) = log(z + sqrt(z*z-1)) + atanh(z) = 1/2 * log((1+z) / (1-z)) + + acsch(z) = asinh(1 / z) + asech(z) = acosh(1 / z) + acoth(z) = atanh(1 / z) = 1/2 * log((1+z) / (z-1)) + +I<arg>, I<abs>, I<log>, I<csc>, I<cot>, I<acsc>, I<acot>, I<csch>, +I<coth>, I<acosech>, I<acotanh>, have aliases I<rho>, I<theta>, I<ln>, +I<cosec>, I<cotan>, I<acosec>, I<acotan>, I<cosech>, I<cotanh>, +I<acosech>, I<acotanh>, respectively. C<Re>, C<Im>, C<arg>, C<abs>, +C<rho>, and C<theta> can be used also as mutators. The C<cbrt> +returns only one of the solutions: if you want all three, use the +C<root> function. + +The I<root> function is available to compute all the I<n> +roots of some complex, where I<n> is a strictly positive integer. +There are exactly I<n> such roots, returned as a list. Getting the +number mathematicians call C<j> such that: + + 1 + j + j*j = 0; + +is a simple matter of writing: + + $j = ((root(1, 3))[1]; + +The I<k>th root for C<z = [r,t]> is given by: + + (root(z, n))[k] = r**(1/n) * exp(i * (t + 2*k*pi)/n) + +You can return the I<k>th root directly by C<root(z, n, k)>, +indexing starting from I<zero> and ending at I<n - 1>. + +The I<spaceship> numeric comparison operator, E<lt>=E<gt>, is also +defined. In order to ensure its restriction to real numbers is conform +to what you would expect, the comparison is run on the real part of +the complex number first, and imaginary parts are compared only when +the real parts match. + +=head1 CREATION + +To create a complex number, use either: + + $z = Math::Complex->make(3, 4); + $z = cplx(3, 4); + +if you know the cartesian form of the number, or + + $z = 3 + 4*i; + +if you like. To create a number using the polar form, use either: + + $z = Math::Complex->emake(5, pi/3); + $x = cplxe(5, pi/3); + +instead. The first argument is the modulus, the second is the angle +(in radians, the full circle is 2*pi). (Mnemonic: C<e> is used as a +notation for complex numbers in the polar form). + +It is possible to write: + + $x = cplxe(-3, pi/4); + +but that will be silently converted into C<[3,-3pi/4]>, since the +modulus must be non-negative (it represents the distance to the origin +in the complex plane). + +It is also possible to have a complex number as either argument of the +C<make>, C<emake>, C<cplx>, and C<cplxe>: the appropriate component of +the argument will be used. + + $z1 = cplx(-2, 1); + $z2 = cplx($z1, 4); + +The C<new>, C<make>, C<emake>, C<cplx>, and C<cplxe> will also +understand a single (string) argument of the forms + + 2-3i + -3i + [2,3] + [2,-3pi/4] + [2] + +in which case the appropriate cartesian and exponential components +will be parsed from the string and used to create new complex numbers. +The imaginary component and the theta, respectively, will default to zero. + +The C<new>, C<make>, C<emake>, C<cplx>, and C<cplxe> will also +understand the case of no arguments: this means plain zero or (0, 0). + +=head1 DISPLAYING + +When printed, a complex number is usually shown under its cartesian +style I<a+bi>, but there are legitimate cases where the polar style +I<[r,t]> is more appropriate. The process of converting the complex +number into a string that can be displayed is known as I<stringification>. + +By calling the class method C<Math::Complex::display_format> and +supplying either C<"polar"> or C<"cartesian"> as an argument, you +override the default display style, which is C<"cartesian">. Not +supplying any argument returns the current settings. + +This default can be overridden on a per-number basis by calling the +C<display_format> method instead. As before, not supplying any argument +returns the current display style for this number. Otherwise whatever you +specify will be the new display style for I<this> particular number. + +For instance: + + use Math::Complex; + + Math::Complex::display_format('polar'); + $j = (root(1, 3))[1]; + print "j = $j\n"; # Prints "j = [1,2pi/3]" + $j->display_format('cartesian'); + print "j = $j\n"; # Prints "j = -0.5+0.866025403784439i" + +The polar style attempts to emphasize arguments like I<k*pi/n> +(where I<n> is a positive integer and I<k> an integer within [-9, +9]), +this is called I<polar pretty-printing>. + +For the reverse of stringifying, see the C<make> and C<emake>. + +=head2 CHANGED IN PERL 5.6 + +The C<display_format> class method and the corresponding +C<display_format> object method can now be called using +a parameter hash instead of just a one parameter. + +The old display format style, which can have values C<"cartesian"> or +C<"polar">, can be changed using the C<"style"> parameter. + + $j->display_format(style => "polar"); + +The one parameter calling convention also still works. + + $j->display_format("polar"); + +There are two new display parameters. + +The first one is C<"format">, which is a sprintf()-style format string +to be used for both numeric parts of the complex number(s). The is +somewhat system-dependent but most often it corresponds to C<"%.15g">. +You can revert to the default by setting the C<format> to C<undef>. + + # the $j from the above example + + $j->display_format('format' => '%.5f'); + print "j = $j\n"; # Prints "j = -0.50000+0.86603i" + $j->display_format('format' => undef); + print "j = $j\n"; # Prints "j = -0.5+0.86603i" + +Notice that this affects also the return values of the +C<display_format> methods: in list context the whole parameter hash +will be returned, as opposed to only the style parameter value. +This is a potential incompatibility with earlier versions if you +have been calling the C<display_format> method in list context. + +The second new display parameter is C<"polar_pretty_print">, which can +be set to true or false, the default being true. See the previous +section for what this means. + +=head1 USAGE + +Thanks to overloading, the handling of arithmetics with complex numbers +is simple and almost transparent. + +Here are some examples: + + use Math::Complex; + + $j = cplxe(1, 2*pi/3); # $j ** 3 == 1 + print "j = $j, j**3 = ", $j ** 3, "\n"; + print "1 + j + j**2 = ", 1 + $j + $j**2, "\n"; + + $z = -16 + 0*i; # Force it to be a complex + print "sqrt($z) = ", sqrt($z), "\n"; + + $k = exp(i * 2*pi/3); + print "$j - $k = ", $j - $k, "\n"; + + $z->Re(3); # Re, Im, arg, abs, + $j->arg(2); # (the last two aka rho, theta) + # can be used also as mutators. + +=head1 CONSTANTS + +=head2 PI + +The constant C<pi> and some handy multiples of it (pi2, pi4, +and pip2 (pi/2) and pip4 (pi/4)) are also available if separately +exported: + + use Math::Complex ':pi'; + $third_of_circle = pi2 / 3; + +=head2 Inf + +The floating point infinity can be exported as a subroutine Inf(): + + use Math::Complex qw(Inf sinh); + my $AlsoInf = Inf() + 42; + my $AnotherInf = sinh(1e42); + print "$AlsoInf is $AnotherInf\n" if $AlsoInf == $AnotherInf; + +Note that the stringified form of infinity varies between platforms: +it can be for example any of + + inf + infinity + INF + 1.#INF + +or it can be something else. + +Also note that in some platforms trying to use the infinity in +arithmetic operations may result in Perl crashing because using +an infinity causes SIGFPE or its moral equivalent to be sent. +The way to ignore this is + + local $SIG{FPE} = sub { }; + +=head1 ERRORS DUE TO DIVISION BY ZERO OR LOGARITHM OF ZERO + +The division (/) and the following functions + + log ln log10 logn + tan sec csc cot + atan asec acsc acot + tanh sech csch coth + atanh asech acsch acoth + +cannot be computed for all arguments because that would mean dividing +by zero or taking logarithm of zero. These situations cause fatal +runtime errors looking like this + + cot(0): Division by zero. + (Because in the definition of cot(0), the divisor sin(0) is 0) + Died at ... + +or + + atanh(-1): Logarithm of zero. + Died at... + +For the C<csc>, C<cot>, C<asec>, C<acsc>, C<acot>, C<csch>, C<coth>, +C<asech>, C<acsch>, the argument cannot be C<0> (zero). For the +logarithmic functions and the C<atanh>, C<acoth>, the argument cannot +be C<1> (one). For the C<atanh>, C<acoth>, the argument cannot be +C<-1> (minus one). For the C<atan>, C<acot>, the argument cannot be +C<i> (the imaginary unit). For the C<atan>, C<acoth>, the argument +cannot be C<-i> (the negative imaginary unit). For the C<tan>, +C<sec>, C<tanh>, the argument cannot be I<pi/2 + k * pi>, where I<k> +is any integer. atan2(0, 0) is undefined, and if the complex arguments +are used for atan2(), a division by zero will happen if z1**2+z2**2 == 0. + +Note that because we are operating on approximations of real numbers, +these errors can happen when merely `too close' to the singularities +listed above. + +=head1 ERRORS DUE TO INDIGESTIBLE ARGUMENTS + +The C<make> and C<emake> accept both real and complex arguments. +When they cannot recognize the arguments they will die with error +messages like the following + + Math::Complex::make: Cannot take real part of ... + Math::Complex::make: Cannot take real part of ... + Math::Complex::emake: Cannot take rho of ... + Math::Complex::emake: Cannot take theta of ... + +=head1 BUGS + +Saying C<use Math::Complex;> exports many mathematical routines in the +caller environment and even overrides some (C<sqrt>, C<log>, C<atan2>). +This is construed as a feature by the Authors, actually... ;-) + +All routines expect to be given real or complex numbers. Don't attempt to +use BigFloat, since Perl has currently no rule to disambiguate a '+' +operation (for instance) between two overloaded entities. + +In Cray UNICOS there is some strange numerical instability that results +in root(), cos(), sin(), cosh(), sinh(), losing accuracy fast. Beware. +The bug may be in UNICOS math libs, in UNICOS C compiler, in Math::Complex. +Whatever it is, it does not manifest itself anywhere else where Perl runs. + +=head1 SEE ALSO + +L<Math::Trig> + +=head1 AUTHORS + +Daniel S. Lewart <F<lewart!at!uiuc.edu>> +Jarkko Hietaniemi <F<jhi!at!iki.fi>> +Raphael Manfredi <F<Raphael_Manfredi!at!pobox.com>> + +=head1 LICENSE + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + +1; + +# eof diff --git a/Master/tlpkg/tlperl/lib/Math/Trig.pm b/Master/tlpkg/tlperl/lib/Math/Trig.pm new file mode 100644 index 00000000000..b7767bebccb --- /dev/null +++ b/Master/tlpkg/tlperl/lib/Math/Trig.pm @@ -0,0 +1,768 @@ +# +# Trigonometric functions, mostly inherited from Math::Complex. +# -- Jarkko Hietaniemi, since April 1997 +# -- Raphael Manfredi, September 1996 (indirectly: because of Math::Complex) +# + +require Exporter; +package Math::Trig; + +use 5.005; +use strict; + +use Math::Complex 1.56; +use Math::Complex qw(:trig :pi); + +use vars qw($VERSION $PACKAGE @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); + +@ISA = qw(Exporter); + +$VERSION = 1.20; + +my @angcnv = qw(rad2deg rad2grad + deg2rad deg2grad + grad2rad grad2deg); + +my @areal = qw(asin_real acos_real); + +@EXPORT = (@{$Math::Complex::EXPORT_TAGS{'trig'}}, + @angcnv, @areal); + +my @rdlcnv = qw(cartesian_to_cylindrical + cartesian_to_spherical + cylindrical_to_cartesian + cylindrical_to_spherical + spherical_to_cartesian + spherical_to_cylindrical); + +my @greatcircle = qw( + great_circle_distance + great_circle_direction + great_circle_bearing + great_circle_waypoint + great_circle_midpoint + great_circle_destination + ); + +my @pi = qw(pi pi2 pi4 pip2 pip4); + +@EXPORT_OK = (@rdlcnv, @greatcircle, @pi, 'Inf'); + +# See e.g. the following pages: +# http://www.movable-type.co.uk/scripts/LatLong.html +# http://williams.best.vwh.net/avform.htm + +%EXPORT_TAGS = ('radial' => [ @rdlcnv ], + 'great_circle' => [ @greatcircle ], + 'pi' => [ @pi ]); + +sub _DR () { pi2/360 } +sub _RD () { 360/pi2 } +sub _DG () { 400/360 } +sub _GD () { 360/400 } +sub _RG () { 400/pi2 } +sub _GR () { pi2/400 } + +# +# Truncating remainder. +# + +sub _remt ($$) { + # Oh yes, POSIX::fmod() would be faster. Possibly. If it is available. + $_[0] - $_[1] * int($_[0] / $_[1]); +} + +# +# Angle conversions. +# + +sub rad2rad($) { _remt($_[0], pi2) } + +sub deg2deg($) { _remt($_[0], 360) } + +sub grad2grad($) { _remt($_[0], 400) } + +sub rad2deg ($;$) { my $d = _RD * $_[0]; $_[1] ? $d : deg2deg($d) } + +sub deg2rad ($;$) { my $d = _DR * $_[0]; $_[1] ? $d : rad2rad($d) } + +sub grad2deg ($;$) { my $d = _GD * $_[0]; $_[1] ? $d : deg2deg($d) } + +sub deg2grad ($;$) { my $d = _DG * $_[0]; $_[1] ? $d : grad2grad($d) } + +sub rad2grad ($;$) { my $d = _RG * $_[0]; $_[1] ? $d : grad2grad($d) } + +sub grad2rad ($;$) { my $d = _GR * $_[0]; $_[1] ? $d : rad2rad($d) } + +# +# acos and asin functions which always return a real number +# + +sub acos_real { + return 0 if $_[0] >= 1; + return pi if $_[0] <= -1; + return acos($_[0]); +} + +sub asin_real { + return &pip2 if $_[0] >= 1; + return -&pip2 if $_[0] <= -1; + return asin($_[0]); +} + +sub cartesian_to_spherical { + my ( $x, $y, $z ) = @_; + + my $rho = sqrt( $x * $x + $y * $y + $z * $z ); + + return ( $rho, + atan2( $y, $x ), + $rho ? acos_real( $z / $rho ) : 0 ); +} + +sub spherical_to_cartesian { + my ( $rho, $theta, $phi ) = @_; + + return ( $rho * cos( $theta ) * sin( $phi ), + $rho * sin( $theta ) * sin( $phi ), + $rho * cos( $phi ) ); +} + +sub spherical_to_cylindrical { + my ( $x, $y, $z ) = spherical_to_cartesian( @_ ); + + return ( sqrt( $x * $x + $y * $y ), $_[1], $z ); +} + +sub cartesian_to_cylindrical { + my ( $x, $y, $z ) = @_; + + return ( sqrt( $x * $x + $y * $y ), atan2( $y, $x ), $z ); +} + +sub cylindrical_to_cartesian { + my ( $rho, $theta, $z ) = @_; + + return ( $rho * cos( $theta ), $rho * sin( $theta ), $z ); +} + +sub cylindrical_to_spherical { + return ( cartesian_to_spherical( cylindrical_to_cartesian( @_ ) ) ); +} + +sub great_circle_distance { + my ( $theta0, $phi0, $theta1, $phi1, $rho ) = @_; + + $rho = 1 unless defined $rho; # Default to the unit sphere. + + my $lat0 = pip2 - $phi0; + my $lat1 = pip2 - $phi1; + + return $rho * + acos_real( cos( $lat0 ) * cos( $lat1 ) * cos( $theta0 - $theta1 ) + + sin( $lat0 ) * sin( $lat1 ) ); +} + +sub great_circle_direction { + my ( $theta0, $phi0, $theta1, $phi1 ) = @_; + + my $distance = great_circle_distance($theta0, $phi0, $theta1, $phi1); + + my $lat0 = pip2 - $phi0; + my $lat1 = pip2 - $phi1; + + my $direction = + acos_real((sin($lat1) - sin($lat0) * cos($distance)) / + (cos($lat0) * sin($distance))); + + $direction = pi2 - $direction + if sin($theta1 - $theta0) < 0; + + return rad2rad($direction); +} + +*great_circle_bearing = \&great_circle_direction; + +sub great_circle_waypoint { + my ( $theta0, $phi0, $theta1, $phi1, $point ) = @_; + + $point = 0.5 unless defined $point; + + my $d = great_circle_distance( $theta0, $phi0, $theta1, $phi1 ); + + return undef if $d == pi; + + my $sd = sin($d); + + return ($theta0, $phi0) if $sd == 0; + + my $A = sin((1 - $point) * $d) / $sd; + my $B = sin( $point * $d) / $sd; + + my $lat0 = pip2 - $phi0; + my $lat1 = pip2 - $phi1; + + my $x = $A * cos($lat0) * cos($theta0) + $B * cos($lat1) * cos($theta1); + my $y = $A * cos($lat0) * sin($theta0) + $B * cos($lat1) * sin($theta1); + my $z = $A * sin($lat0) + $B * sin($lat1); + + my $theta = atan2($y, $x); + my $phi = acos_real($z); + + return ($theta, $phi); +} + +sub great_circle_midpoint { + great_circle_waypoint(@_[0..3], 0.5); +} + +sub great_circle_destination { + my ( $theta0, $phi0, $dir0, $dst ) = @_; + + my $lat0 = pip2 - $phi0; + + my $phi1 = asin_real(sin($lat0)*cos($dst) + + cos($lat0)*sin($dst)*cos($dir0)); + + my $theta1 = $theta0 + atan2(sin($dir0)*sin($dst)*cos($lat0), + cos($dst)-sin($lat0)*sin($phi1)); + + my $dir1 = great_circle_bearing($theta1, $phi1, $theta0, $phi0) + pi; + + $dir1 -= pi2 if $dir1 > pi2; + + return ($theta1, $phi1, $dir1); +} + +1; + +__END__ +=pod + +=head1 NAME + +Math::Trig - trigonometric functions + +=head1 SYNOPSIS + + use Math::Trig; + + $x = tan(0.9); + $y = acos(3.7); + $z = asin(2.4); + + $halfpi = pi/2; + + $rad = deg2rad(120); + + # Import constants pi2, pip2, pip4 (2*pi, pi/2, pi/4). + use Math::Trig ':pi'; + + # Import the conversions between cartesian/spherical/cylindrical. + use Math::Trig ':radial'; + + # Import the great circle formulas. + use Math::Trig ':great_circle'; + +=head1 DESCRIPTION + +C<Math::Trig> defines many trigonometric functions not defined by the +core Perl which defines only the C<sin()> and C<cos()>. The constant +B<pi> is also defined as are a few convenience functions for angle +conversions, and I<great circle formulas> for spherical movement. + +=head1 TRIGONOMETRIC FUNCTIONS + +The tangent + +=over 4 + +=item B<tan> + +=back + +The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot +are aliases) + +B<csc>, B<cosec>, B<sec>, B<sec>, B<cot>, B<cotan> + +The arcus (also known as the inverse) functions of the sine, cosine, +and tangent + +B<asin>, B<acos>, B<atan> + +The principal value of the arc tangent of y/x + +B<atan2>(y, x) + +The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc +and acotan/acot are aliases). Note that atan2(0, 0) is not well-defined. + +B<acsc>, B<acosec>, B<asec>, B<acot>, B<acotan> + +The hyperbolic sine, cosine, and tangent + +B<sinh>, B<cosh>, B<tanh> + +The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch +and cotanh/coth are aliases) + +B<csch>, B<cosech>, B<sech>, B<coth>, B<cotanh> + +The area (also known as the inverse) functions of the hyperbolic +sine, cosine, and tangent + +B<asinh>, B<acosh>, B<atanh> + +The area cofunctions of the hyperbolic sine, cosine, and tangent +(acsch/acosech and acoth/acotanh are aliases) + +B<acsch>, B<acosech>, B<asech>, B<acoth>, B<acotanh> + +The trigonometric constant B<pi> and some of handy multiples +of it are also defined. + +B<pi, pi2, pi4, pip2, pip4> + +=head2 ERRORS DUE TO DIVISION BY ZERO + +The following functions + + acoth + acsc + acsch + asec + asech + atanh + cot + coth + csc + csch + sec + sech + tan + tanh + +cannot be computed for all arguments because that would mean dividing +by zero or taking logarithm of zero. These situations cause fatal +runtime errors looking like this + + cot(0): Division by zero. + (Because in the definition of cot(0), the divisor sin(0) is 0) + Died at ... + +or + + atanh(-1): Logarithm of zero. + Died at... + +For the C<csc>, C<cot>, C<asec>, C<acsc>, C<acot>, C<csch>, C<coth>, +C<asech>, C<acsch>, the argument cannot be C<0> (zero). For the +C<atanh>, C<acoth>, the argument cannot be C<1> (one). For the +C<atanh>, C<acoth>, the argument cannot be C<-1> (minus one). For the +C<tan>, C<sec>, C<tanh>, C<sech>, the argument cannot be I<pi/2 + k * +pi>, where I<k> is any integer. + +Note that atan2(0, 0) is not well-defined. + +=head2 SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS + +Please note that some of the trigonometric functions can break out +from the B<real axis> into the B<complex plane>. For example +C<asin(2)> has no definition for plain real numbers but it has +definition for complex numbers. + +In Perl terms this means that supplying the usual Perl numbers (also +known as scalars, please see L<perldata>) as input for the +trigonometric functions might produce as output results that no more +are simple real numbers: instead they are complex numbers. + +The C<Math::Trig> handles this by using the C<Math::Complex> package +which knows how to handle complex numbers, please see L<Math::Complex> +for more information. In practice you need not to worry about getting +complex numbers as results because the C<Math::Complex> takes care of +details like for example how to display complex numbers. For example: + + print asin(2), "\n"; + +should produce something like this (take or leave few last decimals): + + 1.5707963267949-1.31695789692482i + +That is, a complex number with the real part of approximately C<1.571> +and the imaginary part of approximately C<-1.317>. + +=head1 PLANE ANGLE CONVERSIONS + +(Plane, 2-dimensional) angles may be converted with the following functions. + +=over + +=item deg2rad + + $radians = deg2rad($degrees); + +=item grad2rad + + $radians = grad2rad($gradians); + +=item rad2deg + + $degrees = rad2deg($radians); + +=item grad2deg + + $degrees = grad2deg($gradians); + +=item deg2grad + + $gradians = deg2grad($degrees); + +=item rad2grad + + $gradians = rad2grad($radians); + +=back + +The full circle is 2 I<pi> radians or I<360> degrees or I<400> gradians. +The result is by default wrapped to be inside the [0, {2pi,360,400}[ circle. +If you don't want this, supply a true second argument: + + $zillions_of_radians = deg2rad($zillions_of_degrees, 1); + $negative_degrees = rad2deg($negative_radians, 1); + +You can also do the wrapping explicitly by rad2rad(), deg2deg(), and +grad2grad(). + +=over 4 + +=item rad2rad + + $radians_wrapped_by_2pi = rad2rad($radians); + +=item deg2deg + + $degrees_wrapped_by_360 = deg2deg($degrees); + +=item grad2grad + + $gradians_wrapped_by_400 = grad2grad($gradians); + +=back + +=head1 RADIAL COORDINATE CONVERSIONS + +B<Radial coordinate systems> are the B<spherical> and the B<cylindrical> +systems, explained shortly in more detail. + +You can import radial coordinate conversion functions by using the +C<:radial> tag: + + use Math::Trig ':radial'; + + ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z); + ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z); + ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z); + ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z); + ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi); + ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi); + +B<All angles are in radians>. + +=head2 COORDINATE SYSTEMS + +B<Cartesian> coordinates are the usual rectangular I<(x, y, z)>-coordinates. + +Spherical coordinates, I<(rho, theta, pi)>, are three-dimensional +coordinates which define a point in three-dimensional space. They are +based on a sphere surface. The radius of the sphere is B<rho>, also +known as the I<radial> coordinate. The angle in the I<xy>-plane +(around the I<z>-axis) is B<theta>, also known as the I<azimuthal> +coordinate. The angle from the I<z>-axis is B<phi>, also known as the +I<polar> coordinate. The North Pole is therefore I<0, 0, rho>, and +the Gulf of Guinea (think of the missing big chunk of Africa) I<0, +pi/2, rho>. In geographical terms I<phi> is latitude (northward +positive, southward negative) and I<theta> is longitude (eastward +positive, westward negative). + +B<BEWARE>: some texts define I<theta> and I<phi> the other way round, +some texts define the I<phi> to start from the horizontal plane, some +texts use I<r> in place of I<rho>. + +Cylindrical coordinates, I<(rho, theta, z)>, are three-dimensional +coordinates which define a point in three-dimensional space. They are +based on a cylinder surface. The radius of the cylinder is B<rho>, +also known as the I<radial> coordinate. The angle in the I<xy>-plane +(around the I<z>-axis) is B<theta>, also known as the I<azimuthal> +coordinate. The third coordinate is the I<z>, pointing up from the +B<theta>-plane. + +=head2 3-D ANGLE CONVERSIONS + +Conversions to and from spherical and cylindrical coordinates are +available. Please notice that the conversions are not necessarily +reversible because of the equalities like I<pi> angles being equal to +I<-pi> angles. + +=over 4 + +=item cartesian_to_cylindrical + + ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z); + +=item cartesian_to_spherical + + ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z); + +=item cylindrical_to_cartesian + + ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z); + +=item cylindrical_to_spherical + + ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z); + +Notice that when C<$z> is not 0 C<$rho_s> is not equal to C<$rho_c>. + +=item spherical_to_cartesian + + ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi); + +=item spherical_to_cylindrical + + ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi); + +Notice that when C<$z> is not 0 C<$rho_c> is not equal to C<$rho_s>. + +=back + +=head1 GREAT CIRCLE DISTANCES AND DIRECTIONS + +A great circle is section of a circle that contains the circle +diameter: the shortest distance between two (non-antipodal) points on +the spherical surface goes along the great circle connecting those two +points. + +=head2 great_circle_distance + +You can compute spherical distances, called B<great circle distances>, +by importing the great_circle_distance() function: + + use Math::Trig 'great_circle_distance'; + + $distance = great_circle_distance($theta0, $phi0, $theta1, $phi1, [, $rho]); + +The I<great circle distance> is the shortest distance between two +points on a sphere. The distance is in C<$rho> units. The C<$rho> is +optional, it defaults to 1 (the unit sphere), therefore the distance +defaults to radians. + +If you think geographically the I<theta> are longitudes: zero at the +Greenwhich meridian, eastward positive, westward negative -- and the +I<phi> are latitudes: zero at the North Pole, northward positive, +southward negative. B<NOTE>: this formula thinks in mathematics, not +geographically: the I<phi> zero is at the North Pole, not at the +Equator on the west coast of Africa (Bay of Guinea). You need to +subtract your geographical coordinates from I<pi/2> (also known as 90 +degrees). + + $distance = great_circle_distance($lon0, pi/2 - $lat0, + $lon1, pi/2 - $lat1, $rho); + +=head2 great_circle_direction + +The direction you must follow the great circle (also known as I<bearing>) +can be computed by the great_circle_direction() function: + + use Math::Trig 'great_circle_direction'; + + $direction = great_circle_direction($theta0, $phi0, $theta1, $phi1); + +=head2 great_circle_bearing + +Alias 'great_circle_bearing' for 'great_circle_direction' is also available. + + use Math::Trig 'great_circle_bearing'; + + $direction = great_circle_bearing($theta0, $phi0, $theta1, $phi1); + +The result of great_circle_direction is in radians, zero indicating +straight north, pi or -pi straight south, pi/2 straight west, and +-pi/2 straight east. + +=head2 great_circle_destination + +You can inversely compute the destination if you know the +starting point, direction, and distance: + + use Math::Trig 'great_circle_destination'; + + # $diro is the original direction, + # for example from great_circle_bearing(). + # $distance is the angular distance in radians, + # for example from great_circle_distance(). + # $thetad and $phid are the destination coordinates, + # $dird is the final direction at the destination. + + ($thetad, $phid, $dird) = + great_circle_destination($theta, $phi, $diro, $distance); + +or the midpoint if you know the end points: + +=head2 great_circle_midpoint + + use Math::Trig 'great_circle_midpoint'; + + ($thetam, $phim) = + great_circle_midpoint($theta0, $phi0, $theta1, $phi1); + +The great_circle_midpoint() is just a special case of + +=head2 great_circle_waypoint + + use Math::Trig 'great_circle_waypoint'; + + ($thetai, $phii) = + great_circle_waypoint($theta0, $phi0, $theta1, $phi1, $way); + +Where the $way is a value from zero ($theta0, $phi0) to one ($theta1, +$phi1). Note that antipodal points (where their distance is I<pi> +radians) do not have waypoints between them (they would have an an +"equator" between them), and therefore C<undef> is returned for +antipodal points. If the points are the same and the distance +therefore zero and all waypoints therefore identical, the first point +(either point) is returned. + +The thetas, phis, direction, and distance in the above are all in radians. + +You can import all the great circle formulas by + + use Math::Trig ':great_circle'; + +Notice that the resulting directions might be somewhat surprising if +you are looking at a flat worldmap: in such map projections the great +circles quite often do not look like the shortest routes -- but for +example the shortest possible routes from Europe or North America to +Asia do often cross the polar regions. (The common Mercator projection +does B<not> show great circles as straight lines: straight lines in the +Mercator projection are lines of constant bearing.) + +=head1 EXAMPLES + +To calculate the distance between London (51.3N 0.5W) and Tokyo +(35.7N 139.8E) in kilometers: + + use Math::Trig qw(great_circle_distance deg2rad); + + # Notice the 90 - latitude: phi zero is at the North Pole. + sub NESW { deg2rad($_[0]), deg2rad(90 - $_[1]) } + my @L = NESW( -0.5, 51.3); + my @T = NESW(139.8, 35.7); + my $km = great_circle_distance(@L, @T, 6378); # About 9600 km. + +The direction you would have to go from London to Tokyo (in radians, +straight north being zero, straight east being pi/2). + + use Math::Trig qw(great_circle_direction); + + my $rad = great_circle_direction(@L, @T); # About 0.547 or 0.174 pi. + +The midpoint between London and Tokyo being + + use Math::Trig qw(great_circle_midpoint); + + my @M = great_circle_midpoint(@L, @T); + +or about 69 N 89 E, in the frozen wastes of Siberia. + +B<NOTE>: you B<cannot> get from A to B like this: + + Dist = great_circle_distance(A, B) + Dir = great_circle_direction(A, B) + C = great_circle_destination(A, Dist, Dir) + +and expect C to be B, because the bearing constantly changes when +going from A to B (except in some special case like the meridians or +the circles of latitudes) and in great_circle_destination() one gives +a B<constant> bearing to follow. + +=head2 CAVEAT FOR GREAT CIRCLE FORMULAS + +The answers may be off by few percentages because of the irregular +(slightly aspherical) form of the Earth. The errors are at worst +about 0.55%, but generally below 0.3%. + +=head2 Real-valued asin and acos + +For small inputs asin() and acos() may return complex numbers even +when real numbers would be enough and correct, this happens because of +floating-point inaccuracies. You can see these inaccuracies for +example by trying theses: + + print cos(1e-6)**2+sin(1e-6)**2 - 1,"\n"; + printf "%.20f", cos(1e-6)**2+sin(1e-6)**2,"\n"; + +which will print something like this + + -1.11022302462516e-16 + 0.99999999999999988898 + +even though the expected results are of course exactly zero and one. +The formulas used to compute asin() and acos() are quite sensitive to +this, and therefore they might accidentally slip into the complex +plane even when they should not. To counter this there are two +interfaces that are guaranteed to return a real-valued output. + +=over 4 + +=item asin_real + + use Math::Trig qw(asin_real); + + $real_angle = asin_real($input_sin); + +Return a real-valued arcus sine if the input is between [-1, 1], +B<inclusive> the endpoints. For inputs greater than one, pi/2 +is returned. For inputs less than minus one, -pi/2 is returned. + +=item acos_real + + use Math::Trig qw(acos_real); + + $real_angle = acos_real($input_cos); + +Return a real-valued arcus cosine if the input is between [-1, 1], +B<inclusive> the endpoints. For inputs greater than one, zero +is returned. For inputs less than minus one, pi is returned. + +=back + +=head1 BUGS + +Saying C<use Math::Trig;> exports many mathematical routines in the +caller environment and even overrides some (C<sin>, C<cos>). This is +construed as a feature by the Authors, actually... ;-) + +The code is not optimized for speed, especially because we use +C<Math::Complex> and thus go quite near complex numbers while doing +the computations even when the arguments are not. This, however, +cannot be completely avoided if we want things like C<asin(2)> to give +an answer instead of giving a fatal runtime error. + +Do not attempt navigation using these formulas. + +L<Math::Complex> + +=head1 AUTHORS + +Jarkko Hietaniemi <F<jhi!at!iki.fi>> and +Raphael Manfredi <F<Raphael_Manfredi!at!pobox.com>>. + +=head1 LICENSE + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + +# eof |