my $class = "Math::BigInt";
use 5.006002;
-$VERSION = '1.9991';
+$VERSION = '1.9992';
@ISA = qw(Exporter);
@EXPORT_OK = qw(objectify bgcd blcm);
},
# the original qw() does not work with the TIESCALAR below, why?
-# Order of arguments unsignificant
+# Order of arguments insignificant
'""' => sub { $_[0]->bstr(); },
'0+' => sub { $_[0]->numify(); }
;
return $x if $x->modify('bneg');
- # for +0 dont negate (to have always normalized +0). Does nothing for 'NaN'
+ # for +0 do not negate (to have always normalized +0). Does nothing for 'NaN'
$x->{sign} =~ tr/+-/-+/ unless ($x->{sign} eq '+' && $CALC->_is_zero($x->{value}));
$x;
}
return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # does also -x <=> 0
# have same sign, so compare absolute values. Don't make tests for zero here
- # because it's actually slower than testin in Calc (especially w/ Pari et al)
+ # because it's actually slower than testing in Calc (especially w/ Pari et al)
# post-normalized compare for internal use (honors signs)
if ($x->{sign} eq '+')
}
# $x && $y both < 0
- $CALC->_acmp($y->{value},$x->{value}); # swaped acmp (lib returns 0,1,-1)
+ $CALC->_acmp($y->{value},$x->{value}); # swapped acmp (lib returns 0,1,-1)
}
sub bacmp
}
else
{
- $rem->{sign} = '+'; # dont leave -0
+ $rem->{sign} = '+'; # do not leave -0
}
$rem->round(@r);
return ($x,$rem);
}
else
{
- $x->{sign} = '+'; # dont leave -0
+ $x->{sign} = '+'; # do not leave -0
}
$x->round(@r);
}
Operations with overloaded operators preserve the arguments which is
exactly what you expect.
-=over 2
-
-=item Input
+=head2 Input
Input values to these routines may be any string, that looks like a number
and results in an integer, including hexadecimal and binary numbers.
are always stored in normalized form. If passed a string, creates a BigInt
object from the input.
-=item Output
+=head2 Output
Output values are BigInt objects (normalized), except for the methods which
return a string (see L</SYNOPSIS>).
C<is_nan()>, etc.) return true or false, while others (C<bcmp()>, C<bacmp()>)
return either undef (if NaN is involved), <0, 0 or >0 and are suited for sort.
-=back
-
=head1 METHODS
Each of the methods below (except config(), accuracy() and precision())
are C<accuracy>, C<precision> and C<round_mode>. Please see the section about
L</ACCURACY and PRECISION> for more information.
-=head2 config()
+=over
+
+=item config()
use Data::Dumper;
Math::BigInt
upgrade To which class math operations might be upgraded
Math::BigFloat
- downgrade To which class math operations might be downgraded
- undef
+ downgrade To which class math operations might be
+ downgraded undef
precision Global precision
undef
accuracy Global accuracy
{ trap_inf => 1, precision => 5 }
);
-=head2 accuracy()
+=item accuracy()
$x->accuracy(5); # local for $x
CLASS->accuracy(5); # global for all members of CLASS
Math::BigInt and make the globals of the subclass aliases to the ones from
Math::BigInt.
-=head2 precision()
+=item 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
$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
+ $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
Math::BigInt and make the globals of the subclass aliases to the ones from
Math::BigInt.
-=head2 brsft()
+=item brsft()
$x->brsft($y,$n);
This will print -3, not -2 (as it would if you divide -5 by 2 and truncate the
result).
-=head2 new()
+=item new()
$x = Math::BigInt->new($str,$A,$P,$R);
See L</Input> for more info on accepted input formats.
-=head2 from_oct()
+=item from_oct()
$x = Math::BigInt->from_oct("0775"); # input is octal
right after the prefix, if present, or between any two digits. If the input is
invalid, a NaN is returned.
-=head2 from_hex()
+=item from_hex()
$x = Math::BigInt->from_hex("0xcafe"); # input is hexadecimal
single underscore character may be placed right after the prefix, if present,
or between any two digits. If the input is invalid, a NaN is returned.
-=head2 from_bin()
+=item from_bin()
$x = Math::BigInt->from_bin("0b10011"); # input is binary
single underscore character may be placed right after the prefix, if present,
or between any two digits. If the input is invalid, a NaN is returned.
-=head2 bnan()
+=item bnan()
$x = Math::BigInt->bnan();
$x->bnan();
-=head2 bzero()
+=item bzero()
$x = Math::BigInt->bzero();
$x->bzero();
-=head2 binf()
+=item binf()
$x = Math::BigInt->binf($sign);
$x->binf();
$x->binf('-');
-=head2 bone()
+=item bone()
$x = Math::BigInt->binf($sign);
$x->bone(); # +1
$x->bone('-'); # -1
-=head2 is_one()/is_zero()/is_nan()/is_inf()
+=item is_one()/is_zero()/is_nan()/is_inf()
$x->is_zero(); # true if arg is +0
$x->is_nan(); # true if arg is NaN
if ($x == 0)
-=head2 is_pos()/is_neg()/is_positive()/is_negative()
+=item is_pos()/is_neg()/is_positive()/is_negative()
$x->is_pos(); # true if > 0
$x->is_neg(); # true if < 0
introduced in v1.36, while C<is_pos()> and C<is_neg()> were only introduced
in v1.68.
-=head2 is_odd()/is_even()/is_int()
+=item is_odd()/is_even()/is_int()
$x->is_odd(); # true if odd, false for even
$x->is_even(); # true if even, false for odd
In BigInt, all numbers except C<NaN>, C<+inf> and C<-inf> are integers.
-=head2 bcmp()
+=item bcmp()
$x->bcmp($y);
Compares $x with $y and takes the sign into account.
Returns -1, 0, 1 or undef.
-=head2 bacmp()
+=item bacmp()
$x->bacmp($y);
Compares $x with $y while ignoring their sign. Returns -1, 0, 1 or undef.
-=head2 sign()
+=item sign()
$x->sign();
$x->binf(); # '+inf'
$x->binf('-'); # '-inf'
-=head2 digit()
+=item 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.
-=head2 bneg()
+=item bneg()
$x->bneg();
Negate the number, e.g. change the sign between '+' and '-', or between '+inf'
and '-inf', respectively. Does nothing for NaN or zero.
-=head2 babs()
+=item babs()
$x->babs();
and from '-inf' to '+inf', respectively. Does nothing for NaN or positive
numbers.
-=head2 bsgn()
+=item 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.
+number is negative, zero, or positive, respectively. Does not modify NaNs.
-=head2 bnorm()
+=item bnorm()
$x->bnorm(); # normalize (no-op)
-=head2 bnot()
+=item bnot()
$x->bnot();
but faster.
-=head2 binc()
+=item binc()
$x->binc(); # increment x by 1
-=head2 bdec()
+=item bdec()
$x->bdec(); # decrement x by 1
-=head2 badd()
+=item badd()
$x->badd($y); # addition (add $y to $x)
-=head2 bsub()
+=item bsub()
$x->bsub($y); # subtraction (subtract $y from $x)
-=head2 bmul()
+=item bmul()
$x->bmul($y); # multiplication (multiply $x by $y)
-=head2 bmuladd()
+=item bmuladd()
$x->bmuladd($y,$z);
This method was added in v1.87 of Math::BigInt (June 2007).
-=head2 bdiv()
+=item bdiv()
$x->bdiv($y); # divide, set $x to quotient
# return (quo,rem) or quo if scalar
-=head2 bmod()
+=item bmod()
$x->bmod($y); # modulus (x % y)
-=head2 bmodinv()
+=item bmodinv()
$x->bmodinv($mod); # modular multiplicative inverse
C<bgcd($y, $mod)==1>. 'C<NaN>' is returned when no modular multiplicative
inverse exists.
-=head2 bmodpow()
+=item bmodpow()
$num->bmodpow($exp,$mod); # modular exponentiation
# ($num**$exp % $mod)
bmodinv($num, $mod)
-=head2 bpow()
+=item bpow()
$x->bpow($y); # power of arguments (x ** y)
-=head2 blog()
+=item blog()
$x->blog($base, $accuracy); # logarithm of x to the base $base
print $x->blog(undef, 100); # log(x) to 100 digits
-=head2 bexp()
+=item bexp()
$x->bexp($accuracy); # calculate e ** X
See also L</blog()>.
-=head2 bnok()
+=item bnok()
$x->bnok($y); # x over y (binomial coefficient n over k)
This method was added in v1.84 of Math::BigInt (April 2007).
-=head2 bpi()
+=item bpi()
print Math::BigInt->bpi(100), "\n"; # 3
This method was added in v1.87 of Math::BigInt (June 2007).
-=head2 bcos()
+=item bcos()
my $x = Math::BigInt->new(1);
print $x->bcos(100), "\n";
This method was added in v1.87 of Math::BigInt (June 2007).
-=head2 bsin()
+=item bsin()
my $x = Math::BigInt->new(1);
print $x->bsin(100), "\n";
This method was added in v1.87 of Math::BigInt (June 2007).
-=head2 batan2()
+=item batan2()
my $x = Math::BigInt->new(1);
my $y = Math::BigInt->new(1);
This method was added in v1.87 of Math::BigInt (June 2007).
-=head2 batan()
+=item batan()
my $x = Math::BigFloat->new(0.5);
print $x->batan(100), "\n";
This method was added in v1.87 of Math::BigInt (June 2007).
-=head2 blsft()
+=item blsft()
$x->blsft($y); # left shift in base 2
$x->blsft($y,$n); # left shift, in base $n (like 10)
-=head2 brsft()
+=item brsft()
$x->brsft($y); # right shift in base 2
$x->brsft($y,$n); # right shift, in base $n (like 10)
-=head2 band()
+=item band()
$x->band($y); # bitwise and
-=head2 bior()
+=item bior()
$x->bior($y); # bitwise inclusive or
-=head2 bxor()
+=item bxor()
$x->bxor($y); # bitwise exclusive or
-=head2 bnot()
+=item bnot()
$x->bnot(); # bitwise not (two's complement)
-=head2 bsqrt()
+=item bsqrt()
$x->bsqrt(); # calculate square-root
-=head2 broot()
+=item broot()
$x->broot($N);
Calculates the N'th root of C<$x>.
-=head2 bfac()
+=item bfac()
- $x->bfac(); # factorial of $x (1*2*3*4*..$x)
+ $x->bfac(); # factorial of $x (1*2*3*4*..$x)
-=head2 round()
+=item round()
$x->round($A,$P,$round_mode);
Round $x to accuracy C<$A> or precision C<$P> using the round mode
C<$round_mode>.
-=head2 bround()
+=item bround()
$x->bround($N); # accuracy: preserve $N digits
-=head2 bfround()
+=item bfround()
$x->bfround($N);
123456.123456 -2 123456.12
123456.123456 -3 123456.123
-=head2 bfloor()
+=item bfloor()
$x->bfloor();
Set $x to the integer less or equal than $x. This is a no-op in BigInt, but
does change $x in BigFloat.
-=head2 bceil()
+=item bceil()
$x->bceil();
Set $x to the integer greater or equal than $x. This is a no-op in BigInt, but
does change $x in BigFloat.
-=head2 bgcd()
+=item bgcd()
- bgcd(@values); # greatest common divisor (no OO style)
+ bgcd(@values); # greatest common divisor (no OO style)
-=head2 blcm()
+=item blcm()
- blcm(@values); # lowest common multiple (no OO style)
+ blcm(@values); # lowest common multiple (no OO style)
-head2 length()
+=item length()
$x->length();
($xl,$fl) = $x->length();
In list context, returns the length of the integer and fraction part. For
BigInt's, the length of the fraction part will always be 0.
-=head2 exponent()
+=item exponent()
$x->exponent();
Return the exponent of $x as BigInt.
-=head2 mantissa()
+=item mantissa()
$x->mantissa();
Return the signed mantissa of $x as BigInt.
-=head2 parts()
+=item parts()
$x->parts(); # return (mantissa,exponent) as BigInt
-=head2 copy()
+=item copy()
$x->copy(); # make a true copy of $x (unlike $y = $x;)
-=head2 as_int()/as_number()
+=item as_int()/as_number()
$x->as_int();
C<as_number()> is an alias to this method. C<as_number> was introduced in
v1.22, while C<as_int()> was only introduced in v1.68.
-=head2 bstr()
+=item bstr()
$x->bstr();
Returns a normalized string representation of C<$x>.
-=head2 bsstr()
+=item bsstr()
$x->bsstr(); # normalized string in scientific notation
-=head2 as_hex()
+=item as_hex()
$x->as_hex(); # as signed hexadecimal string with prefixed 0x
-=head2 as_bin()
+=item as_bin()
$x->as_bin(); # as signed binary string with prefixed 0b
-=head2 as_oct()
+=item as_oct()
$x->as_oct(); # as signed octal string with prefixed 0
-=head2 numify()
+=item numify()
print $x->numify();
This loses precision, to avoid this use L<as_int()|/"as_int()/as_number()"> instead.
-=head2 modify()
+=item modify()
$x->modify('bpowd');
This is used for instance by L<Math::BigInt::Constant>.
-=head2 upgrade()/downgrade()
+=item upgrade()/downgrade()
Set/get the class for downgrade/upgrade operations. Thuis is used
for instance by L<bignum>. The defaults are '', thus the following
print $i + $f,"\n"; # print 246
-=head2 div_scale()
+=item div_scale()
Set/get the number of digits for the default precision in divide
operations.
-=head2 round_mode()
+=item round_mode()
Set/get the current round mode.
+=back
+
=head1 ACCURACY and PRECISION
Since version v1.33, Math::BigInt and Math::BigFloat have full support for
of rounding are possible. (Note that random rounding, as in
Math::Round, is not implemented.)
-=over 2
+=over
=item 'trunc'
The handling of A & P in MBI/MBF (the old core code shipped with Perl
versions <= 5.7.2) is like this:
-=over 2
+=over
=item Precision
This is how it works now:
-=over 2
+=over
=item Setting/Accessing
While BigInt has extensive handling of inf and NaN, certain quirks remain.
-=over 2
+=over
=item oct()/hex()
The basic design of Math::BigInt allows simple subclasses with very little
work, as long as a few simple rules are followed:
-=over 2
+=over
=item *
use Math::BigInt upgrade => 'Math::BigFloat';
-As a shortcut, you can use the module C<bignum>:
+As a shortcut, you can use the module L<bignum>:
use bignum;
The following methods upgrade themselves unconditionally; that is if upgrade
is in effect, they will always hand up their work:
-=over 2
+=over
=item bsqrt()
Some things might not work as you expect them. Below is documented what is
known to be troublesome:
-=over 1
+=over
=item bstr(), bsstr() and 'cmp'
$y = Math::BigInt->new($y);
ok ($x,$y); # okay
-Alternatively, simple use C<< <=> >> for comparisons, this will get it
+Alternatively, simply use C<< <=> >> for comparisons, this will get it
always right. There is not yet a way to get a number automatically represented
as a string that matches exactly the way Perl represents it.
$z = $array[$x]; # does work automatically
-=item length
+=item length()
The following will probably not do what you expect:
print scalar $c->length(),"\n"; # prints 3
-=item bdiv
+=item bdiv()
The following will probably not do what you expect:
to use
print $c / 10000,"\n";
- print scalar $c->bdiv(10000),"\n"; # or if you want to modify $c
-instead.
+or, if you want to modify $c instead,
+
+ print scalar $c->bdiv(10000),"\n";
The quotient is always the greatest integer less than or equal to the
real-valued quotient of the two operands, and the remainder (when it is