diff options
Diffstat (limited to 'Master/tlpkg/tlperl/lib/Math/BigInt.pm')
-rw-r--r-- | Master/tlpkg/tlperl/lib/Math/BigInt.pm | 549 |
1 files changed, 309 insertions, 240 deletions
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 |