diff options
author | Florian Ragwitz <rafl@debian.org> | 2010-09-02 23:11:26 +0200 |
---|---|---|
committer | Florian Ragwitz <rafl@debian.org> | 2010-09-02 23:51:30 +0200 |
commit | c510e33d30368bc5440f1651f6b31f73d2354eba (patch) | |
tree | 9286faae98a201e6c1a3da345e868f082d142879 /cpan/Math-BigInt | |
parent | 69f857902b1b105d96448597da9c4bc9cd4e90a3 (diff) | |
download | perl-c510e33d30368bc5440f1651f6b31f73d2354eba.tar.gz |
blead is upstream for Math-BigInt
Diffstat (limited to 'cpan/Math-BigInt')
60 files changed, 0 insertions, 23497 deletions
diff --git a/cpan/Math-BigInt/lib/Math/BigFloat.pm b/cpan/Math-BigInt/lib/Math/BigFloat.pm deleted file mode 100644 index 27d60b3143..0000000000 --- a/cpan/Math-BigInt/lib/Math/BigFloat.pm +++ /dev/null @@ -1,4402 +0,0 @@ -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/cpan/Math-BigInt/lib/Math/BigInt.pm b/cpan/Math-BigInt/lib/Math/BigInt.pm deleted file mode 100644 index f97e438079..0000000000 --- a/cpan/Math-BigInt/lib/Math/BigInt.pm +++ /dev/null @@ -1,5115 +0,0 @@ -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/cpan/Math-BigInt/lib/Math/BigInt/Calc.pm b/cpan/Math-BigInt/lib/Math/BigInt/Calc.pm deleted file mode 100644 index 52e33d232a..0000000000 --- a/cpan/Math-BigInt/lib/Math/BigInt/Calc.pm +++ /dev/null @@ -1,2612 +0,0 @@ -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/cpan/Math-BigInt/lib/Math/BigInt/CalcEmu.pm b/cpan/Math-BigInt/lib/Math/BigInt/CalcEmu.pm deleted file mode 100644 index 5810f5db9f..0000000000 --- a/cpan/Math-BigInt/lib/Math/BigInt/CalcEmu.pm +++ /dev/null @@ -1,329 +0,0 @@ -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/cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm b/cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm deleted file mode 100644 index 94d3f2a624..0000000000 --- a/cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/perl -w - -# for testing subclassing Math::BigFloat - -package Math::BigFloat::Subclass; - -require 5.005_02; -use strict; - -use Exporter; -use Math::BigFloat(1.38); -use vars qw($VERSION @ISA $PACKAGE - $accuracy $precision $round_mode $div_scale); - -@ISA = qw(Exporter Math::BigFloat); - -$VERSION = 0.05; - -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]; - # Store the floating point value - my $self = Math::BigFloat->new($value,$a,$p,$round_mode); - bless $self, $class; - $self->{'_custom'} = 1; # make sure this never goes away - return $self; -} - -BEGIN - { - *objectify = \&Math::BigInt::objectify; - # to allow Math::BigFloat::Subclass::bgcd( ... ) style calls - *bgcd = \&Math::BigFloat::bgcd; - *blcm = \&Math::BigFloat::blcm; - } - -1; diff --git a/cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm b/cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm deleted file mode 100644 index 0bbe861cf8..0000000000 --- a/cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm +++ /dev/null @@ -1,44 +0,0 @@ -package Math::BigInt::BareCalc; - -use 5.005; -use strict; -# use warnings; # dont use warnings for older Perls - -require Exporter; -use vars qw/@ISA $VERSION/; -@ISA = qw(Exporter); - -$VERSION = '0.05'; - -sub api_version () { 1; } - -# Package to to test Bigint's simulation of Calc - -# uses Calc, but only features the strictly necc. methods. - -use Math::BigInt::Calc '0.51'; - -BEGIN - { - no strict 'refs'; - foreach (qw/ - base_len new zero one two ten copy str num add sub mul div mod inc dec - acmp alen len digit zeros - rsft lsft - fac pow gcd log_int sqrt root - is_zero is_one is_odd is_even is_one is_two is_ten check - as_hex as_bin as_oct from_hex from_bin from_oct - modpow modinv - and xor or - /) - { - my $name = "Math::BigInt::Calc::_$_"; - *{"Math::BigInt::BareCalc::_$_"} = \&$name; - } - print "# BareCalc using Calc v$Math::BigInt::Calc::VERSION\n"; - } - -# catch and throw away -sub import { } - -1; diff --git a/cpan/Math-BigInt/t/Math/BigInt/Scalar.pm b/cpan/Math-BigInt/t/Math/BigInt/Scalar.pm deleted file mode 100644 index c20a3e377e..0000000000 --- a/cpan/Math-BigInt/t/Math/BigInt/Scalar.pm +++ /dev/null @@ -1,355 +0,0 @@ -############################################################################### -# core math lib for BigInt, representing big numbers by normal int/float's -# for testing only, will fail any bignum test if range is exceeded - -package Math::BigInt::Scalar; - -use 5.005; -use strict; -# use warnings; # dont use warnings for older Perls - -require Exporter; - -use vars qw/@ISA $VERSION/; -@ISA = qw(Exporter); - -$VERSION = '0.13'; - -sub api_version() { 1; } - -############################################################################## -# global constants, flags and accessory - -# constants for easier life -my $nan = 'NaN'; - -############################################################################## -# create objects from various representations - -sub _new - { - # create scalar ref from string - my $d = $_[1]; - my $x = $d; # make copy - \$x; - } - -sub _from_hex - { - # not used - } - -sub _from_oct - { - # not used - } - -sub _from_bin - { - # not used - } - -sub _zero - { - my $x = 0; \$x; - } - -sub _one - { - my $x = 1; \$x; - } - -sub _two - { - my $x = 2; \$x; - } - -sub _ten - { - my $x = 10; \$x; - } - -sub _copy - { - my $x = $_[1]; - my $z = $$x; - \$z; - } - -# catch and throw away -sub import { } - -############################################################################## -# convert back to string and number - -sub _str - { - # make string - "${$_[1]}"; - } - -sub _num - { - # make a number - 0+${$_[1]}; - } - -sub _zeros - { - my $x = $_[1]; - - $x =~ /\d(0*)$/; - length($1 || ''); - } - -sub _rsft - { - # not used - } - -sub _lsft - { - # not used - } - -sub _mod - { - # not used - } - -sub _gcd - { - # not used - } - -sub _sqrt - { - # not used - } - -sub _root - { - # not used - } - -sub _fac - { - # not used - } - -sub _modinv - { - # not used - } - -sub _modpow - { - # not used - } - -sub _log_int - { - # not used - } - -sub _as_hex - { - sprintf("0x%x",${$_[1]}); - } - -sub _as_bin - { - sprintf("0b%b",${$_[1]}); - } - -sub _as_oct - { - sprintf("0%o",${$_[1]}); - } - -############################################################################## -# actual math code - -sub _add - { - my ($c,$x,$y) = @_; - $$x += $$y; - return $x; - } - -sub _sub - { - my ($c,$x,$y) = @_; - $$x -= $$y; - return $x; - } - -sub _mul - { - my ($c,$x,$y) = @_; - $$x *= $$y; - return $x; - } - -sub _div - { - my ($c,$x,$y) = @_; - - my $u = int($$x / $$y); my $r = $$x % $$y; $$x = $u; - return ($x,\$r) if wantarray; - return $x; - } - -sub _pow - { - my ($c,$x,$y) = @_; - my $u = $$x ** $$y; $$x = $u; - return $x; - } - -sub _and - { - my ($c,$x,$y) = @_; - my $u = int($$x) & int($$y); $$x = $u; - return $x; - } - -sub _xor - { - my ($c,$x,$y) = @_; - my $u = int($$x) ^ int($$y); $$x = $u; - return $x; - } - -sub _or - { - my ($c,$x,$y) = @_; - my $u = int($$x) | int($$y); $$x = $u; - return $x; - } - -sub _inc - { - my ($c,$x) = @_; - my $u = int($$x)+1; $$x = $u; - return $x; - } - -sub _dec - { - my ($c,$x) = @_; - my $u = int($$x)-1; $$x = $u; - return $x; - } - -############################################################################## -# testing - -sub _acmp - { - my ($c,$x, $y) = @_; - return ($$x <=> $$y); - } - -sub _len - { - return length("${$_[1]}"); - } - -sub _digit - { - # return the nth digit, negative values count backward - # 0 is the rightmost digit - my ($c,$x,$n) = @_; - - $n ++; # 0 => 1, 1 => 2 - return substr($$x,-$n,1); # 1 => -1, -2 => 2 etc - } - -############################################################################## -# _is_* routines - -sub _is_zero - { - # return true if arg is zero - my ($c,$x) = @_; - ($$x == 0) <=> 0; - } - -sub _is_even - { - # return true if arg is even - my ($c,$x) = @_; - (!($$x & 1)) <=> 0; - } - -sub _is_odd - { - # return true if arg is odd - my ($c,$x) = @_; - ($$x & 1) <=> 0; - } - -sub _is_one - { - # return true if arg is one - my ($c,$x) = @_; - ($$x == 1) <=> 0; - } - -sub _is_two - { - # return true if arg is one - my ($c,$x) = @_; - ($$x == 2) <=> 0; - } - -sub _is_ten - { - # return true if arg is one - my ($c,$x) = @_; - ($$x == 10) <=> 0; - } - -############################################################################### -# check routine to test internal state of corruptions - -sub _check - { - # no checks yet, pull it out from the test suite - my ($c,$x) = @_; - return "$x is not a reference" if !ref($x); - return 0; - } - -1; -__END__ - -=head1 NAME - -Math::BigInt::Scalar - Pure Perl module to test Math::BigInt with scalars - -=head1 SYNOPSIS - -Provides support for big integer calculations via means of 'small' int/floats. -Only for testing purposes, since it will fail at large values. But it is simple -enough not to introduce bugs on it's own and to serve as a testbed. - -=head1 DESCRIPTION - -Please see Math::BigInt::Calc. - -=head1 LICENSE - -This program is free software; you may redistribute it and/or modify it under -the same terms as Perl itself. - -=head1 AUTHOR - -Tels http://bloodgate.com in 2001 - 2007. - -=head1 SEE ALSO - -L<Math::BigInt>, L<Math::BigInt::Calc>. - -=cut diff --git a/cpan/Math-BigInt/t/Math/BigInt/Subclass.pm b/cpan/Math-BigInt/t/Math/BigInt/Subclass.pm deleted file mode 100644 index d45e9e53ad..0000000000 --- a/cpan/Math-BigInt/t/Math/BigInt/Subclass.pm +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/perl -w - -package Math::BigInt::Subclass; - -require 5.005_02; -use strict; - -use Exporter; -use Math::BigInt (1.64); -# $lib is for the "lib => " test -use vars qw($VERSION @ISA $PACKAGE @EXPORT_OK - $lib - $accuracy $precision $round_mode $div_scale); - -@ISA = qw(Exporter Math::BigInt); -@EXPORT_OK = qw(bgcd objectify); - -$VERSION = 0.04; - -use overload; # inherit overload from BigInt - -# Globals -$accuracy = $precision = undef; -$round_mode = 'even'; -$div_scale = 40; -$lib = ''; - -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; - $self->{'_custom'} = 1; # make sure this never goes away - return $self; -} - -sub bgcd - { - Math::BigInt::bgcd(@_); - } - -sub blcm - { - Math::BigInt::blcm(@_); - } - -sub as_int - { - Math::BigInt->new($_[0]); - } - -BEGIN - { - *objectify = \&Math::BigInt::objectify; - - # these are called by AUTOLOAD from BigFloat, so we need at least these. - # We cheat, of course.. - *bneg = \&Math::BigInt::bneg; - *babs = \&Math::BigInt::babs; - *bnan = \&Math::BigInt::bnan; - *binf = \&Math::BigInt::binf; - *bzero = \&Math::BigInt::bzero; - *bone = \&Math::BigInt::bone; - } - -sub import - { - my $self = shift; - - my @a; my $t = 0; - foreach (@_) - { - # remove the "lib => foo" parameters and store it - $lib = $_, $t = 0, next if $t == 1; - if ($_ eq 'lib') - { - $t = 1; next; - } - push @a,$_; - } - $self->SUPER::import(@a); # need it for subclasses - $self->export_to_level(1,$self,@a); # need this ? - } - -1; diff --git a/cpan/Math-BigInt/t/_e_math.t b/cpan/Math-BigInt/t/_e_math.t deleted file mode 100644 index b3eb644437..0000000000 --- a/cpan/Math-BigInt/t/_e_math.t +++ /dev/null @@ -1,111 +0,0 @@ -#!/usr/bin/perl -w - -# test the helper math routines in Math::BigFloat - -use Test::More; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/_e_math.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../lib); - } - unshift @INC, '../lib'; - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 26; - } - -use Math::BigFloat lib => 'Calc'; - -############################################################################# -# add - -my $a = Math::BigInt::Calc->_new("123"); -my $b = Math::BigInt::Calc->_new("321"); - -my ($x, $xs) = Math::BigFloat::_e_add($a,$b,'+','+'); -is (_str($x,$xs), '+444', 'add two positive numbers'); -is (_str($a,''), '444', 'a modified'); - -($x,$xs) = _add (123,321,'+','+'); -is (_str($x,$xs), '+444', 'add two positive numbers'); - -($x,$xs) = _add (123,321,'+','-'); -is (_str($x,$xs), '-198', 'add +x + -y'); -($x,$xs) = _add (123,321,'-','+'); -is (_str($x,$xs), '+198', 'add -x + +y'); - -($x,$xs) = _add (321,123,'-','+'); -is (_str($x,$xs), '-198', 'add -x + +y'); -($x,$xs) = _add (321,123,'+','-'); -is (_str($x,$xs), '+198', 'add +x + -y'); - -($x,$xs) = _add (10,1,'+','-'); -is (_str($x,$xs), '+9', 'add 10 + -1'); -($x,$xs) = _add (10,1,'-','+'); -is (_str($x,$xs), '-9', 'add -10 + +1'); -($x,$xs) = _add (1,10,'-','+'); -is (_str($x,$xs), '+9', 'add -1 + 10'); -($x,$xs) = _add (1,10,'+','-'); -is (_str($x,$xs), '-9', 'add 1 + -10'); - -############################################################################# -# sub - -$a = Math::BigInt::Calc->_new("123"); -$b = Math::BigInt::Calc->_new("321"); -($x, $xs) = Math::BigFloat::_e_sub($b,$a,'+','+'); -is (_str($x,$xs), '+198', 'sub two positive numbers'); -is (_str($b,''), '198', 'a modified'); - -($x,$xs) = _sub (123,321,'+','-'); -is (_str($x,$xs), '+444', 'sub +x + -y'); -($x,$xs) = _sub (123,321,'-','+'); -is (_str($x,$xs), '-444', 'sub -x + +y'); - -sub _add - { - my ($a,$b,$as,$bs) = @_; - - my $aa = Math::BigInt::Calc->_new($a); - my $bb = Math::BigInt::Calc->_new($b); - my ($x, $xs) = Math::BigFloat::_e_add($aa,$bb,$as,$bs); - is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa), - 'param0 modified'); - ($x,$xs); - } - -sub _sub - { - my ($a,$b,$as,$bs) = @_; - - my $aa = Math::BigInt::Calc->_new($a); - my $bb = Math::BigInt::Calc->_new($b); - my ($x, $xs) = Math::BigFloat::_e_sub($aa,$bb,$as,$bs); - is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa), - 'param0 modified'); - ($x,$xs); - } - -sub _str - { - my ($x,$s) = @_; - - $s . Math::BigInt::Calc->_str($x); - } diff --git a/cpan/Math-BigInt/t/alias.inc b/cpan/Math-BigInt/t/alias.inc deleted file mode 100644 index 746a20c99e..0000000000 --- a/cpan/Math-BigInt/t/alias.inc +++ /dev/null @@ -1,12 +0,0 @@ - -# alias subroutine testing, included by sub_ali.t and mbi_ali.t - -my $x = $CL->new(123); - -is ($x->is_pos(), 1, '123 is positive'); -is ($x->is_neg(), 0, '123 is not negative'); -is ($x->as_int(), 123, '123 is 123 as int'); -is (ref($x->as_int()), 'Math::BigInt', "as_int(123) is of class Math::BigInt"); -$x->bneg(); -is ($x->is_pos(), 0, '-123 is not positive'); -is ($x->is_neg(), 1, '-123 is negative'); diff --git a/cpan/Math-BigInt/t/bare_mbf.t b/cpan/Math-BigInt/t/bare_mbf.t deleted file mode 100644 index 9bb4bcea5f..0000000000 --- a/cpan/Math-BigInt/t/bare_mbf.t +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/perl -w - -use Test; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/bare_mbf.t//i; - print "# $0\n"; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, '../lib'; - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 2308; - } - -use Math::BigFloat lib => 'BareCalc'; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigFloat"; -$CL = "Math::BigInt::BareCalc"; - -require 'bigfltpm.inc'; # all tests here for sharing diff --git a/cpan/Math-BigInt/t/bare_mbi.t b/cpan/Math-BigInt/t/bare_mbi.t deleted file mode 100644 index 637e695b69..0000000000 --- a/cpan/Math-BigInt/t/bare_mbi.t +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/perl -w - -use Test; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/bare_mbi.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 3273; - } - -use Math::BigInt lib => 'BareCalc'; - -print "# ",Math::BigInt->config()->{lib},"\n"; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigInt"; -$CL = "Math::BigInt::BareCalc"; - -my $version = '1.84'; # for $VERSION tests, match current release (by hand!) - -require 'bigintpm.inc'; # perform same tests as bigintpm - diff --git a/cpan/Math-BigInt/t/bare_mif.t b/cpan/Math-BigInt/t/bare_mif.t deleted file mode 100644 index 0cc1de9365..0000000000 --- a/cpan/Math-BigInt/t/bare_mif.t +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/perl -w - -# test rounding, accuracy, precicion and fallback, round_mode and mixing -# of classes under BareCalc - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/bare_mif.t//i; - if ($ENV{PERL_CORE}) - { - @INC = qw(../t/lib); # testing with the core distribution - } - unshift @INC, '../lib'; # for testing manually - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 684 - + 1; # our own tests - } - -print "# ",Math::BigInt->config()->{lib},"\n"; - -use Math::BigInt lib => 'BareCalc'; -use Math::BigFloat lib => 'BareCalc'; - -use vars qw/$mbi $mbf/; - -$mbi = 'Math::BigInt'; -$mbf = 'Math::BigFloat'; - -ok (Math::BigInt->config()->{lib},'Math::BigInt::BareCalc'); - -require 'mbimbf.inc'; - diff --git a/cpan/Math-BigInt/t/big_pi_e.t b/cpan/Math-BigInt/t/big_pi_e.t deleted file mode 100644 index 9ecae00dcc..0000000000 --- a/cpan/Math-BigInt/t/big_pi_e.t +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/perl -w - -# Test bpi() and bexp() - -use Test::More; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/big_pi_e.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../lib); - } - unshift @INC, '../lib'; - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 8; - } - -use Math::BigFloat; - -############################################################################# - -my $pi = Math::BigFloat::bpi(); - -ok (!exists $pi->{_a}, 'A not set'); -ok (!exists $pi->{_p}, 'P not set'); - -$pi = Math::BigFloat->bpi(); - -ok (!exists $pi->{_a}, 'A not set'); -ok (!exists $pi->{_p}, 'P not set'); - -$pi = Math::BigFloat->bpi(10); - -is ($pi->{_a}, 10, 'A set'); -is ($pi->{_p}, undef, 'P not set'); - -############################################################################# -my $e = Math::BigFloat->new(1)->bexp(); - -ok (!exists $e->{_a}, 'A not set'); -ok (!exists $e->{_p}, 'P not set'); - - diff --git a/cpan/Math-BigInt/t/bigfltpm.inc b/cpan/Math-BigInt/t/bigfltpm.inc deleted file mode 100644 index 7d650e5cba..0000000000 --- a/cpan/Math-BigInt/t/bigfltpm.inc +++ /dev/null @@ -1,1823 +0,0 @@ -#include this file into another test for subclass testing... - -ok ($class->config()->{lib},$CL); - -use strict; - -my $z; - -while (<DATA>) - { - $_ =~ s/[\n\r]//g; # remove newlines - $_ =~ s/#.*$//; # remove comments - $_ =~ s/\s+$//; # trailing spaces - next if /^$/; # skip empty lines & comments - if (s/^&//) - { - $f = $_; - } - elsif (/^\$/) - { - $setup = $_; $setup =~ s/\$/\$${class}::/g; # round_mode, div_scale - #print "\$setup== $setup\n"; - } - else - { - if (m|^(.*?):(/.+)$|) - { - $ans = $2; - @args = split(/:/,$1,99); - } - else - { - @args = split(/:/,$_,99); $ans = pop(@args); - } - $try = "\$x = $class->new(\"$args[0]\");"; - if ($f eq "fnorm") - { - $try .= "\$x;"; - } elsif ($f eq "finf") { - $try .= "\$x->finf('$args[1]');"; - } elsif ($f eq "is_inf") { - $try .= "\$x->is_inf('$args[1]');"; - } elsif ($f eq "fone") { - $try .= "\$x->bone('$args[1]');"; - } elsif ($f eq "fstr") { - $try .= "\$x->accuracy($args[1]); \$x->precision($args[2]);"; - $try .= '$x->fstr();'; - } elsif ($f eq "parts") { - # ->bstr() to see if an object is returned - $try .= '($a,$b) = $x->parts(); $a = $a->bstr(); $b = $b->bstr();'; - $try .= '"$a $b";'; - } elsif ($f eq "exponent") { - # ->bstr() to see if an object is returned - $try .= '$x->exponent()->bstr();'; - } elsif ($f eq "mantissa") { - # ->bstr() to see if an object is returned - $try .= '$x->mantissa()->bstr();'; - } elsif ($f =~ /^(numify|length|as_number|as_hex|as_bin)$/) { - $try .= "\$x->$f();"; - # some unary ops (test the fxxx form, since that is done by AUTOLOAD) - } elsif ($f =~ /^f(nan|sstr|neg|floor|ceil|abs)$/) { - $try .= "\$x->f$1();"; - # some is_xxx test function - } elsif ($f =~ /^is_(zero|one|negative|positive|odd|even|nan|int)$/) { - $try .= "\$x->$f();"; - } elsif ($f eq "bpi") { - $try .= '$class->bpi($x);'; - } elsif ($f eq "finc") { - $try .= '++$x;'; - } elsif ($f eq "fdec") { - $try .= '--$x;'; - }elsif ($f eq "fround") { - $try .= "$setup; \$x->fround($args[1]);"; - } elsif ($f eq "ffround") { - $try .= "$setup; \$x->ffround($args[1]);"; - } elsif ($f eq "fsqrt") { - $try .= "$setup; \$x->fsqrt();"; - } elsif ($f eq "ffac") { - $try .= "$setup; \$x->ffac();"; - } elsif ($f eq "flog") { - if (defined $args[1] && $args[1] ne '') - { - $try .= "\$y = $class->new($args[1]);"; - $try .= "$setup; \$x->flog(\$y);"; - } - else - { - $try .= "$setup; \$x->flog();"; - } - } - else - { - $try .= "\$y = $class->new(\"$args[1]\");"; - - if ($f eq "bgcd") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new(\"$args[2]\"); "; - } - $try .= "$class\::bgcd(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - } - elsif ($f eq "blcm") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new(\"$args[2]\"); "; - } - $try .= "$class\::blcm(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - } elsif ($f eq "fcmp") { - $try .= '$x->fcmp($y);'; - } elsif ($f eq "facmp") { - $try .= '$x->facmp($y);'; - } elsif ($f eq "fpow") { - $try .= '$x ** $y;'; - } elsif ($f eq "bnok") { - $try .= '$x->bnok($y);'; - } elsif ($f eq "bcos") { - $try .= '$x->bcos($y);'; - } elsif ($f eq "bsin") { - $try .= '$x->bsin($y);'; - } elsif ($f eq "batan") { - $try .= '$x->batan($y);'; - } elsif ($f eq "froot") { - $try .= "$setup; \$x->froot(\$y);"; - } elsif ($f eq "fadd") { - $try .= '$x + $y;'; - } elsif ($f eq "fsub") { - $try .= '$x - $y;'; - } elsif ($f eq "fmul") { - $try .= '$x * $y;'; - } elsif ($f eq "fdiv") { - $try .= "$setup; \$x / \$y;"; - } elsif ($f eq "fdiv-list") { - $try .= "$setup; join(',',\$x->fdiv(\$y));"; - } elsif ($f eq "frsft") { - $try .= '$x >> $y;'; - } elsif ($f eq "flsft") { - $try .= '$x << $y;'; - } elsif ($f eq "fmod") { - $try .= '$x % $y;'; - } else { - # Functions with three arguments - $try .= "\$z = $class->new(\"$args[2]\");"; - - if( $f eq "bmodpow") { - $try .= '$x->bmodpow($y,$z);'; - } elsif ($f eq "bmuladd"){ - $try .= '$x->bmuladd($y,$z);'; - } elsif ($f eq "batan2"){ - $try .= '$x->batan2($y,$z);'; - } else { warn "Unknown op '$f'"; } - } - } - # print "# Trying: '$try'\n"; - $ans1 = eval $try; - print "# Error: $@\n" if $@; - if ($ans =~ m|^/(.*)$|) - { - my $pat = $1; - if ($ans1 =~ /$pat/) - { - ok (1,1); - } - else - { - print "# '$try' expected: /$pat/ got: '$ans1'\n" if !ok(1,0); - } - } - else - { - if ($ans eq "") - { - ok_undef ($ans1); - } - else - { - print "# Tried: '$try'\n" if !ok ($ans1, $ans); - if (ref($ans1) eq "$class") - { - # float numbers are normalized (for now), so mantissa shouldn't have - # trailing zeros - #print $ans1->_trailing_zeros(),"\n"; - print "# Has trailing zeros after '$try'\n" - if !ok ($CL->_zeros( $ans1->{_m}), 0); - } - } - } # end pattern or string - } - } # end while - -# check whether $class->new( Math::BigInt->new()) destroys it -# ($y == 12 in this case) -$x = Math::BigInt->new(1200); $y = $class->new($x); -ok ($y,1200); ok ($x,1200); - -############################################################################### -# Really huge, big, ultra-mega-biggy-monster exponents -# Technically, the exponents should not be limited (they are BigInts), but -# practically there are a few places were they are limited to a Perl scalar. -# This is sometimes for speed, sometimes because otherwise the number wouldn't -# fit into your memory (just think of 1e123456789012345678901234567890 + 1!) -# anyway. We don't test everything here, but let's make sure it just basically -# works. - -my $monster = '1e1234567890123456789012345678901234567890'; - -# new and exponent -ok ($class->new($monster)->bsstr(), - '1e+1234567890123456789012345678901234567890'); -ok ($class->new($monster)->exponent(), - '1234567890123456789012345678901234567890'); -# cmp -ok ($class->new($monster) > 0,1); - -# sub/mul -ok ($class->new($monster)->bsub( $monster),0); -ok ($class->new($monster)->bmul(2)->bsstr(), - '2e+1234567890123456789012345678901234567890'); - -# mantissa -$monster = '1234567890123456789012345678901234567890e2'; -ok ($class->new($monster)->mantissa(), - '123456789012345678901234567890123456789'); - -############################################################################### -# zero,inf,one,nan - -$x = $class->new(2); $x->fzero(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); -$x = $class->new(2); $x->finf(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); -$x = $class->new(2); $x->fone(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); -$x = $class->new(2); $x->fnan(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); - -############################################################################### -# bone/binf etc as plain calls (Lite failed them) - -ok ($class->fzero(),0); -ok ($class->fone(),1); -ok ($class->fone('+'),1); -ok ($class->fone('-'),-1); -ok ($class->fnan(),'NaN'); -ok ($class->finf(),'inf'); -ok ($class->finf('+'),'inf'); -ok ($class->finf('-'),'-inf'); -ok ($class->finf('-inf'),'-inf'); - -$class->accuracy(undef); $class->precision(undef); # reset - -############################################################################### -# bug in bsstr()/numify() showed up in after-rounding in bdiv() - -$x = $class->new('0.008'); $y = $class->new(2); -$x->bdiv(3,$y); -ok ($x,'0.0027'); - -############################################################################### -# fsqrt() with set global A/P or A/P enabled on $x, also a test whether fsqrt() -# correctly modifies $x - - -$x = $class->new(12); $class->precision(-2); $x->fsqrt(); ok ($x,'3.46'); - -$class->precision(undef); -$x = $class->new(12); $class->precision(0); $x->fsqrt(); ok ($x,'3'); - -$class->precision(-3); $x = $class->new(12); $x->fsqrt(); ok ($x,'3.464'); - -{ - no strict 'refs'; - # A and P set => NaN - ${${class}.'::accuracy'} = 4; $x = $class->new(12); - $x->fsqrt(3); ok ($x,'NaN'); - # supplied arg overrides set global - $class->precision(undef); $x = $class->new(12); $x->fsqrt(3); ok ($x,'3.46'); - $class->accuracy(undef); $class->precision(undef); # reset for further tests -} - -############################################################################# -# can we call objectify (broken until v1.52) - -{ - no strict; - $try = - '@args' . " = $class" . "::objectify(2,$class,4,5);".'join(" ",@args);'; - $ans = eval $try; - ok ($ans,"$class 4 5"); -} - -############################################################################# -# is_one('-') (broken until v1.64) - -ok ($class->new(-1)->is_one(),0); -ok ($class->new(-1)->is_one('-'),1); - -############################################################################# -# bug 1/0.5 leaving 2e-0 instead of 2e0 - -ok ($class->new(1)->fdiv('0.5')->bsstr(),'2e+0'); - -############################################################################### -# [perl #30609] bug with $x -= $x not being 0, but 2*$x - -$x = $class->new(3); $x -= $x; ok ($x, 0); -$x = $class->new(-3); $x -= $x; ok ($x, 0); -$x = $class->new(3); $x += $x; ok ($x, 6); -$x = $class->new(-3); $x += $x; ok ($x, -6); - -$x = $class->new('NaN'); $x -= $x; ok ($x->is_nan(), 1); -$x = $class->new('inf'); $x -= $x; ok ($x->is_nan(), 1); -$x = $class->new('-inf'); $x -= $x; ok ($x->is_nan(), 1); - -$x = $class->new('NaN'); $x += $x; ok ($x->is_nan(), 1); -$x = $class->new('inf'); $x += $x; ok ($x->is_inf(), 1); -$x = $class->new('-inf'); $x += $x; ok ($x->is_inf('-'), 1); - -$x = $class->new('3.14'); $x -= $x; ok ($x, 0); -$x = $class->new('-3.14'); $x -= $x; ok ($x, 0); -$x = $class->new('3.14'); $x += $x; ok ($x, '6.28'); -$x = $class->new('-3.14'); $x += $x; ok ($x, '-6.28'); - -$x = $class->new('3.14'); $x *= $x; ok ($x, '9.8596'); -$x = $class->new('-3.14'); $x *= $x; ok ($x, '9.8596'); -$x = $class->new('3.14'); $x /= $x; ok ($x, '1'); -$x = $class->new('-3.14'); $x /= $x; ok ($x, '1'); -$x = $class->new('3.14'); $x %= $x; ok ($x, '0'); -$x = $class->new('-3.14'); $x %= $x; ok ($x, '0'); - -############################################################################### -# the following two were reported by "kenny" via hotmail.com: - -#perl -MMath::BigFloat -wle 'print Math::BigFloat->new(0)->bpow(".1")' -#Use of uninitialized value in numeric le (<=) at BigFloat.pm line 1851. - -$x = $class->new(0); $y = $class->new('0.1'); -ok ($x ** $y, 0, 'no warnings and zero result'); - -#perl -MMath::BigFloat -lwe 'print Math::BigFloat->new(".222222222222222222222222222222222222222222")->bceil()' -#Use of uninitialized value in numeric le (<=) at BigFloat.pm line 1851. - -$x = $class->new(".222222222222222222222222222222222222222222"); -ok ($x->bceil(), 1, 'no warnings and one as result'); - -############################################################################### -# test **=, <<=, >>= - -# ((2^148)-1)/17 -$x = $class->new(2); $x **= 148; $x++; $x->bdiv(17, 60)->bfloor(); $x->accuracy(undef); -ok ($x,"20988936657440586486151264256610222593863921"); -ok ($x->length(),length "20988936657440586486151264256610222593863921"); - -$x = $class->new('2'); -my $y = $class->new('18'); -ok ($x <<= $y, 2 << 18); -ok ($x, 2 << 18); -ok ($x >>= $y, 2); -ok ($x, 2); - -$x = $class->new('2'); -$y = $class->new('18.2'); -$x <<= $y; # 2 * (2 ** 18.2); - -ok ($x->copy()->bfround(-9), '602248.763144685'); -ok ($x >>= $y, 2); # 2 * (2 ** 18.2) / (2 ** 18.2) => 2 -ok ($x, 2); - -1; # all done - -############################################################################### -# Perl 5.005 does not like ok ($x,undef) - -sub ok_undef - { - my $x = shift; - - ok (1,1) and return if !defined $x; - ok ($x,'undef'); - } - -__DATA__ -&bgcd -inf:12:NaN --inf:12:NaN -12:inf:NaN -12:-inf:NaN -inf:inf:NaN -inf:-inf:NaN --inf:-inf:NaN -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:0 -+0:+1:1 -+1:+0:1 -+1:+1:1 -+2:+3:1 -+3:+2:1 --3:+2:1 --3:-2:1 --144:-60:12 -144:-60:12 -144:60:12 -100:625:25 -4096:81:1 -1034:804:2 -27:90:56:1 -27:90:54:9 -&blcm -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:NaN -+1:+0:0 -+0:+1:0 -+27:+90:270 -+1034:+804:415668 -$div_scale = 40; -&bcos -1.2:10:0.3623577545 -2.4:12:-0.737393715541 -0:10:1 -0:20:1 -1:10:0.5403023059 -1:12:0.540302305868 -&bsin -1:10:0.8414709848 -0:10:0 -0:20:0 -2.1:12:0.863209366649 -1.2:13:0.9320390859672 -0.2:13:0.1986693307951 -3.2:12:-0.0583741434276 -&batan -NaN:10:NaN -inf:14:1.5707963267949 --inf:14:-1.5707963267949 -0.2:13:0.1973955598499 -0.2:14:0.19739555984988 -0:10:0 -1:14:0.78539816339744 --1:14:-0.78539816339744 -# test an argument X > 1 -2:14:1.1071487177941 -&batan2 -NaN:1:10:NaN -NaN:NaN:10:NaN -1:NaN:10:NaN -inf:1:14:1.5707963267949 --inf:1:14:-1.5707963267949 -0:-inf:14:3.1415926535898 --1:-inf:14:-3.1415926535898 -1:-inf:14:3.1415926535898 -0:inf:14:0 -inf:-inf:14:2.3561944901923 --inf:-inf:14:-2.3561944901923 -inf:+inf:14:0.7853981633974 --inf:+inf:14:-0.7853981633974 -1:5:13:0.1973955598499 -1:5:14:0.19739555984988 -0:0:10:0 -0:1:14:0 -0:2:14:0 -1:0:14:1.5707963267949 -5:0:14:1.5707963267949 --1:0:11:-1.5707963268 --2:0:77:-1.5707963267948966192313216916397514420985846996875529104874722961539082031431 -2:0:77:1.5707963267948966192313216916397514420985846996875529104874722961539082031431 --1:5:14:-0.19739555984988 -1:5:14:0.19739555984988 --1:8:14:-0.12435499454676 -1:8:14:0.12435499454676 --1:1:14:-0.78539816339744 -# test an argument X > 1 and one X < 1 -1:2:24:0.463647609000806116214256 -2:1:14:1.1071487177941 --2:1:14:-1.1071487177941 -&bpi -150:3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940813 -77:3.1415926535897932384626433832795028841971693993751058209749445923078164062862 -+0:3.141592653589793238462643383279502884197 -11:3.1415926536 -&bnok -+inf:10:inf -NaN:NaN:NaN -NaN:1:NaN -1:NaN:NaN -1:1:1 -# k > n -1:2:0 -2:3:0 -# k < 0 -1:-2:0 -# 7 over 3 = 35 -7:3:35 -7:6:1 -100:90:17310309456440 -&flog -0::NaN --1::NaN --2::NaN -# base > 0, base != 1 -2:-1:NaN -2:0:NaN -2:1:NaN -# log(1) is always 1, regardless of $base -1::0 -1:1:0 -1:2:0 -2::0.6931471805599453094172321214581765680755 -2.718281828::0.9999999998311266953289851340574956564911 -$div_scale = 20; -2.718281828::0.99999999983112669533 -$div_scale = 15; -123::4.81218435537242 -10::2.30258509299405 -1000::6.90775527898214 -100::4.60517018598809 -2::0.693147180559945 -3.1415::1.14470039286086 -12345::9.42100640177928 -0.001::-6.90775527898214 -# bug until v1.71: -10:10:1 -100:100:1 -# reset for further tests -$div_scale = 40; -1::0 -&frsft -NaNfrsft:2:NaN -0:2:0 -1:1:0.5 -2:1:1 -4:1:2 -123:1:61.5 -32:3:4 -&flsft -NaNflsft:0:NaN -2:1:4 -4:3:32 -5:3:40 -1:2:4 -0:5:0 -&fnorm -1:1 --0:0 -fnormNaN:NaN -+inf:inf --inf:-inf -123:123 --123.4567:-123.4567 -# invalid inputs -1__2:NaN -1E1__2:NaN -11__2E2:NaN -.2E-3.:NaN -1e3e4:NaN -# strange, but valid -.2E2:20 -1.E3:1000 -# some inputs that result in zero -0e0:0 -+0e0:0 -+0e+0:0 --0e+0:0 -0e-0:0 --0e-0:0 -+0e-0:0 -000:0 -00e2:0 -00e02:0 -000e002:0 -000e1230:0 -00e-3:0 -00e+3:0 -00e-03:0 -00e+03:0 --000:0 --00e2:0 --00e02:0 --000e002:0 --000e1230:0 --00e-3:0 --00e+3:0 --00e-03:0 --00e+03:0 -&as_number -0:0 -1:1 -1.2:1 -2.345:2 --2:-2 --123.456:-123 --200:-200 -# test for bug in brsft() not handling cases that return 0 -0.000641:0 -0.0006412:0 -0.00064123:0 -0.000641234:0 -0.0006412345:0 -0.00064123456:0 -0.000641234567:0 -0.0006412345678:0 -0.00064123456789:0 -0.1:0 -0.01:0 -0.001:0 -0.0001:0 -0.00001:0 -0.000001:0 -0.0000001:0 -0.00000001:0 -0.000000001:0 -0.0000000001:0 -0.00000000001:0 -0.12345:0 -0.123456:0 -0.1234567:0 -0.12345678:0 -0.123456789:0 -&finf -1:+:inf -2:-:-inf -3:abc:inf -&as_hex -+inf:inf --inf:-inf -hexNaN:NaN -0:0x0 -5:0x5 --5:-0x5 -&as_bin -+inf:inf --inf:-inf -hexNaN:NaN -0:0b0 -5:0b101 --5:-0b101 -&numify -# uses bsstr() so 5 => 5e+0 to be compatible w/ Perls output -0:0e+1 -+1:1e+0 -1234:1234e+0 -NaN:NaN -+inf:inf --inf:-inf --5:-5e+0 -100:1e+2 --100:-1e+2 -&fnan -abc:NaN -2:NaN --2:NaN -0:NaN -&fone -2:+:1 --2:-:-1 --2:+:1 -2:-:-1 -0::1 --2::1 -abc::1 -2:abc:1 -&fsstr -+inf:inf --inf:-inf -abcfsstr:NaN --abcfsstr:NaN -1234.567:1234567e-3 -123:123e+0 --5:-5e+0 --100:-1e+2 -&fstr -+inf:::inf --inf:::-inf -abcfstr:::NaN -1234.567:9::1234.56700 -1234.567::-6:1234.567000 -12345:5::12345 -0.001234:6::0.00123400 -0.001234::-8:0.00123400 -0:4::0 -0::-4:0.0000 -&fnorm -inf:inf -+inf:inf --inf:-inf -+infinity:NaN -+-inf:NaN -abc:NaN - 1 a:NaN -1bcd2:NaN -11111b:NaN -+1z:NaN --1z:NaN -0e999:0 -0e-999:0 --0e999:0 --0e-999:0 -0:0 -+0:0 -+00:0 -+0_0_0:0 -000000_0000000_00000:0 --0:0 --0000:0 -+1:1 -+01:1 -+001:1 -+00000100000:100000 -123456789:123456789 --1:-1 --01:-1 --001:-1 --123456789:-123456789 --00000100000:-100000 -123.456a:NaN -123.456:123.456 -0.01:0.01 -.002:0.002 -+.2:0.2 --0.0003:-0.0003 --.0000000004:-0.0000000004 -123456E2:12345600 -123456E-2:1234.56 --123456E2:-12345600 --123456E-2:-1234.56 -1e1:10 -2e-11:0.00000000002 -# excercise _split - .02e-1:0.002 - 000001:1 - -00001:-1 - -1:-1 - 000.01:0.01 - -000.0023:-0.0023 - 1.1e1:11 --3e111:-3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 --4e-1111:-0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004 -&fpow -NaN:1:NaN -1:NaN:NaN -NaN:-1:NaN --1:NaN:NaN -NaN:-21:NaN --21:NaN:NaN -NaN:21:NaN -21:NaN:NaN -0:0:1 -0:1:0 -0:9:0 -0:-2:inf -2:2:4 -1:2:1 -1:3:1 --1:2:1 --1:3:-1 -123.456:2:15241.383936 -2:-2:0.25 -2:-3:0.125 -128:-2:0.00006103515625 -abc:123.456:NaN -123.456:abc:NaN -+inf:123.45:inf --inf:123.45:-inf -+inf:-123.45:inf --inf:-123.45:-inf --2:2:4 --2:3:-8 --2:4:16 --2:5:-32 --3:2:9 --3:3:-27 --3:4:81 --3:5:-243 -# 2 ** 0.5 == sqrt(2) -# 1.41..7 and not 1.4170 since fallback (bsqrt(9) is '3', not 3.0...0) -2:0.5:1.41421356237309504880168872420969807857 -#2:0.2:1.148698354997035006798626946777927589444 -#6:1.5:14.6969384566990685891837044482353483518 -$div_scale = 20; -#62.5:12.5:26447206647554886213592.3959144 -$div_scale = 40; -&fneg -fnegNaN:NaN -+inf:-inf --inf:inf -+0:0 -+1:-1 --1:1 -+123456789:-123456789 --123456789:123456789 -+123.456789:-123.456789 --123456.789:123456.789 -&fabs -fabsNaN:NaN -+inf:inf --inf:inf -+0:0 -+1:1 --1:1 -+123456789:123456789 --123456789:123456789 -+123.456789:123.456789 --123456.789:123456.789 -&fround -$round_mode = "trunc" -+inf:5:inf --inf:5:-inf -0:5:0 -NaNfround:5:NaN -+10123456789:5:10123000000 --10123456789:5:-10123000000 -+10123456789.123:5:10123000000 --10123456789.123:5:-10123000000 -+10123456789:9:10123456700 --10123456789:9:-10123456700 -+101234500:6:101234000 --101234500:6:-101234000 -$round_mode = "zero" -+20123456789:5:20123000000 --20123456789:5:-20123000000 -+20123456789.123:5:20123000000 --20123456789.123:5:-20123000000 -+20123456789:9:20123456800 --20123456789:9:-20123456800 -+201234500:6:201234000 --201234500:6:-201234000 -$round_mode = "+inf" -+30123456789:5:30123000000 --30123456789:5:-30123000000 -+30123456789.123:5:30123000000 --30123456789.123:5:-30123000000 -+30123456789:9:30123456800 --30123456789:9:-30123456800 -+301234500:6:301235000 --301234500:6:-301234000 -$round_mode = "-inf" -+40123456789:5:40123000000 --40123456789:5:-40123000000 -+40123456789.123:5:40123000000 --40123456789.123:5:-40123000000 -+40123456789:9:40123456800 --40123456789:9:-40123456800 -+401234500:6:401234000 --401234500:6:-401235000 -$round_mode = "odd" -+50123456789:5:50123000000 --50123456789:5:-50123000000 -+50123456789.123:5:50123000000 --50123456789.123:5:-50123000000 -+50123456789:9:50123456800 --50123456789:9:-50123456800 -+501234500:6:501235000 --501234500:6:-501235000 -$round_mode = "even" -+60123456789:5:60123000000 --60123456789:5:-60123000000 -+60123456789:9:60123456800 --60123456789:9:-60123456800 -+601234500:6:601234000 --601234500:6:-601234000 -+60123456789.0123:5:60123000000 --60123456789.0123:5:-60123000000 -$round_mode = "common" -+60123456789:5:60123000000 --60123456789:5:-60123000000 -+60123456789:6:60123500000 --60123456789:6:-60123500000 -+60123456789:9:60123456800 --60123456789:9:-60123456800 -+601234500:6:601235000 --601234500:6:-601235000 -+601234400:6:601234000 --601234400:6:-601234000 -+601234600:6:601235000 --601234600:6:-601235000 -+601234300:6:601234000 -+60123456789.0123:5:60123000000 --60123456789.0123:5:-60123000000 -&ffround -$round_mode = "trunc" -+inf:5:inf --inf:5:-inf -0:5:0 -NaNffround:5:NaN -+1.23:-1:1.2 -+1.234:-1:1.2 -+1.2345:-1:1.2 -+1.23:-2:1.23 -+1.234:-2:1.23 -+1.2345:-2:1.23 -+1.23:-3:1.230 -+1.234:-3:1.234 -+1.2345:-3:1.234 --1.23:-1:-1.2 -+1.27:-1:1.2 --1.27:-1:-1.2 -+1.25:-1:1.2 --1.25:-1:-1.2 -+1.35:-1:1.3 --1.35:-1:-1.3 --0.0061234567890:-1:0.0 --0.0061:-1:0.0 --0.00612:-1:0.0 --0.00612:-2:0.00 --0.006:-1:0.0 --0.006:-2:0.00 --0.0006:-2:0.00 --0.0006:-3:0.000 --0.0065:-3:/-0\.006|-6e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:0 -0.51:0:0 -0.41:0:0 -$round_mode = "zero" -+2.23:-1:/2.2(?:0{5}\d+)? --2.23:-1:/-2.2(?:0{5}\d+)? -+2.27:-1:/2.(?:3|29{5}\d+) --2.27:-1:/-2.(?:3|29{5}\d+) -+2.25:-1:/2.2(?:0{5}\d+)? --2.25:-1:/-2.2(?:0{5}\d+)? -+2.35:-1:/2.(?:3|29{5}\d+) --2.35:-1:/-2.(?:3|29{5}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.006|-6e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:0 -0.51:0:1 -0.41:0:0 -$round_mode = "+inf" -+3.23:-1:/3.2(?:0{5}\d+)? --3.23:-1:/-3.2(?:0{5}\d+)? -+3.27:-1:/3.(?:3|29{5}\d+) --3.27:-1:/-3.(?:3|29{5}\d+) -+3.25:-1:/3.(?:3|29{5}\d+) --3.25:-1:/-3.2(?:0{5}\d+)? -+3.35:-1:/3.(?:4|39{5}\d+) --3.35:-1:/-3.(?:3|29{5}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.006|-6e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:1 -0.51:0:1 -0.41:0:0 -$round_mode = "-inf" -+4.23:-1:/4.2(?:0{5}\d+)? --4.23:-1:/-4.2(?:0{5}\d+)? -+4.27:-1:/4.(?:3|29{5}\d+) --4.27:-1:/-4.(?:3|29{5}\d+) -+4.25:-1:/4.2(?:0{5}\d+)? --4.25:-1:/-4.(?:3|29{5}\d+) -+4.35:-1:/4.(?:3|29{5}\d+) --4.35:-1:/-4.(?:4|39{5}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.007|-7e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:0 -0.51:0:1 -0.41:0:0 -$round_mode = "odd" -+5.23:-1:/5.2(?:0{5}\d+)? --5.23:-1:/-5.2(?:0{5}\d+)? -+5.27:-1:/5.(?:3|29{5}\d+) --5.27:-1:/-5.(?:3|29{5}\d+) -+5.25:-1:/5.(?:3|29{5}\d+) --5.25:-1:/-5.(?:3|29{5}\d+) -+5.35:-1:/5.(?:3|29{5}\d+) --5.35:-1:/-5.(?:3|29{5}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.007|-7e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:1 -0.51:0:1 -0.41:0:0 -$round_mode = "even" -+6.23:-1:/6.2(?:0{5}\d+)? --6.23:-1:/-6.2(?:0{5}\d+)? -+6.27:-1:/6.(?:3|29{5}\d+) --6.27:-1:/-6.(?:3|29{5}\d+) -+6.25:-1:/6.(?:2(?:0{5}\d+)?|29{5}\d+) --6.25:-1:/-6.(?:2(?:0{5}\d+)?|29{5}\d+) -+6.35:-1:/6.(?:4|39{5}\d+|29{8}\d+) --6.35:-1:/-6.(?:4|39{5}\d+|29{8}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.006|-7e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:0 -0.51:0:1 -0.41:0:0 -0.01234567:-3:0.012 -0.01234567:-4:0.0123 -0.01234567:-5:0.01235 -0.01234567:-6:0.012346 -0.01234567:-7:0.0123457 -0.01234567:-8:0.01234567 -0.01234567:-9:0.012345670 -0.01234567:-12:0.012345670000 -&fcmp -fcmpNaN:fcmpNaN: -fcmpNaN:+0: -+0:fcmpNaN: -+0:+0:0 --1:+0:-1 -+0:-1:1 -+1:+0:1 -+0:+1:-1 --1:+1:-1 -+1:-1:1 --1:-1:0 -+1:+1:0 --1.1:0:-1 -+0:-1.1:1 -+1.1:+0:1 -+0:+1.1:-1 -+123:+123:0 -+123:+12:1 -+12:+123:-1 --123:-123:0 --123:-12:-1 --12:-123:1 -+123:+124:-1 -+124:+123:1 --123:-124:1 --124:-123:-1 -0:0.01:-1 -0:0.0001:-1 -0:-0.0001:1 -0:-0.1:1 -0.1:0:1 -0.00001:0:1 --0.0001:0:-1 --0.1:0:-1 -0:0.0001234:-1 -0:-0.0001234:1 -0.0001234:0:1 --0.0001234:0:-1 -0.0001:0.0005:-1 -0.0005:0.0001:1 -0.005:0.0001:1 -0.001:0.0005:1 -0.000001:0.0005:-1 -0.00000123:0.0005:-1 -0.00512:0.0001:1 -0.005:0.000112:1 -0.00123:0.0005:1 -1.5:2:-1 -2:1.5:1 -1.54321:234:-1 -234:1.54321:1 -# infinity --inf:5432112345:-1 -+inf:5432112345:1 --inf:-5432112345:-1 -+inf:-5432112345:1 --inf:54321.12345:-1 -+inf:54321.12345:1 --inf:-54321.12345:-1 -+inf:-54321.12345:1 -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:1 --inf:+inf:-1 -# return undef -+inf:NaN: -NaN:inf: --inf:NaN: -NaN:-inf: -&facmp -fcmpNaN:fcmpNaN: -fcmpNaN:+0: -+0:fcmpNaN: -+0:+0:0 --1:+0:1 -+0:-1:-1 -+1:+0:1 -+0:+1:-1 --1:+1:0 -+1:-1:0 --1:-1:0 -+1:+1:0 --1.1:0:1 -+0:-1.1:-1 -+1.1:+0:1 -+0:+1.1:-1 -+123:+123:0 -+123:+12:1 -+12:+123:-1 --123:-123:0 --123:-12:1 --12:-123:-1 -+123:+124:-1 -+124:+123:1 --123:-124:-1 --124:-123:1 -0:0.01:-1 -0:0.0001:-1 -0:-0.0001:-1 -0:-0.1:-1 -0.1:0:1 -0.00001:0:1 --0.0001:0:1 --0.1:0:1 -0:0.0001234:-1 -0:-0.0001234:-1 -0.0001234:0:1 --0.0001234:0:1 -0.0001:0.0005:-1 -0.0005:0.0001:1 -0.005:0.0001:1 -0.001:0.0005:1 -0.000001:0.0005:-1 -0.00000123:0.0005:-1 -0.00512:0.0001:1 -0.005:0.000112:1 -0.00123:0.0005:1 -1.5:2:-1 -2:1.5:1 -1.54321:234:-1 -234:1.54321:1 -# infinity --inf:5432112345:1 -+inf:5432112345:1 --inf:-5432112345:1 -+inf:-5432112345:1 --inf:54321.12345:1 -+inf:54321.12345:1 --inf:-54321.12345:1 -+inf:-54321.12345:1 -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:0 --inf:+inf:0 -5:inf:-1 --1:inf:-1 -5:-inf:-1 --1:-inf:-1 -# return undef -+inf:facmpNaN: -facmpNaN:inf: --inf:facmpNaN: -facmpNaN:-inf: -&fdec -fdecNaN:NaN -+inf:inf --inf:-inf -+0:-1 -+1:0 --1:-2 -1.23:0.23 --1.23:-2.23 -100:99 -101:100 --100:-101 --99:-100 --98:-99 -99:98 -&finc -fincNaN:NaN -+inf:inf --inf:-inf -+0:1 -+1:2 --1:0 -1.23:2.23 --1.23:-0.23 -100:101 --100:-99 --99:-98 --101:-100 -99:100 -&fadd -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:-inf:NaN --inf:+inf:NaN -+inf:+inf:inf --inf:-inf:-inf -baddNaN:+inf:NaN -baddNaN:+inf:NaN -+inf:baddNaN:NaN --inf:baddNaN:NaN -+0:+0:0 -+1:+0:1 -+0:+1:1 -+1:+1:2 --1:+0:-1 -+0:-1:-1 --1:-1:-2 --1:+1:0 -+1:-1:0 -+9:+1:10 -+99:+1:100 -+999:+1:1000 -+9999:+1:10000 -+99999:+1:100000 -+999999:+1:1000000 -+9999999:+1:10000000 -+99999999:+1:100000000 -+999999999:+1:1000000000 -+9999999999:+1:10000000000 -+99999999999:+1:100000000000 -+10:-1:9 -+100:-1:99 -+1000:-1:999 -+10000:-1:9999 -+100000:-1:99999 -+1000000:-1:999999 -+10000000:-1:9999999 -+100000000:-1:99999999 -+1000000000:-1:999999999 -+10000000000:-1:9999999999 -+123456789:+987654321:1111111110 --123456789:+987654321:864197532 --123456789:-987654321:-1111111110 -+123456789:-987654321:-864197532 -0.001234:0.0001234:0.0013574 -&fsub -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:-inf:inf --inf:+inf:-inf -+inf:+inf:NaN --inf:-inf:NaN -baddNaN:+inf:NaN -baddNaN:+inf:NaN -+inf:baddNaN:NaN --inf:baddNaN:NaN -+0:+0:0 -+1:+0:1 -+0:+1:-1 -+1:+1:0 --1:+0:-1 -+0:-1:1 --1:-1:0 --1:+1:-2 -+1:-1:2 -+9:+1:8 -+99:+1:98 -+999:+1:998 -+9999:+1:9998 -+99999:+1:99998 -+999999:+1:999998 -+9999999:+1:9999998 -+99999999:+1:99999998 -+999999999:+1:999999998 -+9999999999:+1:9999999998 -+99999999999:+1:99999999998 -+10:-1:11 -+100:-1:101 -+1000:-1:1001 -+10000:-1:10001 -+100000:-1:100001 -+1000000:-1:1000001 -+10000000:-1:10000001 -+100000000:-1:100000001 -+1000000000:-1:1000000001 -+10000000000:-1:10000000001 -+123456789:+987654321:-864197532 --123456789:+987654321:-1111111110 --123456789:-987654321:864197532 -+123456789:-987654321:1111111110 -&bmuladd -abc:abc:0:NaN -abc:+0:0:NaN -+0:abc:0:NaN -+0:0:abc:NaN -NaNmul:+inf:0:NaN -NaNmul:-inf:0:NaN --inf:NaNmul:0:NaN -+inf:NaNmul:0:NaN -+inf:+inf:0:inf -+inf:-inf:0:-inf --inf:+inf:0:-inf --inf:-inf:0:inf -+0:+0:0:0 -+0:+1:0:0 -+1:+0:0:0 -+0:-1:0:0 --1:+0:0:0 -123456789123456789:0:0:0 -0:123456789123456789:0:0 --1:-1:0:1 --1:-1:0:1 --1:+1:0:-1 -+1:-1:0:-1 -+1:+1:0:1 -+2:+3:0:6 --2:+3:0:-6 -+2:-3:0:-6 --2:-3:0:6 -111:111:0:12321 -10101:10101:0:102030201 -1001001:1001001:0:1002003002001 -100010001:100010001:0:10002000300020001 -10000100001:10000100001:0:100002000030000200001 -11111111111:9:0:99999999999 -22222222222:9:0:199999999998 -33333333333:9:0:299999999997 -44444444444:9:0:399999999996 -55555555555:9:0:499999999995 -66666666666:9:0:599999999994 -77777777777:9:0:699999999993 -88888888888:9:0:799999999992 -99999999999:9:0:899999999991 -11111111111:9:1:100000000000 -22222222222:9:1:199999999999 -33333333333:9:1:299999999998 -44444444444:9:1:399999999997 -55555555555:9:1:499999999996 -66666666666:9:1:599999999995 -77777777777:9:1:699999999994 -88888888888:9:1:799999999993 -99999999999:9:1:899999999992 --3:-4:-5:7 -3:-4:-5:-17 --3:4:-5:-17 -3:4:-5:7 --3:4:5:-7 -3:-4:5:-7 -9999999999999999999:10000000000000000000:1234567890:99999999999999999990000000001234567890 -3.2:5.7:8.9:27.14 --3.2:5.197:6.05:-10.5804 -&bmodpow -3:4:8:1 -3:4:7:4 -3:4:7:4 -77777:777:123456789:99995084 -3.2:6.2:5.2:2.970579856718063040273642739529400818 -&fmul -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:NaNmul:NaN -+inf:NaNmul:NaN -NaNmul:+inf:NaN -NaNmul:-inf:NaN -+inf:+inf:inf -+inf:-inf:-inf -+inf:-inf:-inf -+inf:+inf:inf -+inf:123.34:inf -+inf:-123.34:-inf --inf:123.34:-inf --inf:-123.34:inf -123.34:+inf:inf --123.34:+inf:-inf -123.34:-inf:-inf --123.34:-inf:inf -+0:+0:0 -+0:+1:0 -+1:+0:0 -+0:-1:0 --1:+0:0 -+123456789123456789:+0:0 -+0:+123456789123456789:0 --1:-1:1 --1:+1:-1 -+1:-1:-1 -+1:+1:1 -+2:+3:6 --2:+3:-6 -+2:-3:-6 --2:-3:6 -+111:+111:12321 -+10101:+10101:102030201 -+1001001:+1001001:1002003002001 -+100010001:+100010001:10002000300020001 -+10000100001:+10000100001:100002000030000200001 -+11111111111:+9:99999999999 -+22222222222:+9:199999999998 -+33333333333:+9:299999999997 -+44444444444:+9:399999999996 -+55555555555:+9:499999999995 -+66666666666:+9:599999999994 -+77777777777:+9:699999999993 -+88888888888:+9:799999999992 -+99999999999:+9:899999999991 -6:120:720 -10:10000:100000 -&fdiv-list -0:0:NaN,NaN -0:1:0,0 -9:4:2.25,1 -9:5:1.8,4 -# bug in v1.74 with bdiv in list context, when $y is 1 or -1 -2.1:-1:-2.1,0 -2.1:1:2.1,0 --2.1:-1:2.1,0 --2.1:1:-2.1,0 -&fdiv -$div_scale = 40; $round_mode = 'even' -abc:abc:NaN -abc:+1:abc:NaN -+1:abc:NaN --1:abc:NaN -0:abc:NaN -+0:+0:NaN -+0:+1:0 -+1:+0:inf -+3214:+0:inf -+0:-1:0 --1:+0:-inf --3214:+0:-inf -+1:+1:1 --1:-1:1 -+1:-1:-1 --1:+1:-1 -+1:+2:0.5 -+2:+1:2 -123:+inf:0 -123:-inf:0 -+10:+5:2 -+100:+4:25 -+1000:+8:125 -+10000:+16:625 -+10000:-16:-625 -+999999999999:+9:111111111111 -+999999999999:+99:10101010101 -+999999999999:+999:1001001001 -+999999999999:+9999:100010001 -+999999999999999:+99999:10000100001 -+1000000000:+9:111111111.1111111111111111111111111111111 -+2000000000:+9:222222222.2222222222222222222222222222222 -+3000000000:+9:333333333.3333333333333333333333333333333 -+4000000000:+9:444444444.4444444444444444444444444444444 -+5000000000:+9:555555555.5555555555555555555555555555556 -+6000000000:+9:666666666.6666666666666666666666666666667 -+7000000000:+9:777777777.7777777777777777777777777777778 -+8000000000:+9:888888888.8888888888888888888888888888889 -+9000000000:+9:1000000000 -+35500000:+113:314159.2920353982300884955752212389380531 -+71000000:+226:314159.2920353982300884955752212389380531 -+106500000:+339:314159.2920353982300884955752212389380531 -+1000000000:+3:333333333.3333333333333333333333333333333 -2:25.024996000799840031993601279744051189762:0.07992009269196593320152084692285869265447 -123456:1:123456 -$div_scale = 20 -+1000000000:+9:111111111.11111111111 -+2000000000:+9:222222222.22222222222 -+3000000000:+9:333333333.33333333333 -+4000000000:+9:444444444.44444444444 -+5000000000:+9:555555555.55555555556 -+6000000000:+9:666666666.66666666667 -+7000000000:+9:777777777.77777777778 -+8000000000:+9:888888888.88888888889 -+9000000000:+9:1000000000 -1:10:0.1 -1:100:0.01 -1:1000:0.001 -1:10000:0.0001 -1:504:0.001984126984126984127 -2:1.987654321:1.0062111801179738436 -123456789.123456789123456789123456789:1:123456789.12345678912 -# the next two cases are the "old" behaviour, but are now (>v0.01) different -#+35500000:+113:314159.292035398230088 -#+71000000:+226:314159.292035398230088 -+35500000:+113:314159.29203539823009 -+71000000:+226:314159.29203539823009 -+106500000:+339:314159.29203539823009 -+1000000000:+3:333333333.33333333333 -$div_scale = 1 -# round to accuracy 1 after bdiv -+124:+3:40 -123456789.1234:1:100000000 -# reset scale for further tests -$div_scale = 40 -&fmod -+9:4:1 -+9:5:4 -+9000:56:40 -+56:9000:56 -# inf handling, see table in doc -0:inf:0 -0:-inf:0 -5:inf:5 -5:-inf:5 --5:inf:-5 --5:-inf:-5 -inf:5:0 --inf:5:0 -inf:-5:0 --inf:-5:0 -5:5:0 --5:-5:0 -inf:inf:NaN --inf:-inf:NaN --inf:inf:NaN -inf:-inf:NaN -8:0:8 -inf:0:inf -# exceptions to reminder rule --inf:0:-inf --8:0:-8 -0:0:NaN -abc:abc:NaN -abc:1:abc:NaN -1:abc:NaN -0:0:NaN -0:1:0 -1:0:1 -0:-1:0 --1:0:-1 -1:1:0 --1:-1:0 -1:-1:0 --1:1:0 -1:2:1 -2:1:0 -1000000000:9:1 -2000000000:9:2 -3000000000:9:3 -4000000000:9:4 -5000000000:9:5 -6000000000:9:6 -7000000000:9:7 -8000000000:9:8 -9000000000:9:0 -35500000:113:33 -71000000:226:66 -106500000:339:99 -1000000000:3:1 -10:5:0 -100:4:0 -1000:8:0 -10000:16:0 -999999999999:9:0 -999999999999:99:0 -999999999999:999:0 -999999999999:9999:0 -999999999999999:99999:0 --9:+5:1 -+9:-5:-1 --9:-5:-4 --5:3:1 --2:3:1 -4:3:1 -1:3:1 --5:-3:-2 --2:-3:-2 -4:-3:-2 -1:-3:-2 -4095:4095:0 -100041000510123:3:0 -152403346:12345:4321 -87654321:87654321:0 -# now some floating point tests -123:2.5:0.5 -1230:2.5:0 -123.4:2.5:0.9 -123e1:25:5 --2.1:1:0.9 -2.1:1:0.1 --2.1:-1:-0.1 -2.1:-1:-0.9 --3:1:0 -3:1:0 --3:-1:0 -3:-1:0 -&ffac -Nanfac:NaN --1:NaN -+inf:inf --inf:NaN -0:1 -1:1 -2:2 -3:6 -4:24 -5:120 -6:720 -10:3628800 -11:39916800 -12:479001600 -&froot -# sqrt() -+0:2:0 -+1:2:1 --1:2:NaN -# -$x ** (1/2) => -$y, but not in froot() --123.456:2:NaN -+inf:2:inf --inf:2:NaN -2:2:1.41421356237309504880168872420969807857 --2:2:NaN -4:2:2 -9:2:3 -16:2:4 -100:2:10 -123.456:2:11.11107555549866648462149404118219234119 -15241.38393:2:123.4559999756998444766131352122991626468 -1.44:2:1.2 -12:2:3.464101615137754587054892683011744733886 -0.49:2:0.7 -0.0049:2:0.07 -# invalid ones -1:NaN:NaN --1:NaN:NaN -0:NaN:NaN --inf:NaN:NaN -+inf:NaN:NaN -NaN:0:NaN -NaN:2:NaN -NaN:inf:NaN -NaN:inf:NaN -12:-inf:NaN -12:inf:NaN -+0:0:NaN -+1:0:NaN --1:0:NaN --2:0:NaN --123.45:0:NaN -+inf:0:NaN -12:1:12 --12:1:NaN -8:-1:NaN --8:-1:NaN -# cubic root -8:3:2 --8:3:NaN -# fourths root -16:4:2 -81:4:3 -# see t/bigroot() for more tests -&fsqrt -+0:0 --1:NaN --2:NaN --16:NaN --123.45:NaN -nanfsqrt:NaN -+inf:inf --inf:NaN -1:1 -2:1.41421356237309504880168872420969807857 -4:2 -9:3 -16:4 -100:10 -123.456:11.11107555549866648462149404118219234119 -15241.38393:123.4559999756998444766131352122991626468 -1.44:1.2 -# sqrt(1.44) = 1.2, sqrt(e10) = e5 => 12e4 -1.44E10:120000 -2e10:141421.356237309504880168872420969807857 -144e20:120000000000 -# proved to be an endless loop under 7-9 -12:3.464101615137754587054892683011744733886 -0.49:0.7 -0.0049:0.07 -&is_nan -123:0 -abc:1 -NaN:1 --123:0 -&is_inf -+inf::1 --inf::1 -abc::0 -1::0 -NaN::0 --1::0 -+inf:-:0 -+inf:+:1 --inf:-:1 --inf:+:0 -# it must be exactly /^[+-]inf$/ -+infinity::0 --infinity::0 -&is_odd -abc:0 -0:0 --1:1 --3:1 -1:1 -3:1 -1000001:1 -1000002:0 -+inf:0 --inf:0 -123.45:0 --123.45:0 -2:0 -&is_int -NaNis_int:0 -0:1 -1:1 -2:1 --2:1 --1:1 --inf:0 -+inf:0 -123.4567:0 --0.1:0 --0.002:0 -&is_even -abc:0 -0:1 --1:0 --3:0 -1:0 -3:0 -1000001:0 -1000002:1 -2:1 -+inf:0 --inf:0 -123.456:0 --123.456:0 -0.01:0 --0.01:0 -120:1 -1200:1 --1200:1 -&is_positive -0:0 -1:1 --1:0 --123:0 -NaN:0 --inf:0 -+inf:1 -&is_negative -0:0 -1:0 --1:1 --123:1 -NaN:0 --inf:1 -+inf:0 -&parts -0:0 1 -1:1 0 -123:123 0 --123:-123 0 --1200:-12 2 -NaNparts:NaN NaN -+inf:inf inf --inf:-inf inf -&exponent -0:1 -1:0 -123:0 --123:0 --1200:2 -+inf:inf --inf:inf -NaNexponent:NaN -&mantissa -0:0 -1:1 -123:123 --123:-123 --1200:-12 -+inf:inf --inf:-inf -NaNmantissa:NaN -&length -123:3 --123:3 -0:1 -1:1 -12345678901234567890:20 -&is_zero -NaNzero:0 -+inf:0 --inf:0 -0:1 --1:0 -1:0 -&is_one -NaNone:0 -+inf:0 --inf:0 -0:0 -2:0 -1:1 --1:0 --2:0 -&ffloor -0:0 -abc:NaN -+inf:inf --inf:-inf -1:1 --51:-51 --51.2:-52 -12.2:12 -0.12345:0 -0.123456:0 -0.1234567:0 -0.12345678:0 -0.123456789:0 -&fceil -0:0 -abc:NaN -+inf:inf --inf:-inf -1:1 --51:-51 --51.2:-51 -12.2:13 diff --git a/cpan/Math-BigInt/t/bigfltpm.t b/cpan/Math-BigInt/t/bigfltpm.t deleted file mode 100644 index a41996e8ed..0000000000 --- a/cpan/Math-BigInt/t/bigfltpm.t +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/perl -w - -use Test; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/bigfltpm.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../lib); - } - unshift @INC, '../lib'; - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 2308 - + 5; # own tests - } - -use Math::BigInt lib => 'Calc'; -use Math::BigFloat; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigFloat"; -$CL = "Math::BigInt::Calc"; - -ok ($class->config()->{class},$class); -ok ($class->config()->{with}, $CL); - -# bug #17447: Can't call method Math::BigFloat->bsub, not a valid method -my $c = Math::BigFloat->new( '123.3' ); -ok ($c->fsub(123) eq '0.3', 1); # calling fsub on a BigFloat works - -# Bug until BigInt v1.86, the scale wasn't treated as a scalar: -$c = Math::BigFloat->new('0.008'); my $d = Math::BigFloat->new(3); -my $e = $c->bdiv(Math::BigFloat->new(3),$d); - -ok ($e,'0.00267'); # '0.008 / 3 => 0.0027'); -ok (ref($e->{_e}->[0]), ''); # 'Not a BigInt'); - -require 'bigfltpm.inc'; # all tests here for sharing diff --git a/cpan/Math-BigInt/t/bigintc.t b/cpan/Math-BigInt/t/bigintc.t deleted file mode 100644 index 5dbace06a3..0000000000 --- a/cpan/Math-BigInt/t/bigintc.t +++ /dev/null @@ -1,464 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test; - -BEGIN - { - $| = 1; - chdir 't' if -d 't'; - unshift @INC, '../lib'; # for running manually - if ($^O eq 'unicos') # the tests hang under "unicos" - { - print "1..0\n"; - exit(0); - } - plan tests => 375; - } - -use Math::BigInt::Calc; - -my ($BASE_LEN, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN_SMALL, $MAX_VAL) = - Math::BigInt::Calc->_base_len(); - -print "# BASE_LEN = $BASE_LEN\n"; -print "# MAX_VAL = $MAX_VAL\n"; -print "# AND_BITS = $AND_BITS\n"; -print "# XOR_BITS = $XOR_BITS\n"; -print "# IOR_BITS = $OR_BITS\n"; - -# testing of Math::BigInt::Calc - -my $C = 'Math::BigInt::Calc'; # pass classname to sub's - -# _new and _str -my $x = $C->_new("123"); my $y = $C->_new("321"); -ok (ref($x),'ARRAY'); ok ($C->_str($x),123); ok ($C->_str($y),321); - -############################################################################### -# _add, _sub, _mul, _div -ok ($C->_str($C->_add($x,$y)),444); -ok ($C->_str($C->_sub($x,$y)),123); -ok ($C->_str($C->_mul($x,$y)),39483); -ok ($C->_str($C->_div($x,$y)),123); - -############################################################################### -# check that mul/div doesn't change $y -# and returns the same reference, not something new -ok ($C->_str($C->_mul($x,$y)),39483); -ok ($C->_str($x),39483); ok ($C->_str($y),321); - -ok ($C->_str($C->_div($x,$y)),123); -ok ($C->_str($x),123); ok ($C->_str($y),321); - -$x = $C->_new("39483"); -my ($x1,$r1) = $C->_div($x,$y); -ok ("$x1","$x"); -$C->_inc($x1); -ok ("$x1","$x"); -ok ($C->_str($r1),'0'); - -$x = $C->_new("39483"); # reset - -############################################################################### -my $z = $C->_new("2"); -ok ($C->_str($C->_add($x,$z)),39485); -my ($re,$rr) = $C->_div($x,$y); - -ok ($C->_str($re),123); ok ($C->_str($rr),2); - -# is_zero, _is_one, _one, _zero -ok ($C->_is_zero($x)||0,0); -ok ($C->_is_one($x)||0,0); - -ok ($C->_str($C->_zero()),"0"); -ok ($C->_str($C->_one()),"1"); - -# _two() and _ten() -ok ($C->_str($C->_two()),"2"); -ok ($C->_str($C->_ten()),"10"); -ok ($C->_is_ten($C->_two()),0); -ok ($C->_is_two($C->_two()),1); -ok ($C->_is_ten($C->_ten()),1); -ok ($C->_is_two($C->_ten()),0); - -ok ($C->_is_one($C->_one()),1); -ok ($C->_is_one($C->_two()),0); -ok ($C->_is_one($C->_ten()),0); - -ok ($C->_is_one($C->_zero()) || 0,0); - -ok ($C->_is_zero($C->_zero()),1); - -ok ($C->_is_zero($C->_one()) || 0,0); - -# is_odd, is_even -ok ($C->_is_odd($C->_one()),1); ok ($C->_is_odd($C->_zero())||0,0); -ok ($C->_is_even($C->_one()) || 0,0); ok ($C->_is_even($C->_zero()),1); - -# _len -for my $method (qw/_alen _len/) - { - $x = $C->_new("1"); ok ($C->$method($x),1); - $x = $C->_new("12"); ok ($C->$method($x),2); - $x = $C->_new("123"); ok ($C->$method($x),3); - $x = $C->_new("1234"); ok ($C->$method($x),4); - $x = $C->_new("12345"); ok ($C->$method($x),5); - $x = $C->_new("123456"); ok ($C->$method($x),6); - $x = $C->_new("1234567"); ok ($C->$method($x),7); - $x = $C->_new("12345678"); ok ($C->$method($x),8); - $x = $C->_new("123456789"); ok ($C->$method($x),9); - - $x = $C->_new("8"); ok ($C->$method($x),1); - $x = $C->_new("21"); ok ($C->$method($x),2); - $x = $C->_new("321"); ok ($C->$method($x),3); - $x = $C->_new("4321"); ok ($C->$method($x),4); - $x = $C->_new("54321"); ok ($C->$method($x),5); - $x = $C->_new("654321"); ok ($C->$method($x),6); - $x = $C->_new("7654321"); ok ($C->$method($x),7); - $x = $C->_new("87654321"); ok ($C->$method($x),8); - $x = $C->_new("987654321"); ok ($C->$method($x),9); - - $x = $C->_new("0"); ok ($C->$method($x),1); - $x = $C->_new("20"); ok ($C->$method($x),2); - $x = $C->_new("320"); ok ($C->$method($x),3); - $x = $C->_new("4320"); ok ($C->$method($x),4); - $x = $C->_new("54320"); ok ($C->$method($x),5); - $x = $C->_new("654320"); ok ($C->$method($x),6); - $x = $C->_new("7654320"); ok ($C->$method($x),7); - $x = $C->_new("87654320"); ok ($C->$method($x),8); - $x = $C->_new("987654320"); ok ($C->$method($x),9); - - for (my $i = 1; $i < 9; $i++) - { - my $a = "$i" . '0' x ($i-1); - $x = $C->_new($a); - print "# Tried len '$a'\n" unless ok ($C->_len($x),$i); - } - } - -# _digit -$x = $C->_new("123456789"); -ok ($C->_digit($x,0),9); -ok ($C->_digit($x,1),8); -ok ($C->_digit($x,2),7); -ok ($C->_digit($x,-1),1); -ok ($C->_digit($x,-2),2); -ok ($C->_digit($x,-3),3); - -# _copy -foreach (qw/ 1 12 123 1234 12345 123456 1234567 12345678 123456789/) - { - $x = $C->_new("$_"); - ok ($C->_str($C->_copy($x)),"$_"); - ok ($C->_str($x),"$_"); # did _copy destroy original x? - } - -# _zeros -$x = $C->_new("1256000000"); ok ($C->_zeros($x),6); -$x = $C->_new("152"); ok ($C->_zeros($x),0); -$x = $C->_new("123000"); ok ($C->_zeros($x),3); -$x = $C->_new("0"); ok ($C->_zeros($x),0); - -# _lsft, _rsft -$x = $C->_new("10"); $y = $C->_new("3"); -ok ($C->_str($C->_lsft($x,$y,10)),10000); -$x = $C->_new("20"); $y = $C->_new("3"); -ok ($C->_str($C->_lsft($x,$y,10)),20000); - -$x = $C->_new("128"); $y = $C->_new("4"); -ok ($C->_str($C->_lsft($x,$y,2)), 128 << 4); - -$x = $C->_new("1000"); $y = $C->_new("3"); -ok ($C->_str($C->_rsft($x,$y,10)),1); -$x = $C->_new("20000"); $y = $C->_new("3"); -ok ($C->_str($C->_rsft($x,$y,10)),20); -$x = $C->_new("256"); $y = $C->_new("4"); -ok ($C->_str($C->_rsft($x,$y,2)),256 >> 4); - -$x = $C->_new("6411906467305339182857313397200584952398"); -$y = $C->_new("45"); -ok ($C->_str($C->_rsft($x,$y,10)),0); - -# _acmp -$x = $C->_new("123456789"); -$y = $C->_new("987654321"); -ok ($C->_acmp($x,$y),-1); -ok ($C->_acmp($y,$x),1); -ok ($C->_acmp($x,$x),0); -ok ($C->_acmp($y,$y),0); -$x = $C->_new("12"); -$y = $C->_new("12"); -ok ($C->_acmp($x,$y),0); -$x = $C->_new("21"); -ok ($C->_acmp($x,$y),1); -ok ($C->_acmp($y,$x),-1); -$x = $C->_new("123456789"); -$y = $C->_new("1987654321"); -ok ($C->_acmp($x,$y),-1); -ok ($C->_acmp($y,$x),+1); - -$x = $C->_new("1234567890123456789"); -$y = $C->_new("987654321012345678"); -ok ($C->_acmp($x,$y),1); -ok ($C->_acmp($y,$x),-1); -ok ($C->_acmp($x,$x),0); -ok ($C->_acmp($y,$y),0); - -$x = $C->_new("1234"); -$y = $C->_new("987654321012345678"); -ok ($C->_acmp($x,$y),-1); -ok ($C->_acmp($y,$x),1); -ok ($C->_acmp($x,$x),0); -ok ($C->_acmp($y,$y),0); - -# _modinv -$x = $C->_new("8"); -$y = $C->_new("5033"); -my ($xmod,$sign) = $C->_modinv($x,$y); -ok ($C->_str($xmod),'629'); # -629 % 5033 == 4404 -ok ($sign, '-'); - -# _div -$x = $C->_new("3333"); $y = $C->_new("1111"); -ok ($C->_str(scalar $C->_div($x,$y)),3); -$x = $C->_new("33333"); $y = $C->_new("1111"); ($x,$y) = $C->_div($x,$y); -ok ($C->_str($x),30); ok ($C->_str($y),3); -$x = $C->_new("123"); $y = $C->_new("1111"); -($x,$y) = $C->_div($x,$y); ok ($C->_str($x),0); ok ($C->_str($y),123); - -# _num -foreach (qw/1 12 123 1234 12345 1234567 12345678 123456789 1234567890/) - { - $x = $C->_new("$_"); - ok (ref($x)||'','ARRAY'); ok ($C->_str($x),"$_"); - $x = $C->_num($x); ok (ref($x)||'',''); ok ($x,$_); - } - -# _sqrt -$x = $C->_new("144"); ok ($C->_str($C->_sqrt($x)),'12'); -$x = $C->_new("144000000000000"); ok ($C->_str($C->_sqrt($x)),'12000000'); - -# _root -$x = $C->_new("81"); my $n = $C->_new("3"); # 4*4*4 = 64, 5*5*5 = 125 -ok ($C->_str($C->_root($x,$n)),'4'); # 4.xx => 4.0 -$x = $C->_new("81"); $n = $C->_new("4"); # 3*3*3*3 == 81 -ok ($C->_str($C->_root($x,$n)),'3'); - -# _pow (and _root) -$x = $C->_new("0"); $n = $C->_new("3"); # 0 ** y => 0 -ok ($C->_str($C->_pow($x,$n)), 0); -$x = $C->_new("3"); $n = $C->_new("0"); # x ** 0 => 1 -ok ($C->_str($C->_pow($x,$n)), 1); -$x = $C->_new("1"); $n = $C->_new("3"); # 1 ** y => 1 -ok ($C->_str($C->_pow($x,$n)), 1); -$x = $C->_new("5"); $n = $C->_new("1"); # x ** 1 => x -ok ($C->_str($C->_pow($x,$n)), 5); - -$x = $C->_new("81"); $n = $C->_new("3"); # 81 ** 3 == 531441 -ok ($C->_str($C->_pow($x,$n)),81 ** 3); - -ok ($C->_str($C->_root($x,$n)),81); - -$x = $C->_new("81"); -ok ($C->_str($C->_pow($x,$n)),81 ** 3); -ok ($C->_str($C->_pow($x,$n)),'150094635296999121'); # 531441 ** 3 == - -ok ($C->_str($C->_root($x,$n)),'531441'); -ok ($C->_str($C->_root($x,$n)),'81'); - -$x = $C->_new("81"); $n = $C->_new("14"); -ok ($C->_str($C->_pow($x,$n)),'523347633027360537213511521'); -ok ($C->_str($C->_root($x,$n)),'81'); - -$x = $C->_new("523347633027360537213511520"); -ok ($C->_str($C->_root($x,$n)),'80'); - -$x = $C->_new("523347633027360537213511522"); -ok ($C->_str($C->_root($x,$n)),'81'); - -my $res = [ qw/9 31 99 316 999 3162 9999 31622 99999/ ]; - -# 99 ** 2 = 9801, 999 ** 2 = 998001 etc -for my $i (2 .. 9) - { - $x = '9' x $i; $x = $C->_new($x); - $n = $C->_new("2"); - my $rc = '9' x ($i-1). '8' . '0' x ($i-1) . '1'; - print "# _pow( ", '9' x $i, ", 2) \n" unless - ok ($C->_str($C->_pow($x,$n)),$rc); - - # if $i > $BASE_LEN, the test takes a really long time: - if ($i <= $BASE_LEN) - { - $x = '9' x $i; $x = $C->_new($x); - $n = '9' x $i; $n = $C->_new($n); - print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n"; - print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless - ok ($C->_str($C->_root($x,$n)),'1'); - - $x = '9' x $i; $x = $C->_new($x); - $n = $C->_new("2"); - print "# BASE_LEN $BASE_LEN _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless - ok ($C->_str($C->_root($x,$n)), $res->[$i-2]); - } - else - { - ok ("skipped $i", "skipped $i"); - ok ("skipped $i", "skipped $i"); - } - } - -############################################################################## -# _fac -$x = $C->_new("0"); ok ($C->_str($C->_fac($x)),'1'); -$x = $C->_new("1"); ok ($C->_str($C->_fac($x)),'1'); -$x = $C->_new("2"); ok ($C->_str($C->_fac($x)),'2'); -$x = $C->_new("3"); ok ($C->_str($C->_fac($x)),'6'); -$x = $C->_new("4"); ok ($C->_str($C->_fac($x)),'24'); -$x = $C->_new("5"); ok ($C->_str($C->_fac($x)),'120'); -$x = $C->_new("10"); ok ($C->_str($C->_fac($x)),'3628800'); -$x = $C->_new("11"); ok ($C->_str($C->_fac($x)),'39916800'); -$x = $C->_new("12"); ok ($C->_str($C->_fac($x)),'479001600'); -$x = $C->_new("13"); ok ($C->_str($C->_fac($x)),'6227020800'); - -# test that _fac modifes $x in place for small arguments -$x = $C->_new("3"); $C->_fac($x); ok ($C->_str($x),'6'); -$x = $C->_new("13"); $C->_fac($x); ok ($C->_str($x),'6227020800'); - -############################################################################## -# _inc and _dec -foreach (qw/1 11 121 1231 12341 1234561 12345671 123456781 1234567891/) - { - $x = $C->_new("$_"); $C->_inc($x); - print "# \$x = ",$C->_str($x),"\n" - unless ok ($C->_str($x),substr($_,0,length($_)-1) . '2'); - $C->_dec($x); ok ($C->_str($x),$_); - } -foreach (qw/19 119 1219 12319 1234519 12345619 123456719 1234567819/) - { - $x = $C->_new("$_"); $C->_inc($x); - print "# \$x = ",$C->_str($x),"\n" - unless ok ($C->_str($x),substr($_,0,length($_)-2) . '20'); - $C->_dec($x); ok ($C->_str($x),$_); - } -foreach (qw/999 9999 99999 9999999 99999999 999999999 9999999999 99999999999/) - { - $x = $C->_new("$_"); $C->_inc($x); - print "# \$x = ",$C->_str($x),"\n" - unless ok ($C->_str($x), '1' . '0' x (length($_))); - $C->_dec($x); ok ($C->_str($x),$_); - } - -$x = $C->_new("1000"); $C->_inc($x); ok ($C->_str($x),'1001'); -$C->_dec($x); ok ($C->_str($x),'1000'); - -my $BL; -{ - no strict 'refs'; - $BL = &{"$C"."::_base_len"}(); -} - -$x = '1' . '0' x $BL; -$z = '1' . '0' x ($BL-1); $z .= '1'; -$x = $C->_new($x); $C->_inc($x); ok ($C->_str($x),$z); - -$x = '1' . '0' x $BL; $z = '9' x $BL; -$x = $C->_new($x); $C->_dec($x); ok ($C->_str($x),$z); - -# should not happen: -# $x = $C->_new("-2"); $y = $C->_new("4"); ok ($C->_acmp($x,$y),-1); - -############################################################################### -# _mod -$x = $C->_new("1000"); $y = $C->_new("3"); -ok ($C->_str(scalar $C->_mod($x,$y)),1); -$x = $C->_new("1000"); $y = $C->_new("2"); -ok ($C->_str(scalar $C->_mod($x,$y)),0); - -# _and, _or, _xor -$x = $C->_new("5"); $y = $C->_new("2"); -ok ($C->_str(scalar $C->_xor($x,$y)),7); -$x = $C->_new("5"); $y = $C->_new("2"); -ok ($C->_str(scalar $C->_or($x,$y)),7); -$x = $C->_new("5"); $y = $C->_new("3"); -ok ($C->_str(scalar $C->_and($x,$y)),1); - -# _from_hex, _from_bin, _from_oct -ok ($C->_str( $C->_from_hex("0xFf")),255); -ok ($C->_str( $C->_from_bin("0b10101011")),160+11); -ok ($C->_str( $C->_from_oct("0100")), 8*8); -ok ($C->_str( $C->_from_oct("01000")), 8*8*8); -ok ($C->_str( $C->_from_oct("010001")), 8*8*8*8+1); -ok ($C->_str( $C->_from_oct("010007")), 8*8*8*8+7); - -# _as_hex, _as_bin, as_oct -ok ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new("128")))), 128); -ok ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new("128")))), 128); -ok ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("128")))), 128); - -ok ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("123456")))), 123456); -ok ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("123456789")))), "123456789"); -ok ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("1234567890123")))), "1234567890123"); - -my $long = '123456789012345678901234567890'; -ok ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new($long)))), $long); -ok ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new($long)))), $long); -ok ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new($long)))), $long); -ok ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new("0")))), 0); -ok ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new("0")))), 0); -ok ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("0")))), 0); -ok ($C->_as_hex( $C->_new("0")), '0x0'); -ok ($C->_as_bin( $C->_new("0")), '0b0'); -ok ($C->_as_oct( $C->_new("0")), '00'); -ok ($C->_as_hex( $C->_new("12")), '0xc'); -ok ($C->_as_bin( $C->_new("12")), '0b1100'); -ok ($C->_as_oct( $C->_new("64")), '0100'); - -# _1ex -ok ($C->_str($C->_1ex(0)), "1"); -ok ($C->_str($C->_1ex(1)), "10"); -ok ($C->_str($C->_1ex(2)), "100"); -ok ($C->_str($C->_1ex(12)), "1000000000000"); -ok ($C->_str($C->_1ex(16)), "10000000000000000"); - -# _check -$x = $C->_new("123456789"); -ok ($C->_check($x),0); -ok ($C->_check(123),'123 is not a reference'); - -############################################################################### -# __strip_zeros - -{ - no strict 'refs'; - # correct empty arrays - $x = &{$C."::__strip_zeros"}([]); ok (@$x,1); ok ($x->[0],0); - # don't strip single elements - $x = &{$C."::__strip_zeros"}([0]); ok (@$x,1); ok ($x->[0],0); - $x = &{$C."::__strip_zeros"}([1]); ok (@$x,1); ok ($x->[0],1); - # don't strip non-zero elements - $x = &{$C."::__strip_zeros"}([0,1]); - ok (@$x,2); ok ($x->[0],0); ok ($x->[1],1); - $x = &{$C."::__strip_zeros"}([0,1,2]); - ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2); - - # but strip leading zeros - $x = &{$C."::__strip_zeros"}([0,1,2,0]); - ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2); - - $x = &{$C."::__strip_zeros"}([0,1,2,0,0]); - ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2); - - $x = &{$C."::__strip_zeros"}([0,1,2,0,0,0]); - ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2); - - # collapse multiple zeros - $x = &{$C."::__strip_zeros"}([0,0,0,0]); - ok (@$x,1); ok ($x->[0],0); -} - -# done - -1; - diff --git a/cpan/Math-BigInt/t/bigintpm.inc b/cpan/Math-BigInt/t/bigintpm.inc deleted file mode 100644 index 87140ba44d..0000000000 --- a/cpan/Math-BigInt/t/bigintpm.inc +++ /dev/null @@ -1,2511 +0,0 @@ -#include this file into another for subclass testing - -my $version = ${"$class\::VERSION"}; - -use strict; - -############################################################################## -# for testing inheritance of _swap - -package Math::Foo; - -use Math::BigInt lib => $main::CL; -use vars qw/@ISA/; -@ISA = (qw/Math::BigInt/); - -use overload -# customized overload for sub, since original does not use swap there -'-' => sub { my @a = ref($_[0])->_swap(@_); - $a[0]->bsub($a[1])}; - -sub _swap - { - # a fake _swap, which reverses the params - my $self = shift; # for override in subclass - if ($_[2]) - { - my $c = ref ($_[0] ) || 'Math::Foo'; - return ( $_[0]->copy(), $_[1] ); - } - else - { - return ( Math::Foo->new($_[1]), $_[0] ); - } - } - -############################################################################## -package main; - -my $CALC = $class->config()->{lib}; ok ($CALC,$CL); - -my ($f,$z,$a,$exp,@a,$m,$e,$round_mode,$expected_class); - -while (<DATA>) - { - $_ =~ s/[\n\r]//g; # remove newlines - next if /^#/; # skip comments - if (s/^&//) - { - $f = $_; next; - } - elsif (/^\$/) - { - $round_mode = $_; $round_mode =~ s/^\$/$class\->/; next; - } - - @args = split(/:/,$_,99); $ans = pop(@args); - $expected_class = $class; - if ($ans =~ /(.*?)=(.*)/) - { - $expected_class = $2; $ans = $1; - } - $try = "\$x = $class->new(\"$args[0]\");"; - if ($f eq "bnorm") - { - $try = "\$x = $class->bnorm(\"$args[0]\");"; - # some is_xxx tests - } elsif ($f =~ /^is_(zero|one|odd|even|negative|positive|nan|int)$/) { - $try .= "\$x->$f() || 0;"; - } elsif ($f eq "is_inf") { - $try .= "\$x->is_inf('$args[1]');"; - } elsif ($f eq "binf") { - $try .= "\$x->binf('$args[1]');"; - } elsif ($f eq "bone") { - $try .= "\$x->bone('$args[1]');"; - # some unary ops - } elsif ($f =~ /^b(nan|floor|ceil|sstr|neg|abs|inc|dec|not|sqrt|fac)$/) { - $try .= "\$x->$f();"; - } elsif ($f =~ /^(numify|length|stringify|as_hex|as_bin)$/) { - $try .= "\$x->$f();"; - } elsif ($f eq "exponent"){ - # ->bstr() to see if an object is returned - $try .= '$x = $x->exponent()->bstr();'; - } elsif ($f eq "mantissa"){ - # ->bstr() to see if an object is returned - $try .= '$x = $x->mantissa()->bstr();'; - } elsif ($f eq "parts"){ - $try .= '($m,$e) = $x->parts();'; - # ->bstr() to see if an object is returned - $try .= '$m = $m->bstr(); $m = "NaN" if !defined $m;'; - $try .= '$e = $e->bstr(); $e = "NaN" if !defined $e;'; - $try .= '"$m,$e";'; - }elsif ($f eq "bexp"){ - $try .= "\$x->bexp();"; - } elsif ($f eq "bpi"){ - $try .= "$class\->bpi(\$x);"; - } else { - # binary ops - $try .= "\$y = $class->new('$args[1]');"; - if ($f eq "bcmp") - { - $try .= '$x->bcmp($y);'; - } elsif ($f eq "bround") { - $try .= "$round_mode; \$x->bround(\$y);"; - } elsif ($f eq "bacmp"){ - $try .= '$x->bacmp($y);'; - } elsif ($f eq "badd"){ - $try .= '$x + $y;'; - } elsif ($f eq "bsub"){ - $try .= '$x - $y;'; - } elsif ($f eq "bmul"){ - $try .= '$x * $y;'; - } elsif ($f eq "bdiv"){ - $try .= '$x / $y;'; - } elsif ($f eq "bdiv-list"){ - $try .= 'join (",",$x->bdiv($y));'; - # overload via x= - } elsif ($f =~ /^.=$/){ - $try .= "\$x $f \$y;"; - # overload via x - } elsif ($f =~ /^.$/){ - $try .= "\$x $f \$y;"; - } elsif ($f eq "bmod"){ - $try .= '$x % $y;'; - } elsif ($f eq "bgcd") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new('$args[2]'); "; - } - $try .= "$class\::bgcd(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - } - elsif ($f eq "blcm") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new('$args[2]'); "; - } - $try .= "$class\::blcm(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - }elsif ($f eq "blsft"){ - if (defined $args[2]) - { - $try .= "\$x->blsft(\$y,$args[2]);"; - } - else - { - $try .= "\$x << \$y;"; - } - }elsif ($f eq "brsft"){ - if (defined $args[2]) - { - $try .= "\$x->brsft(\$y,$args[2]);"; - } - else - { - $try .= "\$x >> \$y;"; - } - }elsif ($f eq "bnok"){ - $try .= "\$x->bnok(\$y);"; - }elsif ($f eq "broot"){ - $try .= "\$x->broot(\$y);"; - }elsif ($f eq "blog"){ - $try .= "\$x->blog(\$y);"; - }elsif ($f eq "band"){ - $try .= "\$x & \$y;"; - }elsif ($f eq "bior"){ - $try .= "\$x | \$y;"; - }elsif ($f eq "bxor"){ - $try .= "\$x ^ \$y;"; - }elsif ($f eq "bpow"){ - $try .= "\$x ** \$y;"; - } elsif( $f eq "bmodinv") { - $try .= "\$x->bmodinv(\$y);"; - }elsif ($f eq "digit"){ - $try .= "\$x->digit(\$y);"; - }elsif ($f eq "batan2"){ - $try .= "\$x->batan2(\$y);"; - } else { - # Functions with three arguments - $try .= "\$z = $class->new(\"$args[2]\");"; - - if( $f eq "bmodpow") { - $try .= "\$x->bmodpow(\$y,\$z);"; - } elsif ($f eq "bmuladd"){ - $try .= "\$x->bmuladd(\$y,\$z);"; - } else { warn "Unknown op '$f'"; } - } - } # end else all other ops - - $ans1 = eval $try; - # convert hex/binary targets to decimal - if ($ans =~ /^(0x0x|0b0b)/) - { - $ans =~ s/^0[xb]//; $ans = Math::BigInt->new($ans)->bstr(); - } - if ($ans eq "") - { - ok_undef ($ans1); - } - else - { - # print "try: $try ans: $ans1 $ans\n"; - print "# Tried: '$try'\n" if !ok ($ans1, $ans); - ok (ref($ans),$expected_class) if $expected_class ne $class; - } - # check internal state of number objects - is_valid($ans1,$f) if ref $ans1; - } # endwhile data tests -close DATA; - -# test some more -@a = (); -for (my $i = 1; $i < 10; $i++) - { - push @a, $i; - } -ok "@a", "1 2 3 4 5 6 7 8 9"; - -# test whether self-multiplication works correctly (result is 2**64) -$try = "\$x = $class->new('4294967296');"; -$try .= '$a = $x->bmul($x);'; -$ans1 = eval $try; -print "# Tried: '$try'\n" if !ok ($ans1, $class->new(2) ** 64); -# test self-pow -$try = "\$x = $class->new(10);"; -$try .= '$a = $x->bpow($x);'; -$ans1 = eval $try; -print "# Tried: '$try'\n" if !ok ($ans1, $class->new(10) ** 10); - -############################################################################### -# test whether op destroys args or not (should better not) - -$x = $class->new(3); -$y = $class->new(4); -$z = $x & $y; -ok ($x,3); -ok ($y,4); -ok ($z,0); -$z = $x | $y; -ok ($x,3); -ok ($y,4); -ok ($z,7); -$x = $class->new(1); -$y = $class->new(2); -$z = $x | $y; -ok ($x,1); -ok ($y,2); -ok ($z,3); - -$x = $class->new(5); -$y = $class->new(4); -$z = $x ^ $y; -ok ($x,5); -ok ($y,4); -ok ($z,1); - -$x = $class->new(-5); $y = -$x; -ok ($x, -5); - -$x = $class->new(-5); $y = abs($x); -ok ($x, -5); - -$x = $class->new(8); -$y = $class->new(-1); -$z = $class->new(5033); -my $u = $x->copy()->bmodpow($y,$z); -ok ($u,4404); -ok ($y,-1); -ok ($z,5033); - -$x = $class->new(-5); $y = -$x; ok ($x,-5); ok ($y,5); -$x = $class->new(-5); $y = $x->copy()->bneg(); ok ($x,-5); ok ($y,5); - -$x = $class->new(-5); $y = $class->new(3); $x->bmul($y); ok ($x,-15); ok ($y,3); -$x = $class->new(-5); $y = $class->new(3); $x->badd($y); ok ($x,-2); ok ($y,3); -$x = $class->new(-5); $y = $class->new(3); $x->bsub($y); ok ($x,-8); ok ($y,3); -$x = $class->new(-15); $y = $class->new(3); $x->bdiv($y); ok ($x,-5); ok ($y,3); -$x = $class->new(-5); $y = $class->new(3); $x->bmod($y); ok ($x,1); ok ($y,3); - -$x = $class->new(5); $y = $class->new(3); $x->bmul($y); ok ($x,15); ok ($y,3); -$x = $class->new(5); $y = $class->new(3); $x->badd($y); ok ($x,8); ok ($y,3); -$x = $class->new(5); $y = $class->new(3); $x->bsub($y); ok ($x,2); ok ($y,3); -$x = $class->new(15); $y = $class->new(3); $x->bdiv($y); ok ($x,5); ok ($y,3); -$x = $class->new(5); $y = $class->new(3); $x->bmod($y); ok ($x,2); ok ($y,3); - -$x = $class->new(5); $y = $class->new(-3); $x->bmul($y); ok ($x,-15); ok($y,-3); -$x = $class->new(5); $y = $class->new(-3); $x->badd($y); ok ($x,2); ok($y,-3); -$x = $class->new(5); $y = $class->new(-3); $x->bsub($y); ok ($x,8); ok($y,-3); -$x = $class->new(15); $y = $class->new(-3); $x->bdiv($y); ok ($x,-5); ok($y,-3); -$x = $class->new(5); $y = $class->new(-3); $x->bmod($y); ok ($x,-1); ok($y,-3); - -############################################################################### -# check whether overloading cmp works -$try = "\$x = $class->new(0);"; -$try .= "\$y = 10;"; -$try .= "'false' if \$x ne \$y;"; -$ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "false" ); - -# we cant test for working cmpt with other objects here, we would need a dummy -# object with stringify overload for this. see Math::String tests as example - -############################################################################### -# check reversed order of arguments - -$try = "\$x = $class->new(10); \$x = 2 ** \$x;"; -$try .= "'ok' if \$x == 1024;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class->new(10); \$x = 2 * \$x;"; -$try .= "'ok' if \$x == 20;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class->new(10); \$x = 2 + \$x;"; -$try .= "'ok' if \$x == 12;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class\->new(10); \$x = 2 - \$x;"; -$try .= "'ok' if \$x == -8;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class\->new(10); \$x = 20 / \$x;"; -$try .= "'ok' if \$x == 2;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class\->new(3); \$x = 20 % \$x;"; -$try .= "'ok' if \$x == 2;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class\->new(7); \$x = 20 & \$x;"; -$try .= "'ok' if \$x == 4;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class\->new(7); \$x = 0x20 | \$x;"; -$try .= "'ok' if \$x == 0x27;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class\->new(7); \$x = 0x20 ^ \$x;"; -$try .= "'ok' if \$x == 0x27;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -############################################################################### -# check badd(4,5) form - -$try = "\$x = $class\->badd(4,5);"; -$try .= "'ok' if \$x == 9;"; -$ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -############################################################################### -# check undefs: NOT DONE YET - -############################################################################### -# bool - -$x = $class->new(1); if ($x) { ok (1,1); } else { ok($x,'to be true') } -$x = $class->new(0); if (!$x) { ok (1,1); } else { ok($x,'to be false') } - -############################################################################### -# objectify() - -@args = Math::BigInt::objectify(2,4,5); -ok (scalar @args,3); # $class, 4, 5 -ok ($args[0] =~ /^Math::BigInt/); -ok ($args[1],4); -ok ($args[2],5); - -@args = Math::BigInt::objectify(0,4,5); -ok (scalar @args,3); # $class, 4, 5 -ok ($args[0] =~ /^Math::BigInt/); -ok ($args[1],4); -ok ($args[2],5); - -@args = Math::BigInt::objectify(2,4,5); -ok (scalar @args,3); # $class, 4, 5 -ok ($args[0] =~ /^Math::BigInt/); -ok ($args[1],4); -ok ($args[2],5); - -@args = Math::BigInt::objectify(2,4,5,6,7); -ok (scalar @args,5); # $class, 4, 5, 6, 7 -ok ($args[0] =~ /^Math::BigInt/); -ok ($args[1],4); ok (ref($args[1]),$args[0]); -ok ($args[2],5); ok (ref($args[2]),$args[0]); -ok ($args[3],6); ok (ref($args[3]),''); -ok ($args[4],7); ok (ref($args[4]),''); - -@args = Math::BigInt::objectify(2,$class,4,5,6,7); -ok (scalar @args,5); # $class, 4, 5, 6, 7 -ok ($args[0],$class); -ok ($args[1],4); ok (ref($args[1]),$args[0]); -ok ($args[2],5); ok (ref($args[2]),$args[0]); -ok ($args[3],6); ok (ref($args[3]),''); -ok ($args[4],7); ok (ref($args[4]),''); - -############################################################################### -# test whether an opp calls objectify properly or not (or at least does what -# it should do given non-objects, w/ or w/o objectify()) - -ok ($class->new(123)->badd(123),246); -ok ($class->badd(123,321),444); -ok ($class->badd(123,$class->new(321)),444); - -ok ($class->new(123)->bsub(122),1); -ok ($class->bsub(321,123),198); -ok ($class->bsub(321,$class->new(123)),198); - -ok ($class->new(123)->bmul(123),15129); -ok ($class->bmul(123,123),15129); -ok ($class->bmul(123,$class->new(123)),15129); - -ok ($class->new(15129)->bdiv(123),123); -ok ($class->bdiv(15129,123),123); -ok ($class->bdiv(15129,$class->new(123)),123); - -ok ($class->new(15131)->bmod(123),2); -ok ($class->bmod(15131,123),2); -ok ($class->bmod(15131,$class->new(123)),2); - -ok ($class->new(2)->bpow(16),65536); -ok ($class->bpow(2,16),65536); -ok ($class->bpow(2,$class->new(16)),65536); - -ok ($class->new(2**15)->brsft(1),2**14); -ok ($class->brsft(2**15,1),2**14); -ok ($class->brsft(2**15,$class->new(1)),2**14); - -ok ($class->new(2**13)->blsft(1),2**14); -ok ($class->blsft(2**13,1),2**14); -ok ($class->blsft(2**13,$class->new(1)),2**14); - -############################################################################### -# test for floating-point input (other tests in bnorm() below) - -$z = 1050000000000000; # may be int on systems with 64bit? -$x = $class->new($z); ok ($x->bsstr(),'105e+13'); # not 1.05e+15 -$z = 1e+129; # definitely a float (may fail on UTS) -# don't compare to $z, since some Perl versions stringify $z into something -# like '1.e+129' or something equally ugly -$x = $class->new($z); ok ($x->bsstr(),'1e+129'); - -############################################################################### -# test for whitespace inlcuding newlines to be handled correctly - -# ok ($Math::BigInt::strict,1); # the default - -foreach my $c ( - qw/1 12 123 1234 12345 123456 1234567 12345678 123456789 1234567890/) - { - my $m = $class->new($c); - ok ($class->new("$c"),$m); - ok ($class->new(" $c"),$m); - ok ($class->new("$c "),$m); - ok ($class->new(" $c "),$m); - ok ($class->new("\n$c"),$m); - ok ($class->new("$c\n"),$m); - ok ($class->new("\n$c\n"),$m); - ok ($class->new(" \n$c\n"),$m); - ok ($class->new(" \n$c \n"),$m); - ok ($class->new(" \n$c\n "),$m); - ok ($class->new(" \n$c\n1"),'NaN'); - ok ($class->new("1 \n$c\n1"),'NaN'); - } - -############################################################################### -# prime number tests, also test for **= and length() -# found on: http://www.utm.edu/research/primes/notes/by_year.html - -# ((2^148)-1)/17 -$x = $class->new(2); $x **= 148; $x++; $x = $x / 17; -ok ($x,"20988936657440586486151264256610222593863921"); -ok ($x->length(),length "20988936657440586486151264256610222593863921"); - -# MM7 = 2^127-1 -$x = $class->new(2); $x **= 127; $x--; -ok ($x,"170141183460469231731687303715884105727"); - -$x = $class->new('215960156869840440586892398248'); -($x,$y) = $x->length(); -ok ($x,30); ok ($y,0); - -$x = $class->new('1_000_000_000_000'); -($x,$y) = $x->length(); -ok ($x,13); ok ($y,0); - -# test <<=, >>= -$x = $class->new('2'); -my $y = $class->new('18'); -ok ($x <<= $y, 2 << 18); -ok ($x, 2 << 18); -ok ($x >>= $y, 2); -ok ($x, 2); - -# I am afraid the following is not yet possible due to slowness -# Also, testing for 2 meg output is a bit hard ;) -#$x = $class->new(2); $x **= 6972593; $x--; - -# 593573509*2^332162+1 has exactly 1,000,000 digits -# takes about 24 mins on 300 Mhz, so cannot be done yet ;) -#$x = $class->new(2); $x **= 332162; $x *= "593573509"; $x++; -#ok ($x->length(),1_000_000); - -############################################################################### -# inheritance and overriding of _swap - -$x = Math::Foo->new(5); -$x = $x - 8; # 8 - 5 instead of 5-8 -ok ($x,3); -ok (ref($x),'Math::Foo'); - -$x = Math::Foo->new(5); -$x = 8 - $x; # 5 - 8 instead of 8 - 5 -ok ($x,-3); -ok (ref($x),'Math::Foo'); - -############################################################################### -# Test whether +inf eq inf -# This tried to test whether BigInt inf equals Perl inf. Unfortunately, Perl -# hasn't (before 5.7.3 at least) a consistent way to say inf, and some things -# like 1e100000 crash on some platforms. So simple test for the string 'inf' -$x = $class->new('+inf'); ok ($x,'inf'); - -############################################################################### -############################################################################### -# the followin tests only make sense with Math::BigInt::Calc or BareCalc or -# FastCalc - -exit if $CALC !~ /^Math::BigInt::(|Bare|Fast)Calc$/; # for Pari et al. - -############################################################################### -# check proper length of internal arrays - -my $bl = $CL->_base_len(); -my $BASE = '9' x $bl; -my $MAX = $BASE; -$BASE++; - -$x = $class->new($MAX); is_valid($x); # f.i. 9999 -$x += 1; ok ($x,$BASE); is_valid($x); # 10000 -$x -= 1; ok ($x,$MAX); is_valid($x); # 9999 again - -############################################################################### -# check numify - -$x = $class->new($BASE-1); ok ($x->numify(),$BASE-1); -$x = $class->new(-($BASE-1)); ok ($x->numify(),-($BASE-1)); - -# +0 is to protect from 1e15 vs 100000000 (stupid to_string aaaarglburblll...) -$x = $class->new($BASE); ok ($x->numify()+0,$BASE+0); -$x = $class->new(-$BASE); ok ($x->numify(),-$BASE); -$x = $class->new( -($BASE*$BASE*1+$BASE*1+1) ); -ok($x->numify(),-($BASE*$BASE*1+$BASE*1+1)); - -############################################################################### -# test bug in _digits with length($c[-1]) where $c[-1] was "00001" instead of 1 - -$x = $class->new($BASE-2); $x++; $x++; $x++; $x++; -if ($x > $BASE) { ok (1,1) } else { ok ("$x < $BASE","$x > $BASE"); } - -$x = $class->new($BASE+3); $x++; -if ($x > $BASE) { ok (1,1) } else { ok ("$x > $BASE","$x < $BASE"); } - -# test for +0 instead of int(): -$x = $class->new($MAX); ok ($x->length(), length($MAX)); - -############################################################################### -# test bug that $class->digit($string) did not work - -ok ($class->digit(123,2),1); - -############################################################################### -# bug in sub where number with at least 6 trailing zeros after any op failed - -$x = $class->new(123456); $z = $class->new(10000); $z *= 10; $x -= $z; -ok ($z, 100000); -ok ($x, 23456); - -############################################################################### -# bug in shortcut in mul() - -# construct a number with a zero-hole of BASE_LEN_SMALL -{ - my @bl = $CL->_base_len(); my $bl = $bl[4]; - - $x = '1' x $bl . '0' x $bl . '1' x $bl . '0' x $bl; - $y = '1' x (2*$bl); - $x = $class->new($x)->bmul($y); - # result is 123..$bl . $bl x (3*bl-1) . $bl...321 . '0' x $bl - $y = ''; my $d = ''; - for (my $i = 1; $i <= $bl; $i++) - { - $y .= $i; $d = $i.$d; - } - $y .= $bl x (3*$bl-1) . $d . '0' x $bl; - ok ($x,$y); - - - ############################################################################# - # see if mul shortcut for small numbers works - - $x = '9' x $bl; - $x = $class->new($x); - # 999 * 999 => 998 . 001, 9999*9999 => 9998 . 0001 - ok ($x*$x, '9' x ($bl-1) . '8' . '0' x ($bl-1) . '1'); -} - -############################################################################### -# bug with rest "-0" in div, causing further div()s to fail - -$x = $class->new('-322056000'); ($x,$y) = $x->bdiv('-12882240'); - -ok ($y,'0'); is_valid($y); # $y not '-0' - -############################################################################### -# bug in $x->bmod($y) - -# if $x < 0 and $y > 0 -$x = $class->new('-629'); ok ($x->bmod(5033),4404); - -############################################################################### -# bone/binf etc as plain calls (Lite failed them) - -ok ($class->bzero(),0); -ok ($class->bone(),1); -ok ($class->bone('+'),1); -ok ($class->bone('-'),-1); -ok ($class->bnan(),'NaN'); -ok ($class->binf(),'inf'); -ok ($class->binf('+'),'inf'); -ok ($class->binf('-'),'-inf'); -ok ($class->binf('-inf'),'-inf'); - -############################################################################### -# is_one('-') - -ok ($class->new(1)->is_one('-'),0); -ok ($class->new(-1)->is_one('-'),1); -ok ($class->new(1)->is_one(),1); -ok ($class->new(-1)->is_one(),0); - -############################################################################### -# [perl #30609] bug with $x -= $x not being 0, but 2*$x - -$x = $class->new(3); $x -= $x; ok ($x, 0); -$x = $class->new(-3); $x -= $x; ok ($x, 0); -$x = $class->new('NaN'); $x -= $x; ok ($x->is_nan(), 1); -$x = $class->new('inf'); $x -= $x; ok ($x->is_nan(), 1); -$x = $class->new('-inf'); $x -= $x; ok ($x->is_nan(), 1); - -$x = $class->new('NaN'); $x += $x; ok ($x->is_nan(), 1); -$x = $class->new('inf'); $x += $x; ok ($x->is_inf(), 1); -$x = $class->new('-inf'); $x += $x; ok ($x->is_inf('-'), 1); -$x = $class->new(3); $x += $x; ok ($x, 6); -$x = $class->new(-3); $x += $x; ok ($x, -6); - -$x = $class->new(3); $x *= $x; ok ($x, 9); -$x = $class->new(-3); $x *= $x; ok ($x, 9); -$x = $class->new(3); $x /= $x; ok ($x, 1); -$x = $class->new(-3); $x /= $x; ok ($x, 1); -$x = $class->new(3); $x %= $x; ok ($x, 0); -$x = $class->new(-3); $x %= $x; ok ($x, 0); - -############################################################################### -# all tests done - -1; - -############################################################################### -############################################################################### -# Perl 5.005 does not like ok ($x,undef) - -sub ok_undef - { - my $x = shift; - - ok (1,1) and return if !defined $x; - ok ($x,'undef'); - } - -############################################################################### -# sub to check validity of a BigInt internally, to ensure that no op leaves a -# number object in an invalid state (f.i. "-0") - -sub is_valid - { - my ($x,$f) = @_; - - my $e = 0; # error? - - # allow the check to pass for all Lite, and all MBI and subclasses - # ok as reference? - $e = 'Not a reference to Math::BigInt' if ref($x) !~ /^Math::BigInt/; - - if (ref($x) ne 'Math::BigInt::Lite') - { - # has ok sign? - $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'" - if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/; - - $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0; - $e = $CALC->_check($x->{value}) if $e eq '0'; - } - - # test done, see if error did crop up - ok (1,1), return if ($e eq '0'); - - ok (1,$e." after op '$f'"); - } - -__DATA__ -&.= -1234:-345:1234-345 -&+= -1:2:3 --1:-2:-3 -&-= -1:2:-1 --1:-2:1 -&*= -2:3:6 --1:5:-5 -&%= -100:3:1 -8:9:8 --629:5033:4404 -&/= -100:3:33 --8:2:-4 -&|= -2:1:3 -&&= -5:7:5 -&^= -5:7:2 -&blog -NaNlog:2:NaN -122:NaNlog:NaN -NaNlog1:NaNlog:NaN -122:inf:NaN -inf:122:NaN -122:-inf:NaN --inf:122:NaN --inf:-inf:NaN -inf:inf:NaN -0:4:NaN --21:4:NaN -21:-21:NaN -# normal results -1024:2:10 -81:3:4 -# 3.01.. truncate -82:3:4 -# 3.9... truncate -80:3:3 -15625:5:6 -15626:5:6 -15624:5:5 -1000:10:3 -10000:10:4 -100000:10:5 -1000000:10:6 -10000000:10:7 -100000000:10:8 -8916100448256:12:12 -8916100448257:12:12 -8916100448255:12:11 -2251799813685248:8:17 -72057594037927936:2:56 -144115188075855872:2:57 -288230376151711744:2:58 -576460752303423488:2:59 -4096:2:12 -1329227995784915872903807060280344576:2:120 -# $x == $base => result 1 -3:3:1 -# $x < $base => result 0 ($base ** 0 <= $x) -3:4:0 -# $x == 1 => result 0 -1:5:0 -&is_negative -0:0 --1:1 -1:0 -+inf:0 --inf:1 -NaNneg:0 -&is_positive -0:0 --1:0 -1:1 -+inf:1 --inf:0 -NaNneg:0 -&is_int --inf:0 -+inf:0 -NaNis_int:0 -1:1 -0:1 -123e12:1 -&is_odd -abc:0 -0:0 -1:1 -3:1 --1:1 --3:1 -10000001:1 -10000002:0 -2:0 -120:0 -121:1 -&is_even -abc:0 -0:1 -1:0 -3:0 --1:0 --3:0 -10000001:0 -10000002:1 -2:1 -120:1 -121:0 -&bacmp -+0:-0:0 -+0:+1:-1 --1:+1:0 -+1:-1:0 --1:+2:-1 -+2:-1:1 --123456789:+987654321:-1 -+123456789:-987654321:-1 -+987654321:+123456789:1 --987654321:+123456789:1 --123:+4567889:-1 -# NaNs -acmpNaN:123: -123:acmpNaN: -acmpNaN:acmpNaN: -# infinity -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:0 --inf:+inf:0 -+inf:123:1 --inf:123:1 -+inf:-123:1 --inf:-123:1 -123:-inf:-1 --123:inf:-1 --123:-inf:-1 -123:inf:-1 -# return undef -+inf:NaN: -NaN:inf: --inf:NaN: -NaN:-inf: -&bnorm -0e999:0 -0e-999:0 --0e999:0 --0e-999:0 -123:123 -# binary input -0babc:NaN -0b123:NaN -0b0:0 --0b0:0 --0b1:-1 -0b0001:1 -0b001:1 -0b011:3 -0b101:5 -0b1001:9 -0b10001:17 -0b100001:33 -0b1000001:65 -0b10000001:129 -0b100000001:257 -0b1000000001:513 -0b10000000001:1025 -0b100000000001:2049 -0b1000000000001:4097 -0b10000000000001:8193 -0b100000000000001:16385 -0b1000000000000001:32769 -0b10000000000000001:65537 -0b100000000000000001:131073 -0b1000000000000000001:262145 -0b10000000000000000001:524289 -0b100000000000000000001:1048577 -0b1000000000000000000001:2097153 -0b10000000000000000000001:4194305 -0b100000000000000000000001:8388609 -0b1000000000000000000000001:16777217 -0b10000000000000000000000001:33554433 -0b100000000000000000000000001:67108865 -0b1000000000000000000000000001:134217729 -0b10000000000000000000000000001:268435457 -0b100000000000000000000000000001:536870913 -0b1000000000000000000000000000001:1073741825 -0b10000000000000000000000000000001:2147483649 -0b100000000000000000000000000000001:4294967297 -0b1000000000000000000000000000000001:8589934593 -0b10000000000000000000000000000000001:17179869185 -0b_101:NaN -0b1_0_1:5 -0b0_0_0_1:1 -# hex input --0x0:0 -0xabcdefgh:NaN -0x1234:4660 -0xabcdef:11259375 --0xABCDEF:-11259375 --0x1234:-4660 -0x12345678:305419896 -0x1_2_3_4_56_78:305419896 -0xa_b_c_d_e_f:11259375 -0x_123:NaN -0x9:9 -0x11:17 -0x21:33 -0x41:65 -0x81:129 -0x101:257 -0x201:513 -0x401:1025 -0x801:2049 -0x1001:4097 -0x2001:8193 -0x4001:16385 -0x8001:32769 -0x10001:65537 -0x20001:131073 -0x40001:262145 -0x80001:524289 -0x100001:1048577 -0x200001:2097153 -0x400001:4194305 -0x800001:8388609 -0x1000001:16777217 -0x2000001:33554433 -0x4000001:67108865 -0x8000001:134217729 -0x10000001:268435457 -0x20000001:536870913 -0x40000001:1073741825 -0x80000001:2147483649 -0x100000001:4294967297 -0x200000001:8589934593 -0x400000001:17179869185 -0x800000001:34359738369 -# bug found by Mark Lakata in Calc.pm creating too big one-element numbers in _from_hex() -0x2dd59e18a125dbed30a6ab1d93e9c855569f44f75806f0645dc9a2e98b808c3:1295719234436071846486578237372801883390756472611551858964079371952886122691 -# inf input -inf:inf -+inf:inf --inf:-inf -0inf:NaN -# abnormal input -:NaN -abc:NaN - 1 a:NaN -1bcd2:NaN -11111b:NaN -+1z:NaN --1z:NaN -# only one underscore between two digits -_123:NaN -_123_:NaN -123_:NaN -1__23:NaN -1E1__2:NaN -1_E12:NaN -1E_12:NaN -1_E_12:NaN -+_1E12:NaN -+0_1E2:100 -+0_0_1E2:100 --0_0_1E2:-100 --0_0_1E+0_0_2:-100 -E1:NaN -E23:NaN -1.23E1:NaN -1.23E-1:NaN -# bug with two E's in number being valid -1e2e3:NaN -1e2r:NaN -1e2.0:NaN -# bug with two '.' in number being valid -1.2.2:NaN -1.2.3e1:NaN --1.2.3:NaN --1.2.3e-4:NaN -1.2e3.4:NaN -1.2e-3.4:NaN -1.2.3.4:NaN -1.2.t:NaN -1..2:NaN -1..2e1:NaN -1..2e1..1:NaN -12e1..1:NaN -..2:NaN -.-2:NaN -# leading zeros -012:12 -0123:123 -01234:1234 -012345:12345 -0123456:123456 -01234567:1234567 -012345678:12345678 -0123456789:123456789 -01234567891:1234567891 -012345678912:12345678912 -0123456789123:123456789123 -01234567891234:1234567891234 -# some inputs that result in zero -0e0:0 -+0e0:0 -+0e+0:0 --0e+0:0 -0e-0:0 --0e-0:0 -+0e-0:0 -000:0 -00e2:0 -00e02:0 -000e002:0 -000e1230:0 -00e-3:0 -00e+3:0 -00e-03:0 -00e+03:0 --000:0 --00e2:0 --00e02:0 --000e002:0 --000e1230:0 --00e-3:0 --00e+3:0 --00e-03:0 --00e+03:0 -# normal input -0:0 -+0:0 -+00:0 -+000:0 -000000000000000000:0 --0:0 --0000:0 -+1:1 -+01:1 -+001:1 -+00000100000:100000 -123456789:123456789 --1:-1 --01:-1 --001:-1 --123456789:-123456789 --00000100000:-100000 -1_2_3:123 -10000000000E-1_0:1 -1E2:100 -1E1:10 -1E0:1 -1.23E2:123 -100E-1:10 -# floating point input -# .2e2:20 -1.E3:1000 -1.01E2:101 -1010E-1:101 --1010E0:-1010 --1010E1:-10100 -1234.00:1234 -# non-integer numbers --1010E-2:NaN --1.01E+1:NaN --1.01E-1:NaN -&bnan -1:NaN -2:NaN -abc:NaN -&bone -2:+:1 -2:-:-1 -boneNaN:-:-1 -boneNaN:+:1 -2:abc:1 -3::1 -&binf -1:+:inf -2:-:-inf -3:abc:inf -&is_nan -123:0 -abc:1 -NaN:1 --123:0 -&is_inf -+inf::1 --inf::1 -abc::0 -1::0 -NaN::0 --1::0 -+inf:-:0 -+inf:+:1 --inf:-:1 --inf:+:0 --inf:-inf:1 --inf:+inf:0 -+inf:-inf:0 -+inf:+inf:1 -# it must be exactly /^[+-]inf$/ -+infinity::0 --infinity::0 -&blsft -abc:abc:NaN -+2:+2:8 -+1:+32:4294967296 -+1:+48:281474976710656 -+8:-2:NaN -# excercise base 10 -+12345:4:10:123450000 --1234:0:10:-1234 -+1234:0:10:1234 -+2:2:10:200 -+12:2:10:1200 -+1234:-3:10:NaN -1234567890123:12:10:1234567890123000000000000 --3:1:2:-6 --5:1:2:-10 --2:1:2:-4 --102533203:1:2:-205066406 -&brsft -abc:abc:NaN -+8:+2:2 -+4294967296:+32:1 -+281474976710656:+48:1 -+2:-2:NaN -# excercise base 10 --1234:0:10:-1234 -+1234:0:10:1234 -+200:2:10:2 -+1234:3:10:1 -+1234:2:10:12 -+1234:-3:10:NaN -310000:4:10:31 -12300000:5:10:123 -1230000000000:10:10:123 -09876123456789067890:12:10:9876123 -1234561234567890123:13:10:123456 -820265627:1:2:410132813 -# test shifting negative numbers in base 2 --15:1:2:-8 --14:1:2:-7 --13:1:2:-7 --12:1:2:-6 --11:1:2:-6 --10:1:2:-5 --9:1:2:-5 --8:1:2:-4 --7:1:2:-4 --6:1:2:-3 --5:1:2:-3 --4:1:2:-2 --3:1:2:-2 --2:1:2:-1 --1:1:2:-1 --1640531254:2:2:-410132814 --1640531254:1:2:-820265627 --820265627:1:2:-410132814 --205066405:1:2:-102533203 -&bsstr -+inf:inf --inf:-inf -1e+34:1e+34 -123.456E3:123456e+0 -100:1e+2 -bsstrabc:NaN --5:-5e+0 --100:-1e+2 -&numify -numifyabc:NaN -+inf:inf --inf:-inf -5:5 --5:-5 -100:100 --100:-100 -&bneg -bnegNaN:NaN -+inf:-inf --inf:inf -abd:NaN -0:0 -1:-1 --1:1 -+123456789:-123456789 --123456789:123456789 -&babs -babsNaN:NaN -+inf:inf --inf:inf -0:0 -1:1 --1:1 -+123456789:123456789 --123456789:123456789 -&bcmp -bcmpNaN:bcmpNaN: -bcmpNaN:0: -0:bcmpNaN: -0:0:0 --1:0:-1 -0:-1:1 -1:0:1 -0:1:-1 --1:1:-1 -1:-1:1 --1:-1:0 -1:1:0 -123:123:0 -123:12:1 -12:123:-1 --123:-123:0 --123:-12:-1 --12:-123:1 -123:124:-1 -124:123:1 --123:-124:1 --124:-123:-1 -100:5:1 --123456789:987654321:-1 -+123456789:-987654321:1 --987654321:123456789:-1 --inf:5432112345:-1 -+inf:5432112345:1 --inf:-5432112345:-1 -+inf:-5432112345:1 -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:1 --inf:+inf:-1 -5:inf:-1 -5:inf:-1 --5:-inf:1 --5:-inf:1 -# return undef -+inf:NaN: -NaN:inf: --inf:NaN: -NaN:-inf: -&binc -abc:NaN -+inf:inf --inf:-inf -+0:1 -+1:2 --1:0 -&bdec -abc:NaN -+inf:inf --inf:-inf -+0:-1 -+1:0 --1:-2 -&badd -abc:abc:NaN -abc:0:NaN -+0:abc:NaN -+inf:-inf:NaN --inf:+inf:NaN -+inf:+inf:inf --inf:-inf:-inf -baddNaN:+inf:NaN -baddNaN:+inf:NaN -+inf:baddNaN:NaN --inf:baddNaN:NaN -0:0:0 -1:0:1 -0:1:1 -1:1:2 --1:0:-1 -0:-1:-1 --1:-1:-2 --1:+1:0 -+1:-1:0 -+9:+1:10 -+99:+1:100 -+999:+1:1000 -+9999:+1:10000 -+99999:+1:100000 -+999999:+1:1000000 -+9999999:+1:10000000 -+99999999:+1:100000000 -+999999999:+1:1000000000 -+9999999999:+1:10000000000 -+99999999999:+1:100000000000 -+10:-1:9 -+100:-1:99 -+1000:-1:999 -+10000:-1:9999 -+100000:-1:99999 -+1000000:-1:999999 -+10000000:-1:9999999 -+100000000:-1:99999999 -+1000000000:-1:999999999 -+10000000000:-1:9999999999 -+123456789:987654321:1111111110 --123456789:987654321:864197532 --123456789:-987654321:-1111111110 -+123456789:-987654321:-864197532 --1:10001:10000 --1:100001:100000 --1:1000001:1000000 --1:10000001:10000000 --1:100000001:100000000 --1:1000000001:1000000000 --1:10000000001:10000000000 --1:100000000001:100000000000 --1:1000000000001:1000000000000 --1:10000000000001:10000000000000 --1:-10001:-10002 --1:-100001:-100002 --1:-1000001:-1000002 --1:-10000001:-10000002 --1:-100000001:-100000002 --1:-1000000001:-1000000002 --1:-10000000001:-10000000002 --1:-100000000001:-100000000002 --1:-1000000000001:-1000000000002 --1:-10000000000001:-10000000000002 -&bsub -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:-inf:inf --inf:+inf:-inf -+inf:+inf:NaN --inf:-inf:NaN -+0:+0:0 -+1:+0:1 -+0:+1:-1 -+1:+1:0 --1:+0:-1 -+0:-1:1 --1:-1:0 --1:+1:-2 -+1:-1:2 -+9:+1:8 -+99:+1:98 -+999:+1:998 -+9999:+1:9998 -+99999:+1:99998 -+999999:+1:999998 -+9999999:+1:9999998 -+99999999:+1:99999998 -+999999999:+1:999999998 -+9999999999:+1:9999999998 -+99999999999:+1:99999999998 -+10:-1:11 -+100:-1:101 -+1000:-1:1001 -+10000:-1:10001 -+100000:-1:100001 -+1000000:-1:1000001 -+10000000:-1:10000001 -+100000000:-1:100000001 -+1000000000:-1:1000000001 -+10000000000:-1:10000000001 -+123456789:+987654321:-864197532 --123456789:+987654321:-1111111110 --123456789:-987654321:864197532 -+123456789:-987654321:1111111110 -10001:1:10000 -100001:1:100000 -1000001:1:1000000 -10000001:1:10000000 -100000001:1:100000000 -1000000001:1:1000000000 -10000000001:1:10000000000 -100000000001:1:100000000000 -1000000000001:1:1000000000000 -10000000000001:1:10000000000000 -10001:-1:10002 -100001:-1:100002 -1000001:-1:1000002 -10000001:-1:10000002 -100000001:-1:100000002 -1000000001:-1:1000000002 -10000000001:-1:10000000002 -100000000001:-1:100000000002 -1000000000001:-1:1000000000002 -10000000000001:-1:10000000000002 -&bmuladd -abc:abc:0:NaN -abc:+0:0:NaN -+0:abc:0:NaN -+0:0:abc:NaN -NaNmul:+inf:0:NaN -NaNmul:-inf:0:NaN --inf:NaNmul:0:NaN -+inf:NaNmul:0:NaN -+inf:+inf:0:inf -+inf:-inf:0:-inf --inf:+inf:0:-inf --inf:-inf:0:inf -+0:+0:0:0 -+0:+1:0:0 -+1:+0:0:0 -+0:-1:0:0 --1:+0:0:0 -123456789123456789:0:0:0 -0:123456789123456789:0:0 --1:-1:0:1 --1:-1:0:1 --1:+1:0:-1 -+1:-1:0:-1 -+1:+1:0:1 -+2:+3:0:6 --2:+3:0:-6 -+2:-3:0:-6 --2:-3:0:6 -111:111:0:12321 -10101:10101:0:102030201 -1001001:1001001:0:1002003002001 -100010001:100010001:0:10002000300020001 -10000100001:10000100001:0:100002000030000200001 -11111111111:9:0:99999999999 -22222222222:9:0:199999999998 -33333333333:9:0:299999999997 -44444444444:9:0:399999999996 -55555555555:9:0:499999999995 -66666666666:9:0:599999999994 -77777777777:9:0:699999999993 -88888888888:9:0:799999999992 -99999999999:9:0:899999999991 -11111111111:9:1:100000000000 -22222222222:9:1:199999999999 -33333333333:9:1:299999999998 -44444444444:9:1:399999999997 -55555555555:9:1:499999999996 -66666666666:9:1:599999999995 -77777777777:9:1:699999999994 -88888888888:9:1:799999999993 -99999999999:9:1:899999999992 --3:-4:-5:7 -3:-4:-5:-17 --3:4:-5:-17 -3:4:-5:7 --3:4:5:-7 -3:-4:5:-7 -9999999999999999999:10000000000000000000:1234567890:99999999999999999990000000001234567890 -2:3:12345678901234567890:12345678901234567896 -&bmul -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -NaNmul:+inf:NaN -NaNmul:-inf:NaN --inf:NaNmul:NaN -+inf:NaNmul:NaN -+inf:+inf:inf -+inf:-inf:-inf --inf:+inf:-inf --inf:-inf:inf -+0:+0:0 -+0:+1:0 -+1:+0:0 -+0:-1:0 --1:+0:0 -123456789123456789:0:0 -0:123456789123456789:0 --1:-1:1 --1:+1:-1 -+1:-1:-1 -+1:+1:1 -+2:+3:6 --2:+3:-6 -+2:-3:-6 --2:-3:6 -111:111:12321 -10101:10101:102030201 -1001001:1001001:1002003002001 -100010001:100010001:10002000300020001 -10000100001:10000100001:100002000030000200001 -11111111111:9:99999999999 -22222222222:9:199999999998 -33333333333:9:299999999997 -44444444444:9:399999999996 -55555555555:9:499999999995 -66666666666:9:599999999994 -77777777777:9:699999999993 -88888888888:9:799999999992 -99999999999:9:899999999991 -+25:+25:625 -+12345:+12345:152399025 -+99999:+11111:1111088889 -9999:10000:99990000 -99999:100000:9999900000 -999999:1000000:999999000000 -9999999:10000000:99999990000000 -99999999:100000000:9999999900000000 -999999999:1000000000:999999999000000000 -9999999999:10000000000:99999999990000000000 -99999999999:100000000000:9999999999900000000000 -999999999999:1000000000000:999999999999000000000000 -9999999999999:10000000000000:99999999999990000000000000 -99999999999999:100000000000000:9999999999999900000000000000 -999999999999999:1000000000000000:999999999999999000000000000000 -9999999999999999:10000000000000000:99999999999999990000000000000000 -99999999999999999:100000000000000000:9999999999999999900000000000000000 -999999999999999999:1000000000000000000:999999999999999999000000000000000000 -9999999999999999999:10000000000000000000:99999999999999999990000000000000000000 -&bdiv-list -100:20:5,0 -4095:4095:1,0 --4095:-4095:1,0 -4095:-4095:-1,0 --4095:4095:-1,0 -123:2:61,1 -9:5:1,4 -9:4:2,1 -# inf handling and general remainder -5:8:0,5 -0:8:0,0 -11:2:5,1 -11:-2:-5,-1 --11:2:-5,1 -# see table in documentation in MBI -0:inf:0,0 -0:-inf:0,0 -5:inf:0,5 -5:-inf:0,5 --5:inf:0,-5 --5:-inf:0,-5 -inf:5:inf,0 --inf:5:-inf,0 -inf:-5:-inf,0 --inf:-5:inf,0 -5:5:1,0 --5:-5:1,0 -inf:inf:NaN,NaN --inf:-inf:NaN,NaN --inf:inf:NaN,NaN -inf:-inf:NaN,NaN -8:0:inf,8 -inf:0:inf,inf -# exceptions to reminder rule --8:0:-inf,-8 --inf:0:-inf,-inf -0:0:NaN,NaN -# test the shortcut in Calc if @$x == @$yorg -1234567812345678:123456712345678:10,688888898 -12345671234567:1234561234567:10,58888897 -123456123456:12345123456:10,4888896 -1234512345:123412345:10,388895 -1234567890999999999:1234567890:1000000000,999999999 -1234567890000000000:1234567890:1000000000,0 -1234567890999999999:9876543210:124999998,9503086419 -1234567890000000000:9876543210:124999998,8503086420 -96969696969696969696969696969678787878626262626262626262626262:484848484848484848484848486666666666666689898989898989898989:199,484848484848484848484848123012121211954972727272727272727451 -# bug in v1.76 -1267650600228229401496703205375:1267650600228229401496703205376:0,1267650600228229401496703205375 -# excercise shortcut for numbers of the same length in div -999999999999999999999999999999999:999999999999999999999999999999999:1,0 -999999999999999999999999999999999:888888888888888888888888888888888:1,111111111111111111111111111111111 -999999999999999999999999999999999:777777777777777777777777777777777:1,222222222222222222222222222222222 -999999999999999999999999999999999:666666666666666666666666666666666:1,333333333333333333333333333333333 -999999999999999999999999999999999:555555555555555555555555555555555:1,444444444444444444444444444444444 -999999999999999999999999999999999:444444444444444444444444444444444:2,111111111111111111111111111111111 -999999999999999999999999999999999:333333333333333333333333333333333:3,0 -999999999999999999999999999999999:222222222222222222222222222222222:4,111111111111111111111111111111111 -999999999999999999999999999999999:111111111111111111111111111111111:9,0 -9999999_9999999_9999999_9999999:3333333_3333333_3333333_3333333:3,0 -9999999_9999999_9999999_9999999:3333333_0000000_0000000_0000000:3,999999999999999999999 -9999999_9999999_9999999_9999999:3000000_0000000_0000000_0000000:3,999999999999999999999999999 -9999999_9999999_9999999_9999999:2000000_0000000_0000000_0000000:4,1999999999999999999999999999 -9999999_9999999_9999999_9999999:1000000_0000000_0000000_0000000:9,999999999999999999999999999 -9999999_9999999_9999999_9999999:100000_0000000_0000000_0000000:99,99999999999999999999999999 -9999999_9999999_9999999_9999999:10000_0000000_0000000_0000000:999,9999999999999999999999999 -9999999_9999999_9999999_9999999:1000_0000000_0000000_0000000:9999,999999999999999999999999 -9999999_9999999_9999999_9999999:100_0000000_0000000_0000000:99999,99999999999999999999999 -9999999_9999999_9999999_9999999:10_0000000_0000000_0000000:999999,9999999999999999999999 -9999999_9999999_9999999_9999999:1_0000000_0000000_0000000:9999999,999999999999999999999 -&bdiv -abc:abc:NaN -abc:1:NaN -1:abc:NaN -0:0:NaN -# inf handling (see table in doc) -0:inf:0 -0:-inf:0 -5:inf:0 -5:-inf:0 --5:inf:0 --5:-inf:0 -inf:5:inf --inf:5:-inf -inf:-5:-inf --inf:-5:inf -5:5:1 --5:-5:1 -inf:inf:NaN --inf:-inf:NaN --inf:inf:NaN -inf:-inf:NaN -8:0:inf -inf:0:inf --8:0:-inf --inf:0:-inf -0:0:NaN -11:2:5 --11:-2:5 --11:2:-5 -11:-2:-5 -0:1:0 -0:-1:0 -1:1:1 --1:-1:1 -1:-1:-1 --1:1:-1 -1:2:0 -2:1:2 -1:26:0 -1000000000:9:111111111 -2000000000:9:222222222 -3000000000:9:333333333 -4000000000:9:444444444 -5000000000:9:555555555 -6000000000:9:666666666 -7000000000:9:777777777 -8000000000:9:888888888 -9000000000:9:1000000000 -35500000:113:314159 -71000000:226:314159 -106500000:339:314159 -1000000000:3:333333333 -+10:+5:2 -+100:+4:25 -+1000:+8:125 -+10000:+16:625 -999999999999:9:111111111111 -999999999999:99:10101010101 -999999999999:999:1001001001 -999999999999:9999:100010001 -999999999999999:99999:10000100001 -+1111088889:99999:11111 --5:-3:1 --5:3:-1 -4:3:1 -4:-3:-1 -1:3:0 -1:-3:0 --2:-3:0 --2:3:0 -8:3:2 --8:3:-2 -14:-3:-4 --14:3:-4 --14:-3:4 -14:3:4 -# bug in Calc with '99999' vs $BASE-1 -10000000000000000000000000000000000000000000000000000000000000000000000000000000000:10000000375084540248994272022843165711074:999999962491547381984643365663244474111576 -# test the shortcut in Calc if @$x == @$yorg -1234567812345678:123456712345678:10 -12345671234567:1234561234567:10 -123456123456:12345123456:10 -1234512345:123412345:10 -1234567890999999999:1234567890:1000000000 -1234567890000000000:1234567890:1000000000 -1234567890999999999:9876543210:124999998 -1234567890000000000:9876543210:124999998 -96969696969696969696969696969678787878626262626262626262626262:484848484848484848484848486666666666666689898989898989898989:199 -# bug up to v0.35 in Calc (--$q one too many) -84696969696969696956565656566184292929292929292847474747436308080808080808086765396464646464646465:13131313131313131313131313131394949494949494949494949494943535353535353535353535:6449999999999999999 -84696969696969696943434343434871161616161616161452525252486813131313131313143230042929292929292930:13131313131313131313131313131394949494949494949494949494943535353535353535353535:6449999999999999998 -84696969696969696969696969697497424242424242424242424242385803030303030303030300750000000000000000:13131313131313131313131313131394949494949494949494949494943535353535353535353535:6450000000000000000 -84696969696969696930303030303558030303030303030057575757537318181818181818199694689393939393939395:13131313131313131313131313131394949494949494949494949494943535353535353535353535:6449999999999999997 -# excercise shortcut for numbers of the same length in div -999999999999999999999999999999999:999999999999999999999999999999999:1 -999999999999999999999999999999999:888888888888888888888888888888888:1 -999999999999999999999999999999999:777777777777777777777777777777777:1 -999999999999999999999999999999999:666666666666666666666666666666666:1 -999999999999999999999999999999999:555555555555555555555555555555555:1 -999999999999999999999999999999999:444444444444444444444444444444444:2 -999999999999999999999999999999999:333333333333333333333333333333333:3 -999999999999999999999999999999999:222222222222222222222222222222222:4 -999999999999999999999999999999999:111111111111111111111111111111111:9 -9999999_9999999_9999999_9999999:3333333_3333333_3333333_3333333:3 -9999999_9999999_9999999_9999999:3333333_0000000_0000000_0000000:3 -9999999_9999999_9999999_9999999:3000000_0000000_0000000_0000000:3 -9999999_9999999_9999999_9999999:2000000_0000000_0000000_0000000:4 -9999999_9999999_9999999_9999999:1000000_0000000_0000000_0000000:9 -9999999_9999999_9999999_9999999:100000_0000000_0000000_0000000:99 -9999999_9999999_9999999_9999999:10000_0000000_0000000_0000000:999 -9999999_9999999_9999999_9999999:1000_0000000_0000000_0000000:9999 -9999999_9999999_9999999_9999999:100_0000000_0000000_0000000:99999 -9999999_9999999_9999999_9999999:10_0000000_0000000_0000000:999999 -9999999_9999999_9999999_9999999:1_0000000_0000000_0000000:9999999 -# bug with shortcut in Calc 0.44 -949418181818187070707070707070707070:181818181853535353535353535353535353:5 -&bmodinv -# format: number:modulus:result -# bmodinv Data errors -abc:abc:NaN -abc:5:NaN -5:abc:NaN -# bmodinv Expected Results from normal use -1:5:1 -3:5:2 --2:5:2 -8:5033:4404 -1234567891:13:6 --1234567891:13:7 -324958749843759385732954874325984357439658735983745:2348249874968739:1741662881064902 -## bmodinv Error cases / useless use of function -3:-5:NaN -inf:5:NaN -5:inf:NaN --inf:5:NaN -5:-inf:NaN -&bmodpow -# format: number:exponent:modulus:result -# bmodpow Data errors -abc:abc:abc:NaN -5:abc:abc:NaN -abc:5:abc:NaN -abc:abc:5:NaN -5:5:abc:NaN -5:abc:5:NaN -abc:5:5:NaN -# bmodpow Expected results -0:0:2:1 -1:0:2:1 -0:0:1:0 -8:7:5032:3840 -8:-1:5033:4404 -98436739867439843769485798542749827593285729587325:43698764986460981048259837659386739857456983759328457:6943857329857295827698367:3104744730915914415259518 -# bmodpow Error cases -8:8:-5:NaN -8:-1:16:NaN -inf:5:13:NaN -5:inf:13:NaN -&bmod -# inf handling, see table in doc -0:inf:0 -0:-inf:0 -5:inf:5 -5:-inf:5 --5:inf:-5 --5:-inf:-5 -inf:5:0 --inf:5:0 -inf:-5:0 --inf:-5:0 -5:5:0 --5:-5:0 -inf:inf:NaN --inf:-inf:NaN --inf:inf:NaN -inf:-inf:NaN -8:0:8 -inf:0:inf -# exceptions to reminder rule --inf:0:-inf --8:0:-8 -0:0:NaN -abc:abc:NaN -abc:1:abc:NaN -1:abc:NaN -0:0:NaN -0:1:0 -1:0:1 -0:-1:0 --1:0:-1 -1:1:0 --1:-1:0 -1:-1:0 --1:1:0 -1:2:1 -2:1:0 -1000000000:9:1 -2000000000:9:2 -3000000000:9:3 -4000000000:9:4 -5000000000:9:5 -6000000000:9:6 -7000000000:9:7 -8000000000:9:8 -9000000000:9:0 -35500000:113:33 -71000000:226:66 -106500000:339:99 -1000000000:3:1 -10:5:0 -100:4:0 -1000:8:0 -10000:16:0 -999999999999:9:0 -999999999999:99:0 -999999999999:999:0 -999999999999:9999:0 -999999999999999:99999:0 --9:+5:1 -+9:-5:-1 --9:-5:-4 --5:3:1 --2:3:1 -4:3:1 -1:3:1 --5:-3:-2 --2:-3:-2 -4:-3:-2 -1:-3:-2 -4095:4095:0 -100041000510123:3:0 -152403346:12345:4321 -9:5:4 -# test shortcuts in Calc -# 1ex % 9 is always == 1, 1ex % 113 is != 1 for x = (4..9), 1ex % 10 = 0 -1234:9:1 -123456:9:3 -12345678:9:0 -1234567891:9:1 -123456789123:9:6 -12345678912345:9:6 -1234567891234567:9:1 -123456789123456789:9:0 -1234:10:4 -123456:10:6 -12345678:10:8 -1234567891:10:1 -123456789123:10:3 -12345678912345:10:5 -1234567891234567:10:7 -123456789123456789:10:9 -1234:113:104 -123456:113:60 -12345678:113:89 -1234567891:113:64 -123456789123:113:95 -12345678912345:113:53 -1234567891234567:113:56 -123456789123456789:113:39 -# bug in bmod() not modifying the variable in place --629:5033:4404 -# bug in bmod() in Calc in the _div_use_div() shortcut code path, -# when X == X and X was big -111111111111111111111111111111:111111111111111111111111111111:0 -12345678901234567890:12345678901234567890:0 -&bgcd -inf:12:NaN --inf:12:NaN -12:inf:NaN -12:-inf:NaN -inf:inf:NaN -inf:-inf:NaN --inf:-inf:NaN -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:0 -+0:+1:1 -+1:+0:1 -+1:+1:1 -+2:+3:1 -+3:+2:1 --3:+2:1 --3:-2:1 --144:-60:12 -144:-60:12 -144:60:12 -100:625:25 -4096:81:1 -1034:804:2 -27:90:56:1 -27:90:54:9 -&blcm -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:NaN -+1:+0:0 -+0:+1:0 -+27:+90:270 -+1034:+804:415668 -&band -abc:abc:NaN -abc:0:NaN -0:abc:NaN -1:2:0 -3:2:2 -+8:+2:0 -+281474976710656:0:0 -+281474976710656:1:0 -+281474976710656:+281474976710656:281474976710656 -281474976710656:-1:281474976710656 --2:-3:-4 --1:-1:-1 --6:-6:-6 --7:-4:-8 --7:4:0 --4:7:4 -# negative argument is bitwise shorter than positive [perl #26559] -30:-3:28 -123:-1:123 -# equal arguments are treated special, so also do some test with unequal ones -0xFFFF:0xFFFF:0x0xFFFF -0xFFFFFF:0xFFFFFF:0x0xFFFFFF -0xFFFFFFFF:0xFFFFFFFF:0x0xFFFFFFFF -0xFFFFFFFFFF:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0xF0F0:0xF0F0:0x0xF0F0 -0x0F0F:0x0F0F:0x0x0F0F -0xF0F0F0:0xF0F0F0:0x0xF0F0F0 -0x0F0F0F:0x0F0F0F:0x0x0F0F0F -0xF0F0F0F0:0xF0F0F0F0:0x0xF0F0F0F0 -0x0F0F0F0F:0x0F0F0F0F:0x0x0F0F0F0F -0xF0F0F0F0F0:0xF0F0F0F0F0:0x0xF0F0F0F0F0 -0x0F0F0F0F0F:0x0F0F0F0F0F:0x0x0F0F0F0F0F -0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0x0xF0F0F0F0F0F0 -0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0x0x0F0F0F0F0F0F -0x1F0F0F0F0F0F:0x3F0F0F0F0F0F:0x0x1F0F0F0F0F0F -&bior -abc:abc:NaN -abc:0:NaN -0:abc:NaN -1:2:3 -+8:+2:10 -+281474976710656:0:281474976710656 -+281474976710656:1:281474976710657 -+281474976710656:281474976710656:281474976710656 --2:-3:-1 --1:-1:-1 --6:-6:-6 --7:4:-3 --4:7:-1 -+281474976710656:-1:-1 -30:-3:-1 -30:-4:-2 -300:-76:-68 --76:300:-68 -# equal arguments are treated special, so also do some test with unequal ones -0xFFFF:0xFFFF:0x0xFFFF -0xFFFFFF:0xFFFFFF:0x0xFFFFFF -0xFFFFFFFF:0xFFFFFFFF:0x0xFFFFFFFF -0xFFFFFFFFFF:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0:0xFFFF:0x0xFFFF -0:0xFFFFFF:0x0xFFFFFF -0:0xFFFFFFFF:0x0xFFFFFFFF -0:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0xFFFF:0:0x0xFFFF -0xFFFFFF:0:0x0xFFFFFF -0xFFFFFFFF:0:0x0xFFFFFFFF -0xFFFFFFFFFF:0:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0:0x0xFFFFFFFFFFFF -0xF0F0:0xF0F0:0x0xF0F0 -0x0F0F:0x0F0F:0x0x0F0F -0xF0F0:0x0F0F:0x0xFFFF -0xF0F0F0:0xF0F0F0:0x0xF0F0F0 -0x0F0F0F:0x0F0F0F:0x0x0F0F0F -0x0F0F0F:0xF0F0F0:0x0xFFFFFF -0xF0F0F0F0:0xF0F0F0F0:0x0xF0F0F0F0 -0x0F0F0F0F:0x0F0F0F0F:0x0x0F0F0F0F -0x0F0F0F0F:0xF0F0F0F0:0x0xFFFFFFFF -0xF0F0F0F0F0:0xF0F0F0F0F0:0x0xF0F0F0F0F0 -0x0F0F0F0F0F:0x0F0F0F0F0F:0x0x0F0F0F0F0F -0x0F0F0F0F0F:0xF0F0F0F0F0:0x0xFFFFFFFFFF -0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0x0xF0F0F0F0F0F0 -0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0x0x0F0F0F0F0F0F -0x0F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF -0x1F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF -&bxor -abc:abc:NaN -abc:0:NaN -0:abc:NaN -1:2:3 -+8:+2:10 -+281474976710656:0:281474976710656 -+281474976710656:1:281474976710657 -+281474976710656:281474976710656:0 --2:-3:3 --1:-1:0 --6:-6:0 --7:4:-3 --4:7:-5 -4:-7:-3 --4:-7:5 -30:-3:-29 -30:-4:-30 -300:-76:-360 --76:300:-360 -# equal arguments are treated special, so also do some test with unequal ones -0xFFFF:0xFFFF:0 -0xFFFFFF:0xFFFFFF:0 -0xFFFFFFFF:0xFFFFFFFF:0 -0xFFFFFFFFFF:0xFFFFFFFFFF:0 -0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0 -0:0xFFFF:0x0xFFFF -0:0xFFFFFF:0x0xFFFFFF -0:0xFFFFFFFF:0x0xFFFFFFFF -0:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0xFFFF:0:0x0xFFFF -0xFFFFFF:0:0x0xFFFFFF -0xFFFFFFFF:0:0x0xFFFFFFFF -0xFFFFFFFFFF:0:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0:0x0xFFFFFFFFFFFF -0xF0F0:0xF0F0:0 -0x0F0F:0x0F0F:0 -0xF0F0:0x0F0F:0x0xFFFF -0xF0F0F0:0xF0F0F0:0 -0x0F0F0F:0x0F0F0F:0 -0x0F0F0F:0xF0F0F0:0x0xFFFFFF -0xF0F0F0F0:0xF0F0F0F0:0 -0x0F0F0F0F:0x0F0F0F0F:0 -0x0F0F0F0F:0xF0F0F0F0:0x0xFFFFFFFF -0xF0F0F0F0F0:0xF0F0F0F0F0:0 -0x0F0F0F0F0F:0x0F0F0F0F0F:0 -0x0F0F0F0F0F:0xF0F0F0F0F0:0x0xFFFFFFFFFF -0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0 -0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0 -0x0F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF -&bnot -abc:NaN -+0:-1 -+8:-9 -+281474976710656:-281474976710657 --1:0 --2:1 --12:11 -&digit -0:0:0 -12:0:2 -12:1:1 -123:0:3 -123:1:2 -123:2:1 -123:-1:1 -123:-2:2 -123:-3:3 -123456:0:6 -123456:1:5 -123456:2:4 -123456:3:3 -123456:4:2 -123456:5:1 -123456:-1:1 -123456:-2:2 -123456:-3:3 -100000:-3:0 -100000:0:0 -100000:1:0 -&mantissa -abc:NaN -1e4:1 -2e0:2 -123:123 --1:-1 --2:-2 -+inf:inf --inf:-inf -&exponent -abc:NaN -1e4:4 -2e0:0 -123:0 --1:0 --2:0 -0:1 -+inf:inf --inf:inf -&parts -abc:NaN,NaN -1e4:1,4 -2e0:2,0 -123:123,0 --1:-1,0 --2:-2,0 -0:0,1 -+inf:inf,inf --inf:-inf,inf -&bfac --1:NaN -NaNfac:NaN -+inf:inf --inf:NaN -0:1 -1:1 -2:2 -3:6 -4:24 -5:120 -6:720 -7:5040 -8:40320 -9:362880 -10:3628800 -11:39916800 -12:479001600 -20:2432902008176640000 -22:1124000727777607680000 -69:171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000 -&bpow -abc:12:NaN -12:abc:NaN -0:0:1 -0:1:0 -0:2:0 -0:-1:inf -0:-2:inf -1:0:1 -1:1:1 -1:2:1 -1:3:1 -1:-1:1 -1:-2:1 -1:-3:1 -2:0:1 -2:1:2 -2:2:4 -2:3:8 -3:3:27 --2:2:4 --2:3:-8 --2:4:16 --2:5:-32 -2:-1:NaN --2:-1:NaN -2:-2:NaN --2:-2:NaN -# inf tests -+inf:1234500012:inf --inf:1234500012:inf --inf:1234500013:-inf -+inf:-12345000123:inf --inf:-12345000123:-inf -# -inf * -inf = inf --inf:2:inf --inf:0:NaN --inf:-1:0 --inf:inf:NaN -2:inf:inf -2:-inf:0 -0:inf:0 -0:-inf:inf --1:-inf:NaN --1:inf:NaN --2:inf:NaN --2:-inf:0 -NaN:inf:NaN -NaN:-inf:NaN --inf:NaN:NaN -inf:NaN:NaN -inf:-inf:NaN -1:inf:1 -1:-inf:1 -# 1 ** -x => 1 / (1 ** x) --1:0:1 --2:0:1 --1:1:-1 --1:2:1 --1:3:-1 --1:4:1 --1:5:-1 --1:-1:-1 --1:-2:1 --1:-3:-1 --1:-4:1 -10:2:100 -10:3:1000 -10:4:10000 -10:5:100000 -10:6:1000000 -10:7:10000000 -10:8:100000000 -10:9:1000000000 -10:20:100000000000000000000 -123456:2:15241383936 --2:2:4 --2:3:-8 --2:4:16 --2:5:-32 --3:2:9 --3:3:-27 --3:4:81 --3:5:-243 -&length -100:3 -10:2 -1:1 -0:1 -12345:5 -10000000000000000:17 --123:3 -215960156869840440586892398248:30 -&broot -# sqrt() -+0:2:0 -+1:2:1 --1:2:NaN -# -$x ** (1/2) => -$y, but not in froot() --123:2:NaN -+inf:2:inf --inf:2:NaN -2:2:1 --2:2:NaN -4:2:2 -9:2:3 -16:2:4 -100:2:10 -123:2:11 -15241:2:123 -144:2:12 -12:2:3 -0.49:2:0 -0.0049:2:0 -# invalid ones -1:NaN:NaN --1:NaN:NaN -0:NaN:NaN --inf:NaN:NaN -+inf:NaN:NaN -NaN:0:NaN -NaN:2:NaN -NaN:inf:NaN -NaN:inf:NaN -12:-inf:NaN -12:inf:NaN -+0:0:NaN -+1:0:NaN --1:0:NaN --2:0:NaN --123.45:0:NaN -+inf:0:NaN -12:1:12 --12:1:NaN -8:-1:NaN --8:-1:NaN -# cubic root -8:3:2 --8:3:NaN -# fourths root -16:4:2 -81:4:3 -# 2 ** 64 -18446744073709551616:4:65536 -18446744073709551616:8:256 -18446744073709551616:16:16 -18446744073709551616:32:4 -18446744073709551616:64:2 -18446744073709551616:128:1 -# 213 ** 15 -84274086103068221283760416414557757:15:213 -# see t/bigroot.t for more tests -&bsqrt -145:12 -144:12 -143:11 -16:4 -170:13 -169:13 -168:12 -4:2 -3:1 -2:1 -9:3 -12:3 -256:16 -100000000:10000 -4000000000000:2000000 -152399026:12345 -152399025:12345 -152399024:12344 -# 2 ** 64 => 2 ** 32 -18446744073709551616:4294967296 -84274086103068221283760416414557757:290299993288095377 -1:1 -0:0 --2:NaN --123:NaN -Nan:NaN -+inf:inf --inf:NaN -# see t/biglog.t for more tests -&bexp -NaN:NaN -inf:inf -1:2 -2:7 -&batan2 -NaN:1:10:NaN -NaN:NaN:10:NaN -1:NaN:10:NaN -inf:1:14:1 --inf:1:14:-1 -0:-inf:14:3 --1:-inf:14:-3 -1:-inf:14:3 -0:inf:14:0 -inf:-inf:14:2 --inf:-inf:14:-2 -# +- 0.78.... -inf:+inf:14:0 --inf:+inf:14:0 -1:5:13:0 -1:5:14:0 -0:0:10:0 -0:1:14:0 -0:2:14:0 -1:0:14:1 -5:0:14:1 --1:0:11:-1 --2:0:77:-1 -2:0:77:1 --1:5:14:0 -1:5:14:0 --1:8:14:0 -1:8:14:0 --1:1:14:0 -&bpi -77:3 -+0:3 -11:3 -# see t/bignok.t for more tests -&bnok -+inf:10:inf -NaN:NaN:NaN -NaN:1:NaN -1:NaN:NaN -1:1:1 -# k > n -1:2:0 -2:3:0 -# k < 0 -1:-2:0 -# 7 over 3 = 35 -7:3:35 -7:6:1 -100:90:17310309456440 -100:95:75287520 -&bround -$round_mode('trunc') -0:12:0 -NaNbround:12:NaN -+inf:12:inf --inf:12:-inf -1234:0:1234 -1234:2:1200 -123456:4:123400 -123456:5:123450 -123456:6:123456 -+10123456789:5:10123000000 --10123456789:5:-10123000000 -+10123456789:9:10123456700 --10123456789:9:-10123456700 -+101234500:6:101234000 --101234500:6:-101234000 -#+101234500:-4:101234000 -#-101234500:-4:-101234000 -$round_mode('zero') -+20123456789:5:20123000000 --20123456789:5:-20123000000 -+20123456789:9:20123456800 --20123456789:9:-20123456800 -+201234500:6:201234000 --201234500:6:-201234000 -#+201234500:-4:201234000 -#-201234500:-4:-201234000 -+12345000:4:12340000 --12345000:4:-12340000 -$round_mode('+inf') -+30123456789:5:30123000000 --30123456789:5:-30123000000 -+30123456789:9:30123456800 --30123456789:9:-30123456800 -+301234500:6:301235000 --301234500:6:-301234000 -#+301234500:-4:301235000 -#-301234500:-4:-301234000 -+12345000:4:12350000 --12345000:4:-12340000 -$round_mode('-inf') -+40123456789:5:40123000000 --40123456789:5:-40123000000 -+40123456789:9:40123456800 --40123456789:9:-40123456800 -+401234500:6:401234000 -+401234500:6:401234000 -#-401234500:-4:-401235000 -#-401234500:-4:-401235000 -+12345000:4:12340000 --12345000:4:-12350000 -$round_mode('odd') -+50123456789:5:50123000000 --50123456789:5:-50123000000 -+50123456789:9:50123456800 --50123456789:9:-50123456800 -+501234500:6:501235000 --501234500:6:-501235000 -#+501234500:-4:501235000 -#-501234500:-4:-501235000 -+12345000:4:12350000 --12345000:4:-12350000 -$round_mode('even') -+60123456789:5:60123000000 --60123456789:5:-60123000000 -+60123456789:9:60123456800 --60123456789:9:-60123456800 -+601234500:6:601234000 --601234500:6:-601234000 -#+601234500:-4:601234000 -#-601234500:-4:-601234000 -#-601234500:-9:0 -#-501234500:-9:0 -#-601234500:-8:0 -#-501234500:-8:0 -+1234567:7:1234567 -+1234567:6:1234570 -+12345000:4:12340000 --12345000:4:-12340000 -$round_mode('common') -+60123456789:5:60123000000 -+60123199999:5:60123000000 -+60123299999:5:60123000000 -+60123399999:5:60123000000 -+60123499999:5:60123000000 -+60123500000:5:60124000000 -+60123600000:5:60124000000 -+60123700000:5:60124000000 -+60123800000:5:60124000000 -+60123900000:5:60124000000 --60123456789:5:-60123000000 --60123199999:5:-60123000000 --60123299999:5:-60123000000 --60123399999:5:-60123000000 --60123499999:5:-60123000000 --60123500000:5:-60124000000 --60123600000:5:-60124000000 --60123700000:5:-60124000000 --60123800000:5:-60124000000 --60123900000:5:-60124000000 -&is_zero -0:1 -NaNzero:0 -+inf:0 --inf:0 -123:0 --1:0 -1:0 -&is_one -0:0 -NaNone:0 -+inf:0 --inf:0 -1:1 -2:0 --1:0 --2:0 -# floor and ceil tests are pretty pointless in integer space...but play safe -&bfloor -0:0 -NaNfloor:NaN -+inf:inf --inf:-inf --1:-1 --2:-2 -2:2 -3:3 -abc:NaN -&bceil -NaNceil:NaN -+inf:inf --inf:-inf -0:0 --1:-1 --2:-2 -2:2 -3:3 -abc:NaN -&as_hex -128:0x80 --128:-0x80 -0:0x0 --0:0x0 -1:0x1 -0x123456789123456789:0x123456789123456789 -+inf:inf --inf:-inf -NaNas_hex:NaN -&as_bin -128:0b10000000 --128:-0b10000000 -0:0b0 --0:0b0 -1:0b1 -0b1010111101010101010110110110110110101:0b1010111101010101010110110110110110101 -0x123456789123456789:0b100100011010001010110011110001001000100100011010001010110011110001001 -+inf:inf --inf:-inf -NaNas_bin:NaN diff --git a/cpan/Math-BigInt/t/bigintpm.t b/cpan/Math-BigInt/t/bigintpm.t deleted file mode 100644 index b4f5bf2fb1..0000000000 --- a/cpan/Math-BigInt/t/bigintpm.t +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/perl -w - -use Test; -use strict; - -BEGIN - { - $| = 1; - unshift @INC, '../lib'; # for running manually - my $location = $0; $location =~ s/bigintpm.t//; - unshift @INC, $location; # to locate the testing files - chdir 't' if -d 't'; - plan tests => 3273 + 6; - } - -use Math::BigInt lib => 'Calc'; - -use vars qw ($scale $class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigInt"; -$CL = "Math::BigInt::Calc"; - -############################################################################# -# from_hex(), from_bin() and from_oct() tests - -my $x = Math::BigInt->from_hex('0xcafe'); -ok ($x, "51966", 'from_hex() works'); - -$x = Math::BigInt->from_hex('0xcafebabedead'); -ok ($x, "223195403574957", 'from_hex() works with long numbers'); - -$x = Math::BigInt->from_bin('0b1001'); -ok ($x, "9", 'from_bin() works'); - -$x = Math::BigInt->from_bin('0b1001100110011001100110011001'); -ok ($x, "161061273", 'from_bin() works with big numbers'); - -$x = Math::BigInt->from_oct('0775'); -ok ($x, "509", 'from_oct() works'); - -$x = Math::BigInt->from_oct('07777777777777711111111222222222'); -ok ($x, "9903520314281112085086151826", 'from_oct() works with big numbers'); - -############################################################################# -# all the other tests - -require 'bigintpm.inc'; # all tests here for sharing diff --git a/cpan/Math-BigInt/t/bigints.t b/cpan/Math-BigInt/t/bigints.t deleted file mode 100644 index de073e21e5..0000000000 --- a/cpan/Math-BigInt/t/bigints.t +++ /dev/null @@ -1,123 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/bigints.t//i; - if ($ENV{PERL_CORE}) - { - @INC = qw(../t/lib); # testing with the core distribution - } - unshift @INC, '../lib'; # for testing manually - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 51; - } - -# testing of Math::BigInt:Scalar (used by the testsuite), -# primarily for interface/api and not for the math functionality - -use Math::BigInt::Scalar; - -my $C = 'Math::BigInt::Scalar'; # pass classname to sub's - -# _new and _str -my $x = $C->_new("123"); my $y = $C->_new("321"); -ok (ref($x),'SCALAR'); ok ($C->_str($x),123); ok ($C->_str($y),321); - -# _add, _sub, _mul, _div - -ok ($C->_str($C->_add($x,$y)),444); -ok ($C->_str($C->_sub($x,$y)),123); -ok ($C->_str($C->_mul($x,$y)),39483); -ok ($C->_str($C->_div($x,$y)),123); - -ok ($C->_str($C->_mul($x,$y)),39483); -ok ($C->_str($x),39483); -ok ($C->_str($y),321); -my $z = $C->_new("2"); -ok ($C->_str($C->_add($x,$z)),39485); -my ($re,$rr) = $C->_div($x,$y); - -ok ($C->_str($re),123); ok ($C->_str($rr),2); - -# is_zero, _is_one, _one, _zero -ok ($C->_is_zero($x),0); -ok ($C->_is_one($x),0); - -ok ($C->_is_one($C->_one()),1); ok ($C->_is_one($C->_zero()),0); -ok ($C->_is_zero($C->_zero()),1); ok ($C->_is_zero($C->_one()),0); - -# is_odd, is_even -ok ($C->_is_odd($C->_one()),1); ok ($C->_is_odd($C->_zero()),0); -ok ($C->_is_even($C->_one()),0); ok ($C->_is_even($C->_zero()),1); - -# _digit -$x = $C->_new("123456789"); -ok ($C->_digit($x,0),9); -ok ($C->_digit($x,1),8); -ok ($C->_digit($x,2),7); -ok ($C->_digit($x,-1),1); -ok ($C->_digit($x,-2),2); -ok ($C->_digit($x,-3),3); - -# _copy -$x = $C->_new("12356"); -ok ($C->_str($C->_copy($x)),12356); - -# _acmp -$x = $C->_new("123456789"); -$y = $C->_new("987654321"); -ok ($C->_acmp($x,$y),-1); -ok ($C->_acmp($y,$x),1); -ok ($C->_acmp($x,$x),0); -ok ($C->_acmp($y,$y),0); - -# _div -$x = $C->_new("3333"); $y = $C->_new("1111"); -ok ($C->_str( scalar $C->_div($x,$y)),3); -$x = $C->_new("33333"); $y = $C->_new("1111"); ($x,$y) = $C->_div($x,$y); -ok ($C->_str($x),30); ok ($C->_str($y),3); -$x = $C->_new("123"); $y = $C->_new("1111"); -($x,$y) = $C->_div($x,$y); ok ($C->_str($x),0); ok ($C->_str($y),123); - -# _num -$x = $C->_new("12345"); $x = $C->_num($x); ok (ref($x)||'',''); ok ($x,12345); - -# _len -$x = $C->_new("12345"); $x = $C->_len($x); ok (ref($x)||'',''); ok ($x,5); - -# _and, _or, _xor -$x = $C->_new("3"); $y = $C->_new("4"); ok ($C->_str( $C->_or($x,$y)),7); -$x = $C->_new("1"); $y = $C->_new("4"); ok ($C->_str( $C->_xor($x,$y)),5); -$x = $C->_new("7"); $y = $C->_new("3"); ok ($C->_str( $C->_and($x,$y)),3); - -# _pow -$x = $C->_new("2"); $y = $C->_new("4"); ok ($C->_str( $C->_pow($x,$y)),16); -$x = $C->_new("2"); $y = $C->_new("5"); ok ($C->_str( $C->_pow($x,$y)),32); -$x = $C->_new("3"); $y = $C->_new("3"); ok ($C->_str( $C->_pow($x,$y)),27); - - -# _check -$x = $C->_new("123456789"); -ok ($C->_check($x),0); -ok ($C->_check(123),'123 is not a reference'); - -# done - -1; - diff --git a/cpan/Math-BigInt/t/biglog.t b/cpan/Math-BigInt/t/biglog.t deleted file mode 100644 index 9478f7634e..0000000000 --- a/cpan/Math-BigInt/t/biglog.t +++ /dev/null @@ -1,213 +0,0 @@ -#!/usr/bin/perl -w - -# Test blog function (and bpow, since it uses blog), as well as bexp(). - -# It is too slow to be simple included in bigfltpm.inc, where it would get -# executed 3 times. One time would be under BareCalc, which shouldn't make any -# difference since there is no CALC->_log() function, and one time under a -# subclass, which *should* work. - -# But it is better to test the numerical functionality, instead of not testing -# it at all (which did lead to wrong answers for 0 < $x < 1 in blog() in -# versions up to v1.63, and for bsqrt($x) when $x << 1 for instance). - -use Test::More; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/biglog.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../lib); - } - unshift @INC, '../lib'; - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 70; - } - -use Math::BigFloat; -use Math::BigInt; - -my $cl = "Math::BigInt"; - -############################################################################# -# test log($n) in BigInt (broken until 1.80) - -is ($cl->new(2)->blog(), '0', "blog(2)"); -is ($cl->new(288)->blog(), '5',"blog(288)"); -is ($cl->new(2000)->blog(), '7', "blog(2000)"); - -############################################################################# -# test exp($n) in BigInt - -is ($cl->new(1)->bexp(), '2', "bexp(1)"); -is ($cl->new(2)->bexp(), '7',"bexp(2)"); -is ($cl->new(3)->bexp(), '20', "bexp(3)"); - -############################################################################# -############################################################################# -# BigFloat tests - -############################################################################# -# test log(2, N) where N > 67 (broken until 1.82) - -$cl = "Math::BigFloat"; - -# These tests can take quite a while, but are nec. Maybe protect them with -# some alarm()? - -# this triggers the calculation and caching of ln(2): -ok ($cl->new(5)->blog(undef,71), -'1.6094379124341003746007593332261876395256013542685177219126478914741790'); - -# if the cache was correct, we should get this result, fast: -ok ($cl->new(2)->blog(undef,71), -'0.69314718055994530941723212145817656807550013436025525412068000949339362'); - -ok ($cl->new(10)->blog(undef,71), -'2.3025850929940456840179914546843642076011014886287729760333279009675726'); - -ok ($cl->new(21)->blog(undef,71), -'3.0445224377234229965005979803657054342845752874046106401940844835750742'); - -############################################################################# - -# These tests are now really fast, since they collapse to blog(10), basically -# Don't attempt to run them with older versions. You are warned. - -# $x < 0 => NaN -ok ($cl->new(-2)->blog(), 'NaN'); -ok ($cl->new(-1)->blog(), 'NaN'); -ok ($cl->new(-10)->blog(), 'NaN'); -ok ($cl->new(-2,2)->blog(), 'NaN'); - -my $ten = $cl->new(10)->blog(); - -# 10 is cached (up to 75 digits) -ok ($cl->new(10)->blog(), '2.302585092994045684017991454684364207601'); - -# 0.1 is using the cached value for log(10), too - -ok ($cl->new(0.1)->blog(), -$ten); -ok ($cl->new(0.01)->blog(), -$ten * 2); -ok ($cl->new(0.001)->blog(), -$ten * 3); -ok ($cl->new(0.0001)->blog(), -$ten * 4); - -# also cached -ok ($cl->new(2)->blog(), '0.6931471805599453094172321214581765680755'); -ok ($cl->new(4)->blog(), $cl->new(2)->blog * 2); - -# These are still slow, so do them only to 10 digits - -ok ($cl->new('0.2')->blog(undef,10), '-1.609437912'); -ok ($cl->new('0.3')->blog(undef,10), '-1.203972804'); -ok ($cl->new('0.4')->blog(undef,10), '-0.9162907319'); -ok ($cl->new('0.5')->blog(undef,10), '-0.6931471806'); -ok ($cl->new('0.6')->blog(undef,10), '-0.5108256238'); -ok ($cl->new('0.7')->blog(undef,10), '-0.3566749439'); -ok ($cl->new('0.8')->blog(undef,10), '-0.2231435513'); -ok ($cl->new('0.9')->blog(undef,10), '-0.1053605157'); - -ok ($cl->new('9')->blog(undef,10), '2.197224577'); - -ok ($cl->new('10')->blog(10,10), '1.000000000'); -ok ($cl->new('20')->blog(20,10), '1.000000000'); -ok ($cl->new('100')->blog(100,10), '1.000000000'); - -ok ($cl->new('100')->blog(10,10), '2.000000000'); # 10 ** 2 == 100 -ok ($cl->new('400')->blog(20,10), '2.000000000'); # 20 ** 2 == 400 - -ok ($cl->new('4')->blog(2,10), '2.000000000'); # 2 ** 2 == 4 -ok ($cl->new('16')->blog(2,10), '4.000000000'); # 2 ** 4 == 16 - -ok ($cl->new('1.2')->bpow('0.3',10), '1.056219968'); -ok ($cl->new('10')->bpow('0.6',10), '3.981071706'); - -# blog should handle bigint input -is (Math::BigFloat::blog(Math::BigInt->new(100),10), 2, "blog(100)"); - -############################################################################# -# some integer results -is ($cl->new(2)->bpow(32)->blog(2), '32', "2 ** 32"); -is ($cl->new(3)->bpow(32)->blog(3), '32', "3 ** 32"); -is ($cl->new(2)->bpow(65)->blog(2), '65', "2 ** 65"); - -my $x = Math::BigInt->new( '777' ) ** 256; -my $base = Math::BigInt->new( '12345678901234' ); -is ($x->copy()->blog($base), 56, 'blog(777**256, 12345678901234)'); - -$x = Math::BigInt->new( '777' ) ** 777; -$base = Math::BigInt->new( '777' ); -is ($x->copy()->blog($base), 777, 'blog(777**777, 777)'); - -############################################################################# -# test for bug in bsqrt() not taking negative _e into account -test_bpow ('200','0.5',10, '14.14213562'); -test_bpow ('20','0.5',10, '4.472135955'); -test_bpow ('2','0.5',10, '1.414213562'); -test_bpow ('0.2','0.5',10, '0.4472135955'); -test_bpow ('0.02','0.5',10, '0.1414213562'); -test_bpow ('0.49','0.5',undef , '0.7'); -test_bpow ('0.49','0.5',10 , '0.7000000000'); -test_bpow ('0.002','0.5',10, '0.04472135955'); -test_bpow ('0.0002','0.5',10, '0.01414213562'); -test_bpow ('0.0049','0.5',undef,'0.07'); -test_bpow ('0.0049','0.5',10 , '0.07000000000'); -test_bpow ('0.000002','0.5',10, '0.001414213562'); -test_bpow ('0.021','0.5',10, '0.1449137675'); -test_bpow ('1.2','0.5',10, '1.095445115'); -test_bpow ('1.23','0.5',10, '1.109053651'); -test_bpow ('12.3','0.5',10, '3.507135583'); - -test_bpow ('9.9','0.5',10, '3.146426545'); -test_bpow ('9.86902225','0.5',10, '3.141500000'); -test_bpow ('9.86902225','0.5',undef, '3.1415'); - -test_bpow ('0.2','0.41',10, '0.5169187652'); - -############################################################################# -# test bexp() with cached results - -is ($cl->new(1)->bexp(), '2.718281828459045235360287471352662497757', 'bexp(1)'); -is ($cl->new(2)->bexp(40), $cl->new(1)->bexp(45)->bpow(2,40), 'bexp(2)'); - -is ($cl->new("12.5")->bexp(61), $cl->new(1)->bexp(65)->bpow(12.5,61), 'bexp(12.5)'); - -############################################################################# -# test bexp() with big values (non-cached) - -is ($cl->new(1)->bexp(100), - '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427', - 'bexp(100)'); - -is ($cl->new("12.5")->bexp(91), $cl->new(1)->bexp(95)->bpow(12.5,91), - 'bexp(12.5) to 91 digits'); - -# all done -1; - -############################################################################# -sub test_bpow - { - my ($x,$y,$scale,$result) = @_; - - print "# Tried: $x->bpow($y,$scale);\n" - unless ok ($cl->new($x)->bpow($y,$scale),$result); - } - - diff --git a/cpan/Math-BigInt/t/bigroot.t b/cpan/Math-BigInt/t/bigroot.t deleted file mode 100644 index 41fee89970..0000000000 --- a/cpan/Math-BigInt/t/bigroot.t +++ /dev/null @@ -1,69 +0,0 @@ -#!/usr/bin/perl -w - -# Test broot function (and bsqrt() function, since it is used by broot()). - -# It is too slow to be simple included in bigfltpm.inc, where it would get -# executed 3 times. - -# But it is better to test the numerical functionality, instead of not testing -# it at all. - -use Test::More; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/bigroot.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../lib); - } - unshift @INC, '../lib'; - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 4 * 2; - } - -use Math::BigFloat; -use Math::BigInt; - -my $cl = "Math::BigFloat"; -my $c = "Math::BigInt"; - -# 2 ** 240 = -# 1766847064778384329583297500742918515827483896875618958121606201292619776 - -# takes way too long -#test_broot ('2','240', 8, undef, '1073741824'); -#test_broot ('2','240', 9, undef, '106528681.3099908308759836475139583940127'); -#test_broot ('2','120', 9, undef, '10321.27324073880096577298929482324664787'); -#test_broot ('2','120', 17, undef, '133.3268493632747279600707813049418888729'); - -test_broot ('2','120', 8, undef, '32768'); -test_broot ('2','60', 8, undef, '181.0193359837561662466161566988413540569'); -test_broot ('2','60', 9, undef, '101.5936673259647663841091609134277286651'); -test_broot ('2','60', 17, undef, '11.54672461623965153271017217302844672562'); - -sub test_broot - { - my ($x,$n,$y,$scale,$result) = @_; - - my $s = $scale || 'undef'; - is ($cl->new($x)->bpow($n)->broot($y,$scale),$result, "Try: $cl $x->bpow($n)->broot($y,$s) == $result"); - $result =~ s/\..*//; - is ($c->new($x)->bpow($n)->broot($y,$scale),$result, "Try: $c $x->bpow($n)->broot($y,$s) == $result"); - } - diff --git a/cpan/Math-BigInt/t/calling.t b/cpan/Math-BigInt/t/calling.t deleted file mode 100644 index 4789cc7357..0000000000 --- a/cpan/Math-BigInt/t/calling.t +++ /dev/null @@ -1,176 +0,0 @@ -#!/usr/bin/perl -w - -# test calling conventions, and :constant overloading - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/calling.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../lib lib); - } - else - { - unshift @INC, '../lib'; - } - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - my $tests = 160; - plan tests => $tests; - } - -package Math::BigInt::Test; - -use Math::BigInt; -use vars qw/@ISA/; -@ISA = qw/Math::BigInt/; # child of MBI -use overload; - -package Math::BigFloat::Test; - -use Math::BigFloat; -use vars qw/@ISA/; -@ISA = qw/Math::BigFloat/; # child of MBI -use overload; - -package main; - -use Math::BigInt try => 'Calc'; -use Math::BigFloat; - -my ($x,$y,$z,$u); -my $version = '1.76'; # adjust manually to match latest release - -############################################################################### -# check whether op's accept normal strings, even when inherited by subclasses - -# do one positive and one negative test to avoid false positives by "accident" - -my ($func,@args,$ans,$rc,$class,$try); -while (<DATA>) - { - $_ =~ s/[\n\r]//g; # remove newlines - next if /^#/; # skip comments - if (s/^&//) - { - $func = $_; - } - else - { - @args = split(/:/,$_,99); - $ans = pop @args; - foreach $class (qw/ - Math::BigInt Math::BigFloat Math::BigInt::Test Math::BigFloat::Test/) - { - $try = "'$args[0]'"; # quote it - $try = $args[0] if $args[0] =~ /'/; # already quoted - $try = '' if $args[0] eq ''; # undef, no argument - $try = "$class\->$func($try);"; - $rc = eval $try; - print "# Tried: '$try'\n" if !ok ($rc, $ans); - } - } - - } - -$class = 'Math::BigInt'; - -# XXX TODO this test does not work/fail. -# test whether use Math::BigInt qw/version/ works -#$try = "use $class ($version.'1');"; -#$try .= ' $x = $class->new(123); $x = "$x";'; -#eval $try; -#ok_undef ( $x ); # should result in error! - -# test whether fallback to calc works -$try = "use $class ($version,'try','foo, bar , ');"; -$try .= "$class\->config()->{lib};"; -$ans = eval $try; -ok ( $ans =~ /^Math::BigInt::(Fast)?Calc\z/, 1); - -# test whether constant works or not, also test for qw($version) -# bgcd() is present in subclass, too -$try = "use Math::BigInt ($version,'bgcd',':constant');"; -$try .= ' $x = 2**150; bgcd($x); $x = "$x";'; -$ans = eval $try; -ok ( $ans, "1427247692705959881058285969449495136382746624"); - -# test wether Math::BigInt::Scalar via use works (w/ dff. spellings of calc) -$try = "use $class ($version,'lib','Scalar');"; -$try .= ' $x = 2**10; $x = "$x";'; -$ans = eval $try; ok ( $ans, "1024"); -$try = "use $class ($version,'lib','$class\::Scalar');"; -$try .= ' $x = 2**10; $x = "$x";'; -$ans = eval $try; ok ( $ans, "1024"); - -# all done - -__END__ -&is_zero -1:0 -0:1 -&is_one -1:1 -0:0 -&is_positive -1:1 --1:0 -&is_negative -1:0 --1:1 -&is_nan -abc:1 -1:0 -&is_inf -inf:1 -0:0 -&bstr -5:5 -10:10 --10:-10 -abc:NaN -'+inf':inf -'-inf':-inf -&bsstr -1:1e+0 -0:0e+1 -2:2e+0 -200:2e+2 --5:-5e+0 --100:-1e+2 -abc:NaN -'+inf':inf -&babs --1:1 -1:1 -&bnot --2:1 -1:-2 -&bzero -:0 -&bnan -:NaN -abc:NaN -&bone -:1 -'+':1 -'-':-1 -&binf -:inf -'+':inf -'-':-inf diff --git a/cpan/Math-BigInt/t/config.t b/cpan/Math-BigInt/t/config.t deleted file mode 100644 index 3bc9d2efee..0000000000 --- a/cpan/Math-BigInt/t/config.t +++ /dev/null @@ -1,136 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More; - -BEGIN - { - $| = 1; - chdir 't' if -d 't'; - unshift @INC, '../lib'; # for running manually - plan tests => 55; - } - -# test whether Math::BigInt->config() and Math::BigFloat->config() works - -use Math::BigInt lib => 'Calc'; -use Math::BigFloat; - -my $mbi = 'Math::BigInt'; my $mbf = 'Math::BigFloat'; - -############################################################################## -# BigInt - -ok ($mbi->can('config')); - -my $cfg = $mbi->config(); - -ok (ref($cfg),'HASH'); - -is ($cfg->{lib},'Math::BigInt::Calc', 'lib'); -is ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION, 'lib_version'); -is ($cfg->{class},$mbi,'class'); -is ($cfg->{upgrade}||'','', 'upgrade'); -is ($cfg->{div_scale},40, 'div_Scale'); - -is ($cfg->{precision}||0,0, 'precision'); # should test for undef -is ($cfg->{accuracy}||0,0,'accuracy'); -is ($cfg->{round_mode},'even','round_mode'); - -is ($cfg->{trap_nan},0, 'trap_nan'); -is ($cfg->{trap_inf},0, 'trap_inf'); - -is ($mbi->config('lib'), 'Math::BigInt::Calc', 'config("lib")'); - -# can set via hash ref? -$cfg = $mbi->config( { trap_nan => 1 } ); -is ($cfg->{trap_nan},1, 'can set via hash ref'); - -# reset for later -$mbi->config( trap_nan => 0 ); - -############################################################################## -# BigFloat - -ok ($mbf->can('config')); - -$cfg = $mbf->config(); - -ok (ref($cfg),'HASH'); - -is ($cfg->{lib},'Math::BigInt::Calc', 'lib'); -is ($cfg->{with},'Math::BigInt::Calc', 'with'); -is ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION, 'lib_version'); -is ($cfg->{class},$mbf,'class'); -is ($cfg->{upgrade}||'','', 'upgrade'); -is ($cfg->{div_scale},40, 'div_Scale'); - -is ($cfg->{precision}||0,0, 'precision'); # should test for undef -is ($cfg->{accuracy}||0,0,'accuracy'); -is ($cfg->{round_mode},'even','round_mode'); - -is ($cfg->{trap_nan},0, 'trap_nan'); -is ($cfg->{trap_inf},0, 'trap_inf'); - -is ($mbf->config('lib'), 'Math::BigInt::Calc', 'config("lib")'); - -# can set via hash ref? -$cfg = $mbf->config( { trap_nan => 1 } ); -is ($cfg->{trap_nan},1, 'can set via hash ref'); - -# reset for later -$mbf->config( trap_nan => 0 ); - -############################################################################## -# test setting values - -my $test = { - trap_nan => 1, - trap_inf => 1, - accuracy => 2, - precision => 3, - round_mode => 'zero', - div_scale => '100', - upgrade => 'Math::BigInt::SomeClass', - downgrade => 'Math::BigInt::SomeClass', - }; - -my $c; - -foreach my $key (keys %$test) - { - # see if setting in MBI works - eval ( "$mbi\->config( $key => '$test->{$key}' );" ); - $c = $mbi->config(); ok ("$key = $c->{$key}", "$key = $test->{$key}"); - $c = $mbf->config(); - # see if setting it in MBI leaves MBF alone - if (($c->{$key}||0) ne $test->{$key}) - { - is (1,1); - } - else - { - is ("$key eq $c->{$key}","$key ne $test->{$key}", "$key"); - } - - # see if setting in MBF works - eval ( "$mbf\->config( $key => '$test->{$key}' );" ); - $c = $mbf->config(); ok ("$key = $c->{$key}", "$key = $test->{$key}"); - } - -############################################################################## -# test setting illegal keys (should croak) - -$@ = ""; my $never_reached = 0; -eval ("$mbi\->config( 'some_garbage' => 1 ); $never_reached = 1;"); -is ($never_reached,0); - -$@ = ""; $never_reached = 0; -eval ("$mbf\->config( 'some_garbage' => 1 ); $never_reached = 1;"); -is ($never_reached,0); - -# this does not work. Why? -#ok ($@ eq "Illegal keys 'some_garbage' passed to Math::BigInt->config() at ./config.t line 104", 1); - -# all tests done - diff --git a/cpan/Math-BigInt/t/const_mbf.t b/cpan/Math-BigInt/t/const_mbf.t deleted file mode 100644 index a73177edb6..0000000000 --- a/cpan/Math-BigInt/t/const_mbf.t +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/perl -w - -# test BigFloat constants alone (w/o BigInt loading) - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/const_mbf.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 2; - } - -use Math::BigFloat ':constant'; - -ok (1.0 / 3.0, '0.3333333333333333333333333333333333333333'); - -# BigInt was not loadede with ':constant', so only floats are handled -ok (ref(2 ** 2),''); - diff --git a/cpan/Math-BigInt/t/constant.t b/cpan/Math-BigInt/t/constant.t deleted file mode 100644 index 3e69bae0f7..0000000000 --- a/cpan/Math-BigInt/t/constant.t +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/constant.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 7; - } - -use Math::BigInt ':constant'; - -ok (2 ** 255,'57896044618658097711785492504343953926634992332820282019728792003956564819968'); - -{ - no warnings 'portable'; # protect against "non-portable" warnings -# hexadecimal constants -ok (0x123456789012345678901234567890, - Math::BigInt->new('0x123456789012345678901234567890')); -# binary constants -ok (0b01010100011001010110110001110011010010010110000101101101, - Math::BigInt->new( - '0b01010100011001010110110001110011010010010110000101101101')); -} - -use Math::BigFloat ':constant'; -ok (1.0 / 3.0, '0.3333333333333333333333333333333333333333'); - -# stress-test Math::BigFloat->import() - -Math::BigFloat->import( qw/:constant/ ); -ok (1,1); - -Math::BigFloat->import( qw/:constant upgrade Math::BigRat/ ); -ok (1,1); - -Math::BigFloat->import( qw/upgrade Math::BigRat :constant/ ); -ok (1,1); - -# all tests done - diff --git a/cpan/Math-BigInt/t/downgrade.t b/cpan/Math-BigInt/t/downgrade.t deleted file mode 100644 index 25d672c50f..0000000000 --- a/cpan/Math-BigInt/t/downgrade.t +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/perl -w - -use Test; -use strict; - -BEGIN - { - $| = 1; - unshift @INC, '../lib'; # for running manually - my $location = $0; $location =~ s/downgrade.t//; - unshift @INC, $location; # to locate the testing files - chdir 't' if -d 't'; - plan tests => 15; - } - -use Math::BigInt upgrade => 'Math::BigFloat'; -use Math::BigFloat downgrade => 'Math::BigInt', upgrade => 'Math::BigInt'; - -use vars qw ($scale $class $try $x $y $f @args $ans $ans1 $ans1_str $setup - $ECL $CL); -$class = "Math::BigInt"; -$CL = "Math::BigInt::Calc"; -$ECL = "Math::BigFloat"; - -# simplistic test for now -ok (Math::BigFloat->downgrade(),'Math::BigInt'); -ok (Math::BigFloat->upgrade(),'Math::BigInt'); - -# these downgrade -ok (ref(Math::BigFloat->new('inf')),'Math::BigInt'); -ok (ref(Math::BigFloat->new('-inf')),'Math::BigInt'); -ok (ref(Math::BigFloat->new('NaN')),'Math::BigInt'); -ok (ref(Math::BigFloat->new('0')),'Math::BigInt'); -ok (ref(Math::BigFloat->new('1')),'Math::BigInt'); -ok (ref(Math::BigFloat->new('10')),'Math::BigInt'); -ok (ref(Math::BigFloat->new('-10')),'Math::BigInt'); -ok (ref(Math::BigFloat->new('-10.0E1')),'Math::BigInt'); - -# bug until v1.67: -ok (Math::BigFloat->new('0.2E0'), '0.2'); -ok (Math::BigFloat->new('0.2E1'), '2'); -# until v1.67 resulted in 200: -ok (Math::BigFloat->new('0.2E2'), '20'); - -# disable, otherwise it screws calculations -Math::BigFloat->upgrade(undef); -ok (Math::BigFloat->upgrade()||'',''); - -Math::BigFloat->div_scale(20); # make it a bit faster -my $x = Math::BigFloat->new(2); # downgrades -# the following test upgrade for bsqrt() and also makes new() NOT downgrade -# for the bpow() side -ok (Math::BigFloat->bpow('2','0.5'),$x->bsqrt()); - -#require 'upgrade.inc'; # all tests here for sharing diff --git a/cpan/Math-BigInt/t/inf_nan.t b/cpan/Math-BigInt/t/inf_nan.t deleted file mode 100644 index 0e5294fe44..0000000000 --- a/cpan/Math-BigInt/t/inf_nan.t +++ /dev/null @@ -1,355 +0,0 @@ -#!/usr/bin/perl -w - -# test inf/NaN handling all in one place -# Thanx to Jarkko for the excellent explanations and the tables - -use Test::More; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/inf_nan.t//i; - if ($ENV{PERL_CORE}) - { - @INC = qw(../t/lib); # testing with the core distribution - } - unshift @INC, '../lib'; # for testing manually - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - # values groups operators classes tests - plan tests => 7 * 6 * 5 * 4 * 2 + - 7 * 6 * 2 * 4 * 1 # bmod -; -# see bottom: + 4 * 10; # 4 classes * 10 NaN == NaN tests - } - -use Math::BigInt; -use Math::BigFloat; -use Math::BigInt::Subclass; -use Math::BigFloat::Subclass; - -my @classes = - qw/Math::BigInt Math::BigFloat - Math::BigInt::Subclass Math::BigFloat::Subclass - /; - -my (@args,$x,$y,$z); - -# + -foreach (qw/ - -inf:-inf:-inf - -1:-inf:-inf - -0:-inf:-inf - 0:-inf:-inf - 1:-inf:-inf - inf:-inf:NaN - NaN:-inf:NaN - - -inf:-1:-inf - -1:-1:-2 - -0:-1:-1 - 0:-1:-1 - 1:-1:0 - inf:-1:inf - NaN:-1:NaN - - -inf:0:-inf - -1:0:-1 - -0:0:0 - 0:0:0 - 1:0:1 - inf:0:inf - NaN:0:NaN - - -inf:1:-inf - -1:1:0 - -0:1:1 - 0:1:1 - 1:1:2 - inf:1:inf - NaN:1:NaN - - -inf:inf:NaN - -1:inf:inf - -0:inf:inf - 0:inf:inf - 1:inf:inf - inf:inf:inf - NaN:inf:NaN - - -inf:NaN:NaN - -1:NaN:NaN - -0:NaN:NaN - 0:NaN:NaN - 1:NaN:NaN - inf:NaN:NaN - NaN:NaN:NaN - /) - { - @args = split /:/,$_; - for my $class (@classes) - { - $x = $class->new($args[0]); - $y = $class->new($args[1]); - $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 - my $r = $x->badd($y); - - is($x->bstr(),$args[2],"x $class $args[0] + $args[1]"); - is($x->bstr(),$args[2],"r $class $args[0] + $args[1]"); - } - } - -# - -foreach (qw/ - -inf:-inf:NaN - -1:-inf:inf - -0:-inf:inf - 0:-inf:inf - 1:-inf:inf - inf:-inf:inf - NaN:-inf:NaN - - -inf:-1:-inf - -1:-1:0 - -0:-1:1 - 0:-1:1 - 1:-1:2 - inf:-1:inf - NaN:-1:NaN - - -inf:0:-inf - -1:0:-1 - -0:0:-0 - 0:0:0 - 1:0:1 - inf:0:inf - NaN:0:NaN - - -inf:1:-inf - -1:1:-2 - -0:1:-1 - 0:1:-1 - 1:1:0 - inf:1:inf - NaN:1:NaN - - -inf:inf:-inf - -1:inf:-inf - -0:inf:-inf - 0:inf:-inf - 1:inf:-inf - inf:inf:NaN - NaN:inf:NaN - - -inf:NaN:NaN - -1:NaN:NaN - -0:NaN:NaN - 0:NaN:NaN - 1:NaN:NaN - inf:NaN:NaN - NaN:NaN:NaN - /) - { - @args = split /:/,$_; - for my $class (@classes) - { - $x = $class->new($args[0]); - $y = $class->new($args[1]); - $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 - my $r = $x->bsub($y); - - is($x->bstr(),$args[2],"x $class $args[0] - $args[1]"); - is($r->bstr(),$args[2],"r $class $args[0] - $args[1]"); - } - } - -# * -foreach (qw/ - -inf:-inf:inf - -1:-inf:inf - -0:-inf:NaN - 0:-inf:NaN - 1:-inf:-inf - inf:-inf:-inf - NaN:-inf:NaN - - -inf:-1:inf - -1:-1:1 - -0:-1:0 - 0:-1:-0 - 1:-1:-1 - inf:-1:-inf - NaN:-1:NaN - - -inf:0:NaN - -1:0:-0 - -0:0:-0 - 0:0:0 - 1:0:0 - inf:0:NaN - NaN:0:NaN - - -inf:1:-inf - -1:1:-1 - -0:1:-0 - 0:1:0 - 1:1:1 - inf:1:inf - NaN:1:NaN - - -inf:inf:-inf - -1:inf:-inf - -0:inf:NaN - 0:inf:NaN - 1:inf:inf - inf:inf:inf - NaN:inf:NaN - - -inf:NaN:NaN - -1:NaN:NaN - -0:NaN:NaN - 0:NaN:NaN - 1:NaN:NaN - inf:NaN:NaN - NaN:NaN:NaN - /) - { - @args = split /:/,$_; - for my $class (@classes) - { - $x = $class->new($args[0]); - $y = $class->new($args[1]); - $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 - $args[2] = '0' if $args[2] eq '-0'; # BigInt hasn't got -0 - my $r = $x->bmul($y); - - is($x->bstr(),$args[2],"x $class $args[0] * $args[1]"); - is($r->bstr(),$args[2],"r $class $args[0] * $args[1]"); - } - } - -# / -foreach (qw/ - -inf:-inf:NaN - -1:-inf:0 - -0:-inf:0 - 0:-inf:-0 - 1:-inf:-0 - inf:-inf:NaN - NaN:-inf:NaN - - -inf:-1:inf - -1:-1:1 - -0:-1:0 - 0:-1:-0 - 1:-1:-1 - inf:-1:-inf - NaN:-1:NaN - - -inf:0:-inf - -1:0:-inf - -0:0:NaN - 0:0:NaN - 1:0:inf - inf:0:inf - NaN:0:NaN - - -inf:1:-inf - -1:1:-1 - -0:1:-0 - 0:1:0 - 1:1:1 - inf:1:inf - NaN:1:NaN - - -inf:inf:NaN - -1:inf:-0 - -0:inf:-0 - 0:inf:0 - 1:inf:0 - inf:inf:NaN - NaN:inf:NaN - - -inf:NaN:NaN - -1:NaN:NaN - -0:NaN:NaN - 0:NaN:NaN - 1:NaN:NaN - inf:NaN:NaN - NaN:NaN:NaN - /) - { - @args = split /:/,$_; - for my $class (@classes) - { - $x = $class->new($args[0]); - $y = $class->new($args[1]); - $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 - - my $t = $x->copy(); - my $tmod = $t->copy(); - - # bdiv in scalar context - my $r = $x->bdiv($y); - is($x->bstr(),$args[2],"x $class $args[0] / $args[1]"); - is($r->bstr(),$args[2],"r $class $args[0] / $args[1]"); - - # bmod and bdiv in list context - my ($d,$rem) = $t->bdiv($y); - - # bdiv in list context - is($t->bstr(),$args[2],"t $class $args[0] / $args[1]"); - is($d->bstr(),$args[2],"d $class $args[0] / $args[1]"); - - # bmod - my $m = $tmod->bmod($y); - - # bmod() agrees with bdiv? - is($m->bstr(),$rem->bstr(),"m $class $args[0] % $args[1]"); - # bmod() return agrees with set value? - is($tmod->bstr(),$m->bstr(),"o $class $args[0] % $args[1]"); - - } - } - -############################################################################# -# overloaded comparisations - -# these are disabled for now, since Perl itself can't seem to make up it's -# mind what NaN actually is, see [perl #33106]. - -# -#foreach my $c (@classes) -# { -# my $x = $c->bnan(); -# my $y = $c->bnan(); # test with two different objects, too -# my $a = $c->bzero(); -# -# is ($x == $y, undef, 'NaN == NaN: undef'); -# is ($x != $y, 1, 'NaN != NaN: 1'); -# -# is ($x == $x, undef, 'NaN == NaN: undef'); -# is ($x != $x, 1, 'NaN != NaN: 1'); -# -# is ($a != $x, 1, '0 != NaN: 1'); -# is ($a == $x, undef, '0 == NaN: undef'); -# -# is ($a < $x, undef, '0 < NaN: undef'); -# is ($a <= $x, undef, '0 <= NaN: undef'); -# is ($a >= $x, undef, '0 >= NaN: undef'); -# is ($a > $x, undef, '0 > NaN: undef'); -# } - -# All done. diff --git a/cpan/Math-BigInt/t/isa.t b/cpan/Math-BigInt/t/isa.t deleted file mode 100644 index adb80f9331..0000000000 --- a/cpan/Math-BigInt/t/isa.t +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/perl -w - -use Test; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/isa.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 7; - } - -use Math::BigInt::Subclass; -use Math::BigFloat::Subclass; -use Math::BigInt; -use Math::BigFloat; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigInt::Subclass"; -$CL = "Math::BigInt::Calc"; - -# Check that a subclass is still considered a BigInt -ok ($class->new(123)->isa('Math::BigInt'),1); - -# ditto for plain Math::BigInt -ok (Math::BigInt->new(123)->isa('Math::BigInt'),1); - -# But Math::BigFloats aren't -ok (Math::BigFloat->new(123)->isa('Math::BigInt') || 0,0); - -# see what happens if we feed a Math::BigFloat into new() -$x = Math::BigInt->new(Math::BigFloat->new(123)); -ok (ref($x),'Math::BigInt'); -ok ($x->isa('Math::BigInt'),1); - -# ditto for subclass -$x = Math::BigInt->new(Math::BigFloat->new(123)); -ok (ref($x),'Math::BigInt'); -ok ($x->isa('Math::BigInt'),1); - diff --git a/cpan/Math-BigInt/t/lib_load.t b/cpan/Math-BigInt/t/lib_load.t deleted file mode 100644 index ff3972effa..0000000000 --- a/cpan/Math-BigInt/t/lib_load.t +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/perl -w - -use Test::More; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/sub_mbf.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, '../lib'; - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 4; - } - -# first load BigInt with Calc -use Math::BigInt lib => 'Calc'; - -# BigFloat will remember that we loaded Calc -require Math::BigFloat; -is (Math::BigFloat::config()->{lib}, 'Math::BigInt::Calc', 'BigFloat got Calc'); - -# now load BigInt again with a different lib -Math::BigInt->import( lib => 'BareCalc' ); - -# and finally test that BigFloat knows about BareCalc - -is (Math::BigFloat::config()->{lib}, 'Math::BigInt::BareCalc', 'BigFloat was notified'); - -# See that Math::BigFloat supports "only" -eval "Math::BigFloat->import('only' => 'Calc')"; -is (Math::BigFloat::config()->{lib}, 'Math::BigInt::Calc', '"only" worked'); - -# See that Math::BigFloat supports "try" -eval "Math::BigFloat->import('try' => 'BareCalc')"; -is (Math::BigFloat::config()->{lib}, 'Math::BigInt::BareCalc', '"try" worked'); - diff --git a/cpan/Math-BigInt/t/mbf_ali.t b/cpan/Math-BigInt/t/mbf_ali.t deleted file mode 100644 index 1ca43157e3..0000000000 --- a/cpan/Math-BigInt/t/mbf_ali.t +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/perl -w - -# test that the new alias names work - -use Test::More; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/mbf_ali.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 6; - } - -use Math::BigFloat; - -use vars qw/$x $CL/; - -$CL = 'Math::BigFloat'; - -require 'alias.inc'; - - diff --git a/cpan/Math-BigInt/t/mbi_ali.t b/cpan/Math-BigInt/t/mbi_ali.t deleted file mode 100644 index 402801733b..0000000000 --- a/cpan/Math-BigInt/t/mbi_ali.t +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/perl -w - -# test that the new alias names work - -use Test::More; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/mbi_ali.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 6; - } - -use Math::BigInt; - -use vars qw/$x $CL/; - -$CL = 'Math::BigInt'; - -require 'alias.inc'; - - diff --git a/cpan/Math-BigInt/t/mbi_rand.t b/cpan/Math-BigInt/t/mbi_rand.t deleted file mode 100644 index e2bf6637de..0000000000 --- a/cpan/Math-BigInt/t/mbi_rand.t +++ /dev/null @@ -1,93 +0,0 @@ -#!/usr/bin/perl -w - -use Test::More; -use strict; - -my $count; - -BEGIN - { - $| = 1; - if ($^O eq 'os390') { print "1..0\n"; exit(0) } # test takes too long there - unshift @INC, '../lib'; # for running manually - my $location = $0; $location =~ s/mbi_rand.t//; - unshift @INC, $location; # to locate the testing files - chdir 't' if -d 't'; - $count = 128; - plan tests => $count*4; - } - -use Math::BigInt; -my $c = 'Math::BigInt'; - -my $length = 128; - -# If you get a failure here, please re-run the test with the printed seed -# value as input "perl t/mbi_rand.t seed" and send me the output - -my $seed = ($#ARGV == 0) ? $ARGV[0] : int(rand(1165537)); -print "# seed: $seed\n"; srand($seed); - -print "# lib: ", Math::BigInt->config()->{lib},"\n"; -if (Math::BigInt->config()->{lib} =~ /::Calc/) - { - print "# base len: ", scalar Math::BigInt::Calc->_base_len(),"\n"; - } - -my ($A,$B,$As,$Bs,$ADB,$AMB,$la,$lb); -my $two = Math::BigInt->new(2); -for (my $i = 0; $i < $count; $i++) - { - # length of A and B - $la = int(rand($length)+1); $lb = int(rand($length)+1); - $As = ''; $Bs = ''; - - # we create the numbers from "patterns", e.g. get a random number and a - # random count and string them together. This means things like - # "100000999999999999911122222222" are much more likely. If we just strung - # together digits, we would end up with "1272398823211223" etc. It also means - # that we get more frequently equal numbers or other special cases. - while (length($As) < $la) { $As .= int(rand(100)) x int(rand(16)); } - while (length($Bs) < $lb) { $Bs .= int(rand(100)) x int(rand(16)); } - - $As =~ s/^0+//; $Bs =~ s/^0+//; - $As = $As || '0'; $Bs = $Bs || '0'; -# print "# As $As\n# Bs $Bs\n"; - $A = $c->new($As); $B = $c->new($Bs); - print "# A $A\n# B $B\n"; - if ($A->is_zero() || $B->is_zero()) - { - for (1..4) { is (1,1, 'skipped this test'); } next; - } - - # check that int(A/B)*B + A % B == A holds for all inputs - - # $X = ($A/$B)*$B + 2 * ($A % $B) - ($A % $B); - - ($ADB,$AMB) = $A->copy()->bdiv($B); - print "# ($A / $B, $A % $B ) = $ADB $AMB\n"; - - print "# seed $seed, ". join(' ',Math::BigInt::Calc->_base_len()),"\n". - "# tried $ADB * $B + $two*$AMB - $AMB\n" - unless is ($ADB*$B+$two*$AMB-$AMB,$As, "ADB * B + 2 * AMB - AMB == A"); - if (is ($ADB*$B/$B,$ADB, "ADB * B / B == ADB")) - { - print "# seed: $seed, \$ADB * \$B / \$B = ", $ADB * $B / $B, " != $ADB (\$B=$B)\n"; - if (Math::BigInt->config()->{lib} =~ /::Calc/) - { - print "# ADB->[-1]: ", $ADB->{value}->[-1], " B->[-1]: ", $B->{value}->[-1],"\n"; - } - } - # swap 'em and try this, too - # $X = ($B/$A)*$A + $B % $A; - ($ADB,$AMB) = $B->copy()->bdiv($A); - # print "check: $ADB $AMB"; - print "# seed $seed, ". join(' ',Math::BigInt::Calc->_base_len()),"\n". - "# tried $ADB * $A + $two*$AMB - $AMB\n" - unless is ($ADB*$A+$two*$AMB-$AMB,$Bs, "ADB * A + 2 * AMB - AMB == B"); - print "# +$two * $AMB = ",$ADB * $A + $two * $AMB,"\n"; - print "# -$AMB = ",$ADB * $A + $two * $AMB - $AMB,"\n"; - print "# seed $seed, \$ADB * \$A / \$A = ", $ADB * $A / $A, " != $ADB (\$A=$A)\n" - unless is ($ADB*$A/$A,$ADB, "ADB * A/A == ADB"); - } - diff --git a/cpan/Math-BigInt/t/mbimbf.inc b/cpan/Math-BigInt/t/mbimbf.inc deleted file mode 100644 index b057eee3ec..0000000000 --- a/cpan/Math-BigInt/t/mbimbf.inc +++ /dev/null @@ -1,967 +0,0 @@ -# test rounding, accuracy, precicion and fallback, round_mode and mixing -# of classes - -# Make sure you always quote any bare floating-point values, lest 123.46 will -# be stringified to 123.4599999999 due to limited float prevision. - -use strict; -my ($x,$y,$z,$u,$rc); - -############################################################################### -# test defaults and set/get - -{ - no strict 'refs'; - ok_undef (${"$mbi\::accuracy"}); - ok_undef (${"$mbi\::precision"}); - ok_undef ($mbi->accuracy()); - ok_undef ($mbi->precision()); - ok (${"$mbi\::div_scale"},40); - ok (${"$mbi\::round_mode"},'even'); - ok ($mbi->round_mode(),'even'); - - ok_undef (${"$mbf\::accuracy"}); - ok_undef (${"$mbf\::precision"}); - ok_undef ($mbf->precision()); - ok_undef ($mbf->precision()); - ok (${"$mbf\::div_scale"},40); - ok (${"$mbf\::round_mode"},'even'); - ok ($mbf->round_mode(),'even'); -} - -# accessors -foreach my $class ($mbi,$mbf) - { - ok_undef ($class->accuracy()); - ok_undef ($class->precision()); - ok ($class->round_mode(),'even'); - ok ($class->div_scale(),40); - - ok ($class->div_scale(20),20); - $class->div_scale(40); ok ($class->div_scale(),40); - - ok ($class->round_mode('odd'),'odd'); - $class->round_mode('even'); ok ($class->round_mode(),'even'); - - ok ($class->accuracy(2),2); - $class->accuracy(3); ok ($class->accuracy(),3); - ok_undef ($class->accuracy(undef)); - - ok ($class->precision(2),2); - ok ($class->precision(-2),-2); - $class->precision(3); ok ($class->precision(),3); - ok_undef ($class->precision(undef)); - } - -{ - no strict 'refs'; - # accuracy - foreach (qw/5 42 -1 0/) - { - ok (${"$mbf\::accuracy"} = $_,$_); - ok (${"$mbi\::accuracy"} = $_,$_); - } - ok_undef (${"$mbf\::accuracy"} = undef); - ok_undef (${"$mbi\::accuracy"} = undef); - - # precision - foreach (qw/5 42 -1 0/) - { - ok (${"$mbf\::precision"} = $_,$_); - ok (${"$mbi\::precision"} = $_,$_); - } - ok_undef (${"$mbf\::precision"} = undef); - ok_undef (${"$mbi\::precision"} = undef); - - # fallback - foreach (qw/5 42 1/) - { - ok (${"$mbf\::div_scale"} = $_,$_); - ok (${"$mbi\::div_scale"} = $_,$_); - } - # illegal values are possible for fallback due to no accessor - - # round_mode - foreach (qw/odd even zero trunc +inf -inf/) - { - ok (${"$mbf\::round_mode"} = $_,$_); - ok (${"$mbi\::round_mode"} = $_,$_); - } - ${"$mbf\::round_mode"} = 'zero'; - ok (${"$mbf\::round_mode"},'zero'); - ok (${"$mbi\::round_mode"},'-inf'); # from above - - # reset for further tests - ${"$mbi\::accuracy"} = undef; - ${"$mbi\::precision"} = undef; - ${"$mbf\::div_scale"} = 40; -} - -# local copies -$x = $mbf->new('123.456'); -ok_undef ($x->accuracy()); -ok ($x->accuracy(5),5); -ok_undef ($x->accuracy(undef),undef); -ok_undef ($x->precision()); -ok ($x->precision(5),5); -ok_undef ($x->precision(undef),undef); - -{ - no strict 'refs'; - # see if MBF changes MBIs values - ok (${"$mbi\::accuracy"} = 42,42); - ok (${"$mbf\::accuracy"} = 64,64); - ok (${"$mbi\::accuracy"},42); # should be still 42 - ok (${"$mbf\::accuracy"},64); # should be now 64 -} - -############################################################################### -# see if creating a number under set A or P will round it - -{ - no strict 'refs'; - ${"$mbi\::accuracy"} = 4; - ${"$mbi\::precision"} = undef; - - ok ($mbi->new(123456),123500); # with A - ${"$mbi\::accuracy"} = undef; - ${"$mbi\::precision"} = 3; - ok ($mbi->new(123456),123000); # with P - - ${"$mbf\::accuracy"} = 4; - ${"$mbf\::precision"} = undef; - ${"$mbi\::precision"} = undef; - - ok ($mbf->new('123.456'),'123.5'); # with A - ${"$mbf\::accuracy"} = undef; - ${"$mbf\::precision"} = -1; - ok ($mbf->new('123.456'),'123.5'); # with P from MBF, not MBI! - - ${"$mbf\::precision"} = undef; # reset -} - -############################################################################### -# see if MBI leaves MBF's private parts alone - -{ - no strict 'refs'; - ${"$mbi\::precision"} = undef; ${"$mbf\::precision"} = undef; - ${"$mbi\::accuracy"} = 4; ${"$mbf\::accuracy"} = undef; - ok ($mbf->new('123.456'),'123.456'); - ${"$mbi\::accuracy"} = undef; # reset -} - -############################################################################### -# see if setting accuracy/precision actually rounds the number - -$x = $mbf->new('123.456'); $x->accuracy(4); ok ($x,'123.5'); -$x = $mbf->new('123.456'); $x->precision(-2); ok ($x,'123.46'); - -$x = $mbi->new(123456); $x->accuracy(4); ok ($x,123500); -$x = $mbi->new(123456); $x->precision(2); ok ($x,123500); - -############################################################################### -# test actual rounding via round() - -$x = $mbf->new('123.456'); -ok ($x->copy()->round(5),'123.46'); -ok ($x->copy()->round(4),'123.5'); -ok ($x->copy()->round(5,2),'NaN'); -ok ($x->copy()->round(undef,-2),'123.46'); -ok ($x->copy()->round(undef,2),120); - -$x = $mbi->new('123'); -ok ($x->round(5,2),'NaN'); - -$x = $mbf->new('123.45000'); -ok ($x->copy()->round(undef,-1,'odd'),'123.5'); - -# see if rounding is 'sticky' -$x = $mbf->new('123.4567'); -$y = $x->copy()->bround(); # no-op since nowhere A or P defined - -ok ($y,123.4567); -$y = $x->copy()->round(5); -ok ($y->accuracy(),5); -ok_undef ($y->precision()); # A has precedence, so P still unset -$y = $x->copy()->round(undef,2); -ok ($y->precision(),2); -ok_undef ($y->accuracy()); # P has precedence, so A still unset - -# see if setting A clears P and vice versa -$x = $mbf->new('123.4567'); -ok ($x,'123.4567'); -ok ($x->accuracy(4),4); -ok ($x->precision(-2),-2); # clear A -ok_undef ($x->accuracy()); - -$x = $mbf->new('123.4567'); -ok ($x,'123.4567'); -ok ($x->precision(-2),-2); -ok ($x->accuracy(4),4); # clear P -ok_undef ($x->precision()); - -# does copy work? -$x = $mbf->new(123.456); $x->accuracy(4); $x->precision(2); -$z = $x->copy(); ok_undef ($z->accuracy(),undef); ok ($z->precision(),2); - -# does $x->bdiv($y,d) work when $d > div_scale? -$x = $mbf->new('0.008'); $x->accuracy(8); - -for my $e ( 4, 8, 16, 32 ) - { - print "# Tried: $x->bdiv(3,$e)\n" - unless ok (scalar $x->copy()->bdiv(3,$e), '0.002' . ('6' x ($e-2)) . '7'); - } - -# does accuracy()/precision work on zeros? -foreach my $c ($mbi,$mbf) - { - $x = $c->bzero(); $x->accuracy(5); ok ($x->{_a},5); - $x = $c->bzero(); $x->precision(5); ok ($x->{_p},5); - $x = $c->new(0); $x->accuracy(5); ok ($x->{_a},5); - $x = $c->new(0); $x->precision(5); ok ($x->{_p},5); - - $x = $c->bzero(); $x->round(5); ok ($x->{_a},5); - $x = $c->bzero(); $x->round(undef,5); ok ($x->{_p},5); - $x = $c->new(0); $x->round(5); ok ($x->{_a},5); - $x = $c->new(0); $x->round(undef,5); ok ($x->{_p},5); - - # see if trying to increasing A in bzero() doesn't do something - $x = $c->bzero(); $x->{_a} = 3; $x->round(5); ok ($x->{_a},3); - } - -############################################################################### -# test whether an opp calls objectify properly or not (or at least does what -# it should do given non-objects, w/ or w/o objectify()) - -foreach my $c ($mbi,$mbf) - { -# ${"$c\::precision"} = undef; # reset -# ${"$c\::accuracy"} = undef; # reset - - ok ($c->new(123)->badd(123),246); - ok ($c->badd(123,321),444); - ok ($c->badd(123,$c->new(321)),444); - - ok ($c->new(123)->bsub(122),1); - ok ($c->bsub(321,123),198); - ok ($c->bsub(321,$c->new(123)),198); - - ok ($c->new(123)->bmul(123),15129); - ok ($c->bmul(123,123),15129); - ok ($c->bmul(123,$c->new(123)),15129); - -# ok ($c->new(15129)->bdiv(123),123); -# ok ($c->bdiv(15129,123),123); -# ok ($c->bdiv(15129,$c->new(123)),123); - - ok ($c->new(15131)->bmod(123),2); - ok ($c->bmod(15131,123),2); - ok ($c->bmod(15131,$c->new(123)),2); - - ok ($c->new(2)->bpow(16),65536); - ok ($c->bpow(2,16),65536); - ok ($c->bpow(2,$c->new(16)),65536); - - ok ($c->new(2**15)->brsft(1),2**14); - ok ($c->brsft(2**15,1),2**14); - ok ($c->brsft(2**15,$c->new(1)),2**14); - - ok ($c->new(2**13)->blsft(1),2**14); - ok ($c->blsft(2**13,1),2**14); - ok ($c->blsft(2**13,$c->new(1)),2**14); - } - -############################################################################### -# test wether operations round properly afterwards -# These tests are not complete, since they do not excercise every "return" -# statement in the op's. But heh, it's better than nothing... - -$x = $mbf->new('123.456'); -$y = $mbf->new('654.321'); -$x->{_a} = 5; # $x->accuracy(5) would round $x straightaway -$y->{_a} = 4; # $y->accuracy(4) would round $x straightaway - -$z = $x + $y; ok ($z,'777.8'); -$z = $y - $x; ok ($z,'530.9'); -$z = $y * $x; ok ($z,'80780'); -$z = $x ** 2; ok ($z,'15241'); -$z = $x * $x; ok ($z,'15241'); - -# not: $z = -$x; ok ($z,'-123.46'); ok ($x,'123.456'); -$z = $x->copy(); $z->{_a} = 2; $z = $z / 2; ok ($z,62); -$x = $mbf->new(123456); $x->{_a} = 4; -$z = $x->copy; $z++; ok ($z,123500); - -$x = $mbi->new(123456); -$y = $mbi->new(654321); -$x->{_a} = 5; # $x->accuracy(5) would round $x straightaway -$y->{_a} = 4; # $y->accuracy(4) would round $x straightaway - -$z = $x + $y; ok ($z,777800); -$z = $y - $x; ok ($z,530900); -$z = $y * $x; ok ($z,80780000000); -$z = $x ** 2; ok ($z,15241000000); -# not yet: $z = -$x; ok ($z,-123460); ok ($x,123456); -$z = $x->copy; $z++; ok ($z,123460); -$z = $x->copy(); $z->{_a} = 2; $z = $z / 2; ok ($z,62000); - -$x = $mbi->new(123400); $x->{_a} = 4; -ok ($x->bnot(),-123400); # not -1234001 - -# both babs() and bneg() don't need to round, since the input will already -# be rounded (either as $x or via new($string)), and they don't change the -# value. The two tests below peek at this by using _a (illegally) directly -$x = $mbi->new(-123401); $x->{_a} = 4; ok ($x->babs(),123401); -$x = $mbi->new(-123401); $x->{_a} = 4; ok ($x->bneg(),123401); - -# test fdiv rounding to A and R (bug in v1.48 and maybe earlier versions) -$mbf->round_mode('even'); -$x = $mbf->new('740.7')->fdiv('6',4,undef,'zero'); ok ($x,'123.4'); - -$x = $mbi->new('123456'); $y = $mbi->new('123456'); $y->{_a} = 6; -ok ($x->bdiv($y),1); ok ($x->{_a},6); # carried over - -$x = $mbi->new('123456'); $y = $mbi->new('123456'); $x->{_a} = 6; -ok ($x->bdiv($y),1); ok ($x->{_a},6); # carried over - -$x = $mbi->new('123456'); $y = $mbi->new('223456'); $y->{_a} = 6; -ok ($x->bdiv($y),0); ok ($x->{_a},6); # carried over - -$x = $mbi->new('123456'); $y = $mbi->new('223456'); $x->{_a} = 6; -ok ($x->bdiv($y),0); ok ($x->{_a},6); # carried over - -############################################################################### -# test that bop(0) does the same than bop(undef) - -$x = $mbf->new('1234567890'); -ok ($x->copy()->bsqrt(0),$x->copy()->bsqrt(undef)); -ok ($x->copy->bsqrt(0),'35136.41828644462161665823116758077037159'); - -ok_undef ($x->{_a}); - -# test that bsqrt() modifies $x and does not just return something else -# (especially under BareCalc) -$z = $x->bsqrt(); -ok ($z,$x); ok ($x,'35136.41828644462161665823116758077037159'); - -$x = $mbf->new('1.234567890123456789'); -ok ($x->copy()->bpow('0.5',0),$x->copy()->bpow('0.5',undef)); -ok ($x->copy()->bpow('0.5',0),$x->copy()->bsqrt(undef)); -ok ($x->copy()->bpow('2',0),'1.524157875323883675019051998750190521'); - -############################################################################### -# test (also under Bare) that bfac() rounds at last step - -ok ($mbi->new(12)->bfac(),'479001600'); -ok ($mbi->new(12)->bfac(2),'480000000'); -$x = $mbi->new(12); $x->accuracy(2); ok ($x->bfac(),'480000000'); -$x = $mbi->new(13); $x->accuracy(2); ok ($x->bfac(),'6200000000'); -$x = $mbi->new(13); $x->accuracy(3); ok ($x->bfac(),'6230000000'); -$x = $mbi->new(13); $x->accuracy(4); ok ($x->bfac(),'6227000000'); -# this does 1,2,3...9,10,11,12...20 -$x = $mbi->new(20); $x->accuracy(1); ok ($x->bfac(),'2000000000000000000'); - -############################################################################### -# test bsqrt) rounding to given A/P/R (bug prior to v1.60) -$x = $mbi->new('123456')->bsqrt(2,undef); ok ($x,'350'); # not 351 -$x = $mbi->new('3')->bsqrt(2,undef); ok ($x->accuracy(),2); - -$mbi->round_mode('even'); $x = $mbi->new('126025')->bsqrt(2,undef,'+inf'); -ok ($x,'360'); # not 355 nor 350 - -$x = $mbi->new('126025')->bsqrt(undef,2); ok ($x,'400'); # not 355 - - -############################################################################### -# test mixed arguments - -$x = $mbf->new(10); -$u = $mbf->new(2.5); -$y = $mbi->new(2); - -$z = $x + $y; ok ($z,12); ok (ref($z),$mbf); -$z = $x / $y; ok ($z,5); ok (ref($z),$mbf); -$z = $u * $y; ok ($z,5); ok (ref($z),$mbf); - -$y = $mbi->new(12345); -$z = $u->copy()->bmul($y,2,undef,'odd'); ok ($z,31000); -$z = $u->copy()->bmul($y,3,undef,'odd'); ok ($z,30900); -$z = $u->copy()->bmul($y,undef,0,'odd'); ok ($z,30863); -$z = $u->copy()->bmul($y,undef,1,'odd'); ok ($z,30863); -$z = $u->copy()->bmul($y,undef,2,'odd'); ok ($z,30860); -$z = $u->copy()->bmul($y,undef,3,'odd'); ok ($z,30900); -$z = $u->copy()->bmul($y,undef,-1,'odd'); ok ($z,30862.5); - -my $warn = ''; $SIG{__WARN__} = sub { $warn = shift; }; -# these should no longer warn, even tho '3.17' is a NaN in BigInt (>= returns -# now false, bug until v1.80) -$warn = ''; eval "\$z = 3.17 <= \$y"; ok ($z, ''); -print "# Got: '$warn'\n" unless -ok ($warn !~ /^Use of uninitialized value (\$y )?(in numeric le \(<=\) |)at/); -$warn = ''; eval "\$z = \$y >= 3.17"; ok ($z, ''); -print "# Got: '$warn'\n" unless -ok ($warn !~ /^Use of uninitialized value (\$y )?(in numeric ge \(>=\) |)at/); - -# XXX TODO breakage: -# $z = $y->copy()->bmul($u,2,0,'odd'); ok ($z,31000); -# $z = $y * $u; ok ($z,5); ok (ref($z),$mbi); -# $z = $y + $x; ok ($z,12); ok (ref($z),$mbi); -# $z = $y / $x; ok ($z,0); ok (ref($z),$mbi); - -############################################################################### -# rounding in bdiv with fallback and already set A or P - -{ - no strict 'refs'; - ${"$mbf\::accuracy"} = undef; - ${"$mbf\::precision"} = undef; - ${"$mbf\::div_scale"} = 40; -} - - $x = $mbf->new(10); $x->{_a} = 4; - ok ($x->bdiv(3),'3.333'); - ok ($x->{_a},4); # set's it since no fallback - -$x = $mbf->new(10); $x->{_a} = 4; $y = $mbf->new(3); -ok ($x->bdiv($y),'3.333'); -ok ($x->{_a},4); # set's it since no fallback - -# rounding to P of x -$x = $mbf->new(10); $x->{_p} = -2; -ok ($x->bdiv(3),'3.33'); - -# round in div with requested P -$x = $mbf->new(10); -ok ($x->bdiv(3,undef,-2),'3.33'); - -# round in div with requested P greater than fallback -{ - no strict 'refs'; - ${"$mbf\::div_scale"} = 5; - $x = $mbf->new(10); - ok ($x->bdiv(3,undef,-8),'3.33333333'); - ${"$mbf\::div_scale"} = 40; -} - -$x = $mbf->new(10); $y = $mbf->new(3); $y->{_a} = 4; -ok ($x->bdiv($y),'3.333'); -ok ($x->{_a},4); ok ($y->{_a},4); # set's it since no fallback -ok_undef ($x->{_p}); ok_undef ($y->{_p}); - -# rounding to P of y -$x = $mbf->new(10); $y = $mbf->new(3); $y->{_p} = -2; -ok ($x->bdiv($y),'3.33'); -ok ($x->{_p},-2); - ok ($y->{_p},-2); -ok_undef ($x->{_a}); ok_undef ($y->{_a}); - -############################################################################### -# test whether bround(-n) fails in MBF (undocumented in MBI) -eval { $x = $mbf->new(1); $x->bround(-2); }; -ok ($@ =~ /^bround\(\) needs positive accuracy/,1); - -# test whether rounding to higher accuracy is no-op -$x = $mbf->new(1); $x->{_a} = 4; -ok ($x,'1.000'); -$x->bround(6); # must be no-op -ok ($x->{_a},4); -ok ($x,'1.000'); - -$x = $mbi->new(1230); $x->{_a} = 3; -ok ($x,'1230'); -$x->bround(6); # must be no-op -ok ($x->{_a},3); -ok ($x,'1230'); - -# bround(n) should set _a -$x->bround(2); # smaller works -ok ($x,'1200'); -ok ($x->{_a},2); - -# bround(-n) is undocumented and only used by MBF -# bround(-n) should set _a -$x = $mbi->new(12345); -$x->bround(-1); -ok ($x,'12300'); -ok ($x->{_a},4); - -# bround(-n) should set _a -$x = $mbi->new(12345); -$x->bround(-2); -ok ($x,'12000'); -ok ($x->{_a},3); - -# bround(-n) should set _a -$x = $mbi->new(12345); $x->{_a} = 5; -$x->bround(-3); -ok ($x,'10000'); -ok ($x->{_a},2); - -# bround(-n) should set _a -$x = $mbi->new(12345); $x->{_a} = 5; -$x->bround(-4); -ok ($x,'0'); -ok ($x->{_a},1); - -# bround(-n) should be noop if n too big -$x = $mbi->new(12345); -$x->bround(-5); -ok ($x,'0'); # scale to "big" => 0 -ok ($x->{_a},0); - -# bround(-n) should be noop if n too big -$x = $mbi->new(54321); -$x->bround(-5); -ok ($x,'100000'); # used by MBF to round 0.0054321 at 0.0_6_00000 -ok ($x->{_a},0); - -# bround(-n) should be noop if n too big -$x = $mbi->new(54321); $x->{_a} = 5; -$x->bround(-6); -ok ($x,'100000'); # no-op -ok ($x->{_a},0); - -# bround(n) should set _a -$x = $mbi->new(12345); $x->{_a} = 5; -$x->bround(5); # must be no-op -ok ($x,'12345'); -ok ($x->{_a},5); - -# bround(n) should set _a -$x = $mbi->new(12345); $x->{_a} = 5; -$x->bround(6); # must be no-op -ok ($x,'12345'); - -$x = $mbf->new('0.0061'); $x->bfround(-2); ok ($x,'0.01'); -$x = $mbf->new('0.004'); $x->bfround(-2); ok ($x,'0.00'); -$x = $mbf->new('0.005'); $x->bfround(-2); ok ($x,'0.00'); - -$x = $mbf->new('12345'); $x->bfround(2); ok ($x,'12340'); -$x = $mbf->new('12340'); $x->bfround(2); ok ($x,'12340'); - -# MBI::bfround should clear A for negative P -$x = $mbi->new('1234'); $x->accuracy(3); $x->bfround(-2); -ok_undef ($x->{_a}); - -# test that bfround() and bround() work with large numbers - -$x = $mbf->new(1)->bdiv(5678,undef,-63); -ok ($x, '0.000176118351532229658330398027474462839027826699542092286016203'); - -$x = $mbf->new(1)->bdiv(5678,undef,-90); -ok ($x, '0.000176118351532229658330398027474462839027826699542092286016202888340965128566396618527651'); - -$x = $mbf->new(1)->bdiv(5678,80); -ok ($x, '0.00017611835153222965833039802747446283902782669954209228601620288834096512856639662'); - -############################################################################### -# rounding with already set precision/accuracy - -$x = $mbf->new(1); $x->{_p} = -5; -ok ($x,'1.00000'); - -# further rounding donw -ok ($x->bfround(-2),'1.00'); -ok ($x->{_p},-2); - -$x = $mbf->new(12345); $x->{_a} = 5; -ok ($x->bround(2),'12000'); -ok ($x->{_a},2); - -$x = $mbf->new('1.2345'); $x->{_a} = 5; -ok ($x->bround(2),'1.2'); -ok ($x->{_a},2); - -# mantissa/exponent format and A/P -$x = $mbf->new('12345.678'); $x->accuracy(4); -ok ($x,'12350'); ok ($x->{_a},4); ok_undef ($x->{_p}); - -#ok_undef ($x->{_m}->{_a}); ok_undef ($x->{_e}->{_a}); -#ok_undef ($x->{_m}->{_p}); ok_undef ($x->{_e}->{_p}); - -# check for no A/P in case of fallback -# result -$x = $mbf->new(100) / 3; -ok_undef ($x->{_a}); ok_undef ($x->{_p}); - -# result & reminder -$x = $mbf->new(100) / 3; ($x,$y) = $x->bdiv(3); -ok_undef ($x->{_a}); ok_undef ($x->{_p}); -ok_undef ($y->{_a}); ok_undef ($y->{_p}); - -############################################################################### -# math with two numbers with differen A and P - -$x = $mbf->new(12345); $x->accuracy(4); # '12340' -$y = $mbf->new(12345); $y->accuracy(2); # '12000' -ok ($x+$y,24000); # 12340+12000=> 24340 => 24000 - -$x = $mbf->new(54321); $x->accuracy(4); # '12340' -$y = $mbf->new(12345); $y->accuracy(3); # '12000' -ok ($x-$y,42000); # 54320+12300=> 42020 => 42000 - -$x = $mbf->new('1.2345'); $x->precision(-2); # '1.23' -$y = $mbf->new('1.2345'); $y->precision(-4); # '1.2345' -ok ($x+$y,'2.46'); # 1.2345+1.2300=> 2.4645 => 2.46 - -############################################################################### -# round should find and use proper class - -#$x = Foo->new(); -#ok ($x->round($Foo::accuracy),'a' x $Foo::accuracy); -#ok ($x->round(undef,$Foo::precision),'p' x $Foo::precision); -#ok ($x->bfround($Foo::precision),'p' x $Foo::precision); -#ok ($x->bround($Foo::accuracy),'a' x $Foo::accuracy); - -############################################################################### -# find out whether _find_round_parameters is doing what's it's supposed to do - -{ - no strict 'refs'; - ${"$mbi\::accuracy"} = undef; - ${"$mbi\::precision"} = undef; - ${"$mbi\::div_scale"} = 40; - ${"$mbi\::round_mode"} = 'odd'; -} - -$x = $mbi->new(123); -my @params = $x->_find_round_parameters(); -ok (scalar @params,1); # nothing to round - -@params = $x->_find_round_parameters(1); -ok (scalar @params,4); # a=1 -ok ($params[0],$x); # self -ok ($params[1],1); # a -ok_undef ($params[2]); # p -ok ($params[3],'odd'); # round_mode - -@params = $x->_find_round_parameters(undef,2); -ok (scalar @params,4); # p=2 -ok ($params[0],$x); # self -ok_undef ($params[1]); # a -ok ($params[2],2); # p -ok ($params[3],'odd'); # round_mode - -eval { @params = $x->_find_round_parameters(undef,2,'foo'); }; -ok ($@ =~ /^Unknown round mode 'foo'/,1); - -@params = $x->_find_round_parameters(undef,2,'+inf'); -ok (scalar @params,4); # p=2 -ok ($params[0],$x); # self -ok_undef ($params[1]); # a -ok ($params[2],2); # p -ok ($params[3],'+inf'); # round_mode - -@params = $x->_find_round_parameters(2,-2,'+inf'); -ok (scalar @params,1); # error, A and P defined -ok ($params[0],$x); # self - -{ - no strict 'refs'; - ${"$mbi\::accuracy"} = 1; - @params = $x->_find_round_parameters(undef,-2); - ok (scalar @params,1); # error, A and P defined - ok ($params[0],$x); # self - ok ($x->is_nan(),1); # and must be NaN - - ${"$mbi\::accuracy"} = undef; - ${"$mbi\::precision"} = 1; - @params = $x->_find_round_parameters(1,undef); - ok (scalar @params,1); # error, A and P defined - ok ($params[0],$x); # self - ok ($x->is_nan(),1); # and must be NaN - - ${"$mbi\::precision"} = undef; # reset -} - -############################################################################### -# test whether bone/bzero take additional A & P, or reset it etc - -foreach my $c ($mbi,$mbf) - { - $x = $c->new(2)->bzero(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); - $x = $c->new(2)->bone(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); - $x = $c->new(2)->binf(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); - $x = $c->new(2)->bnan(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); - - $x = $c->new(2); $x->{_a} = 1; $x->{_p} = 2; $x->bnan(); - ok_undef ($x->{_a}); ok_undef ($x->{_p}); - $x = $c->new(2); $x->{_a} = 1; $x->{_p} = 2; $x->binf(); - ok_undef ($x->{_a}); ok_undef ($x->{_p}); - - $x = $c->new(2,1); ok ($x->{_a},1); ok_undef ($x->{_p}); - $x = $c->new(2,undef,1); ok_undef ($x->{_a}); ok ($x->{_p},1); - - $x = $c->new(2,1)->bzero(); ok ($x->{_a},1); ok_undef ($x->{_p}); - $x = $c->new(2,undef,1)->bzero(); ok_undef ($x->{_a}); ok ($x->{_p},1); - - $x = $c->new(2,1)->bone(); ok ($x->{_a},1); ok_undef ($x->{_p}); - $x = $c->new(2,undef,1)->bone(); ok_undef ($x->{_a}); ok ($x->{_p},1); - - $x = $c->new(2); $x->bone('+',2,undef); ok ($x->{_a},2); ok_undef ($x->{_p}); - $x = $c->new(2); $x->bone('+',undef,2); ok_undef ($x->{_a}); ok ($x->{_p},2); - $x = $c->new(2); $x->bone('-',2,undef); ok ($x->{_a},2); ok_undef ($x->{_p}); - $x = $c->new(2); $x->bone('-',undef,2); ok_undef ($x->{_a}); ok ($x->{_p},2); - - $x = $c->new(2); $x->bzero(2,undef); ok ($x->{_a},2); ok_undef ($x->{_p}); - $x = $c->new(2); $x->bzero(undef,2); ok_undef ($x->{_a}); ok ($x->{_p},2); - } - -############################################################################### -# test whether bone/bzero honour globals - -for my $c ($mbi,$mbf) - { - $c->accuracy(2); - $x = $c->bone(); ok ($x->accuracy(),2); - $x = $c->bzero(); ok ($x->accuracy(),2); - $c->accuracy(undef); - - $c->precision(-2); - $x = $c->bone(); ok ($x->precision(),-2); - $x = $c->bzero(); ok ($x->precision(),-2); - $c->precision(undef); - } - -############################################################################### -# check whether mixing A and P creates a NaN - -# new with set accuracy/precision and with parameters -{ - no strict 'refs'; - foreach my $c ($mbi,$mbf) - { - ok ($c->new(123,4,-3),'NaN'); # with parameters - ${"$c\::accuracy"} = 42; - ${"$c\::precision"} = 2; - ok ($c->new(123),'NaN'); # with globals - ${"$c\::accuracy"} = undef; - ${"$c\::precision"} = undef; - } -} - -# binary ops -foreach my $class ($mbi,$mbf) - { - foreach (qw/add sub mul pow mod/) - #foreach (qw/add sub mul div pow mod/) - { - my $try = "my \$x = $class->new(1234); \$x->accuracy(5); "; - $try .= "my \$y = $class->new(12); \$y->precision(-3); "; - $try .= "\$x->b$_(\$y);"; - $rc = eval $try; - print "# Tried: '$try'\n" if !ok ($rc, 'NaN'); - } - } - -# unary ops -foreach (qw/new bsqrt/) - { - my $try = 'my $x = $mbi->$_(1234,5,-3); '; - $rc = eval $try; - print "# Tried: '$try'\n" if !ok ($rc, 'NaN'); - } - -# see if $x->bsub(0) and $x->badd(0) really round -foreach my $class ($mbi,$mbf) - { - $x = $class->new(123); $class->accuracy(2); $x->bsub(0); - ok ($x,120); - $class->accuracy(undef); - $x = $class->new(123); $class->accuracy(2); $x->badd(0); - ok ($x,120); - $class->accuracy(undef); - } - -############################################################################### -# test whether shortcuts returning zero/one preserve A and P - -my ($ans1,$f,$a,$p,$xp,$yp,$xa,$ya,$try,$ans,@args); -my $CALC = Math::BigInt->config()->{lib}; -while (<DATA>) - { - $_ =~ s/[\n\r]//g; # remove newlines - next if /^\s*(#|$)/; # skip comments and empty lines - if (s/^&//) - { - $f = $_; next; # function - } - @args = split(/:/,$_,99); - my $ans = pop(@args); - - ($x,$xa,$xp) = split (/,/,$args[0]); - $xa = $xa || ''; $xp = $xp || ''; - $try = "\$x = $mbi->new('$x'); "; - $try .= "\$x->accuracy($xa); " if $xa ne ''; - $try .= "\$x->precision($xp); " if $xp ne ''; - - ($y,$ya,$yp) = split (/,/,$args[1]); - $ya = $ya || ''; $yp = $yp || ''; - $try .= "\$y = $mbi->new('$y'); "; - $try .= "\$y->accuracy($ya); " if $ya ne ''; - $try .= "\$y->precision($yp); " if $yp ne ''; - - $try .= "\$x->$f(\$y);"; - - # print "trying $try\n"; - $rc = eval $try; - # convert hex/binary targets to decimal - if ($ans =~ /^(0x0x|0b0b)/) - { - $ans =~ s/^0[xb]//; - $ans = $mbi->new($ans)->bstr(); - } - print "# Tried: '$try'\n" if !ok ($rc, $ans); - # check internal state of number objects - is_valid($rc,$f) if ref $rc; - - # now check whether A and P are set correctly - # only one of $a or $p will be set (no crossing here) - $a = $xa || $ya; $p = $xp || $yp; - - # print "Check a=$a p=$p\n"; - # print "# Tried: '$try'\n"; - if ($a ne '') - { - if (!(ok ($x->{_a}, $a) && ok_undef ($x->{_p}))) - { - print "# Check: A=$a and P=undef\n"; - print "# Tried: '$try'\n"; - } - } - if ($p ne '') - { - if (!(ok ($x->{_p}, $p) && ok_undef ($x->{_a}))) - { - print "# Check: A=undef and P=$p\n"; - print "# Tried: '$try'\n"; - } - } - } - -# all done -1; - -############################################################################### -############################################################################### -# Perl 5.005 does not like ok ($x,undef) - -sub ok_undef - { - my $x = shift; - - ok (1,1) and return 1 if !defined $x; - ok ($x,'undef'); - print "# Called from ",join(' ',caller()),"\n"; - return 0; - } - -############################################################################### -# sub to check validity of a BigInt internally, to ensure that no op leaves a -# number object in an invalid state (f.i. "-0") - -sub is_valid - { - my ($x,$f) = @_; - - my $e = 0; # error? - # ok as reference? - $e = 'Not a reference' if !ref($x); - - # has ok sign? - $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'" - if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/; - - $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0; - $e = $CALC->_check($x->{value}) if $e eq '0'; - - # test done, see if error did crop up - ok (1,1), return if ($e eq '0'); - - ok (1,$e." after op '$f'"); - } - -# format is: -# x,A,P:x,A,P:result -# 123,,3 means 123 with precision 3 (A is undef) -# the A or P of the result is calculated automatically -__DATA__ -&badd -123,,:123,,:246 -123,3,:0,,:123 -123,,-3:0,,:123 -123,,:0,3,:123 -123,,:0,,-3:123 -&bmul -123,,:1,,:123 -123,3,:0,,:0 -123,,-3:0,,:0 -123,,:0,3,:0 -123,,:0,,-3:0 -123,3,:1,,:123 -123,,-3:1,,:123 -123,,:1,3,:123 -123,,:1,,-3:123 -1,3,:123,,:123 -1,,-3:123,,:123 -1,,:123,3,:123 -1,,:123,,-3:123 -&bdiv -123,,:1,,:123 -123,4,:1,,:123 -123,,:1,4,:123 -123,,:1,,-4:123 -123,,-4:1,,:123 -1,4,:123,,:0 -1,,:123,4,:0 -1,,:123,,-4:0 -1,,-4:123,,:0 -&band -1,,:3,,:1 -1234,1,:0,,:0 -1234,,:0,1,:0 -1234,,-1:0,,:0 -1234,,:0,,-1:0 -0xFF,,:0x10,,:0x0x10 -0xFF,2,:0xFF,,:250 -0xFF,,:0xFF,2,:250 -0xFF,,1:0xFF,,:250 -0xFF,,:0xFF,,1:250 -&bxor -1,,:3,,:2 -1234,1,:0,,:1000 -1234,,:0,1,:1000 -1234,,3:0,,:1000 -1234,,:0,,3:1000 -0xFF,,:0x10,,:239 -# 250 ^ 255 => 5 -0xFF,2,:0xFF,,:5 -0xFF,,:0xFF,2,:5 -0xFF,,1:0xFF,,:5 -0xFF,,:0xFF,,1:5 -# 250 ^ 4095 = 3845 => 3800 -0xFF,2,:0xFFF,,:3800 -# 255 ^ 4100 = 4347 => 4300 -0xFF,,:0xFFF,2,:4300 -0xFF,,2:0xFFF,,:3800 -# 255 ^ 4100 = 10fb => 4347 => 4300 -0xFF,,:0xFFF,,2:4300 -&bior -1,,:3,,:3 -1234,1,:0,,:1000 -1234,,:0,1,:1000 -1234,,3:0,,:1000 -1234,,:0,,3:1000 -0xFF,,:0x10,,:0x0xFF -# FF | FA = FF => 250 -250,2,:0xFF,,:250 -0xFF,,:250,2,:250 -0xFF,,1:0xFF,,:250 -0xFF,,:0xFF,,1:250 -&bpow -2,,:3,,:8 -2,,:0,,:1 -2,2,:0,,:1 -2,,:0,2,:1 diff --git a/cpan/Math-BigInt/t/mbimbf.t b/cpan/Math-BigInt/t/mbimbf.t deleted file mode 100644 index 1ac9adadbd..0000000000 --- a/cpan/Math-BigInt/t/mbimbf.t +++ /dev/null @@ -1,110 +0,0 @@ -#!/usr/bin/perl -w - -# test rounding, accuracy, precicion and fallback, round_mode and mixing -# of classes - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/mbimbf.t//i; - if ($ENV{PERL_CORE}) - { - @INC = qw(../lib); # testing with the core distribution - } - else - { - unshift @INC, '../lib'; # for testing manually - } - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 684 - + 26; # own tests - } - -use Math::BigInt 1.70; -use Math::BigFloat 1.43; - -use vars qw/$mbi $mbf/; - -$mbi = 'Math::BigInt'; -$mbf = 'Math::BigFloat'; - -require 'mbimbf.inc'; - -# some tests that won't work with subclasses, since the things are only -# garantied in the Math::BigInt/BigFloat (unless subclass chooses to support -# this) - -Math::BigInt->round_mode('even'); # reset for tests -Math::BigFloat->round_mode('even'); # reset for tests - -ok ($Math::BigInt::rnd_mode,'even'); -ok ($Math::BigFloat::rnd_mode,'even'); - -my $x = eval '$mbi->round_mode("huhmbi");'; -print "# Got '$@'\n" unless - ok ($@ =~ /^Unknown round mode 'huhmbi' at/); - -$x = eval '$mbf->round_mode("huhmbf");'; -print "# Got '$@'\n" unless - ok ($@ =~ /^Unknown round mode 'huhmbf' at/); - -# old way (now with test for validity) -$x = eval '$Math::BigInt::rnd_mode = "huhmbi";'; -print "# Got '$@'\n" unless - ok ($@ =~ /^Unknown round mode 'huhmbi' at/); -$x = eval '$Math::BigFloat::rnd_mode = "huhmbf";'; -print "# Got '$@'\n" unless - ok ($@ =~ /^Unknown round mode 'huhmbf' at/); -# see if accessor also changes old variable -$mbi->round_mode('odd'); ok ($Math::BigInt::rnd_mode,'odd'); -$mbf->round_mode('odd'); ok ($Math::BigInt::rnd_mode,'odd'); - -foreach my $class (qw/Math::BigInt Math::BigFloat/) - { - ok ($class->accuracy(5),5); # set A - ok_undef ($class->precision()); # and now P must be cleared - ok ($class->precision(5),5); # set P - ok_undef ($class->accuracy()); # and now A must be cleared - } - -foreach my $class (qw/Math::BigInt Math::BigFloat/) - { - $class->accuracy(42); - my $x = $class->new(123); # $x gets A of 42, too! - ok ($x->accuracy(),42); # really? - ok ($x->accuracy(undef),42); # $x has no A, but the - # global is still in effect for $x - # so the return value of that operation should - # be 42, not undef - ok ($x->accuracy(),42); # so $x should still have A = 42 - $class->accuracy(undef); # reset for further tests - $class->precision(undef); - } -# bug with flog(Math::BigFloat,Math::BigInt) -$x = Math::BigFloat->new(100); -$x = $x->blog(Math::BigInt->new(10)); - -ok ($x,2); - -# bug until v1.88 for sqrt() with enough digits -for my $i (80,88,100) - { - $x = Math::BigFloat->new("1." . ("0" x $i) . "1"); - $x = $x->bsqrt; - ok ($x, 1); - } diff --git a/cpan/Math-BigInt/t/nan_cmp.t b/cpan/Math-BigInt/t/nan_cmp.t deleted file mode 100644 index ffe7b14b23..0000000000 --- a/cpan/Math-BigInt/t/nan_cmp.t +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/perl -w - -# test that overloaded compare works when NaN are involved - -use strict; -use Test::More; - -BEGIN - { - $| = 1; - chdir 't' if -d 't'; - unshift @INC, '../lib'; # for running manually - plan tests => 26; - } - -use Math::BigInt; -use Math::BigFloat; - -compare (Math::BigInt->bnan(), Math::BigInt->bone() ); -compare (Math::BigFloat->bnan(), Math::BigFloat->bone() ); - -sub compare - { - my ($nan, $one) = @_; - - is ($one, $one, '1 == 1'); - - is ($one != $nan, 1, "1 != NaN"); - is ($nan != $one, 1, "NaN != 1"); - is ($nan != $nan, 1, "NaN != NaN"); - - is ($nan == $one, '', "NaN == 1"); - is ($one == $nan, '', "1 == NaN"); - is ($nan == $nan, '', "NaN == NaN"); - - is ($nan <= $one, '', "NaN <= 1"); - is ($one <= $nan, '', "1 <= NaN"); - is ($nan <= $nan, '', "NaN <= NaN"); - - is ($nan >= $one, '', "NaN >= 1"); - is ($one >= $nan, '', "1 >= NaN"); - is ($nan >= $nan, '', "NaN >= NaN"); - } - diff --git a/cpan/Math-BigInt/t/new_overloaded.t b/cpan/Math-BigInt/t/new_overloaded.t deleted file mode 100644 index 08708dc557..0000000000 --- a/cpan/Math-BigInt/t/new_overloaded.t +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/perl -w - -# Math::BigFloat->new had a bug where it would assume any object is a -# BigInt which broke overloaded non-BigInts. - -use Test::More tests => 4; - - -package Overloaded::Num; - -use overload '0+' => sub { ${$_[0]} }, - fallback => 1; -sub new { - my($class, $num) = @_; - return bless \$num, $class; -} - - -package main; - -use Math::BigFloat; - -my $overloaded_num = Overloaded::Num->new(2.23); -is $overloaded_num, 2.23; - -my $bigfloat = Math::BigFloat->new($overloaded_num); -is $bigfloat, 2.23, 'BigFloat->new accepts overloaded numbers'; - -my $bigint = Math::BigInt->new(Overloaded::Num->new(3)); -is $bigint, 3, 'BigInt->new accepts overloaded numbers'; - -is( Math::BigFloat->new($bigint), 3, 'BigFloat from BigInt' ); diff --git a/cpan/Math-BigInt/t/req_mbf0.t b/cpan/Math-BigInt/t/req_mbf0.t deleted file mode 100644 index 90cd57cc9a..0000000000 --- a/cpan/Math-BigInt/t/req_mbf0.t +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/perl -w - -# check that simple requiring BigFloat and then bzero() works - -use strict; -use Test::More; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/req_mbf0.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 1; - } - -require Math::BigFloat; -my $x = Math::BigFloat->bzero(); $x++; -is ($x,1, '$x is 1'); - -# all tests done - diff --git a/cpan/Math-BigInt/t/req_mbf1.t b/cpan/Math-BigInt/t/req_mbf1.t deleted file mode 100644 index b0b4aea810..0000000000 --- a/cpan/Math-BigInt/t/req_mbf1.t +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/perl -w - -# check that simple requiring BigFloat and then bone() works - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/req_mbf1.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 1; - } - -require Math::BigFloat; my $x = Math::BigFloat->bone(); ok ($x,1); - -# all tests done - diff --git a/cpan/Math-BigInt/t/req_mbfa.t b/cpan/Math-BigInt/t/req_mbfa.t deleted file mode 100644 index b2d2a07099..0000000000 --- a/cpan/Math-BigInt/t/req_mbfa.t +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/perl -w - -# check that simple requiring BigFloat and then bnan() works - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/req_mbfa.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 1; - } - -require Math::BigFloat; my $x = Math::BigFloat->bnan(1); ok ($x,'NaN'); - -# all tests done - diff --git a/cpan/Math-BigInt/t/req_mbfi.t b/cpan/Math-BigInt/t/req_mbfi.t deleted file mode 100644 index 2c0ec67b02..0000000000 --- a/cpan/Math-BigInt/t/req_mbfi.t +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/perl -w - -# check that simple requiring BigFloat and then binf() works - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/req_mbfi.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 1; - } - -require Math::BigFloat; my $x = Math::BigFloat->binf(); ok ($x,'inf'); - -# all tests done - diff --git a/cpan/Math-BigInt/t/req_mbfn.t b/cpan/Math-BigInt/t/req_mbfn.t deleted file mode 100644 index e3887d41de..0000000000 --- a/cpan/Math-BigInt/t/req_mbfn.t +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/perl -w - -# check that simple requiring BigFloat and then new() works - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/req_mbfn.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 1; - } - -require Math::BigFloat; my $x = Math::BigFloat->new(1); ++$x; ok ($x,2); - -# all tests done - diff --git a/cpan/Math-BigInt/t/req_mbfw.t b/cpan/Math-BigInt/t/req_mbfw.t deleted file mode 100644 index 10afc7a272..0000000000 --- a/cpan/Math-BigInt/t/req_mbfw.t +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/perl -w - -# check that requiring BigFloat and then calling import() works - -use strict; -use Test::More; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/req_mbfw.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 3; - } - -# normal require that calls import automatically (we thus have MBI afterwards) -require Math::BigFloat; -my $x = Math::BigFloat->new(1); ++$x; -is ($x,2, '$x is 2'); - -like (Math::BigFloat->config()->{with}, qr/^Math::BigInt::(Fast)?Calc\z/, 'with ignored' ); - -# now override -Math::BigFloat->import ( with => 'Math::BigInt::Subclass' ); - -# the "with" argument is ignored -like (Math::BigFloat->config()->{with}, qr/^Math::BigInt::(Fast)?Calc\z/, 'with ignored' ); - -# all tests done - diff --git a/cpan/Math-BigInt/t/require.t b/cpan/Math-BigInt/t/require.t deleted file mode 100644 index 50831e611d..0000000000 --- a/cpan/Math-BigInt/t/require.t +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/perl -w - -# check that simple requiring BigInt works - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/require.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 1; - } - -my ($x); - -require Math::BigInt; $x = Math::BigInt->new(1); ++$x; - -ok ($x||'undef',2); - -# all tests done - diff --git a/cpan/Math-BigInt/t/round.t b/cpan/Math-BigInt/t/round.t deleted file mode 100644 index 90c46758a7..0000000000 --- a/cpan/Math-BigInt/t/round.t +++ /dev/null @@ -1,120 +0,0 @@ -#!/usr/bin/perl -w - -# test rounding with non-integer A and P parameters - -use strict; -use Test::More; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/round.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 95; - } - -use Math::BigFloat; - -my $cf = 'Math::BigFloat'; -my $ci = 'Math::BigInt'; - -my $x = $cf->new('123456.123456'); - -# unary ops with A -_do_a($x, 'round', 3, '123000'); -_do_a($x, 'bfround', 3, '123500'); -_do_a($x, 'bfround', 2, '123460'); -_do_a($x, 'bfround', -2, '123456.12'); -_do_a($x, 'bfround', -3, '123456.123'); - -_do_a($x, 'bround', 4, '123500'); -_do_a($x, 'bround', 3, '123000'); -_do_a($x, 'bround', 2, '120000'); - -_do_a($x, 'bsqrt', 4, '351.4'); -_do_a($x, 'bsqrt', 3, '351'); -_do_a($x, 'bsqrt', 2, '350'); - -# setting P -_do_p($x, 'bsqrt', 2, '350'); -_do_p($x, 'bsqrt', -2, '351.36'); - -# binary ops -_do_2_a($x, 'bdiv', 2, 6, '61728.1'); -_do_2_a($x, 'bdiv', 2, 4, '61730'); -_do_2_a($x, 'bdiv', 2, 3, '61700'); - -_do_2_p($x, 'bdiv', 2, -6, '61728.061728'); -_do_2_p($x, 'bdiv', 2, -4, '61728.0617'); -_do_2_p($x, 'bdiv', 2, -3, '61728.062'); - -# all tests done - -############################################################################# - -sub _do_a - { - my ($x, $method, $A, $result) = @_; - - is ($x->copy->$method($A), $result, "$method($A)"); - is ($x->copy->$method($A.'.1'), $result, "$method(${A}.1)"); - is ($x->copy->$method($A.'.5'), $result, "$method(${A}.5)"); - is ($x->copy->$method($A.'.6'), $result, "$method(${A}.6)"); - is ($x->copy->$method($A.'.9'), $result, "$method(${A}.9)"); - } - -sub _do_p - { - my ($x, $method, $P, $result) = @_; - - is ($x->copy->$method(undef,$P), $result, "$method(undef,$P)"); - is ($x->copy->$method(undef,$P.'.1'), $result, "$method(undef,${P}.1)"); - is ($x->copy->$method(undef,$P.'.5'), $result, "$method(undef.${P}.5)"); - is ($x->copy->$method(undef,$P.'.6'), $result, "$method(undef,${P}.6)"); - is ($x->copy->$method(undef,$P.'.9'), $result, "$method(undef,${P}.9)"); - } - -sub _do_2_a - { - my ($x, $method, $y, $A, $result) = @_; - - my $cy = $cf->new($y); - - is ($x->copy->$method($cy,$A), $result, "$method($cy,$A)"); - is ($x->copy->$method($cy,$A.'.1'), $result, "$method($cy,${A}.1)"); - is ($x->copy->$method($cy,$A.'.5'), $result, "$method($cy,${A}.5)"); - is ($x->copy->$method($cy,$A.'.6'), $result, "$method($cy,${A}.6)"); - is ($x->copy->$method($cy,$A.'.9'), $result, "$method($cy,${A}.9)"); - } - -sub _do_2_p - { - my ($x, $method, $y, $P, $result) = @_; - - my $cy = $cf->new($y); - - is ($x->copy->$method($cy,undef,$P), $result, "$method(undef,$P)"); - is ($x->copy->$method($cy,undef,$P.'.1'), $result, "$method($cy,undef,${P}.1)"); - is ($x->copy->$method($cy,undef,$P.'.5'), $result, "$method($cy,undef.${P}.5)"); - is ($x->copy->$method($cy,undef,$P.'.6'), $result, "$method($cy,undef,${P}.6)"); - is ($x->copy->$method($cy,undef,$P.'.9'), $result, "$method($cy,undef,${P}.9)"); - } - diff --git a/cpan/Math-BigInt/t/sub_ali.t b/cpan/Math-BigInt/t/sub_ali.t deleted file mode 100644 index 93620a9d82..0000000000 --- a/cpan/Math-BigInt/t/sub_ali.t +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/perl -w - -# test that the new alias names work - -use Test::More; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/sub_ali.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 6; - } - -use Math::BigInt::Subclass; - -use vars qw/$CL $x/; -$CL = 'Math::BigInt::Subclass'; - -require 'alias.inc'; - diff --git a/cpan/Math-BigInt/t/sub_mbf.t b/cpan/Math-BigInt/t/sub_mbf.t deleted file mode 100644 index 11f63ddc4a..0000000000 --- a/cpan/Math-BigInt/t/sub_mbf.t +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/perl -w - -use Test; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/sub_mbf.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, '../lib'; - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 2308 - + 6; # + our own tests - } - -use Math::BigFloat::Subclass; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigFloat::Subclass"; -$CL = Math::BigFloat->config()->{lib}; # "Math::BigInt::Calc"; or FastCalc - -require 'bigfltpm.inc'; # perform same tests as bigfltpm - -############################################################################### -# Now do custom tests for Subclass itself -my $ms = $class->new(23); -print "# Missing custom attribute \$ms->{_custom}" if !ok (1, $ms->{_custom}); - -# Check that subclass is a Math::BigFloat, but not a Math::Bigint -ok ($ms->isa('Math::BigFloat'),1); -ok ($ms->isa('Math::BigInt') || 0,0); - -use Math::BigFloat; - -my $bf = Math::BigFloat->new(23); # same as other -$ms += $bf; -print "# Tried: \$ms += \$bf, got $ms" if !ok (46, $ms); -print "# Missing custom attribute \$ms->{_custom}" if !ok (1, $ms->{_custom}); -print "# Wrong class: ref(\$ms) was ".ref($ms) if !ok ($class, ref($ms)); diff --git a/cpan/Math-BigInt/t/sub_mbi.t b/cpan/Math-BigInt/t/sub_mbi.t deleted file mode 100644 index 7a6b1e2b26..0000000000 --- a/cpan/Math-BigInt/t/sub_mbi.t +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/perl -w - -use Test; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/sub_mbi.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 3273 - + 5; # +5 own tests - } - -use Math::BigInt::Subclass; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigInt::Subclass"; -$CL = "Math::BigInt::Calc"; - -my $version = '0.02'; # for $VERSION tests, match current release (by hand!) - -require 'bigintpm.inc'; # perform same tests as bigintpm - -############################################################################### -# Now do custom tests for Subclass itself - -my $ms = $class->new(23); -print "# Missing custom attribute \$ms->{_custom}" if !ok (1, $ms->{_custom}); - -# Check that a subclass is still considered a BigInt -ok ($ms->isa('Math::BigInt'),1); - -use Math::BigInt; - -my $bi = Math::BigInt->new(23); # same as other -$ms += $bi; -print "# Tried: \$ms += \$bi, got $ms" if !ok (46, $ms); -print "# Missing custom attribute \$ms->{_custom}" if !ok (1, $ms->{_custom}); -print "# Wrong class: ref(\$ms) was ".ref($ms) if !ok ($class, ref($ms)); diff --git a/cpan/Math-BigInt/t/sub_mif.t b/cpan/Math-BigInt/t/sub_mif.t deleted file mode 100644 index cd0c863075..0000000000 --- a/cpan/Math-BigInt/t/sub_mif.t +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/perl -w - -# test rounding, accuracy, precicion and fallback, round_mode and mixing -# of classes - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/sub_mif.t//i; - if ($ENV{PERL_CORE}) - { - @INC = qw(../t/lib); # testing with the core distribution - } - unshift @INC, '../lib'; # for testing manually - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 684; - } - -use Math::BigInt::Subclass; -use Math::BigFloat::Subclass; - -use vars qw/$mbi $mbf/; - -$mbi = 'Math::BigInt::Subclass'; -$mbf = 'Math::BigFloat::Subclass'; - -require 'mbimbf.inc'; - diff --git a/cpan/Math-BigInt/t/trap.t b/cpan/Math-BigInt/t/trap.t deleted file mode 100644 index 94a7da4acb..0000000000 --- a/cpan/Math-BigInt/t/trap.t +++ /dev/null @@ -1,92 +0,0 @@ -#!/usr/bin/perl -w - -# test that config ( trap_nan => 1, trap_inf => 1) really works/dies - -use strict; -use Test::More; - -BEGIN - { - $| = 1; - chdir 't' if -d 't'; - unshift @INC, '../lib'; # for running manually - plan tests => 43; - } - -use Math::BigInt; -use Math::BigFloat; - -my $mbi = 'Math::BigInt'; my $mbf = 'Math::BigFloat'; -my ($cfg,$x); - -foreach my $class ($mbi, $mbf) - { - # can do and defaults are okay? - ok ($class->can('config'), 'can config()'); - is ($class->config()->{trap_nan}, 0, 'trap_nan defaults to 0'); - is ($class->config()->{trap_inf}, 0, 'trap_inf defaults to 0'); - - # can set? - $cfg = $class->config( trap_nan => 1 ); - is ($cfg->{trap_nan},1, 'trap_nan now true'); - - # also test that new() still works normally - eval ("\$x = \$class->new('42'); \$x->bnan();"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,42,'$x after new() never modified'); - - # can reset? - $cfg = $class->config( trap_nan => 0 ); - is ($cfg->{trap_nan}, 0, 'trap_nan disabled'); - - # can set? - $cfg = $class->config( trap_inf => 1 ); - is ($cfg->{trap_inf}, 1, 'trap_inf enabled'); - - eval ("\$x = \$class->new('4711'); \$x->binf();"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,4711,'$x after new() never modified'); - - eval ("\$x = \$class->new('inf');"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,4711,'$x after new() never modified'); - - eval ("\$x = \$class->new('-inf');"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,4711,'$x after new() never modified'); - - # +$x/0 => +inf - eval ("\$x = \$class->new('4711'); \$x->bdiv(0);"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,4711,'$x after new() never modified'); - - # -$x/0 => -inf - eval ("\$x = \$class->new('-0815'); \$x->bdiv(0);"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,'-815', '$x after new not modified'); - - $cfg = $class->config( trap_nan => 1 ); - # 0/0 => NaN - eval ("\$x = \$class->new('0'); \$x->bdiv(0);"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,'0', '$x after new not modified'); - } - -############################################################################## -# BigInt - -$x = Math::BigInt->new(2); -eval ("\$x = \$mbi->new('0.1');"); -is ($x,2,'never modified since it dies'); -eval ("\$x = \$mbi->new('0a.1');"); -is ($x,2,'never modified since it dies'); - -############################################################################## -# BigFloat - -$x = Math::BigFloat->new(2); -eval ("\$x = \$mbf->new('0.1a');"); -is ($x,2,'never modified since it dies'); - -# all tests done - diff --git a/cpan/Math-BigInt/t/upgrade.inc b/cpan/Math-BigInt/t/upgrade.inc deleted file mode 100644 index 1160a21c6b..0000000000 --- a/cpan/Math-BigInt/t/upgrade.inc +++ /dev/null @@ -1,1500 +0,0 @@ -# include this file into another for subclass testing - -# This file is nearly identical to bigintpm.t, except that certain results -# are _requird_ to be different due to "upgrading" or "promoting" to BigFloat. -# The reverse is not true, any unmarked results can be either BigInt or -# BigFloat, depending on how good the internal optimization is (e.g. it -# is usually desirable to have 2 ** 2 return a BigInt, not a BigFloat). - -# Results that are required to be BigFloat are marked with C<^> at the end. - -# Please note that the testcount goes up by two for each extra result marked -# with ^, since then we test whether it has the proper class and that it left -# the upgrade variable alone. - -my $version = ${"$class\::VERSION"}; - -############################################################################## -# for testing inheritance of _swap - -package Math::Foo; - -use Math::BigInt lib => $main::CL; -use vars qw/@ISA/; -@ISA = (qw/Math::BigInt/); - -use overload -# customized overload for sub, since original does not use swap there -'-' => sub { my @a = ref($_[0])->_swap(@_); - $a[0]->bsub($a[1])}; - -sub _swap - { - # a fake _swap, which reverses the params - my $self = shift; # for override in subclass - if ($_[2]) - { - my $c = ref ($_[0] ) || 'Math::Foo'; - return ( $_[0]->copy(), $_[1] ); - } - else - { - return ( Math::Foo->new($_[1]), $_[0] ); - } - } - -############################################################################## -package main; - -my $CALC = $class->config()->{lib}; ok ($CALC,$CL); - -my ($f,$z,$a,$exp,@a,$m,$e,$round_mode,$expected_class); - -while (<DATA>) - { - $_ =~ s/[\n\r]//g; # remove newlines - next if /^#/; # skip comments - if (s/^&//) - { - $f = $_; next; - } - elsif (/^\$/) - { - $round_mode = $_; $round_mode =~ s/^\$/$class\->/; next; - } - - @args = split(/:/,$_,99); $ans = pop(@args); - $expected_class = $class; - if ($ans =~ /\^$/) - { - $expected_class = $ECL; $ans =~ s/\^$//; - } - $try = "\$x = $class->new(\"$args[0]\");"; - if ($f eq "bnorm") - { - $try = "\$x = $class->bnorm(\"$args[0]\");"; - # some is_xxx tests - } elsif ($f =~ /^is_(zero|one|odd|even|negative|positive|nan|int)$/) { - $try .= "\$x->$f();"; - } elsif ($f eq "as_hex") { - $try .= '$x->as_hex();'; - } elsif ($f eq "as_bin") { - $try .= '$x->as_bin();'; - } elsif ($f eq "is_inf") { - $try .= "\$x->is_inf('$args[1]');"; - } elsif ($f eq "binf") { - $try .= "\$x->binf('$args[1]');"; - } elsif ($f eq "bone") { - $try .= "\$x->bone('$args[1]');"; - # some unary ops - } elsif ($f =~ /^b(nan|floor|ceil|sstr|neg|abs|inc|dec|not|sqrt|fac)$/) { - $try .= "\$x->$f();"; - } elsif ($f eq "length") { - $try .= '$x->length();'; - } elsif ($f eq "exponent"){ - # ->bstr() to see if an object is returned - $try .= '$x = $x->exponent()->bstr();'; - } elsif ($f eq "mantissa"){ - # ->bstr() to see if an object is returned - $try .= '$x = $x->mantissa()->bstr();'; - } elsif ($f eq "parts"){ - $try .= '($m,$e) = $x->parts();'; - # ->bstr() to see if an object is returned - $try .= '$m = $m->bstr(); $m = "NaN" if !defined $m;'; - $try .= '$e = $e->bstr(); $e = "NaN" if !defined $e;'; - $try .= '"$m,$e";'; - } else { - if ($args[1] !~ /\./) - { - $try .= "\$y = $class->new(\"$args[1]\");"; # BigInt - } - else - { - $try .= "\$y = $ECL->new(\"$args[1]\");"; # BigFloat - } - if ($f eq "bcmp") - { - $try .= '$x->bcmp($y);'; - } elsif ($f eq "bround") { - $try .= "$round_mode; \$x->bround(\$y);"; - } elsif ($f eq "broot") { - $try .= "\$x->broot(\$y);"; - } elsif ($f eq "bacmp"){ - $try .= '$x->bacmp($y);'; - } elsif ($f eq "badd"){ - $try .= '$x + $y;'; - } elsif ($f eq "bsub"){ - $try .= '$x - $y;'; - } elsif ($f eq "bmul"){ - $try .= '$x * $y;'; - } elsif ($f eq "bdiv"){ - $try .= '$x / $y;'; - } elsif ($f eq "bdiv-list"){ - $try .= 'join (",",$x->bdiv($y));'; - # overload via x= - } elsif ($f =~ /^.=$/){ - $try .= "\$x $f \$y;"; - # overload via x - } elsif ($f =~ /^.$/){ - $try .= "\$x $f \$y;"; - } elsif ($f eq "bmod"){ - $try .= '$x % $y;'; - } elsif ($f eq "bgcd") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new('$args[2]'); "; - } - $try .= "$class\::bgcd(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - } - elsif ($f eq "blcm") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new('$args[2]'); "; - } - $try .= "$class\::blcm(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - }elsif ($f eq "blsft"){ - if (defined $args[2]) - { - $try .= "\$x->blsft(\$y,$args[2]);"; - } - else - { - $try .= "\$x << \$y;"; - } - }elsif ($f eq "brsft"){ - if (defined $args[2]) - { - $try .= "\$x->brsft(\$y,$args[2]);"; - } - else - { - $try .= "\$x >> \$y;"; - } - }elsif ($f eq "band"){ - $try .= "\$x & \$y;"; - }elsif ($f eq "bior"){ - $try .= "\$x | \$y;"; - }elsif ($f eq "bxor"){ - $try .= "\$x ^ \$y;"; - }elsif ($f eq "bpow"){ - $try .= "\$x ** \$y;"; - }elsif ($f eq "digit"){ - $try = "\$x = $class->new('$args[0]'); \$x->digit($args[1]);"; - } else { warn "Unknown op '$f'"; } - } # end else all other ops - - $ans1 = eval $try; - # convert hex/binary targets to decimal - if ($ans =~ /^(0x0x|0b0b)/) - { - $ans =~ s/^0[xb]//; $ans = Math::BigInt->new($ans)->bstr(); - } - if ($ans eq "") - { - ok_undef ($ans1); - } - else - { - # print "try: $try ans: $ans1 $ans\n"; - print "# Tried: '$try'\n" if !ok ($ans1, $ans); - if ($expected_class ne $class) - { - ok (ref($ans1),$expected_class); # also checks that it really is ref! - ok ($Math::BigInt::upgrade,'Math::BigFloat'); # still okay? - } - } - # check internal state of number objects - is_valid($ans1,$f) if ref $ans1; - } # endwhile data tests -close DATA; - -my $warn = ''; $SIG{__WARN__} = sub { $warn = shift; }; - -# these should not warn -$warn = ''; eval "\$z = 3.17 <= \$y"; ok ($z, 1); ok ($warn, ''); -$warn = ''; eval "\$z = \$y >= 3.17"; ok ($z, 1); ok ($warn, ''); - -# all tests done - -1; - -############################################################################### -############################################################################### -# Perl 5.005 does not like ok ($x,undef) - -sub ok_undef - { - my $x = shift; - - ok (1,1) and return if !defined $x; - ok ($x,'undef'); - } - -############################################################################### -# sub to check validity of a BigInt internally, to ensure that no op leaves a -# number object in an invalid state (f.i. "-0") - -sub is_valid - { - my ($x,$f,$c) = @_; - - # The checks here are loosened a bit to allow BigInt or BigFloats to pass - - my $e = 0; # error? - # ok as reference? - # $e = "Not a reference to $c" if (ref($x) || '') ne $c; - - # has ok sign? - $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'" - if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/; - - $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0; - #$e = $CALC->_check($x->{value}) if $e eq '0'; - - # test done, see if error did crop up - ok (1,1), return if ($e eq '0'); - - ok (1,$e." after op '$f'"); - } - -__DATA__ -&.= -1234:-345:1234-345 -&+= -1:2:3 --1:-2:-3 -&-= -1:2:-1 --1:-2:1 -&*= -2:3:6 --1:5:-5 -&%= -100:3:1 -8:9:8 -&/= -100:3:33.33333333333333333333333333333333333333 --8:2:-4 -&|= -2:1:3 -&&= -5:7:5 -&^= -5:7:2 -&is_negative -0:0 --1:1 -1:0 -+inf:0 --inf:1 -NaNneg:0 -&is_positive -0:0 --1:0 -1:1 -+inf:1 --inf:0 -NaNneg:0 -&is_odd -abc:0 -0:0 -1:1 -3:1 --1:1 --3:1 -10000001:1 -10000002:0 -2:0 -120:0 -121:1 -&is_int -NaN:0 -inf:0 --inf:0 -1:1 -12:1 -123e12:1 -&is_even -abc:0 -0:1 -1:0 -3:0 --1:0 --3:0 -10000001:0 -10000002:1 -2:1 -120:1 -121:0 -&bacmp -+0:-0:0 -+0:+1:-1 --1:+1:0 -+1:-1:0 --1:+2:-1 -+2:-1:1 --123456789:+987654321:-1 -+123456789:-987654321:-1 -+987654321:+123456789:1 --987654321:+123456789:1 --123:+4567889:-1 -# NaNs -acmpNaN:123: -123:acmpNaN: -acmpNaN:acmpNaN: -# infinity -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:0 --inf:+inf:0 -+inf:123:1 --inf:123:1 -+inf:-123:1 --inf:-123:1 -# return undef -+inf:NaN: -NaN:inf: --inf:NaN: -NaN:-inf: -&bnorm -123:123 -12.3:12.3^ -# binary input -0babc:NaN -0b123:NaN -0b0:0 --0b0:0 --0b1:-1 -0b0001:1 -0b001:1 -0b011:3 -0b101:5 -0b1001:9 -0b10001:17 -0b100001:33 -0b1000001:65 -0b10000001:129 -0b100000001:257 -0b1000000001:513 -0b10000000001:1025 -0b100000000001:2049 -0b1000000000001:4097 -0b10000000000001:8193 -0b100000000000001:16385 -0b1000000000000001:32769 -0b10000000000000001:65537 -0b100000000000000001:131073 -0b1000000000000000001:262145 -0b10000000000000000001:524289 -0b100000000000000000001:1048577 -0b1000000000000000000001:2097153 -0b10000000000000000000001:4194305 -0b100000000000000000000001:8388609 -0b1000000000000000000000001:16777217 -0b10000000000000000000000001:33554433 -0b100000000000000000000000001:67108865 -0b1000000000000000000000000001:134217729 -0b10000000000000000000000000001:268435457 -0b100000000000000000000000000001:536870913 -0b1000000000000000000000000000001:1073741825 -0b10000000000000000000000000000001:2147483649 -0b100000000000000000000000000000001:4294967297 -0b1000000000000000000000000000000001:8589934593 -0b10000000000000000000000000000000001:17179869185 -0b_101:NaN -0b1_0_1:5 -0b0_0_0_1:1 -# hex input --0x0:0 -0xabcdefgh:NaN -0x1234:4660 -0xabcdef:11259375 --0xABCDEF:-11259375 --0x1234:-4660 -0x12345678:305419896 -0x1_2_3_4_56_78:305419896 -0xa_b_c_d_e_f:11259375 -0x_123:NaN -0x9:9 -0x11:17 -0x21:33 -0x41:65 -0x81:129 -0x101:257 -0x201:513 -0x401:1025 -0x801:2049 -0x1001:4097 -0x2001:8193 -0x4001:16385 -0x8001:32769 -0x10001:65537 -0x20001:131073 -0x40001:262145 -0x80001:524289 -0x100001:1048577 -0x200001:2097153 -0x400001:4194305 -0x800001:8388609 -0x1000001:16777217 -0x2000001:33554433 -0x4000001:67108865 -0x8000001:134217729 -0x10000001:268435457 -0x20000001:536870913 -0x40000001:1073741825 -0x80000001:2147483649 -0x100000001:4294967297 -0x200000001:8589934593 -0x400000001:17179869185 -0x800000001:34359738369 -# inf input -inf:inf -+inf:inf --inf:-inf -0inf:NaN -# abnormal input -:NaN -abc:NaN - 1 a:NaN -1bcd2:NaN -11111b:NaN -+1z:NaN --1z:NaN -# only one underscore between two digits -_123:NaN -_123_:NaN -123_:NaN -1__23:NaN -1E1__2:NaN -1_E12:NaN -1E_12:NaN -1_E_12:NaN -+_1E12:NaN -+0_1E2:100 -+0_0_1E2:100 --0_0_1E2:-100 --0_0_1E+0_0_2:-100 -E1:NaN -E23:NaN -1.23E1:12.3^ -1.23E-1:0.123^ -# bug with two E's in number being valid -1e2e3:NaN -1e2r:NaN -1e2.0:NaN -# leading zeros -012:12 -0123:123 -01234:1234 -012345:12345 -0123456:123456 -01234567:1234567 -012345678:12345678 -0123456789:123456789 -01234567891:1234567891 -012345678912:12345678912 -0123456789123:123456789123 -01234567891234:1234567891234 -# normal input -0:0 -+0:0 -+00:0 -+000:0 -000000000000000000:0 --0:0 --0000:0 -+1:1 -+01:1 -+001:1 -+00000100000:100000 -123456789:123456789 --1:-1 --01:-1 --001:-1 --123456789:-123456789 --00000100000:-100000 -1_2_3:123 -10000000000E-1_0:1 -1E2:100 -1E1:10 -1E0:1 -1.23E2:123 -100E-1:10 -# floating point input -# .2e2:20 -1.E3:1000 -1.01E2:101 -1010E-1:101 --1010E0:-1010 --1010E1:-10100 -1234.00:1234 -# non-integer numbers --1010E-2:-10.1^ --1.01E+1:-10.1^ --1.01E-1:-0.101^ -&bnan -1:NaN -2:NaN -abc:NaN -&bone -2:+:1 -2:-:-1 -boneNaN:-:-1 -boneNaN:+:1 -2:abc:1 -3::1 -&binf -1:+:inf -2:-:-inf -3:abc:inf -&is_nan -123:0 -abc:1 -NaN:1 --123:0 -&is_inf -+inf::1 --inf::1 -abc::0 -1::0 -NaN::0 --1::0 -+inf:-:0 -+inf:+:1 --inf:-:1 --inf:+:0 -# it must be exactly /^[+-]inf$/ -+infinity::0 --infinity::0 -&blsft -abc:abc:NaN -+2:+2:8 -+1:+32:4294967296 -+1:+48:281474976710656 -+8:-2:NaN -# excercise base 10 -+12345:4:10:123450000 --1234:0:10:-1234 -+1234:0:10:1234 -+2:2:10:200 -+12:2:10:1200 -+1234:-3:10:NaN -1234567890123:12:10:1234567890123000000000000 -&brsft -abc:abc:NaN -+8:+2:2 -+4294967296:+32:1 -+281474976710656:+48:1 -+2:-2:NaN -# excercise base 10 --1234:0:10:-1234 -+1234:0:10:1234 -+200:2:10:2 -+1234:3:10:1 -+1234:2:10:12 -+1234:-3:10:NaN -310000:4:10:31 -12300000:5:10:123 -1230000000000:10:10:123 -09876123456789067890:12:10:9876123 -1234561234567890123:13:10:123456 -&bsstr -1e+34:1e+34 -123.456E3:123456e+0 -100:1e+2 -abc:NaN -&bneg -bnegNaN:NaN -+inf:-inf --inf:inf -abd:NaN -0:0 -1:-1 --1:1 -+123456789:-123456789 --123456789:123456789 -&babs -babsNaN:NaN -+inf:inf --inf:inf -0:0 -1:1 --1:1 -+123456789:123456789 --123456789:123456789 -&bcmp -bcmpNaN:bcmpNaN: -bcmpNaN:0: -0:bcmpNaN: -0:0:0 --1:0:-1 -0:-1:1 -1:0:1 -0:1:-1 --1:1:-1 -1:-1:1 --1:-1:0 -1:1:0 -123:123:0 -123:12:1 -12:123:-1 --123:-123:0 --123:-12:-1 --12:-123:1 -123:124:-1 -124:123:1 --123:-124:1 --124:-123:-1 -100:5:1 --123456789:987654321:-1 -+123456789:-987654321:1 --987654321:123456789:-1 --inf:5432112345:-1 -+inf:5432112345:1 --inf:-5432112345:-1 -+inf:-5432112345:1 -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:1 --inf:+inf:-1 -5:inf:-1 -5:inf:-1 --5:-inf:1 --5:-inf:1 -# return undef -+inf:NaN: -NaN:inf: --inf:NaN: -NaN:-inf: -&binc -abc:NaN -+inf:inf --inf:-inf -+0:1 -+1:2 --1:0 -&bdec -abc:NaN -+inf:inf --inf:-inf -+0:-1 -+1:0 --1:-2 -&badd -abc:abc:NaN -abc:0:NaN -+0:abc:NaN -+inf:-inf:NaN --inf:+inf:NaN -+inf:+inf:inf --inf:-inf:-inf -baddNaN:+inf:NaN -baddNaN:+inf:NaN -+inf:baddNaN:NaN --inf:baddNaN:NaN -0:0:0 -1:0:1 -0:1:1 -1:1:2 --1:0:-1 -0:-1:-1 --1:-1:-2 --1:+1:0 -+1:-1:0 -+9:+1:10 -+99:+1:100 -+999:+1:1000 -+9999:+1:10000 -+99999:+1:100000 -+999999:+1:1000000 -+9999999:+1:10000000 -+99999999:+1:100000000 -+999999999:+1:1000000000 -+9999999999:+1:10000000000 -+99999999999:+1:100000000000 -+10:-1:9 -+100:-1:99 -+1000:-1:999 -+10000:-1:9999 -+100000:-1:99999 -+1000000:-1:999999 -+10000000:-1:9999999 -+100000000:-1:99999999 -+1000000000:-1:999999999 -+10000000000:-1:9999999999 -+123456789:987654321:1111111110 --123456789:987654321:864197532 --123456789:-987654321:-1111111110 -+123456789:-987654321:-864197532 -2:2.5:4.5^ --123:-1.5:-124.5^ --1.2:1:-0.2^ -&bsub -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:-inf:inf --inf:+inf:-inf -+inf:+inf:NaN --inf:-inf:NaN -+0:+0:0 -+1:+0:1 -+0:+1:-1 -+1:+1:0 --1:+0:-1 -+0:-1:1 --1:-1:0 --1:+1:-2 -+1:-1:2 -+9:+1:8 -+99:+1:98 -+999:+1:998 -+9999:+1:9998 -+99999:+1:99998 -+999999:+1:999998 -+9999999:+1:9999998 -+99999999:+1:99999998 -+999999999:+1:999999998 -+9999999999:+1:9999999998 -+99999999999:+1:99999999998 -+10:-1:11 -+100:-1:101 -+1000:-1:1001 -+10000:-1:10001 -+100000:-1:100001 -+1000000:-1:1000001 -+10000000:-1:10000001 -+100000000:-1:100000001 -+1000000000:-1:1000000001 -+10000000000:-1:10000000001 -+123456789:+987654321:-864197532 --123456789:+987654321:-1111111110 --123456789:-987654321:864197532 -+123456789:-987654321:1111111110 -&bmul -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -NaNmul:+inf:NaN -NaNmul:-inf:NaN --inf:NaNmul:NaN -+inf:NaNmul:NaN -+inf:+inf:inf -+inf:-inf:-inf --inf:+inf:-inf --inf:-inf:inf -+0:+0:0 -+0:+1:0 -+1:+0:0 -+0:-1:0 --1:+0:0 -123456789123456789:0:0 -0:123456789123456789:0 --1:-1:1 --1:+1:-1 -+1:-1:-1 -+1:+1:1 -+2:+3:6 --2:+3:-6 -+2:-3:-6 --2:-3:6 -111:111:12321 -10101:10101:102030201 -1001001:1001001:1002003002001 -100010001:100010001:10002000300020001 -10000100001:10000100001:100002000030000200001 -11111111111:9:99999999999 -22222222222:9:199999999998 -33333333333:9:299999999997 -44444444444:9:399999999996 -55555555555:9:499999999995 -66666666666:9:599999999994 -77777777777:9:699999999993 -88888888888:9:799999999992 -99999999999:9:899999999991 -+25:+25:625 -+12345:+12345:152399025 -+99999:+11111:1111088889 -9999:10000:99990000 -99999:100000:9999900000 -999999:1000000:999999000000 -9999999:10000000:99999990000000 -99999999:100000000:9999999900000000 -999999999:1000000000:999999999000000000 -9999999999:10000000000:99999999990000000000 -99999999999:100000000000:9999999999900000000000 -999999999999:1000000000000:999999999999000000000000 -9999999999999:10000000000000:99999999999990000000000000 -99999999999999:100000000000000:9999999999999900000000000000 -999999999999999:1000000000000000:999999999999999000000000000000 -9999999999999999:10000000000000000:99999999999999990000000000000000 -99999999999999999:100000000000000000:9999999999999999900000000000000000 -999999999999999999:1000000000000000000:999999999999999999000000000000000000 -9999999999999999999:10000000000000000000:99999999999999999990000000000000000000 -3:3.5:10.5^ -3.5:3:10.5^ -&bdiv-list -100:20:5,0 -4095:4095:1,0 --4095:-4095:1,0 -4095:-4095:-1,0 --4095:4095:-1,0 -123:2:61.5,1 -9:5:1.8,4 -9:4:2.25,1 -# inf handling and general remainder -5:8:0.625,5 -0:8:0,0 -11:2:5.5,1 -11:-2:-5.5,-1 --11:2:-5.5,1 -# see table in documentation in MBI -0:inf:0,0 -0:-inf:0,0 -5:inf:0,5 -5:-inf:0,5 --5:inf:0,-5 --5:-inf:0,-5 -inf:5:inf,0 --inf:5:-inf,0 -inf:-5:-inf,0 --inf:-5:inf,0 -5:5:1,0 --5:-5:1,0 -inf:inf:NaN,NaN --inf:-inf:NaN,NaN --inf:inf:NaN,NaN -inf:-inf:NaN,NaN -8:0:inf,8 -inf:0:inf,inf -# exceptions to reminder rule --8:0:-inf,-8 --inf:0:-inf,-inf -0:0:NaN,NaN -&bdiv -abc:abc:NaN -abc:1:NaN -1:abc:NaN -0:0:NaN -# inf handling (see table in doc) -0:inf:0 -0:-inf:0 -5:inf:0 -5:-inf:0 --5:inf:0 --5:-inf:0 -inf:5:inf --inf:5:-inf -inf:-5:-inf --inf:-5:inf -5:5:1 --5:-5:1 -inf:inf:NaN --inf:-inf:NaN --inf:inf:NaN -inf:-inf:NaN -8:0:inf -inf:0:inf --8:0:-inf --inf:0:-inf -0:0:NaN -11:2:5.5^ --11:-2:5.5^ --11:2:-5.5^ -11:-2:-5.5^ -0:1:0 -0:-1:0 -1:1:1 --1:-1:1 -1:-1:-1 --1:1:-1 -1:2:0.5^ -2:1:2 -1000000000:9:111111111.1111111111111111111111111111111^ -2000000000:9:222222222.2222222222222222222222222222222^ -3000000000:9:333333333.3333333333333333333333333333333^ -4000000000:9:444444444.4444444444444444444444444444444^ -5000000000:9:555555555.5555555555555555555555555555556^ -6000000000:9:666666666.6666666666666666666666666666667^ -7000000000:9:777777777.7777777777777777777777777777778^ -8000000000:9:888888888.8888888888888888888888888888889^ -9000000000:9:1000000000 -35500000:113:314159.2920353982300884955752212389380531^ -71000000:226:314159.2920353982300884955752212389380531^ -106500000:339:314159.2920353982300884955752212389380531^ -1000000000:3:333333333.3333333333333333333333333333333^ -+10:+5:2 -+100:+4:25 -+1000:+8:125 -+10000:+16:625 -999999999999:9:111111111111 -999999999999:99:10101010101 -999999999999:999:1001001001 -999999999999:9999:100010001 -999999999999999:99999:10000100001 -+1111088889:99999:11111 --5:-3:1.666666666666666666666666666666666666667^ --5:3:-1.666666666666666666666666666666666666667^ -4:3:1.333333333333333333333333333333333333333^ -4:-3:-1.333333333333333333333333333333333333333^ -1:3:0.3333333333333333333333333333333333333333^ -1:-3:-0.3333333333333333333333333333333333333333^ --2:-3:0.6666666666666666666666666666666666666667^ --2:3:-0.6666666666666666666666666666666666666667^ -8:5:1.6^ --8:5:-1.6^ -14:-3:-4.666666666666666666666666666666666666667^ --14:3:-4.666666666666666666666666666666666666667^ --14:-3:4.666666666666666666666666666666666666667^ -14:3:4.666666666666666666666666666666666666667^ -# bug in Calc with '99999' vs $BASE-1 -#10000000000000000000000000000000000000000000000000000000000000000000000000000000000:10000000375084540248994272022843165711074:999999962491547381984643365663244474111576 -12:24:0.5^ -&bmod -# inf handling, see table in doc -0:inf:0 -0:-inf:0 -5:inf:5 -5:-inf:5 --5:inf:-5 --5:-inf:-5 -inf:5:0 --inf:5:0 -inf:-5:0 --inf:-5:0 -5:5:0 --5:-5:0 -inf:inf:NaN --inf:-inf:NaN --inf:inf:NaN -inf:-inf:NaN -8:0:8 -inf:0:inf -# exceptions to reminder rule --inf:0:-inf --8:0:-8 -0:0:NaN -abc:abc:NaN -abc:1:abc:NaN -1:abc:NaN -0:0:NaN -0:1:0 -1:0:1 -0:-1:0 --1:0:-1 -1:1:0 --1:-1:0 -1:-1:0 --1:1:0 -1:2:1 -2:1:0 -1000000000:9:1 -2000000000:9:2 -3000000000:9:3 -4000000000:9:4 -5000000000:9:5 -6000000000:9:6 -7000000000:9:7 -8000000000:9:8 -9000000000:9:0 -35500000:113:33 -71000000:226:66 -106500000:339:99 -1000000000:3:1 -10:5:0 -100:4:0 -1000:8:0 -10000:16:0 -999999999999:9:0 -999999999999:99:0 -999999999999:999:0 -999999999999:9999:0 -999999999999999:99999:0 --9:+5:1 -+9:-5:-1 --9:-5:-4 --5:3:1 --2:3:1 -4:3:1 -1:3:1 --5:-3:-2 --2:-3:-2 -4:-3:-2 -1:-3:-2 -4095:4095:0 -100041000510123:3:0 -152403346:12345:4321 -9:5:4 -&bgcd -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:0 -+0:+1:1 -+1:+0:1 -+1:+1:1 -+2:+3:1 -+3:+2:1 --3:+2:1 -100:625:25 -4096:81:1 -1034:804:2 -27:90:56:1 -27:90:54:9 -&blcm -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:NaN -+1:+0:0 -+0:+1:0 -+27:+90:270 -+1034:+804:415668 -&band -abc:abc:NaN -abc:0:NaN -0:abc:NaN -1:2:0 -3:2:2 -+8:+2:0 -+281474976710656:0:0 -+281474976710656:1:0 -+281474976710656:+281474976710656:281474976710656 --2:-3:-4 --1:-1:-1 --6:-6:-6 --7:-4:-8 --7:4:0 --4:7:4 -# equal arguments are treated special, so also do some test with unequal ones -0xFFFF:0xFFFF:0x0xFFFF -0xFFFFFF:0xFFFFFF:0x0xFFFFFF -0xFFFFFFFF:0xFFFFFFFF:0x0xFFFFFFFF -0xFFFFFFFFFF:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0xF0F0:0xF0F0:0x0xF0F0 -0x0F0F:0x0F0F:0x0x0F0F -0xF0F0F0:0xF0F0F0:0x0xF0F0F0 -0x0F0F0F:0x0F0F0F:0x0x0F0F0F -0xF0F0F0F0:0xF0F0F0F0:0x0xF0F0F0F0 -0x0F0F0F0F:0x0F0F0F0F:0x0x0F0F0F0F -0xF0F0F0F0F0:0xF0F0F0F0F0:0x0xF0F0F0F0F0 -0x0F0F0F0F0F:0x0F0F0F0F0F:0x0x0F0F0F0F0F -0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0x0xF0F0F0F0F0F0 -0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0x0x0F0F0F0F0F0F -0x1F0F0F0F0F0F:0x3F0F0F0F0F0F:0x0x1F0F0F0F0F0F -&bior -abc:abc:NaN -abc:0:NaN -0:abc:NaN -1:2:3 -+8:+2:10 -+281474976710656:0:281474976710656 -+281474976710656:1:281474976710657 -+281474976710656:281474976710656:281474976710656 --2:-3:-1 --1:-1:-1 --6:-6:-6 --7:4:-3 --4:7:-1 -# equal arguments are treated special, so also do some test with unequal ones -0xFFFF:0xFFFF:0x0xFFFF -0xFFFFFF:0xFFFFFF:0x0xFFFFFF -0xFFFFFFFF:0xFFFFFFFF:0x0xFFFFFFFF -0xFFFFFFFFFF:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0:0xFFFF:0x0xFFFF -0:0xFFFFFF:0x0xFFFFFF -0:0xFFFFFFFF:0x0xFFFFFFFF -0:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0xFFFF:0:0x0xFFFF -0xFFFFFF:0:0x0xFFFFFF -0xFFFFFFFF:0:0x0xFFFFFFFF -0xFFFFFFFFFF:0:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0:0x0xFFFFFFFFFFFF -0xF0F0:0xF0F0:0x0xF0F0 -0x0F0F:0x0F0F:0x0x0F0F -0xF0F0:0x0F0F:0x0xFFFF -0xF0F0F0:0xF0F0F0:0x0xF0F0F0 -0x0F0F0F:0x0F0F0F:0x0x0F0F0F -0x0F0F0F:0xF0F0F0:0x0xFFFFFF -0xF0F0F0F0:0xF0F0F0F0:0x0xF0F0F0F0 -0x0F0F0F0F:0x0F0F0F0F:0x0x0F0F0F0F -0x0F0F0F0F:0xF0F0F0F0:0x0xFFFFFFFF -0xF0F0F0F0F0:0xF0F0F0F0F0:0x0xF0F0F0F0F0 -0x0F0F0F0F0F:0x0F0F0F0F0F:0x0x0F0F0F0F0F -0x0F0F0F0F0F:0xF0F0F0F0F0:0x0xFFFFFFFFFF -0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0x0xF0F0F0F0F0F0 -0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0x0x0F0F0F0F0F0F -0x0F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF -0x1F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF -&bxor -abc:abc:NaN -abc:0:NaN -0:abc:NaN -1:2:3 -+8:+2:10 -+281474976710656:0:281474976710656 -+281474976710656:1:281474976710657 -+281474976710656:281474976710656:0 --2:-3:3 --1:-1:0 --6:-6:0 --7:4:-3 --4:7:-5 -4:-7:-3 --4:-7:5 -# equal arguments are treated special, so also do some test with unequal ones -0xFFFF:0xFFFF:0 -0xFFFFFF:0xFFFFFF:0 -0xFFFFFFFF:0xFFFFFFFF:0 -0xFFFFFFFFFF:0xFFFFFFFFFF:0 -0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0 -0:0xFFFF:0x0xFFFF -0:0xFFFFFF:0x0xFFFFFF -0:0xFFFFFFFF:0x0xFFFFFFFF -0:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0xFFFF:0:0x0xFFFF -0xFFFFFF:0:0x0xFFFFFF -0xFFFFFFFF:0:0x0xFFFFFFFF -0xFFFFFFFFFF:0:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0:0x0xFFFFFFFFFFFF -0xF0F0:0xF0F0:0 -0x0F0F:0x0F0F:0 -0xF0F0:0x0F0F:0x0xFFFF -0xF0F0F0:0xF0F0F0:0 -0x0F0F0F:0x0F0F0F:0 -0x0F0F0F:0xF0F0F0:0x0xFFFFFF -0xF0F0F0F0:0xF0F0F0F0:0 -0x0F0F0F0F:0x0F0F0F0F:0 -0x0F0F0F0F:0xF0F0F0F0:0x0xFFFFFFFF -0xF0F0F0F0F0:0xF0F0F0F0F0:0 -0x0F0F0F0F0F:0x0F0F0F0F0F:0 -0x0F0F0F0F0F:0xF0F0F0F0F0:0x0xFFFFFFFFFF -0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0 -0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0 -0x0F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF -&bnot -abc:NaN -+0:-1 -+8:-9 -+281474976710656:-281474976710657 --1:0 --2:1 --12:11 -&digit -0:0:0 -12:0:2 -12:1:1 -123:0:3 -123:1:2 -123:2:1 -123:-1:1 -123:-2:2 -123:-3:3 -123456:0:6 -123456:1:5 -123456:2:4 -123456:3:3 -123456:4:2 -123456:5:1 -123456:-1:1 -123456:-2:2 -123456:-3:3 -100000:-3:0 -100000:0:0 -100000:1:0 -&mantissa -abc:NaN -1e4:1 -2e0:2 -123:123 --1:-1 --2:-2 -+inf:inf --inf:-inf -&exponent -abc:NaN -1e4:4 -2e0:0 -123:0 --1:0 --2:0 -0:1 -+inf:inf --inf:inf -&parts -abc:NaN,NaN -1e4:1,4 -2e0:2,0 -123:123,0 --1:-1,0 --2:-2,0 -0:0,1 -+inf:inf,inf --inf:-inf,inf -&bfac --1:NaN -NaNfac:NaN -+inf:inf --inf:NaN -0:1 -1:1 -2:2 -3:6 -4:24 -5:120 -6:720 -10:3628800 -11:39916800 -12:479001600 -&bpow -abc:12:NaN -12:abc:NaN -0:0:1 -0:1:0 -0:2:0 -0:-1:inf -0:-2:inf -1:0:1 -1:1:1 -1:2:1 -1:3:1 -1:-1:1 -1:-2:1 -1:-3:1 -2:0:1 -2:1:2 -2:2:4 -2:3:8 -3:3:27 -2:-1:0.5^ --2:-1:-0.5^ -2:-2:0.25^ -# Y is even => result positive --2:-2:0.25^ -# Y is odd => result negative --2:-3:-0.125^ -+inf:1234500012:inf --inf:1234500012:inf --inf:1234500013:-inf -+inf:-12345000123:inf --inf:-12345000123:-inf -# 1 ** -x => 1 / (1 ** x) --1:0:1 --2:0:1 --1:1:-1 --1:2:1 --1:3:-1 --1:4:1 --1:5:-1 --1:-1:-1 --1:-2:1 --1:-3:-1 --1:-4:1 --2:2:4 --2:3:-8 --2:4:16 --2:5:-32 --3:2:9 --3:3:-27 --3:4:81 --3:5:-243 -10:2:100 -10:3:1000 -10:4:10000 -10:5:100000 -10:6:1000000 -10:7:10000000 -10:8:100000000 -10:9:1000000000 -10:20:100000000000000000000 -123456:2:15241383936 -#2:0.5:1.41^ -&length -100:3 -10:2 -1:1 -0:1 -12345:5 -10000000000000000:17 --123:3 -215960156869840440586892398248:30 -# broot always upgrades -&broot -144:2:12^ -123:2:11.09053650640941716205160010260993291846^ -# bsqrt always upgrades -&bsqrt -145:12.04159457879229548012824103037860805243^ -144:12^ -143:11.95826074310139802112984075619561661399^ -16:4 -170:13.03840481040529742916594311485836883306^ -169:13 -168:12.96148139681572046193193487217599331541^ -4:2 -3:1.732050807568877293527446341505872366943^ -2:1.41421356237309504880168872420969807857^ -9:3 -12:3.464101615137754587054892683011744733886^ -256:16 -100000000:10000 -4000000000000:2000000 -152399026:12345.00004050222755607815159966235881398^ -152399025:12345 -152399024:12344.99995949777231103967404745303741942^ -1:1 -0:0 --2:NaN --123:NaN -Nan:NaN -+inf:inf --inf:NaN -&bround -$round_mode('trunc') -0:12:0 -NaNbround:12:NaN -+inf:12:inf --inf:12:-inf -1234:0:1234 -1234:2:1200 -123456:4:123400 -123456:5:123450 -123456:6:123456 -+10123456789:5:10123000000 --10123456789:5:-10123000000 -+10123456789:9:10123456700 --10123456789:9:-10123456700 -+101234500:6:101234000 --101234500:6:-101234000 -#+101234500:-4:101234000 -#-101234500:-4:-101234000 -$round_mode('zero') -+20123456789:5:20123000000 --20123456789:5:-20123000000 -+20123456789:9:20123456800 --20123456789:9:-20123456800 -+201234500:6:201234000 --201234500:6:-201234000 -#+201234500:-4:201234000 -#-201234500:-4:-201234000 -+12345000:4:12340000 --12345000:4:-12340000 -$round_mode('+inf') -+30123456789:5:30123000000 --30123456789:5:-30123000000 -+30123456789:9:30123456800 --30123456789:9:-30123456800 -+301234500:6:301235000 --301234500:6:-301234000 -#+301234500:-4:301235000 -#-301234500:-4:-301234000 -+12345000:4:12350000 --12345000:4:-12340000 -$round_mode('-inf') -+40123456789:5:40123000000 --40123456789:5:-40123000000 -+40123456789:9:40123456800 --40123456789:9:-40123456800 -+401234500:6:401234000 -+401234500:6:401234000 -#-401234500:-4:-401235000 -#-401234500:-4:-401235000 -+12345000:4:12340000 --12345000:4:-12350000 -$round_mode('odd') -+50123456789:5:50123000000 --50123456789:5:-50123000000 -+50123456789:9:50123456800 --50123456789:9:-50123456800 -+501234500:6:501235000 --501234500:6:-501235000 -#+501234500:-4:501235000 -#-501234500:-4:-501235000 -+12345000:4:12350000 --12345000:4:-12350000 -$round_mode('even') -+60123456789:5:60123000000 --60123456789:5:-60123000000 -+60123456789:9:60123456800 --60123456789:9:-60123456800 -+601234500:6:601234000 --601234500:6:-601234000 -#+601234500:-4:601234000 -#-601234500:-4:-601234000 -#-601234500:-9:0 -#-501234500:-9:0 -#-601234500:-8:0 -#-501234500:-8:0 -+1234567:7:1234567 -+1234567:6:1234570 -+12345000:4:12340000 --12345000:4:-12340000 -&is_zero -0:1 -NaNzero:0 -+inf:0 --inf:0 -123:0 --1:0 -1:0 -&is_one -0:0 -NaNone:0 -+inf:0 --inf:0 -1:1 -2:0 --1:0 --2:0 -# floor and ceil tests are pretty pointless in integer space...but play safe -&bfloor -0:0 -NaNfloor:NaN -+inf:inf --inf:-inf --1:-1 --2:-2 -2:2 -3:3 -abc:NaN -&bceil -NaNceil:NaN -+inf:inf --inf:-inf -0:0 --1:-1 --2:-2 -2:2 -3:3 -abc:NaN -&as_hex -128:0x80 --128:-0x80 -0:0x0 --0:0x0 -1:0x1 -0x123456789123456789:0x123456789123456789 -+inf:inf --inf:-inf -NaNas_hex:NaN -&as_bin -128:0b10000000 --128:-0b10000000 -0:0b0 --0:0b0 -1:0b1 -0b1010111101010101010110110110110110101:0b1010111101010101010110110110110110101 -+inf:inf --inf:-inf -NaNas_bin:NaN diff --git a/cpan/Math-BigInt/t/upgrade.t b/cpan/Math-BigInt/t/upgrade.t deleted file mode 100644 index 20d8990c55..0000000000 --- a/cpan/Math-BigInt/t/upgrade.t +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/perl -w - -use Test; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/upgrade.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 2112 - + 2; # our own tests - } - -use Math::BigInt upgrade => 'Math::BigFloat'; -use Math::BigFloat; - -use vars qw ($scale $class $try $x $y $f @args $ans $ans1 $ans1_str $setup - $ECL $CL); -$class = "Math::BigInt"; -$CL = "Math::BigInt::Calc"; -$ECL = "Math::BigFloat"; - -ok (Math::BigInt->upgrade(),'Math::BigFloat'); -ok (Math::BigInt->downgrade()||'',''); - -require 'upgrade.inc'; # all tests here for sharing diff --git a/cpan/Math-BigInt/t/upgradef.t b/cpan/Math-BigInt/t/upgradef.t deleted file mode 100644 index 437268db65..0000000000 --- a/cpan/Math-BigInt/t/upgradef.t +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/perl -w - -use Test; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/upgradef.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 0 - + 6; # our own tests - } - -############################################################################### -package Math::BigFloat::Test; - -use Math::BigFloat; -require Exporter; -use vars qw/@ISA/; -@ISA = qw/Exporter Math::BigFloat/; - -use overload; - -sub isa - { - my ($self,$class) = @_; - return if $class =~ /^Math::Big(Int|Float)/; # we aren't one of these - UNIVERSAL::isa($self,$class); - } - -sub bmul - { - return __PACKAGE__->new(123); - } - -sub badd - { - return __PACKAGE__->new(321); - } - -############################################################################### -package main; - -# use Math::BigInt upgrade => 'Math::BigFloat'; -use Math::BigFloat upgrade => 'Math::BigFloat::Test'; - -use vars qw ($scale $class $try $x $y $z $f @args $ans $ans1 $ans1_str $setup - $ECL $CL); -$class = "Math::BigFloat"; -$CL = "Math::BigInt::Calc"; -$ECL = "Math::BigFloat::Test"; - -ok (Math::BigFloat->upgrade(),$ECL); -ok (Math::BigFloat->downgrade()||'',''); - -$x = $class->new(123); $y = $ECL->new(123); $z = $x->bmul($y); -ok (ref($z),$ECL); ok ($z,123); - -$x = $class->new(123); $y = $ECL->new(123); $z = $x->badd($y); -ok (ref($z),$ECL); ok ($z,321); - - - -# not yet: -# require 'upgrade.inc'; # all tests here for sharing diff --git a/cpan/Math-BigInt/t/use.t b/cpan/Math-BigInt/t/use.t deleted file mode 100644 index 1f09f5e43c..0000000000 --- a/cpan/Math-BigInt/t/use.t +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/perl -w - -# use Module(); doesn't call import() - thanx for cpan testers David. M. Town -# and Andreas Marcel Riechert for spotting it. It is fixed by the same code -# that fixes require Math::BigInt, but we make a test to be sure it really -# works. - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/use.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 1; - } - -my ($try,$ans,$x); - -use Math::BigInt(); $x = Math::BigInt->new(1); ++$x; - -ok ($x||'undef',2); - -# all tests done - -1; - diff --git a/cpan/Math-BigInt/t/use_lib1.t b/cpan/Math-BigInt/t/use_lib1.t deleted file mode 100644 index a6eda82b37..0000000000 --- a/cpan/Math-BigInt/t/use_lib1.t +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/perl -w - -# see if using Math::BigInt and Math::BigFloat works together nicely. -# all use_lib*.t should be equivalent - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/use_lib1.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 2; - } - -use Math::BigFloat lib => 'BareCalc'; - -ok (Math::BigInt->config()->{lib},'Math::BigInt::BareCalc'); - -ok (Math::BigFloat->new(123)->badd(123),246); - diff --git a/cpan/Math-BigInt/t/use_lib2.t b/cpan/Math-BigInt/t/use_lib2.t deleted file mode 100644 index aa4ba5fbe2..0000000000 --- a/cpan/Math-BigInt/t/use_lib2.t +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/perl -w - -# see if using Math::BigInt and Math::BigFloat works together nicely. -# all use_lib*.t should be equivalent - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/use_lib2.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 2; - } - -use Math::BigInt; -use Math::BigFloat lib => 'BareCalc'; - -ok (Math::BigInt->config()->{lib},'Math::BigInt::BareCalc'); - -ok (Math::BigFloat->new(123)->badd(123),246); - diff --git a/cpan/Math-BigInt/t/use_lib3.t b/cpan/Math-BigInt/t/use_lib3.t deleted file mode 100644 index b46b939551..0000000000 --- a/cpan/Math-BigInt/t/use_lib3.t +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/perl -w - -# see if using Math::BigInt and Math::BigFloat works together nicely. -# all use_lib*.t should be equivalent - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/use_lib3.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 2; - } - -use Math::BigInt lib => 'BareCalc'; -use Math::BigFloat; - -ok (Math::BigInt->config()->{lib},'Math::BigInt::BareCalc'); - -ok (Math::BigFloat->new(123)->badd(123),246); - diff --git a/cpan/Math-BigInt/t/use_lib4.t b/cpan/Math-BigInt/t/use_lib4.t deleted file mode 100644 index bfd85d5f70..0000000000 --- a/cpan/Math-BigInt/t/use_lib4.t +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/perl -w - -# see if using Math::BigInt and Math::BigFloat works together nicely. -# all use_lib*.t should be equivalent, except this, since the later overrides -# the former lib statement - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/use_lib4.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 2; - } - -use Math::BigInt lib => 'BareCalc'; -use Math::BigFloat lib => 'Calc'; - -ok (Math::BigInt->config()->{lib},'Math::BigInt::Calc'); - -ok (Math::BigFloat->new(123)->badd(123),246); - diff --git a/cpan/Math-BigInt/t/use_mbfw.t b/cpan/Math-BigInt/t/use_mbfw.t deleted file mode 100644 index c6a047143e..0000000000 --- a/cpan/Math-BigInt/t/use_mbfw.t +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/perl -w - -# check that using BigFloat with "with" and "lib" at the same time works -# broken in versions up to v1.63 - -use strict; -use Test; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/use_mbfw.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, qw(../lib); # to locate the modules - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 2; - } - - -# the replacement lib can handle the lib statement, but it could also ignore -# it completely, for instance, when it is a 100% replacement for BigInt, but -# doesn't know the concept of alternative libs. But it still needs to cope -# with "lib => ". SubClass does record it, so we test here essential if -# BigFloat hands the lib properly down, any more is outside out testing reach. - -use Math::BigFloat with => 'Math::BigInt::Subclass', lib => 'BareCalc'; - -ok (Math::BigFloat->config()->{with}, 'Math::BigInt::BareCalc' ); - -# ok ($Math::BigInt::Subclass::lib, 'BareCalc' ); - -# it never arrives here, but that is a design decision in SubClass -ok (Math::BigInt->config->{lib}, 'Math::BigInt::BareCalc' ); - -# all tests done - diff --git a/cpan/Math-BigInt/t/with_sub.t b/cpan/Math-BigInt/t/with_sub.t deleted file mode 100644 index 878fe07f1f..0000000000 --- a/cpan/Math-BigInt/t/with_sub.t +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/perl -w - -# Test use Math::BigFloat with => 'Math::BigInt::SomeSubclass'; - -use Test; -use strict; - -BEGIN - { - $| = 1; - # to locate the testing files - my $location = $0; $location =~ s/with_sub.t//i; - if ($ENV{PERL_CORE}) - { - # testing with the core distribution - @INC = qw(../t/lib); - } - unshift @INC, '../lib'; - if (-d 't') - { - chdir 't'; - require File::Spec; - unshift @INC, File::Spec->catdir(File::Spec->updir, $location); - } - else - { - unshift @INC, $location; - } - print "# INC = @INC\n"; - - plan tests => 2308 - + 1; - } - -use Math::BigFloat with => 'Math::BigInt::Subclass', lib => 'Calc'; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigFloat"; -$CL = "Math::BigInt::Calc"; - -# the with argument is ignored -ok (Math::BigFloat->config()->{with}, 'Math::BigInt::Calc'); - -require 'bigfltpm.inc'; # all tests here for sharing |