diff options
author | Steve Hay <steve.m.hay@googlemail.com> | 2018-07-03 12:18:02 +0100 |
---|---|---|
committer | Steve Hay <steve.m.hay@googlemail.com> | 2018-07-04 13:12:31 +0100 |
commit | 3cc1ad36861cc317127383e977487e01ef7e2311 (patch) | |
tree | a56b206bcc7758645b47a7a26aad9f035370bb5a /cpan/Math-BigRat | |
parent | 8c2a913217b49976911e8fb13834c833f3b2640e (diff) | |
download | perl-3cc1ad36861cc317127383e977487e01ef7e2311.tar.gz |
Upgrade Math::BigRat from version 0.2613 to 0.2614
Diffstat (limited to 'cpan/Math-BigRat')
-rw-r--r-- | cpan/Math-BigRat/lib/Math/BigRat.pm | 254 | ||||
-rw-r--r-- | cpan/Math-BigRat/t/Math/BigRat/Test.pm | 2 | ||||
-rw-r--r-- | cpan/Math-BigRat/t/bigfltpm.inc | 33 | ||||
-rw-r--r-- | cpan/Math-BigRat/t/bigfltrt.t | 8 | ||||
-rw-r--r-- | cpan/Math-BigRat/t/biglog.t | 72 | ||||
-rw-r--r-- | cpan/Math-BigRat/t/bigratpm.inc | 356 | ||||
-rw-r--r-- | cpan/Math-BigRat/t/bigratpm.t | 8 | ||||
-rw-r--r-- | cpan/Math-BigRat/t/bigroot.t | 43 | ||||
-rw-r--r-- | cpan/Math-BigRat/t/trap.t | 94 |
9 files changed, 533 insertions, 337 deletions
diff --git a/cpan/Math-BigRat/lib/Math/BigRat.pm b/cpan/Math-BigRat/lib/Math/BigRat.pm index 520b443b01..e3d172836c 100644 --- a/cpan/Math-BigRat/lib/Math/BigRat.pm +++ b/cpan/Math-BigRat/lib/Math/BigRat.pm @@ -16,11 +16,11 @@ use 5.006; use strict; use warnings; -use Carp (); +use Carp qw< carp croak >; use Math::BigFloat 1.999718; -our $VERSION = '0.2613'; +our $VERSION = '0.2614'; our @ISA = qw(Math::BigFloat); @@ -42,7 +42,6 @@ use overload '/' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bdiv($_[0]) : $_[0] -> copy() -> bdiv($_[1]); }, - '%' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bmod($_[0]) : $_[0] -> copy() -> bmod($_[1]); }, @@ -69,7 +68,6 @@ use overload '**=' => sub { $_[0]->bpow($_[1]); }, - '<<=' => sub { $_[0]->blsft($_[1]); }, '>>=' => sub { $_[0]->brsft($_[1]); }, @@ -207,7 +205,7 @@ BEGIN { # only one library loaded) *_e_add = \&Math::BigFloat::_e_add; *_e_sub = \&Math::BigFloat::_e_sub; - *as_int = \&as_number; + *as_number = \&as_int; *is_pos = \&is_positive; *is_neg = \&is_negative; } @@ -226,8 +224,7 @@ $downgrade = undef; $_trap_nan = 0; # are NaNs ok? set w/ config() $_trap_inf = 0; # are infs ok? set w/ config() -# the package we are using for our private parts, defaults to: -# Math::BigInt->config()->{lib} +# the math backend library my $LIB = 'Math::BigInt::Calc'; @@ -249,17 +246,17 @@ sub new { # Check the way we are called. if ($protoref) { - Carp::croak("new() is a class method, not an instance method"); + croak("new() is a class method, not an instance method"); } if (@_ < 1) { - #Carp::carp("Using new() with no argument is deprecated;", + #carp("Using new() with no argument is deprecated;", # " use bzero() or new(0) instead"); return $class -> bzero(); } if (@_ > 2) { - Carp::carp("Superfluous arguments to new() ignored."); + carp("Superfluous arguments to new() ignored."); } # Get numerator and denominator. If any of the arguments is undefined, @@ -270,7 +267,7 @@ sub new { if (@_ == 1 && !defined $n || @_ == 2 && (!defined $n || !defined $d)) { - #Carp::carp("Use of uninitialized value in new()"); + #carp("Use of uninitialized value in new()"); return $class -> bzero(); } @@ -553,7 +550,7 @@ sub bnan { $self = bless {}, $class unless $selfref; if ($_trap_nan) { - Carp::croak ("Tried to set a variable to NaN in $class->bnan()"); + croak ("Tried to set a variable to NaN in $class->bnan()"); } $self -> {sign} = $nan; @@ -577,7 +574,7 @@ sub binf { $sign = defined($sign) && substr($sign, 0, 1) eq '-' ? '-inf' : '+inf'; if ($_trap_inf) { - Carp::croak ("Tried to set a variable to +-inf in $class->binf()"); + croak ("Tried to set a variable to +-inf in $class->binf()"); } $self -> {sign} = $sign; @@ -685,10 +682,10 @@ sub bnorm { # Both parts must be objects of whatever we are using today. if (my $c = $LIB->_check($x->{_n})) { - Carp::croak("n did not pass the self-check ($c) in bnorm()"); + croak("n did not pass the self-check ($c) in bnorm()"); } if (my $c = $LIB->_check($x->{_d})) { - Carp::croak("d did not pass the self-check ($c) in bnorm()"); + croak("d did not pass the self-check ($c) in bnorm()"); } # no normalize for NaN, inf etc. @@ -742,7 +739,7 @@ sub _bnan { # partial object (happens under trap_nan), so fix it beforehand $self->{_d} = $LIB->_zero() unless defined $self->{_d}; $self->{_n} = $LIB->_zero() unless defined $self->{_n}; - Carp::croak ("Tried to set $self to NaN in $class\::_bnan()"); + croak ("Tried to set $self to NaN in $class\::_bnan()"); } $self->{_n} = $LIB->_zero(); $self->{_d} = $LIB->_zero(); @@ -758,7 +755,7 @@ sub _binf { # partial object (happens under trap_nan), so fix it beforehand $self->{_d} = $LIB->_zero() unless defined $self->{_d}; $self->{_n} = $LIB->_zero() unless defined $self->{_n}; - Carp::croak ("Tried to set $self to inf in $class\::_binf()"); + croak ("Tried to set $self to inf in $class\::_binf()"); } $self->{_n} = $LIB->_zero(); $self->{_d} = $LIB->_zero(); @@ -1137,9 +1134,11 @@ sub is_one { # return true if arg (BRAT or num_str) is +1 or -1 if signis given my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_); - my $sign = $_[2] || ''; $sign = '+' if $sign ne '-'; - return 1 - if ($x->{sign} eq $sign && $LIB->_is_one($x->{_n}) && $LIB->_is_one($x->{_d})); + croak "too many arguments for is_one()" if @_ > 2; + my $sign = $_[1] || ''; + $sign = '+' if $sign ne '-'; + return 1 if ($x->{sign} eq $sign && + $LIB->_is_one($x->{_n}) && $LIB->_is_one($x->{_d})); 0; } @@ -1280,69 +1279,62 @@ sub bpow { ($class, $x, $y, @r) = objectify(2, @_); } - return $x if $x->{sign} =~ /^[+-]inf$/; # -inf/+inf ** x - return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan; - return $x->bone(@r) if $y->is_zero(); - return $x->round(@r) if $x->is_one() || $y->is_one(); + # $x and/or $y is a NaN + return $x->bnan() if $x->is_nan() || $y->is_nan(); - if ($x->{sign} eq '-' && $LIB->_is_one($x->{_n}) && $LIB->_is_one($x->{_d})) { - # if $x == -1 and odd/even y => +1/-1 - return $y->is_odd() ? $x->round(@r) : $x->babs()->round(@r); - # my Casio FX-5500L has a bug here: -1 ** 2 is -1, but -1 * -1 is 1; + # $x and/or $y is a +/-Inf + if ($x->is_inf("-")) { + return $x->bzero() if $y->is_negative(); + return $x->bnan() if $y->is_zero(); + return $x if $y->is_odd(); + return $x->bneg(); + } elsif ($x->is_inf("+")) { + return $x->bzero() if $y->is_negative(); + return $x->bnan() if $y->is_zero(); + return $x; + } elsif ($y->is_inf("-")) { + return $x->bnan() if $x -> is_one("-"); + return $x->binf("+") if $x > -1 && $x < 1; + return $x->bone() if $x -> is_one("+"); + return $x->bzero(); + } elsif ($y->is_inf("+")) { + return $x->bnan() if $x -> is_one("-"); + return $x->bzero() if $x > -1 && $x < 1; + return $x->bone() if $x -> is_one("+"); + return $x->binf("+"); + } + + if ($x->is_zero()) { + return $x->binf() if $y->is_negative(); + return $x->bone("+") if $y->is_zero(); + return $x; + } elsif ($x->is_one()) { + return $x->round(@r) if $y->is_odd(); # x is -1, y is odd => -1 + return $x->babs()->round(@r); # x is -1, y is even => 1 + } elsif ($y->is_zero()) { + return $x->bone(@r); # x^0 and x != 0 => 1 + } elsif ($y->is_one()) { + return $x->round(@r); # x^1 => x } - # 1 ** -y => 1 / (1 ** |y|) - # so do test for negative $y after above's clause - return $x->round(@r) if $x->is_zero(); # 0**y => 0 (if not y <= 0) - - # shortcut if y == 1/N (is then sqrt() respective broot()) - if ($LIB->_is_one($y->{_n})) { - return $x->bsqrt(@r) if $LIB->_is_two($y->{_d}); # 1/2 => sqrt - return $x->broot($LIB->_str($y->{_d}), @r); # 1/N => root(N) - } + # we don't support complex numbers, so return NaN + return $x->bnan() if $x->is_negative() && !$y->is_int(); - # shortcut y/1 (and/or x/1) - if ($LIB->_is_one($y->{_d})) { - # shortcut for x/1 and y/1 - if ($LIB->_is_one($x->{_d})) { - $x->{_n} = $LIB->_pow($x->{_n}, $y->{_n}); # x/1 ** y/1 => (x ** y)/1 - if ($y->{sign} eq '-') { - # 0.2 ** -3 => 1/(0.2 ** 3) - ($x->{_n}, $x->{_d}) = ($x->{_d}, $x->{_n}); # swap - } - # correct sign; + ** + => + - if ($x->{sign} eq '-') { - # - * - => +, - * - * - => - - $x->{sign} = '+' if $x->{sign} eq '-' && $LIB->_is_even($y->{_n}); - } - return $x->round(@r); - } + # (a/b)^-(c/d) = (b/a)^(c/d) + ($x->{_n}, $x->{_d}) = ($x->{_d}, $x->{_n}) if $y->is_negative(); - # x/z ** y/1 - $x->{_n} = $LIB->_pow($x->{_n}, $y->{_n}); # 5/2 ** y/1 => 5 ** y / 2 ** y + unless ($LIB->_is_one($y->{_n})) { + $x->{_n} = $LIB->_pow($x->{_n}, $y->{_n}); $x->{_d} = $LIB->_pow($x->{_d}, $y->{_n}); - if ($y->{sign} eq '-') { - # 0.2 ** -3 => 1/(0.2 ** 3) - ($x->{_n}, $x->{_d}) = ($x->{_d}, $x->{_n}); # swap - } - # correct sign; + ** + => + - $x->{sign} = '+' if $x->{sign} eq '-' && $LIB->_is_even($y->{_n}); - return $x->round(@r); } - # print STDERR "# $x $y\n"; - - # otherwise: - - # n/d n ______________ - # a/b = -\/ (a/b) ** d - - # (a/b) ** n == (a ** n) / (b ** n) - $LIB->_pow($x->{_n}, $y->{_n}); - $LIB->_pow($x->{_d}, $y->{_n}); + unless ($LIB->_is_one($y->{_d})) { + return $x->bsqrt(@r) if $LIB->_is_two($y->{_d}); # 1/2 => sqrt + return $x->broot($LIB->_str($y->{_d}), @r); # 1/N => root(N) + } - return $x->broot($LIB->_str($y->{_d}), @r); # n/d => root(n) + return $x->round(@r); } sub blog { @@ -1616,16 +1608,38 @@ sub bsqrt { return $x if $x->{sign} eq '+inf'; # sqrt(inf) == inf return $x->round(@r) if $x->is_zero() || $x->is_one(); - local $Math::BigFloat::upgrade = undef; + my $n = $x -> {_n}; + my $d = $x -> {_d}; + + # Look for an exact solution. For the numerator and the denominator, take + # the square root and square it and see if we got the original value. If we + # did, for both the numerator and the denominator, we have an exact + # solution. + + { + my $nsqrt = $LIB -> _sqrt($LIB -> _copy($n)); + my $n2 = $LIB -> _mul($LIB -> _copy($nsqrt), $nsqrt); + if ($LIB -> _acmp($n, $n2) == 0) { + my $dsqrt = $LIB -> _sqrt($LIB -> _copy($d)); + my $d2 = $LIB -> _mul($LIB -> _copy($dsqrt), $dsqrt); + if ($LIB -> _acmp($d, $d2) == 0) { + $x -> {_n} = $nsqrt; + $x -> {_d} = $dsqrt; + return $x->round(@r); + } + } + } + + local $Math::BigFloat::upgrade = undef; local $Math::BigFloat::downgrade = undef; local $Math::BigFloat::precision = undef; - local $Math::BigFloat::accuracy = undef; - local $Math::BigInt::upgrade = undef; - local $Math::BigInt::precision = undef; - local $Math::BigInt::accuracy = undef; + local $Math::BigFloat::accuracy = undef; + local $Math::BigInt::upgrade = undef; + local $Math::BigInt::precision = undef; + local $Math::BigInt::accuracy = undef; - my $xn = Math::BigFloat -> new($LIB -> _str($x->{_n})); - my $xd = Math::BigFloat -> new($LIB -> _str($x->{_d})); + my $xn = Math::BigFloat -> new($LIB -> _str($n)); + my $xd = Math::BigFloat -> new($LIB -> _str($d)); my $xtmp = Math::BigRat -> new($xn -> bdiv($xd) -> bsqrt() -> bsstr()); @@ -1671,8 +1685,8 @@ sub band { my $xref = ref($x); my $class = $xref || $x; - Carp::croak 'band() is an instance method, not a class method' unless $xref; - Carp::croak 'Not enough arguments for band()' if @_ < 1; + croak 'band() is an instance method, not a class method' unless $xref; + croak 'Not enough arguments for band()' if @_ < 1; my $y = shift; $y = $class -> new($y) unless ref($y); @@ -1695,8 +1709,8 @@ sub bior { my $xref = ref($x); my $class = $xref || $x; - Carp::croak 'bior() is an instance method, not a class method' unless $xref; - Carp::croak 'Not enough arguments for bior()' if @_ < 1; + croak 'bior() is an instance method, not a class method' unless $xref; + croak 'Not enough arguments for bior()' if @_ < 1; my $y = shift; $y = $class -> new($y) unless ref($y); @@ -1719,8 +1733,8 @@ sub bxor { my $xref = ref($x); my $class = $xref || $x; - Carp::croak 'bxor() is an instance method, not a class method' unless $xref; - Carp::croak 'Not enough arguments for bxor()' if @_ < 1; + croak 'bxor() is an instance method, not a class method' unless $xref; + croak 'Not enough arguments for bxor()' if @_ < 1; my $y = shift; $y = $class -> new($y) unless ref($y); @@ -1743,7 +1757,7 @@ sub bnot { my $xref = ref($x); my $class = $xref || $x; - Carp::croak 'bnot() is an instance method, not a class method' unless $xref; + croak 'bnot() is an instance method, not a class method' unless $xref; my @r = @_; @@ -1852,8 +1866,8 @@ sub beq { my $selfref = ref $self; my $class = $selfref || $self; - Carp::croak 'beq() is an instance method, not a class method' unless $selfref; - Carp::croak 'Wrong number of arguments for beq()' unless @_ == 1; + croak 'beq() is an instance method, not a class method' unless $selfref; + croak 'Wrong number of arguments for beq()' unless @_ == 1; my $cmp = $self -> bcmp(shift); return defined($cmp) && ! $cmp; @@ -1864,8 +1878,8 @@ sub bne { my $selfref = ref $self; my $class = $selfref || $self; - Carp::croak 'bne() is an instance method, not a class method' unless $selfref; - Carp::croak 'Wrong number of arguments for bne()' unless @_ == 1; + croak 'bne() is an instance method, not a class method' unless $selfref; + croak 'Wrong number of arguments for bne()' unless @_ == 1; my $cmp = $self -> bcmp(shift); return defined($cmp) && ! $cmp ? '' : 1; @@ -1876,8 +1890,8 @@ sub blt { my $selfref = ref $self; my $class = $selfref || $self; - Carp::croak 'blt() is an instance method, not a class method' unless $selfref; - Carp::croak 'Wrong number of arguments for blt()' unless @_ == 1; + croak 'blt() is an instance method, not a class method' unless $selfref; + croak 'Wrong number of arguments for blt()' unless @_ == 1; my $cmp = $self -> bcmp(shift); return defined($cmp) && $cmp < 0; @@ -1888,8 +1902,8 @@ sub ble { my $selfref = ref $self; my $class = $selfref || $self; - Carp::croak 'ble() is an instance method, not a class method' unless $selfref; - Carp::croak 'Wrong number of arguments for ble()' unless @_ == 1; + croak 'ble() is an instance method, not a class method' unless $selfref; + croak 'Wrong number of arguments for ble()' unless @_ == 1; my $cmp = $self -> bcmp(shift); return defined($cmp) && $cmp <= 0; @@ -1900,8 +1914,8 @@ sub bgt { my $selfref = ref $self; my $class = $selfref || $self; - Carp::croak 'bgt() is an instance method, not a class method' unless $selfref; - Carp::croak 'Wrong number of arguments for bgt()' unless @_ == 1; + croak 'bgt() is an instance method, not a class method' unless $selfref; + croak 'Wrong number of arguments for bgt()' unless @_ == 1; my $cmp = $self -> bcmp(shift); return defined($cmp) && $cmp > 0; @@ -1912,9 +1926,9 @@ sub bge { my $selfref = ref $self; my $class = $selfref || $self; - Carp::croak 'bge() is an instance method, not a class method' + croak 'bge() is an instance method, not a class method' unless $selfref; - Carp::croak 'Wrong number of arguments for bge()' unless @_ == 1; + croak 'Wrong number of arguments for bge()' unless @_ == 1; my $cmp = $self -> bcmp(shift); return defined($cmp) && $cmp >= 0; @@ -1941,7 +1955,7 @@ sub numify { return $x->{sign} eq '-' ? 0 - $abs : 0 + $abs; } -sub as_number { +sub as_int { my ($self, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_); # NaN, inf etc @@ -2076,13 +2090,13 @@ sub import { # LIB already loaded, so feed it our lib arguments Math::BigInt->import(@import); - $LIB = Math::BigFloat->config()->{lib}; + $LIB = Math::BigFloat->config("lib"); # register us with LIB to get notified of future lib changes Math::BigInt::_register_callback($class, sub { $LIB = $_[0]; }); - # any non :constant stuff is handled by our parent, Exporter (loaded - # by Math::BigFloat, even if @_ is empty, to give it a chance + # any non :constant stuff is handled by Exporter (loaded by parent class) + # even if @_ is empty, to give it a chance $class->SUPER::import(@a); # for subclasses $class->export_to_level(1, $class, @a); # need this, too } @@ -2193,7 +2207,7 @@ BigInts. Returns the object as a scalar. This will lose some data if the object cannot be represented by a normal Perl scalar (integer or float), so -use L<as_int()|/"as_int()/as_number()"> or L</as_float()> instead. +use L</as_int()> or L</as_float()> instead. This routine is automatically used whenever a scalar is required: @@ -2201,7 +2215,9 @@ This routine is automatically used whenever a scalar is required: @array = (0, 1, 2, 3); $y = $array[$x]; # set $y to 3 -=item as_int()/as_number() +=item as_int() + +=item as_number() $x = Math::BigRat->new('13/7'); print $x->as_int(), "\n"; # '1' @@ -2642,21 +2658,19 @@ This method was added in v0.20 of Math::BigRat (May 2007). =item config() - use Data::Dumper; - - print Dumper ( Math::BigRat->config() ); - print Math::BigRat->config()->{lib}, "\n"; + Math::BigRat->config("trap_nan" => 1); # set + $accu = Math::BigRat->config("accuracy"); # get -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. +Set or get configuration parameter values. Read-only parameters are marked as +RO. Read-write parameters are marked as RW. The following parameters are +supported. - key RO/RW Description + Parameter RO/RW Description Example ============================================================ - lib RO Name of the Math library + lib RO Name of the math backend library Math::BigInt::Calc - lib_version RO Version of 'lib' + lib_version RO Version of the math backend library 0.30 class RO The class of config you just called Math::BigRat @@ -2672,17 +2686,13 @@ appropriate information. undef round_mode RW Global round mode even - div_scale RW Fallback accuracy for div + div_scale RW Fallback accuracy for div, sqrt etc. 40 - trap_nan RW Trap creation of NaN (undef = no) + trap_nan RW Trap NaNs undef - trap_inf RW Trap creation of +inf/-inf (undef = no) + trap_inf RW Trap +inf/-inf undef -By passing a reference to a hash you may set the configuration values. This -works only for values that a marked with a C<RW> above, anything else is -read-only. - =back =head1 BUGS diff --git a/cpan/Math-BigRat/t/Math/BigRat/Test.pm b/cpan/Math-BigRat/t/Math/BigRat/Test.pm index c61b65289b..820406b448 100644 --- a/cpan/Math-BigRat/t/Math/BigRat/Test.pm +++ b/cpan/Math-BigRat/t/Math/BigRat/Test.pm @@ -11,7 +11,7 @@ use Math::BigFloat; our @ISA = qw(Math::BigRat Exporter); our $VERSION = '0.04'; -use overload; # inherit overload from BigRat +use overload; # inherit overload from BigRat # Globals our $accuracy = undef; diff --git a/cpan/Math-BigRat/t/bigfltpm.inc b/cpan/Math-BigRat/t/bigfltpm.inc index 90dea8efb4..b3dcaa606e 100644 --- a/cpan/Math-BigRat/t/bigfltpm.inc +++ b/cpan/Math-BigRat/t/bigfltpm.inc @@ -10,16 +10,16 @@ my $z; while (<DATA>) { chomp; - $_ =~ s/#.*$//; # remove comments - $_ =~ s/\s+$//; # trailing spaces - next if /^$/; # skip empty lines & comments + $_ =~ 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 + $setup = $_; $setup =~ s/\$/\$${class}::/g; # round_mode, div_scale #print "\$setup== $setup\n"; } else @@ -163,13 +163,13 @@ while (<DATA>) { 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"; + { + # 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); - } + if !ok ($CL->_zeros( $ans1->{_m}), 0); + } } } # end pattern or string } @@ -193,9 +193,9 @@ my $monster = '1e1234567890123456789012345678901234567890'; # new and exponent ok ($class->new($monster)->bsstr(), - '1e+1234567890123456789012345678901234567890'); + '1e+1234567890123456789012345678901234567890'); ok ($class->new($monster)->exponent(), - '1234567890123456789012345678901234567890'); + '1234567890123456789012345678901234567890'); # cmp ok ($class->new($monster) > 0,1); @@ -207,7 +207,7 @@ ok ($class->new($monster)->bmul(2)->bsstr(), # mantissa $monster = '1234567890123456789012345678901234567890e2'; ok ($class->new($monster)->mantissa(), - '123456789012345678901234567890123456789'); + '123456789012345678901234567890123456789'); ############################################################################### # zero,inf,one,nan @@ -230,7 +230,7 @@ ok ($class->finf('+'),'inf'); ok ($class->finf('-'),'-inf'); ok ($class->finf('-inf'),'-inf'); -$class->accuracy(undef); $class->precision(undef); # reset +$class->accuracy(undef); $class->precision(undef); # reset ############################################################################### # bug in bsstr()/numify() showed up in after-rounding in bdiv() @@ -243,7 +243,6 @@ 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); @@ -343,10 +342,10 @@ ok ($x, 2); $x = $class->new('2'); $y = $class->new('18.2'); -$x <<= $y; # 2 * (2 ** 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 >>= $y, 2); # 2 * (2 ** 18.2) / (2 ** 18.2) => 2 ok ($x, 2); 1; # all done diff --git a/cpan/Math-BigRat/t/bigfltrt.t b/cpan/Math-BigRat/t/bigfltrt.t index 4f36bde3ea..856318614f 100644 --- a/cpan/Math-BigRat/t/bigfltrt.t +++ b/cpan/Math-BigRat/t/bigfltrt.t @@ -7,13 +7,13 @@ use lib 't'; use Test::More tests => 1; -use Math::BigRat::Test lib => 'Calc'; # test via this Subclass +use Math::BigRat::Test lib => 'Calc'; # test via this Subclass -our ($CLASS, $CALC); +our ($CLASS, $LIB); $CLASS = "Math::BigRat::Test"; -$CALC = "Math::BigInt::Calc"; +$LIB = "Math::BigInt::Calc"; pass(); # fails still too many tests -#require 't/bigfltpm.inc'; # all tests here for sharing +#require './t/bigfltpm.inc'; # all tests here for sharing diff --git a/cpan/Math-BigRat/t/biglog.t b/cpan/Math-BigRat/t/biglog.t deleted file mode 100644 index 44f5962cad..0000000000 --- a/cpan/Math-BigRat/t/biglog.t +++ /dev/null @@ -1,72 +0,0 @@ -#!perl - -# Test blog function (and bpow, since it uses blog), as well as bexp(). - -use strict; -use warnings; - -use Test::More tests => 17; - -use Math::BigRat; - -my $cl = "Math::BigRat"; - -############################################################################# -# test log($n) - -# does not work yet -#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) - -is($cl->new(1)->bexp()->as_int(), '2', qq|$cl->new(1)->bexp()->as_int()|); -is($cl->new(2)->bexp()->as_int(), '7', qq|$cl->new(1)->bexp()->as_int()|); -is($cl->new(3)->bexp()->as_int(), '20', qq|$cl->new(1)->bexp()->as_int()|); - -# rounding not implemented yet -#is ($cl->new(3)->bexp(10), '20', "bexp(3,10)"); - -# $x < 0 => NaN -is($cl->new(-2)->blog(), 'NaN', qq|$cl->new(-2)->blog()|); -is($cl->new(-1)->blog(), 'NaN', qq|$cl->new(-1)->blog()|); -is($cl->new(-10)->blog(), 'NaN', qq|$cl->new(-10)->blog()|); -is($cl->new(-2,2)->blog(), 'NaN', qq|$cl->new(-2,2)->blog()|); - -############################################################################# -# test bexp() with cached results - -is($cl->new(1)->bexp(), - '90933395208605785401971970164779391644753259799242' . '/' . - '33452526613163807108170062053440751665152000000000', - 'bexp(1)'); -is($cl->new(2)->bexp(1,40), $cl->new(1)->bexp(1,45)->bpow(2,40), 'bexp(2)'); - -is($cl->new("12.5")->bexp(1,61), $cl->new(1)->bexp(1,65)->bpow(12.5,61), 'bexp(12.5)'); - -############################################################################# -# test bexp() with big values (non-cached) - -is($cl->new(1)->bexp(1,100)->as_float(100), - '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427', - 'bexp(100)'); - -is($cl->new("12.5")->bexp(1,91), $cl->new(1)->bexp(1,95)->bpow(12.5,91), - 'bexp(12.5) to 91 digits'); - -############################################################################# -# 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)'); diff --git a/cpan/Math-BigRat/t/bigratpm.inc b/cpan/Math-BigRat/t/bigratpm.inc index a031919bc8..b3015360b8 100644 --- a/cpan/Math-BigRat/t/bigratpm.inc +++ b/cpan/Math-BigRat/t/bigratpm.inc @@ -3,9 +3,9 @@ use strict; use warnings; -our ($CLASS, $try, $x, $y, $z, $f, @args, $want, $got, $setup, $CALC); +our ($CLASS, $try, $x, $y, $z, $f, @args, $want, $got, $setup, $LIB); -is($CLASS->config()->{lib}, $CALC); +is($CLASS->config()->{lib}, $LIB); $setup = ''; @@ -40,8 +40,6 @@ while (<DATA>) { } elsif ($f eq "finf") { my $a = $args[1] || ''; $try .= qq| \$x->binf("$a");|; - } elsif ($f eq "is_inf") { - $try .= qq| \$x->is_inf("$args[1]");|; } elsif ($f eq "fone") { $try .= qq| \$x->bone("$args[1]");|; } elsif ($f eq "fstr") { @@ -66,8 +64,11 @@ while (<DATA>) { } elsif ($f =~ /^(log|exp|sin|cos|atan2|int|neg|abs|sqrt)$/) { $try .= " \$x = $f(\$x);"; # some is_xxx test function - } elsif ($f =~ /^is_(zero|one|pos|neg|negative|positive|odd|even|nan|int)\z/) { + } elsif ($f =~ /^is_(zero|pos|neg|negative|positive|odd|even|nan|int)\z/) { $try .= " \$x->$f();"; + } elsif ($f =~ /^is_(one|inf)$/) { + $try .= @args == 1 ? qq| \$x->$f();| + : qq| \$x->$f("$args[1]");|; } elsif ($f =~ /^(as_number|as_int)\z/) { $try .= " \$x->$1();"; } elsif ($f eq "finc") { @@ -93,8 +94,6 @@ while (<DATA>) { } elsif ($f eq "bacmp") { $try .= ' $x->bacmp($y);'; } elsif ($f eq "bpow") { - $try .= ' $x ** $y;'; - } elsif ($f eq "fpow") { $try .= ' $x->bpow($y);'; } elsif ($f eq "badd") { $try .= ' $x + $y;'; @@ -179,6 +178,7 @@ is($x->{_a}, undef, qq|\$x = $CLASS->new(2); \$x->bnan(); \$x->{_a}|); is($x->{_p}, undef, qq|\$x = $CLASS->new(2); \$x->bnan(); \$x->{_p}|); __DATA__ + &digit 123:2:1 1234:0:4 @@ -191,6 +191,7 @@ __DATA__ 1234:-4:4 0:0:0 0:1:0 + &bmodinv # format: number:modulus:result # bmodinv Data errors @@ -211,6 +212,7 @@ inf:5:NaN 5:inf:NaN -inf:5:NaN 5:-inf:NaN + &as_number 144/7:20 12/1:12 @@ -219,6 +221,7 @@ inf:5:NaN NaN:NaN +inf:inf -inf:-inf + &as_int 144/7:20 12/1:12 @@ -227,6 +230,7 @@ NaN:NaN NaN:NaN +inf:inf -inf:-inf + &bmodpow # format: number:exponent:modulus:result # bmodpow Data errors @@ -249,6 +253,7 @@ abc:5:5:NaN 8:-1:16:NaN inf:5:13:NaN 5:inf:13:NaN + &bmod NaN:1:NaN 1:NaN:NaN @@ -268,6 +273,7 @@ NaN:1:NaN -7/4:4/28:3/28 7/4:-4/28:-3/28 -7/4:-4/28:-1/28 + &fsqrt 1:1 0:0 @@ -279,10 +285,16 @@ NaN:NaN 144/4:6 25/16:5/4 -3:NaN +4/9:2/3 +36/49:6/7 +49/121:7/11 +999966000289/99999820000081:999983/9999991 + &flog NaN:NaN 0:-inf -2:NaN + &blog NaN:NaN:NaN 0:NaN:NaN @@ -292,10 +304,12 @@ NaN:1:NaN 0:2:-inf 0:-2:NaN 3:-2:NaN + &finf 1:+:inf 2:-:-inf 3:abc:inf + &numify 0:0 +1:1 @@ -307,11 +321,13 @@ NaN:1:NaN NaN:NaN +inf:inf -inf:-inf + &fnan abc:NaN 2:NaN -2:NaN 0:NaN + &fone 2:+:1 -2:-:-1 @@ -321,6 +337,7 @@ abc:NaN -2::1 abc::1 2:abc:1 + &fsstr +inf:inf -inf:-inf @@ -328,6 +345,7 @@ abcfsstr:NaN 1:1/1 3/1:3/1 0.1:1/10 + &bnorm 1:1 -0:0 @@ -403,6 +421,7 @@ abc:NaN 1 / 3:1/3 1/ 3:1/3 1 /3:1/3 + &fneg fnegNaN:NaN +inf:-inf @@ -417,6 +436,7 @@ fnegNaN:NaN 123/7:-123/7 -123/7:123/7 123/-7:123/7 + &fabs fabsNaN:NaN +inf:inf @@ -428,6 +448,7 @@ fabsNaN:NaN -123456789:123456789 +123.456789:123456789/1000000 -123456.789:123456789/1000 + &badd abc:abc:NaN abc:+0:NaN @@ -476,6 +497,7 @@ baddNaN:+inf:NaN +123456789:-987654321:-864197532 1/3:1/3:2/3 2/3:-1/3:1/3 + &bsub abc:abc:NaN abc:+0:NaN @@ -528,6 +550,7 @@ baddNaN:+inf:NaN -2/3:-2/3:0 0:-123:123 0:123:-123 + &bmul abc:abc:NaN abc:+0:NaN @@ -580,6 +603,7 @@ NaNmul:-inf:NaN 6:120:720 10:10000:100000 1/4:1/3:1/12 + &bdiv-list 0:0:NaN,0 0:1:0,0 @@ -591,6 +615,7 @@ NaNmul:-inf:NaN -9:-4:2,-1 11/7:2/3:2,5/21 -11/7:2/3:-3,3/7 + &bdiv $div_scale = 40; $round_mode = "even" abc:abc:NaN @@ -641,24 +666,41 @@ abc:+1:abc:NaN 1/4:1/3:3/4 # reset scale for further tests $div_scale = 40 + &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 +# without sign argument +abc:0 +NaN:0 ++inf:1 +-inf:1 +-1:0 +0:0 +1:0 +# with sign argument "+" +abc:+:0 +NaN:+:0 +inf:+:1 --inf:-:1 -inf:+:0 -+infinity::1 --infinity::1 +-1:+:0 +0:+:0 +1:+:0 +# with sign argument "-" +abc:-:0 +NaN:-:0 ++inf:-:0 +-inf:-:1 +-1:-:0 +0:-:0 +1:-:0 ++infinity:1 +-infinity:1 + &is_odd abc:0 0:0 @@ -673,6 +715,7 @@ abc:0 123.45:0 -123.45:0 2:0 + &is_int NaNis_int:0 0:1 @@ -687,6 +730,7 @@ NaNis_int:0 -0.002:0 1/3:0 3/1:1 + &is_even abc:0 0:1 @@ -706,6 +750,7 @@ abc:0 120:1 1200:1 -1200:1 + &is_pos 0:0 1:1 @@ -714,6 +759,7 @@ abc:0 NaN:0 -inf:0 +inf:1 + &is_positive 0:0 1:1 @@ -722,6 +768,7 @@ NaN:0 NaN:0 -inf:0 +inf:1 + &is_neg 0:0 1:0 @@ -730,6 +777,7 @@ NaN:0 NaN:0 -inf:1 +inf:0 + &is_negative 0:0 1:0 @@ -738,6 +786,7 @@ NaN:0 NaN:0 -inf:1 +inf:0 + &parts 0:0 1 1:1 1 @@ -749,12 +798,14 @@ NaN:0 NaNparts:NaN NaN +inf:inf inf -inf:-inf inf + &length 123:3 -123:3 0:1 1:1 12345678901234567890:20 + &is_zero NaNzero:0 +inf:0 @@ -766,19 +817,43 @@ NaNzero:0 1/3:0 -0/3:1 5/inf:1 + &is_one -NaNone:0 +# with no sign argument +invalid:0 +NaN:0 +inf:0 -inf:0 +-2:0 +-1:0 0:0 -2:0 1:1 --1:0 -2:0 +# with sign argument "+" +invalid:+:0 +NaN:+:0 ++inf:+:0 +-inf:+:0 +-2:+:0 +-1:+:0 +0:+:0 +1:+:1 +-2:+:0 +# with sign argument "-" +invalid:-:0 +NaN:-:0 ++inf:-:0 +-inf:-:0 +-2:-:0 +-1:-:1 +0:-:0 +1:-:0 +-2:-:0 1/3:0 100/100:1 0.1/0.1:1 5/inf:0 + &ffloor 0:0 abc:NaN @@ -802,6 +877,7 @@ abc:NaN -13/7:-2 -14/7:-2 -15/7:-3 + &fceil 0:0 abc:NaN @@ -823,14 +899,227 @@ abc:NaN -13/7:-1 -14/7:-2 -15/7:-2 + &ffac NaN:NaN 1:1 -1:NaN + &bpow -# bpow test for overload of ** +# +abc:123:NaN +123:abc:NaN +# +-inf:-inf:0 +-inf:-3:0 +-inf:-2:0 +-inf:-3/2:0 +-inf:-1:0 +-inf:-1/2:0 +-inf:0:NaN +-inf:1/2:inf # directed infinity +-inf:1:-inf +-inf:3/2:inf # directed infinity +-inf:2:inf +-inf:3:-inf +-inf:inf:inf # complex infinity +-inf:NaN:NaN +# +-3:-inf:0 +-3:-3:-1/27 +-3:-2:1/9 +-3:-3/2:NaN +-3:-1:-1/3 +-3:-1/2:NaN +-3:0:1 +-3:1/2:NaN +-3:1:-3 +-3:3/2:NaN +-3:2:9 +-3:3:-27 +-3:inf:inf # complex infinity +-3:NaN:NaN +# +-2:-inf:0 +-2:-3:-1/8 +-2:-2:1/4 +-2:-3/2:NaN +-2:-1:-1/2 +-2:-1/2:NaN +-2:0:1 +-2:1/2:NaN +-2:1:-2 +-2:3/2:NaN +-2:2:4 +-2:3:-8 +-2:inf:inf # complex infinity +-2:NaN:NaN +# +-3/2:-inf:0 +-3/2:-3:-8/27 +-3/2:-2:4/9 +-3/2:-3/2:NaN +-3/2:-1:-2/3 +-3/2:-1/2:NaN +-3/2:0:1 +-3/2:1/2:NaN +-3/2:1:-3/2 +-3/2:3/2:NaN +-3/2:2:9/4 +-3/2:3:-27/8 +-3/2:inf:inf # complex infinity +-3/2:NaN:NaN +# +-1:-inf:NaN +-1:-3:-1 +-1:-2:1 +-1:-3/2:NaN +-1:-1:-1 +-1:-1/2:NaN +-1:0:1 +-1:1/2:NaN +-1:1:-1 +-1:3/2:NaN +-1:2:1 +-1:3:-1 +-1:inf:NaN +-1:NaN:NaN +# +-1/2:-inf:inf # complex infinity +-1/2:-3:-8 +-1/2:-2:4 +-1/2:-3/2:NaN +-1/2:-1:-2 +-1/2:-1/2:NaN +-1/2:0:1 +-1/2:1/2:NaN +-1/2:1:-1/2 +-1/2:3/2:NaN +-1/2:2:1/4 +-1/2:3:-1/8 +-1/2:inf:0 +-1/2:NaN:NaN +# +0:-inf:inf # complex infinity +0:-3:inf # complex infinity +0:-2:inf # complex infinity +0:-3/2:inf # complex infinity +0:-1:inf # complex infinity +0:-1/2:inf # complex infinity +0:0:1 +0:1/2:0 +0:1:0 +0:3/2:0 +0:2:0 +0:3:0 +0:inf:0 +0:NaN:NaN +# +1/2:-inf:inf +1/2:-3:8 +1/2:-2:4 +#1/2:-3/2:2.828427124746190097603377448419396157139 +1/2:-1:2 +#1/2:-1/2:1.41421356237309504880168872420969807857 +1/2:0:1 +#1/2:1/2:0.7071067811865475244008443621048490392848 +1/2:1:1/2 +#1/2:3/2:0.3535533905932737622004221810524245196424 +1/2:2:1/4 +1/2:3:1/8 +1/2:inf:0 +1/2:NaN:NaN +# +1:-inf:1 +1:-3:1 +1:-2:1 +1:-3/2:1 +1:-1:1 +1:-1/2:1 +1:0:1 +1:1/2:1 +1:1:1 +1:3/2:1 +1:2:1 +1:3:1 +1:inf:1 +1:NaN:NaN +# +3/2:-inf:0 +3/2:-3:8/27 +3/2:-2:4/9 +#3/2:-3/2:0.5443310539518173551549520166013091982147 +3/2:-1:2/3 +#3/2:-1/2:0.816496580927726032732428024901963797322 +3/2:0:1 +#3/2:1/2:1.224744871391589049098642037352945695983 +3/2:1:3/2 +#3/2:3/2:1.837117307087383573647963056029418543974 +3/2:2:9/4 +3/2:3:27/8 +3/2:inf:inf +3/2:NaN:NaN +# +2:-inf:0 +2:-3:1/8 +2:-2:1/4 +#2:-3/2:0.3535533905932737622004221810524245196424 +2:-1:1/2 +#2:-1/2:0.7071067811865475244008443621048490392848 +2:0:1 +#2:1/2:1.41421356237309504880168872420969807857 +2:1:2 +#2:3/2:2.828427124746190097603377448419396157139 2:2:4 +2:3:8 +2:inf:inf +2:NaN:NaN +# +3:-inf:0 +3:-3:1/27 +3:-2:1/9 +#3:-3/2:0.1924500897298752548363829268339858185492 +3:-1:1/3 +#3:-1/2:0.5773502691896257645091487805019574556476 +3:0:1 +#3:1/2:1.732050807568877293527446341505872366943 +3:1:3 +#3:3/2:5.196152422706631880582339024517617100828 +3:2:9 3:3:27 +3:inf:inf +3:NaN:NaN +# +inf:-inf:0 +inf:-3:0 +inf:-2:0 +inf:-3/2:0 +inf:-1:0 +inf:-1/2:0 +inf:0:NaN +inf:1/2:inf +inf:1:inf +inf:3/2:inf +inf:2:inf +inf:3:inf +inf:inf:inf +inf:NaN:NaN +# +NaN:-inf:NaN +NaN:-3:NaN +NaN:-2:NaN +NaN:-3/2:NaN +NaN:-1:NaN +NaN:-1/2:NaN +NaN:0:NaN +NaN:1/2:NaN +NaN:1:NaN +NaN:3/2:NaN +NaN:2:NaN +NaN:3:NaN +NaN:inf:NaN +NaN:NaN:NaN + &bacmp +0:-0:0 +0:+1:-1 @@ -884,19 +1173,7 @@ NaN:-inf: 1/3:2/3:-1 2/3:1/3:1 2/3:2/3:0 -&fpow -2/1:3/1:8 -3/1:3/1:27 -5/2:3/1:125/8 --2/1:3/1:-8 --3/1:3/1:-27 --5/2:3/1:-125/8 --2/1:4/1:16 --3/1:4/1:81 --5/2:4/1:625/16 --5/2:-4/1:16/625 -1/5:-3:125 --1/5:-3:-125 + &numerator NaN:NaN inf:inf @@ -906,6 +1183,7 @@ inf:inf 0:0 1:1 5/-3:-5 + &denominator NaN:NaN inf:1 @@ -916,18 +1194,21 @@ inf:1 -1/1:1 -3/7:7 4/-5:5 + &finc 3/2:5/2 -15/6:-3/2 NaN:NaN -1/3:2/3 -2/7:5/7 + &fdec 15/6:3/2 -3/2:-5/2 1/3:-2/3 2/7:-5/7 NaN:NaN + &log -1:NaN 0:-inf @@ -936,10 +1217,17 @@ NaN:NaN -inf:inf inf:inf NaN:NaN + &exp + &sin + &cos + &atan2 + &int + &abs + &sqrt diff --git a/cpan/Math-BigRat/t/bigratpm.t b/cpan/Math-BigRat/t/bigratpm.t index a5bb9471e5..40f9f6eb8a 100644 --- a/cpan/Math-BigRat/t/bigratpm.t +++ b/cpan/Math-BigRat/t/bigratpm.t @@ -3,12 +3,12 @@ use strict; use warnings; -use Test::More tests => 703; +use Test::More tests => 905; use Math::BigRat lib => 'Calc'; -our ($CLASS, $CALC); +our ($CLASS, $LIB); $CLASS = "Math::BigRat"; -$CALC = "Math::BigInt::Calc"; # backend +$LIB = "Math::BigInt::Calc"; # backend -require 't/bigratpm.inc'; # all tests here for sharing +require './t/bigratpm.inc'; # all tests here for sharing diff --git a/cpan/Math-BigRat/t/bigroot.t b/cpan/Math-BigRat/t/bigroot.t deleted file mode 100644 index 5be7faa48d..0000000000 --- a/cpan/Math-BigRat/t/bigroot.t +++ /dev/null @@ -1,43 +0,0 @@ -#!perl - -# 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 strict; -use warnings; - -use Test::More tests => 8 * 2; - -use Math::BigFloat; -use Math::BigInt; - -my $cl = "Math::BigFloat"; -my $c = "Math::BigInt"; - -# 2 ** 240 = -# 1766847064778384329583297500742918515827483896875618958121606201292619776 - -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-BigRat/t/trap.t b/cpan/Math-BigRat/t/trap.t index 0daef7afe8..bab85d4e5d 100644 --- a/cpan/Math-BigRat/t/trap.t +++ b/cpan/Math-BigRat/t/trap.t @@ -9,60 +9,74 @@ use Test::More tests => 29; use Math::BigRat; -my $mbi = 'Math::BigRat'; -my ($cfg, $x); +my $mbr = 'Math::BigRat'; +my $x; -foreach my $class ($mbi) { +foreach my $class ($mbr) { - # can do and defaults are okay? + # can do? can_ok($class, 'config'); - is($class->config()->{trap_nan}, 0, qq|$class->config()->{trap_nan}|); - is($class->config()->{trap_inf}, 0, qq|$class->config()->{trap_inf}|); + + ########################################################################### + # Default values. + ########################################################################### + + # defaults are okay? + is($class->config("trap_nan"), 0, qq|$class->config("trap_nan")|); + is($class->config("trap_inf"), 0, qq|$class->config("trap_inf")|); + + ########################################################################### + # Trap NaN. + ########################################################################### # can set? - $cfg = $class->config( trap_nan => 1 ); - is($cfg->{trap_nan}, 1, q|$cfg->{trap_nan}|); + $class->config( trap_nan => 1 ); + is($class->config("trap_nan"), 1, q|$class->config("trap_nan")|); + + # can reset? + $class->config( trap_nan => 0 ); + is($class->config("trap_nan"), 0, qq|$class->config("trap_nan")|); # can set via hash ref? - $cfg = $class->config( { trap_nan => 1 } ); - is($cfg->{trap_nan}, 1, q|$cfg->{trap_nan}|); + $class->config( { trap_nan => 1 } ); + is($class->config("trap_nan"), 1, q|$class->config("trap_nan")|); # also test that new() still works normally - eval("\$x = $class->new('42'); \$x->bnan();"); - like($@, qr/^Tried to set/, "\$x = $class->new('42'); \$x->bnan();"); + eval { $x = $class->new("42"); $x->bnan(); }; + like($@, qr/^Tried to set/, qq|\$x = $class->new("42"); \$x->bnan();|); # after new() never modified - is($x, 42, "\$x = $class->new('42'); \$x->bnan();"); + is($x, 42, qq|\$x = $class->new("42"); \$x->bnan();|); - # can reset? - $cfg = $class->config( trap_nan => 0 ); - is($cfg->{trap_nan}, 0, q|$cfg->{trap_nan}|); + # 0/0 => NaN + eval { $x = $class->new("0"); $x->bdiv(0); }; + like($@, qr/^Tried to set/, qq|\$x = $class->new("0"); \$x->bdiv(0);|); + # after new() never modified + is($x, 0, qq|\$x = $class->new("0"); \$x->bdiv(0);|); + + ########################################################################### + # Trap inf. + ########################################################################### # can set? - $cfg = $class->config( trap_inf => 1 ); - is($cfg->{trap_inf}, 1, q|$cfg->{trap_inf}|); - eval("\$x = $class->new('4711'); \$x->binf();"); - like($@, qr/^Tried to set/, "\$x = $class->new('4711'); \$x->binf();"); + $class->config( trap_inf => 1 ); + is($class->config("trap_inf"), 1, qq|$class->config("trap_inf")|); + + eval { $x = $class->new("4711"); $x->binf(); }; + like($@, qr/^Tried to set/, qq|\$x = $class->new("4711"); \$x->binf();|); # after new() never modified - is($x, 4711, "\$x = $class->new('4711'); \$x->binf();"); + is($x, 4711, qq|\$x = $class->new("4711"); \$x->binf();|); # +$x/0 => +inf - eval("\$x =\$class->new('4711'); \$x->bdiv(0);"); - like($@, qr/^Tried to set/, "\$x =\$class->new('4711'); \$x->bdiv(0);"); + eval { $x = $class->new("4711"); $x->bdiv(0); }; + like($@, qr/^Tried to set/, qq|\$x =\$class->new("4711"); \$x->bdiv(0);|); # after new() never modified - is($x, 4711, "\$x =\$class->new('4711'); \$x->bdiv(0);"); + is($x, 4711, qq|\$x =\$class->new("4711"); \$x->bdiv(0);|); # -$x/0 => -inf - eval("\$x = $class->new('-0815'); \$x->bdiv(0);"); - like($@, qr/^Tried to set/, "\$x = $class->new('-0815'); \$x->bdiv(0);"); - # after new() never modified - is($x, -815, "\$x = $class->new('-0815'); \$x->bdiv(0);"); - - $cfg = $class->config( trap_nan => 1 ); - # 0/0 => NaN - eval("\$x = $class->new('0'); \$x->bdiv(0);"); - like($@, qr/^Tried to set/, "\$x = $class->new('0'); \$x->bdiv(0);"); + eval { $x = $class->new("-0815"); $x->bdiv(0); }; + like($@, qr/^Tried to set/, qq|\$x = $class->new("-0815"); \$x->bdiv(0);|); # after new() never modified - is($x, 0, "\$x = $class->new('0'); \$x->bdiv(0);"); + is($x, -815, qq|\$x = $class->new("-0815"); \$x->bdiv(0);|); } ############################################################################## @@ -78,14 +92,14 @@ for my $trap (qw/ 0.1a +inf inf -inf /) { # In each of the cases below, $x is not modified, because the code dies. - eval("\$x = $mbi->new('$trap');"); - is($x, '7/4', "\$x = $mbi->new('$trap');"); + eval { $x = $mbr->new("$trap"); }; + is($x, "7/4", qq|\$x = $mbr->new("$trap");|); - eval("\$x = $mbi->new('$trap');"); - is($x, '7/4', "\$x = $mbi->new('$trap');"); + eval { $x = $mbr->new("$trap"); }; + is($x, "7/4", qq|\$x = $mbr->new("$trap");|); - eval("\$x = $mbi->new('$trap/7');"); - is($x, '7/4', "\$x = $mbi->new('$trap/7');"); + eval { $x = $mbr->new("$trap/7"); }; + is($x, "7/4", qq|\$x = $mbr->new("$trap/7");|); } # all tests done |