diff options
author | Karl Berry <karl@freefriends.org> | 2012-11-20 18:08:54 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2012-11-20 18:08:54 +0000 |
commit | c5add2ea5067382269ae6f19e345fda0b9a7bd21 (patch) | |
tree | 02f512fda46d93079c9dc59c0d76f0e398150f83 /Master/tlpkg/tlperl/lib/Math | |
parent | 6c35e87bdc5a3f64833dbbc42e7d42e683db9d5b (diff) |
perl 5.16.2, compiled without optimization for Windows (from siep)
git-svn-id: svn://tug.org/texlive/trunk@28315 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/tlpkg/tlperl/lib/Math')
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigFloat.pm | 342 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigFloat/Trace.pm | 2 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigInt.pm | 549 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigInt/Calc.pm | 2 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigInt/CalcEmu.pm | 2 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigInt/FastCalc.pm | 4 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigInt/Trace.pm | 2 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigRat.pm | 4 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/Complex.pm | 43 | ||||
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/Trig.pm | 37 |
10 files changed, 536 insertions, 451 deletions
diff --git a/Master/tlpkg/tlperl/lib/Math/BigFloat.pm b/Master/tlpkg/tlperl/lib/Math/BigFloat.pm index 06a6e48417c..c860582d6dc 100644 --- a/Master/tlpkg/tlperl/lib/Math/BigFloat.pm +++ b/Master/tlpkg/tlperl/lib/Math/BigFloat.pm @@ -12,7 +12,7 @@ package Math::BigFloat; # _a : accuracy # _p : precision -$VERSION = '1.993'; +$VERSION = '1.997'; require 5.006002; require Exporter; @@ -437,10 +437,10 @@ sub bsstr sub numify { - # Make a number from a BigFloat object - # simple return a string and let Perl's atoi()/atof() handle the rest + # Convert a Perl scalar number from a BigFloat object. + # Create a string and let Perl's atoi()/atof() handle the rest. my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - $x->bsstr(); + return 0 + $x->bsstr(); } ############################################################################## @@ -3816,122 +3816,122 @@ 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 exponentiation (($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 + 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 pre- + # serve $x, use $z = $x->copy()->bXXX($y); See under L</CAVEATS> for + # 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 exponentiation (($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 + bgcd(@values); # greatest common divisor + blcm(@values); # lowest common multiplicator - $x->bstr(); # return string - $x->bsstr(); # return string in scientific notation + $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->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->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 + $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 + # 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 @@ -4023,7 +4023,7 @@ This might change in the future, so do not depend on it. =head2 Accuracy vs. Precision -See also: L<Rounding|Rounding>. +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 @@ -4040,8 +4040,8 @@ 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 + $d = Math::BigFloat->div_scale(); # query + Math::BigFloat->div_scale($n); # set to $n digits The default value for C<div_scale> is 40. @@ -4049,13 +4049,13 @@ 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 + $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 @@ -4138,12 +4138,12 @@ differences: =head2 accuracy - $x->accuracy(5); # local for $x - CLASS->accuracy(5); # global for all members of CLASS - # Note: This also applies to new()! + $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 + $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()! @@ -4153,7 +4153,7 @@ 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 +L<Math::BigInt/round()>, L<Math::BigInt/bround()> or L<Math::BigInt/bfround()> or by passing the desired accuracy to the math operation as additional parameter: my $x = Math::BigInt->new(30000); @@ -4163,18 +4163,20 @@ to the math operation as additional parameter: =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 + $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 + $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 +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() @@ -4187,7 +4189,7 @@ 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) + $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: @@ -4232,7 +4234,7 @@ This method was added in v1.87 of Math::BigInt (June 2007). print $y->batan2($x), "\n"; Calculate the arcus tanges of C<$y> divided by C<$x>, modifying $y in place. -See also L<batan()>. +See also L</batan()>. This method was added in v1.87 of Math::BigInt (June 2007). @@ -4241,7 +4243,7 @@ This method was added in v1.87 of Math::BigInt (June 2007). 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()>. +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). @@ -4351,10 +4353,10 @@ Please see the file BUGS in the CPAN distribution Math::BigInt for known bugs. 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 + 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: @@ -4378,8 +4380,8 @@ The following will probably not print what you expect: It prints both quotient and remainder 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 + print $c / 123.456,"\n"; + print scalar $c->bdiv(123.456),"\n"; # or if you want to modify $c instead. @@ -4424,42 +4426,44 @@ C<badd()> etc. The first will modify $x, the second one won't: =item precision() vs. accuracy() -A common pitfall is to use L<precision()> when you want to round a result to +A common pitfall is to use L</precision()> when you want to round a result to a certain number of digits: - use Math::BigFloat; + 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 + 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: +Replacing L</precision()> with L</accuracy> is probably not what you want, either: - use Math::BigFloat; + 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 + 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; + 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 + 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 diff --git a/Master/tlpkg/tlperl/lib/Math/BigFloat/Trace.pm b/Master/tlpkg/tlperl/lib/Math/BigFloat/Trace.pm index 871b2a969e8..5e1636e59d0 100644 --- a/Master/tlpkg/tlperl/lib/Math/BigFloat/Trace.pm +++ b/Master/tlpkg/tlperl/lib/Math/BigFloat/Trace.pm @@ -12,7 +12,7 @@ use vars qw($VERSION @ISA $PACKAGE @EXPORT_OK @ISA = qw(Exporter Math::BigFloat); -$VERSION = 0.01; +$VERSION = '0.29'; use overload; # inherit overload from BigFloat diff --git a/Master/tlpkg/tlperl/lib/Math/BigInt.pm b/Master/tlpkg/tlperl/lib/Math/BigInt.pm index 62c021ecf71..3f55c9b551e 100644 --- a/Master/tlpkg/tlperl/lib/Math/BigInt.pm +++ b/Master/tlpkg/tlperl/lib/Math/BigInt.pm @@ -18,7 +18,7 @@ package Math::BigInt; my $class = "Math::BigInt"; use 5.006002; -$VERSION = '1.994'; +$VERSION = '1.998'; @ISA = qw(Exporter); @EXPORT_OK = qw(objectify bgcd blcm); @@ -40,6 +40,8 @@ use strict; # Thus inheritance of overload operators becomes possible and transparent for # our subclasses without the need to repeat the entire overload section there. +# We register ops that are not registerable yet, so suppress warnings +{ no warnings; use overload '=' => sub { $_[0]->copy(); }, @@ -151,6 +153,7 @@ use overload '""' => sub { $_[0]->bstr(); }, '0+' => sub { $_[0]->numify(); } ; +} # no warnings scope ############################################################################## # global constants, flags and accessory @@ -1013,6 +1016,18 @@ sub babs $x; } +sub bsgn { + # Signum function. + + my $self = shift; + + return $self if $self->modify('bsgn'); + + return $self -> bone("+") if $self -> is_pos(); + return $self -> bone("-") if $self -> is_neg(); + return $self; # zero or NaN +} + sub bneg { # (BINT or num_str) return BINT @@ -2577,102 +2592,137 @@ sub as_oct ############################################################################## # 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]; +sub objectify { + # Convert strings and "foreign objects" to the objects we want. + + # The first argument, $count, is the number of following arguments that + # objectify() looks at and converts to objects. The first is a classname. + # If the given count is 0, all arguments will be used. + + # After the count is read, objectify obtains the name of the class to which + # the following arguments are converted. If the second argument is a + # reference, use the reference type as the class name. Otherwise, if it is + # a string that looks like a class name, use that. Otherwise, use $class. + + # 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 + + # A shortcut for the common case $x->unary_op(): + + return (ref($_[1]), $_[1]) if (@_ == 2) && ($_[0]||0 == 1) && ref($_[1]); + + # Check the context. + + unless (wantarray) { + require Carp; + Carp::croak ("${class}::objectify() needs list context"); } - else + + # Get the number of arguments to objectify. + + my $count = shift; + $count ||= @_; + + # Initialize the output array. + + my @a = @_; + + # If the first argument is a reference, use that reference type as our + # class name. Otherwise, if the first argument looks like a class name, + # then use that as our class name. Otherwise, use the default class name. + { - # 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? + if (ref($a[0])) { # reference? + unshift @a, ref($a[0]); + last; + } + if ($a[0] =~ /^[A-Z].*::/) { # string with class name? + last; + } + unshift @a, $class; # default class name } - 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; + no strict 'refs'; + + # What we upgrade to, if anything. + + my $up = ${"$a[0]::upgrade"}; + + # Disable downgrading, because Math::BigFloat -> foo('1.0','2.0') needs + # floats. + + my $down; + if (defined ${"$a[0]::downgrade"}) { + $down = ${"$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); + for my $i (1 .. $count) { + my $ref = ref $a[$i]; + + # If it is an object of the right class, all is fine. + + if ($ref eq $a[0]) { + next; } - 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); + + # Don't do anything with undefs. + + unless (defined($a[$i])) { + next; } - elsif (ref($k) ne $a[0] and !defined $up || ref $k ne $up) - { - # 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"); + + # Perl scalars are fed to the appropriate constructor. + + unless ($ref) { + $a[$i] = $a[0] -> new($a[$i]); + next; + } + + # Upgrading is OK, so skip further tests if the argument is upgraded. + + if (defined $up && $ref eq $up) { + next; + } + + # If we want a Math::BigInt, see if the object can become one. + # Support the old misnomer as_number(). + + if ($a[0] eq 'Math::BigInt') { + if ($a[$i] -> can('as_int')) { + $a[$i] = $a[$i] -> as_int(); + next; + } + if ($a[$i] -> can('as_number')) { + $a[$i] = $a[$i] -> as_number(); + next; + } + } + + # If we want a Math::BigFloat, see if the object can become one. + + if ($a[0] eq 'Math::BigFloat') { + if ($a[$i] -> can('as_float')) { + $a[$i] = $a[$i] -> as_float(); + next; + } + } + + # Last resort. + + $a[$i] = $a[0] -> new($a[$i]); } - ${"$a[0]::downgrade"} = $d; - @a; - } + + # Reset the downgrading. + + ${"$a[0]::downgrade"} = $down; + + return @a; +} sub _register_callback { @@ -3297,9 +3347,10 @@ Math::BigInt - Arbitrary size integer/float math package $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. + # The following all modify their first argument. If you want to pre- + # serve $x, use $z = $x->copy()->bXXX($y); See under L<CAVEATS> for + # why this is necessary when mixing $a = $b assignments with non-over- + # loaded math. $x->bzero(); # set $x to 0 $x->bnan(); # set $x to NaN @@ -3310,6 +3361,7 @@ Math::BigInt - Arbitrary size integer/float math package $x->bneg(); # negation $x->babs(); # absolute value + $x->bsgn(); # sign function (-1, 0, 1, or NaN) $x->bnorm(); # normalize (no-op in BigInt) $x->bnot(); # two's complement (bit wise not) $x->binc(); # increment $x by 1 @@ -3329,10 +3381,12 @@ Math::BigInt - Arbitrary size integer/float math package $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 + # returns (quo,rem) or quo if in sca- + # lar 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 + # returns (quo,rem) or quo if in sca- + # lar context $x->band($y); # bitwise and $x->bior($y); # bitwise inclusive or @@ -3349,7 +3403,8 @@ Math::BigInt - Arbitrary size integer/float math package $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->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 @@ -3369,36 +3424,38 @@ Math::BigInt - Arbitrary size integer/float math package 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 + ($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!) + $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!) # conversion 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 + $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 + $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 + 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 @@ -3453,7 +3510,7 @@ object from the input. =item Output Output values are BigInt objects (normalized), except for the methods which -return a string (see L<SYNOPSIS>). +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()>) @@ -3466,7 +3523,7 @@ return either undef (if NaN is involved), <0, 0 or >0 and are suited for sort. 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. +L</ACCURACY and PRECISION> for more information. =head2 config() @@ -3479,33 +3536,33 @@ 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 + 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 + 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: @@ -3514,16 +3571,18 @@ The following values can be set by passing C<config()> a reference to a hash: Example: - $new_cfg = Math::BigInt->config( { trap_inf => 1, precision => 5 } ); + $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()! + $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 + $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()! @@ -3533,34 +3592,35 @@ 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 +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 + 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. +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); + $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 + $y = Math::BigInt->new(1234567); # unrounded + print Math::BigInt->accuracy(4),"\n"; # set 4, print 4 + $x = Math::BigInt->new(123456); # $x will be automatic- + # ally 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 @@ -3569,18 +3629,20 @@ 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 + $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 + 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 + $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 +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 @@ -3591,21 +3653,21 @@ 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. +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); + $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"! + $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 @@ -3645,7 +3707,7 @@ 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. +See L</Input> for more info on accepted input formats. =head2 from_oct() @@ -3714,12 +3776,12 @@ If used on an object, it will set it to one: =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 '+') + $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 @@ -3783,7 +3845,7 @@ If you want $x to have a certain sign, use one of the following methods: =head2 digit() - $x->digit($n); # return the nth digit, counting from right + $x->digit($n); # return the nth digit, counting from right If C<$n> is negative, returns the digit counting from left. @@ -3802,6 +3864,13 @@ 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 bsgn() + + $x->bsgn(); + +Signum function. Set the number to -1, 0, or 1, depending on whether the +number is negative, zero, or positive, respectivly. Does not modify NaNs. + =head2 bnorm() $x->bnorm(); # normalize (no-op) @@ -3818,23 +3887,23 @@ but faster. =head2 binc() - $x->binc(); # increment x by 1 + $x->binc(); # increment x by 1 =head2 bdec() - $x->bdec(); # decrement x by 1 + $x->bdec(); # decrement x by 1 =head2 badd() - $x->badd($y); # addition (add $y to $x) + $x->badd($y); # addition (add $y to $x) =head2 bsub() - $x->bsub($y); # subtraction (subtract $y from $x) + $x->bsub($y); # subtraction (subtract $y from $x) =head2 bmul() - $x->bmul($y); # multiplication (multiply $x by $y) + $x->bmul($y); # multiplication (multiply $x by $y) =head2 bmuladd() @@ -3846,16 +3915,16 @@ 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 + $x->bdiv($y); # divide, set $x to quotient + # return (quo,rem) or quo if scalar =head2 bmod() - $x->bmod($y); # modulus (x % y) + $x->bmod($y); # modulus (x % y) =head2 bmodinv() - $x->bmodinv($mod); # modular multiplicative inverse + $x->bmodinv($mod); # modular multiplicative inverse Returns the multiplicative inverse of C<$x> modulo C<$mod>. If @@ -3894,29 +3963,29 @@ is exactly equivalent to =head2 bpow() - $x->bpow($y); # power of arguments (x ** y) + $x->bpow($y); # power of arguments (x ** y) =head2 blog() - $x->blog($base, $accuracy); # logarithm of x to the base $base + $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 + print $x->blog(undef, 100); # log(x) to 100 digits =head2 bexp() - $x->bexp($accuracy); # calculate e ** X + $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()>. +See also L</blog()>. =head2 bnok() - $x->bnok($y); # x over y (binomial coefficient n over k) + $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: @@ -4106,11 +4175,11 @@ Return the signed mantissa of $x as BigInt. =head2 parts() - $x->parts(); # return (mantissa,exponent) as BigInt + $x->parts(); # return (mantissa,exponent) as BigInt =head2 copy() - $x->copy(); # make a true copy of $x (unlike $y = $x;) + $x->copy(); # make a true copy of $x (unlike $y = $x;) =head2 as_int()/as_number() @@ -4130,19 +4199,19 @@ Returns a normalized string representation of C<$x>. =head2 bsstr() - $x->bsstr(); # normalized string in scientific notation + $x->bsstr(); # normalized string in scientific notation =head2 as_hex() - $x->as_hex(); # as signed hexadecimal string with prefixed 0x + $x->as_hex(); # as signed hexadecimal string with prefixed 0x =head2 as_bin() - $x->as_bin(); # as signed binary string with prefixed 0b + $x->as_bin(); # as signed binary string with prefixed 0b =head2 as_oct() - $x->as_oct(); # as signed octal string with prefixed 0 + $x->as_oct(); # as signed octal string with prefixed 0 =head2 numify() @@ -4151,7 +4220,7 @@ Returns a normalized string representation of C<$x>. 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. +This loses precision, to avoid this use L<as_int()|/"as_int()/as_number()"> instead. =head2 modify() @@ -4367,25 +4436,25 @@ This is how it works now: =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() >> + * You can set the A global via Math::BigInt->accuracy() or + Math::BigFloat->accuracy() or whatever class you are using. + * You can also set P globally by using 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 undefine A, use Math::SomeCLass->accuracy(undef); + * to undefine P, use Math::SomeClass->precision(undef); + * Setting Math::SomeClass->accuracy() clears automatically + 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 + * to find out the current global A, use Math::SomeClass->accuracy() + * to find out the current global P, use Math::SomeClass->precision() + * use $x->accuracy() respective $x->precision() for the local + setting of $x. + * Please note that $x->accuracy() respective $x->precision() + return eventually defined global A or P, when $x's A or P is not set. =item Creating numbers @@ -4397,11 +4466,11 @@ This is how it works now: 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 + * If given undef for A and P, NO rounding will occur, and the globals will + 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) >>: + $x = Math::BigInt->new($number,undef,undef): use Math::BigInt::SomeSubclass; use Math::BigInt; @@ -4493,11 +4562,11 @@ This is how it works now: =item Local settings - * You can set A or P locally by using C<< $x->accuracy() >> or - C<< $x->precision() >> + * You can set A or P locally by using $x->accuracy() or + $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. + * $x->accuracy() clears $x->precision(), and vice versa. =item Rounding @@ -4507,12 +4576,12 @@ This is how it works now: * 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 + * you can set/get the global R by using Math::SomeClass->round_mode() + or by setting $Math::SomeClass::round_mode + * after each operation, $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); >> + * to manually round a number, call $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: @@ -4801,13 +4870,13 @@ 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. +L</MATH LIBRARY> for more information. For more benchmark results see L<http://bloodgate.com/perl/benchmarks.html>. -=head2 SUBCLASSING +=head1 SUBCLASSING -=head1 Subclassing Math::BigInt +=head2 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: @@ -5249,7 +5318,7 @@ If you want a better approximation of the square root, then use: =item brsft() -For negative numbers in base see also L<brsft|brsft>. +For negative numbers in base see also L<brsft|/brsft()>. =back diff --git a/Master/tlpkg/tlperl/lib/Math/BigInt/Calc.pm b/Master/tlpkg/tlperl/lib/Math/BigInt/Calc.pm index 25f9a3b99d9..69092370533 100644 --- a/Master/tlpkg/tlperl/lib/Math/BigInt/Calc.pm +++ b/Master/tlpkg/tlperl/lib/Math/BigInt/Calc.pm @@ -4,7 +4,7 @@ use 5.006002; use strict; # use warnings; # dont use warnings for older Perls -our $VERSION = '1.993'; +our $VERSION = '1.997'; # Package to store unsigned big integers in decimal and do math with them diff --git a/Master/tlpkg/tlperl/lib/Math/BigInt/CalcEmu.pm b/Master/tlpkg/tlperl/lib/Math/BigInt/CalcEmu.pm index ee0b677c53f..1d754ff1511 100644 --- a/Master/tlpkg/tlperl/lib/Math/BigInt/CalcEmu.pm +++ b/Master/tlpkg/tlperl/lib/Math/BigInt/CalcEmu.pm @@ -5,7 +5,7 @@ use strict; # use warnings; # dont use warnings for older Perls use vars qw/$VERSION/; -$VERSION = '1.993'; +$VERSION = '1.997'; package Math::BigInt; diff --git a/Master/tlpkg/tlperl/lib/Math/BigInt/FastCalc.pm b/Master/tlpkg/tlperl/lib/Math/BigInt/FastCalc.pm index 9abb12091f1..81f29a16e4f 100644 --- a/Master/tlpkg/tlperl/lib/Math/BigInt/FastCalc.pm +++ b/Master/tlpkg/tlperl/lib/Math/BigInt/FastCalc.pm @@ -4,11 +4,11 @@ use 5.006; use strict; use warnings; -use Math::BigInt::Calc 1.993; +use Math::BigInt::Calc 1.997; use vars '$VERSION'; -$VERSION = '0.28'; +$VERSION = '0.30'; ############################################################################## # global constants, flags and accessory diff --git a/Master/tlpkg/tlperl/lib/Math/BigInt/Trace.pm b/Master/tlpkg/tlperl/lib/Math/BigInt/Trace.pm index 4733d226343..5826f6123bd 100644 --- a/Master/tlpkg/tlperl/lib/Math/BigInt/Trace.pm +++ b/Master/tlpkg/tlperl/lib/Math/BigInt/Trace.pm @@ -12,7 +12,7 @@ use vars qw($VERSION @ISA $PACKAGE @EXPORT_OK @ISA = qw(Exporter Math::BigInt); -$VERSION = 0.01; +$VERSION = '0.29'; use overload; # inherit overload from BigInt diff --git a/Master/tlpkg/tlperl/lib/Math/BigRat.pm b/Master/tlpkg/tlperl/lib/Math/BigRat.pm index 135645fc43a..eb07e8260d2 100644 --- a/Master/tlpkg/tlperl/lib/Math/BigRat.pm +++ b/Master/tlpkg/tlperl/lib/Math/BigRat.pm @@ -24,7 +24,7 @@ use vars qw($VERSION @ISA $upgrade $downgrade @ISA = qw(Math::BigFloat); -$VERSION = '0.26_02'; +$VERSION = '0.2603'; $VERSION = eval $VERSION; # inherit overload from Math::BigFloat, but disable the bitwise ops that don't @@ -1428,8 +1428,8 @@ sub as_number return Math::BigInt->new($x->{sign}) if $x->{sign} !~ /^[+-]$/; my $u = Math::BigInt->bzero(); - $u->{sign} = $x->{sign}; $u->{value} = $MBI->_div( $MBI->_copy($x->{_n}), $x->{_d}); # 22/7 => 3 + $u->bneg if $x->{sign} eq '-'; # no negative zero $u; } diff --git a/Master/tlpkg/tlperl/lib/Math/Complex.pm b/Master/tlpkg/tlperl/lib/Math/Complex.pm index 8475a2b5d09..19fb16486bf 100644 --- a/Master/tlpkg/tlperl/lib/Math/Complex.pm +++ b/Master/tlpkg/tlperl/lib/Math/Complex.pm @@ -7,14 +7,14 @@ package Math::Complex; +{ use 5.006; } use strict; -use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $Inf $ExpInf); - -$VERSION = 1.56; +our $VERSION = 1.59; use Config; +our($Inf, $ExpInf); BEGIN { my %DBL_MAX = ( @@ -96,7 +96,7 @@ my $gre = qr'\s*([\+\-]?(?:(?:(?:\d+(?:_\d+)*(?:\.\d*(?:_\d+)*)?|\.\d+(?:_\d+)*) require Exporter; -@ISA = qw(Exporter); +our @ISA = qw(Exporter); my @trig = qw( pi @@ -110,7 +110,7 @@ my @trig = qw( acsch acosech asech acoth acotanh ); -@EXPORT = (qw( +our @EXPORT = (qw( i Re Im rho theta arg sqrt log ln log10 logn cbrt root @@ -121,18 +121,24 @@ my @trig = qw( my @pi = qw(pi pi2 pi4 pip2 pip4 Inf); -@EXPORT_OK = @pi; +our @EXPORT_OK = @pi; -%EXPORT_TAGS = ( +our %EXPORT_TAGS = ( 'trig' => [@trig], 'pi' => [@pi], ); use overload + '=' => \&_copy, + '+=' => \&_plus, '+' => \&_plus, + '-=' => \&_minus, '-' => \&_minus, + '*=' => \&_multiply, '*' => \&_multiply, + '/=' => \&_divide, '/' => \&_divide, + '**=' => \&_power, '**' => \&_power, '==' => \&_numeq, '<=>' => \&_spaceship, @@ -144,7 +150,6 @@ use overload 'log' => \&log, 'sin' => \&sin, 'cos' => \&cos, - 'tan' => \&tan, 'atan2' => \&atan2, '""' => \&_stringify; @@ -217,6 +222,19 @@ sub _emake { return ($p, $q); } +sub _copy { + my $self = shift; + my $clone = {%$self}; + if ($self->{'cartesian'}) { + $clone->{'cartesian'} = [@{$self->{'cartesian'}}]; + } + if ($self->{'polar'}) { + $clone->{'polar'} = [@{$self->{'polar'}}]; + } + bless $clone,__PACKAGE__; + return $clone; +} + # # ->make # @@ -1539,7 +1557,7 @@ sub _stringify_polar { if (defined $format) { $r = sprintf($format, $r); - $theta = sprintf($format, $theta) unless defined $theta; + $theta = sprintf($format, $t) unless defined $theta; } else { $theta = $t unless defined $theta; } @@ -2077,9 +2095,10 @@ L<Math::Trig> =head1 AUTHORS -Daniel S. Lewart <F<lewart!at!uiuc.edu>> -Jarkko Hietaniemi <F<jhi!at!iki.fi>> -Raphael Manfredi <F<Raphael_Manfredi!at!pobox.com>> +Daniel S. Lewart <F<lewart!at!uiuc.edu>>, +Jarkko Hietaniemi <F<jhi!at!iki.fi>>, +Raphael Manfredi <F<Raphael_Manfredi!at!pobox.com>>, +Zefram <zefram@fysh.org> =head1 LICENSE diff --git a/Master/tlpkg/tlperl/lib/Math/Trig.pm b/Master/tlpkg/tlperl/lib/Math/Trig.pm index b7767bebccb..1d9612a41c2 100644 --- a/Master/tlpkg/tlperl/lib/Math/Trig.pm +++ b/Master/tlpkg/tlperl/lib/Math/Trig.pm @@ -4,20 +4,18 @@ # -- Raphael Manfredi, September 1996 (indirectly: because of Math::Complex) # -require Exporter; package Math::Trig; -use 5.005; +{ use 5.006; } use strict; -use Math::Complex 1.56; +use Math::Complex 1.59; use Math::Complex qw(:trig :pi); +require Exporter; -use vars qw($VERSION $PACKAGE @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); - -@ISA = qw(Exporter); +our @ISA = qw(Exporter); -$VERSION = 1.20; +our $VERSION = 1.23; my @angcnv = qw(rad2deg rad2grad deg2rad deg2grad @@ -25,7 +23,7 @@ my @angcnv = qw(rad2deg rad2grad my @areal = qw(asin_real acos_real); -@EXPORT = (@{$Math::Complex::EXPORT_TAGS{'trig'}}, +our @EXPORT = (@{$Math::Complex::EXPORT_TAGS{'trig'}}, @angcnv, @areal); my @rdlcnv = qw(cartesian_to_cylindrical @@ -46,13 +44,13 @@ my @greatcircle = qw( my @pi = qw(pi pi2 pi4 pip2 pip4); -@EXPORT_OK = (@rdlcnv, @greatcircle, @pi, 'Inf'); +our @EXPORT_OK = (@rdlcnv, @greatcircle, @pi, 'Inf'); # See e.g. the following pages: # http://www.movable-type.co.uk/scripts/LatLong.html # http://williams.best.vwh.net/avform.htm -%EXPORT_TAGS = ('radial' => [ @rdlcnv ], +our %EXPORT_TAGS = ('radial' => [ @rdlcnv ], 'great_circle' => [ @greatcircle ], 'pi' => [ @pi ]); @@ -166,19 +164,13 @@ sub great_circle_distance { sub great_circle_direction { my ( $theta0, $phi0, $theta1, $phi1 ) = @_; - my $distance = great_circle_distance($theta0, $phi0, $theta1, $phi1); - my $lat0 = pip2 - $phi0; my $lat1 = pip2 - $phi1; - my $direction = - acos_real((sin($lat1) - sin($lat0) * cos($distance)) / - (cos($lat0) * sin($distance))); - - $direction = pi2 - $direction - if sin($theta1 - $theta0) < 0; - - return rad2rad($direction); + return rad2rad(pi2 - + atan2(sin($theta0-$theta1) * cos($lat1), + cos($lat0) * sin($lat1) - + sin($lat0) * cos($lat1) * cos($theta0-$theta1))); } *great_circle_bearing = \&great_circle_direction; @@ -755,8 +747,9 @@ L<Math::Complex> =head1 AUTHORS -Jarkko Hietaniemi <F<jhi!at!iki.fi>> and -Raphael Manfredi <F<Raphael_Manfredi!at!pobox.com>>. +Jarkko Hietaniemi <F<jhi!at!iki.fi>>, +Raphael Manfredi <F<Raphael_Manfredi!at!pobox.com>>, +Zefram <zefram@fysh.org> =head1 LICENSE |