$hinthash->{bigint};
}
+#############################################################################
+# the following two routines are for "use bigint qw/hex oct/;":
+
+sub _hex_global
+ {
+ my $i = $_[0];
+ $i = '0x'.$i unless $i =~ /^0x/;
+ Math::BigInt->new($i);
+ }
+
+sub _oct_global
+ {
+ my $i = $_[0];
+ return Math::BigInt->from_oct($i) if $i =~ /^0[0-7]/;
+ Math::BigInt->new($i);
+ }
+
+#############################################################################
+# the following two routines are for Perl 5.9.4 or later and are lexical
+
+sub _hex
+ {
+ return CORE::hex($_[0]) unless in_effect(1);
+ my $i = $_[0];
+ $i = '0x'.$i unless $i =~ /^0x/;
+ Math::BigInt->new($i);
+ }
+
+sub _oct
+ {
+ return CORE::oct($_[0]) unless in_effect(1);
+ my $i = $_[0];
+ return Math::BigInt->from_oct($i) if $i =~ /^0[0-7]/;
+ Math::BigInt->new($i);
+ }
+
sub import
{
my $self = shift;
$^H{bigint} = 1; # we are in effect
+ # for newer Perls always override hex() and oct() with a lexical version:
+ if ($] > 5.009004)
+ {
+ no warnings 'redefine';
+ *CORE::GLOBAL::oct = \&_oct;
+ *CORE::GLOBAL::hex = \&_hex;
+ }
# some defaults
my $lib = ''; my $lib_kind = 'try';
$trace = 1;
splice @a, $j, 1; $j --;
}
+ elsif ($_[$i] eq 'hex')
+ {
+ splice @a, $j, 1; $j --;
+ no warnings 'redefine';
+ *CORE::GLOBAL::hex = \&_hex_global;
+ }
+ elsif ($_[$i] eq 'oct')
+ {
+ splice @a, $j, 1; $j --;
+ no warnings 'redefine';
+ *CORE::GLOBAL::oct = \&_oct_global;
+ }
else { die "unknown option $_[$i]"; }
}
my $class;
print 2 ** 512,"\n"; # really is what you think it is
print inf + 42,"\n"; # inf
print NaN * 7,"\n"; # NaN
+ print hex("0x1234567890123490"),"\n"; # Perl v5.9.4 or later
{
no bigint;
print 2 ** 256,"\n"; # a normal Perl scalar now
}
+ # Note that this will be global:
+ use bigint qw/hex oct/;
+ print hex("0x1234567890123490"),"\n";
+ print oct("01234567890123490"),"\n";
+
=head1 DESCRIPTION
All operators (including basic math operations) are overloaded. Integer
2
# perl -Mbigint -wle 'print exp(1)'
2
- # perl -Mbigint -wle 'print exp(1)'
+ # perl -Minteger -wle 'print exp(1)'
2.71828182845905
- # perl -Mbigint -wle 'print exp(1) + 0'
+ # perl -Minteger -wle 'print exp(1) + 0'
2
In practice this makes seldom a difference as B<parts and results> of
This enables a trace mode and is primarily for debugging bigint or
Math::BigInt.
+=item hex
+
+Override the build-in hex() method with a version that can handle big
+integers. Note that under Perl v5.9.4 or ealier, this will be global
+and cannot be disabled with "no bigint;".
+
+=item oct
+
+Override the build-in oct() method with a version that can handle big
+integers. Note that under Perl v5.9.4 or ealier, this will be global
+and cannot be disabled with "no bigint;".
+
=item l, lib, try or only
Load a different math lib, see L<Math Library>.
See the documentation about the copy constructor and C<=> in overload, as
well as the documentation in BigInt for further details.
+=head1 CAVAETS
+
+=over 2
+
+=item in_effect()
+
+This method only works on Perl v5.9.4 or later.
+
+=item hex()/oct()
+
+C<bigint> overrides these routines with versions that can also handle
+big integer values. Under Perl prior to version v5.9.4, however, this
+will not happen unless you specifically ask for it with the two
+import tags "hex" and "oct" - and then it will be global and cannot be
+disabled inside a scope with "no bigint":
+
+ use bigint qw/hex oct/;
+
+ print hex("0x1234567890123456");
+ {
+ no bigint;
+ print hex("0x1234567890123456");
+ }
+
+The second call to hex() will warn about a non-portable constant.
+
+Compare this to:
+
+ use bigint;
+
+ # will warn only under Perl older than v5.9.4
+ print hex("0x1234567890123456");
+
+=back
+
=head1 MODULES USED
C<bigint> is just a thin wrapper around various modules of the Math::BigInt
$VERSION = '0.22';
use Exporter;
+@ISA = qw( bigint );
@EXPORT_OK = qw( );
@EXPORT = qw( inf NaN );
-@ISA = qw( Exporter );
use strict;
use overload;
+require bigint; # no "use" to avoid import being called
##############################################################################
+BEGIN
+ {
+ *inf = \&bigint::inf;
+ *NaN = \&bigint::NaN;
+ }
+
# These are all alike, and thus faked by AUTOLOAD
my @faked = qw/round_mode accuracy precision div_scale/;
Carp::croak ("Can't call bignum\-\>$name, not a valid method");
}
-sub upgrade
- {
- $Math::BigInt::upgrade;
- }
-
-sub _binary_constant
- {
- # this takes a binary/hexadecimal/octal constant string and returns it
- # as string suitable for new. Basically it converts octal to decimal, and
- # passes every thing else unmodified back.
- my $string = shift;
-
- return Math::BigInt->new($string) if $string =~ /^0[bx]/;
-
- # so it must be an octal constant
- Math::BigInt->from_oct($string);
- }
-
sub unimport
{
$^H{bignum} = undef; # no longer in effect
$hinthash->{bignum};
}
+#############################################################################
+# the following two routines are for Perl 5.9.4 or later and are lexical
+
+sub _hex
+ {
+ return CORE::hex($_[0]) unless in_effect(1);
+ my $i = $_[0];
+ $i = '0x'.$i unless $i =~ /^0x/;
+ Math::BigInt->new($i);
+ }
+
+sub _oct
+ {
+ return CORE::oct($_[0]) unless in_effect(1);
+ my $i = $_[0];
+ return Math::BigInt->from_oct($i) if $i =~ /^0[0-7]/;
+ Math::BigInt->new($i);
+ }
+
sub import
{
my $self = shift;
$^H{bignum} = 1; # we are in effect
+ # for newer Perls override hex() and oct() with a lexical version:
+ if ($] > 5.009003)
+ {
+ no warnings 'redefine';
+ *CORE::GLOBAL::oct = \&_oct;
+ *CORE::GLOBAL::hex = \&_hex;
+ }
+
# some defaults
my $lib = ''; my $lib_kind = 'try';
my $upgrade = 'Math::BigFloat';
$trace = 1;
splice @a, $j, 1; $j --;
}
+ elsif ($_[$i] eq 'hex')
+ {
+ splice @a, $j, 1; $j --;
+ no warnings 'redefine';
+ # override with a global version
+ *CORE::GLOBAL::hex = \&bigint::_hex_global;
+ }
+ elsif ($_[$i] eq 'oct')
+ {
+ splice @a, $j, 1; $j --;
+ no warnings 'redefine';
+ # override with a global version
+ *CORE::GLOBAL::oct = \&bigint::_oct_global;
+ }
else { die "unknown option $_[$i]"; }
}
my $class;
}
# Take care of octal/hexadecimal constants
- overload::constant binary => sub { _binary_constant(shift) };
+ overload::constant binary => sub { bigint::_binary_constant(shift) };
# if another big* was already loaded:
my ($package) = caller();
}
}
-sub inf () { Math::BigInt->binf(); }
-sub NaN () { Math::BigInt->bnan(); }
-
1;
__END__
print 2 ** 256,"\n"; # a normal Perl scalar now
}
+ # for older Perls, note that this will be global:
+ use bignum qw/hex oct/;
+ print hex("0x1234567890123490"),"\n";
+ print oct("01234567890123490"),"\n";
+
=head1 DESCRIPTION
All operators (including basic math operations) are overloaded. Integer and
BigInt/BigFloat on them. This even works to some extent on expressions:
perl -Mbignum -le '$x = 1234; print $x->bdec()'
- perl -Mbignum -le 'print 1234->binc();'
- perl -Mbignum -le 'print 1234->binc->badd(6);'
- perl -Mbignum -le 'print +(1234)->binc()'
+ perl -Mbignum -le 'print 1234->copy()->binc();'
+ perl -Mbignum -le 'print 1234->copy()->binc->badd(6);'
+ perl -Mbignum -le 'print +(1234)->copy()->binc()'
(Note that print doesn't do what you expect if the expression starts with
'(' hence the C<+>)
You can even chain the operations together as usual:
- perl -Mbignum -le 'print 1234->binc->badd(6);'
+ perl -Mbignum -le 'print 1234->copy()->binc->badd(6);'
1241
Under bignum (or bigint or bigrat), Perl will "upgrade" the numbers
12381/10
The entire upgrading/downgrading is still experimental and might not work
-as you expect or may even have bugs.
-
-You might get errors like this:
+as you expect or may even have bugs. You might get errors like this:
Can't use an undefined value as an ARRAY reference at
/usr/local/lib/perl5/5.8.0/Math/BigInt/Calc.pm line 864
This will be hopefully fixed soon ;)
+=item hex
+
+Override the build-in hex() method with a version that can handle big
+integers. Note that under Perl older than v5.9.4, this will be global
+and cannot be disabled with "no bigint;".
+
+=item oct
+
+Override the build-in oct() method with a version that can handle big
+integers. Note that under Perl older than v5.9.4, this will be global
+and cannot be disabled with "no bigint;".
+
=item v or version
This prints out the name and version of all modules used and then exits.
$x = 9; $y = $x;
print $x->bmul(2), " ", $y,"\n"; # prints 18 18
-Using methods that do not modify, but testthe contents works:
+Using methods that do not modify, but test the contents works:
$x = 9; $y = $x;
$z = 9 if $x->is_zero(); # works fine
minus infinity. You will get '+inf' when dividing a positive number by 0, and
'-inf' when dividing any negative number by 0.
+=head1 CAVAETS
+
+=over 2
+
+=item in_effect()
+
+This method only works on Perl v5.9.4 or later.
+
+=item hex()/oct()
+
+C<bigint> overrides these routines with versions that can also handle
+big integer values. Under Perl prior to version v5.9.4, however, this
+will not happen unless you specifically ask for it with the two
+import tags "hex" and "oct" - and then it will be global and cannot be
+disabled inside a scope with "no bigint":
+
+ use bigint qw/hex oct/;
+
+ print hex("0x1234567890123456");
+ {
+ no bigint;
+ print hex("0x1234567890123456");
+ }
+
+The second call to hex() will warn about a non-portable constant.
+
+Compare this to:
+
+ use bigint;
+
+ # will warn only under older than v5.9.4
+ print hex("0x1234567890123456");
+
+=back
+
=head1 MODULES USED
C<bignum> is just a thin wrapper around various modules of the Math::BigInt
$| = 1;
chdir 't' if -d 't';
unshift @INC, '../lib';
- plan tests => 36;
+ plan tests => 51;
}
-use bigint;
+use bigint qw/hex oct/;
###############################################################################
# _constant tests
ok (bigint->round_mode(),'odd');
###############################################################################
+# hex() and oct()
+
+my $c = 'Math::BigInt';
+
+ok (ref(hex(1)), $c);
+ok (ref(hex(0x1)), $c);
+ok (ref(hex("af")), $c);
+ok (hex("af"), Math::BigInt->new(0xaf));
+ok (ref(hex("0x1")), $c);
+
+ok (ref(oct("0x1")), $c);
+ok (ref(oct("01")), $c);
+ok (ref(oct("0b01")), $c);
+ok (ref(oct("1")), $c);
+ok (ref(oct(" 1")), $c);
+ok (ref(oct(" 0x1")), $c);
+
+ok (ref(oct(0x1)), $c);
+ok (ref(oct(01)), $c);
+ok (ref(oct(0b01)), $c);
+ok (ref(oct(1)), $c);
+
+###############################################################################
###############################################################################
# Perl 5.005 does not like ok ($x,undef)
$| = 1;
chdir 't' if -d 't';
unshift @INC, '../lib';
- plan tests => 20;
+ plan tests => 35;
}
-use bignum;
+use bignum qw/oct hex/;
###############################################################################
# general tests
ok (bignum->round_mode(),'odd');
###############################################################################
+# hex() and oct()
+
+my $c = 'Math::BigInt';
+
+ok (ref(hex(1)), $c);
+ok (ref(hex(0x1)), $c);
+ok (ref(hex("af")), $c);
+ok (hex("af"), Math::BigInt->new(0xaf));
+ok (ref(hex("0x1")), $c);
+
+ok (ref(oct("0x1")), $c);
+ok (ref(oct("01")), $c);
+ok (ref(oct("0b01")), $c);
+ok (ref(oct("1")), $c);
+ok (ref(oct(" 1")), $c);
+ok (ref(oct(" 0x1")), $c);
+
+ok (ref(oct(0x1)), $c);
+ok (ref(oct(01)), $c);
+ok (ref(oct(0b01)), $c);
+ok (ref(oct(1)), $c);
+
+###############################################################################
###############################################################################
# Perl 5.005 does not like ok ($x,undef)
$| = 1;
chdir 't' if -d 't';
unshift @INC, '../lib';
- plan tests => 25;
+ plan tests => 40;
}
-use bigrat;
+use bigrat qw/oct hex/;
###############################################################################
# general tests
ok (bigrat->round_mode(),'odd');
###############################################################################
+# hex() and oct()
+
+my $c = 'Math::BigInt';
+
+ok (ref(hex(1)), $c);
+ok (ref(hex(0x1)), $c);
+ok (ref(hex("af")), $c);
+ok (hex("af"), Math::BigInt->new(0xaf));
+ok (ref(hex("0x1")), $c);
+
+ok (ref(oct("0x1")), $c);
+ok (ref(oct("01")), $c);
+ok (ref(oct("0b01")), $c);
+ok (ref(oct("1")), $c);
+ok (ref(oct(" 1")), $c);
+ok (ref(oct(" 0x1")), $c);
+
+ok (ref(oct(0x1)), $c);
+ok (ref(oct(01)), $c);
+ok (ref(oct(0b01)), $c);
+ok (ref(oct(1)), $c);
+
+###############################################################################
###############################################################################
# Perl 5.005 does not like ok ($x,undef)
can_ok ('bigrat', qw/in_effect/);
SKIP: {
- skip ('Need at least Perl v5.9.4', 3) unless $] > 5.009004;
+ skip ('Need at least Perl v5.9.4', 3) if $] < "5.009005";
is (bigint::in_effect(), 1, 'bigint in effect');
is (bignum::in_effect(), 1, 'bignum in effect');
#!/usr/bin/perl -w
###############################################################################
-# Test no bignum;
+# Test "no bignum;" and overloading of hex()/oct() for newer Perls
use Test::More;
use strict;
$| = 1;
chdir 't' if -d 't';
unshift @INC, '../lib';
- plan tests => 6;
+ plan tests => 10;
}
+# no :hex and :oct means these do not get overloaded for older Perls:
use bignum;
isnt (ref(1), '', 'is in effect');
isnt (ref(2.0), '', 'is in effect');
isnt (ref(0x20), '', 'is in effect');
+SKIP: {
+ skip ('Need at least Perl v5.9.4', 2) if $] < 5.009004;
+
+ is (ref(hex(9)), 'Math::BigInt', 'hex is overloaded');
+ is (ref(oct(07)), 'Math::BigInt', 'oct is overloaded');
+ }
+
{
no bignum;
is (ref(1), '', 'is not in effect');
is (ref(2.0), '', 'is not in effect');
is (ref(0x20), '', 'is not in effect');
+
+ isnt (ref(hex(9)), 'Math::BigInt', 'hex is not overloaded');
+ isnt (ref(oct(07)), 'Math::BigInt', 'oct is not overloaded');
}
#!/usr/bin/perl -w
###############################################################################
-# Test no bigint;
+# Test "no bigint;" and overloading of hex()/oct() for newer Perls
use Test::More;
use strict;
$| = 1;
chdir 't' if -d 't';
unshift @INC, '../lib';
- plan tests => 6;
+ plan tests => 10;
}
+# no :hex and :oct means these do not get overloaded for older Perls:
use bigint;
isnt (ref(1), '', 'is in effect');
isnt (ref(2.0), '', 'is in effect');
isnt (ref(0x20), '', 'is in effect');
+SKIP: {
+ skip ('Need at least Perl v5.9.4', 2) if $] < "5.009004"; # quote due to "use bigint;"
+
+ is (ref(hex(9)), 'Math::BigInt', 'hex is overloaded');
+ is (ref(oct(07)), 'Math::BigInt', 'oct is overloaded');
+ }
+
{
no bigint;
is (ref(1), '', 'is not in effect');
is (ref(2.0), '', 'is not in effect');
is (ref(0x20), '', 'is not in effect');
+
+ isnt (ref(hex(9)), 'Math::BigInt', 'hex is not overloaded');
+ isnt (ref(oct(07)), 'Math::BigInt', 'oct is not overloaded');
+
}
#!/usr/bin/perl -w
###############################################################################
-# Test no bigint;
+# Test "no bigrat;" and overloading of hex()/oct() for newer Perls
use Test::More;
use strict;
$| = 1;
chdir 't' if -d 't';
unshift @INC, '../lib';
- plan tests => 6;
+ plan tests => 10;
}
+# no :hex and :oct means these do not get overloaded for older Perls:
use bigrat;
isnt (ref(1), '', 'is in effect');
isnt (ref(2.0), '', 'is in effect');
isnt (ref(0x20), '', 'is in effect');
+SKIP: {
+ skip ('Need at least Perl v5.9.4', 2) if $] < 5.009004;
+
+ is (ref(hex(9)), 'Math::BigInt', 'hex is overloaded');
+ is (ref(oct(07)), 'Math::BigInt', 'oct is overloaded');
+ }
+
{
no bigrat;
is (ref(1), '', 'is not in effect');
is (ref(2.0), '', 'is not in effect');
is (ref(0x20), '', 'is not in effect');
+
+ isnt (ref(hex(9)), 'Math::BigInt', 'hex is not overloaded');
+ isnt (ref(oct(07)), 'Math::BigInt', 'oct is not overloaded');
}
$VERSION = '0.22';
require Exporter;
-@ISA = qw( Exporter );
+@ISA = qw( bigint );
@EXPORT_OK = qw( );
@EXPORT = qw( inf NaN );
use strict;
use overload;
+require bigint; # no "use" to avoid callind import
##############################################################################
+BEGIN
+ {
+ *inf = \&bigint::inf;
+ *NaN = \&bigint::NaN;
+ }
+
# These are all alike, and thus faked by AUTOLOAD
my @faked = qw/round_mode accuracy precision div_scale/;
Carp::croak ("Can't call bigrat\-\>$name, not a valid method");
}
-sub upgrade
- {
- $Math::BigInt::upgrade;
- }
-
-sub _binary_constant
- {
- # this takes a binary/hexadecimal/octal constant string and returns it
- # as string suitable for new. Basically it converts octal to decimal, and
- # passes every thing else unmodified back.
- my $string = shift;
-
- return Math::BigInt->new($string) if $string =~ /^0[bx]/;
-
- # so it must be an octal constant
- Math::BigInt->from_oct($string);
- }
-
sub unimport
{
$^H{bigrat} = undef; # no longer in effect
$hinthash->{bigrat};
}
+#############################################################################
+# the following two routines are for Perl 5.9.4 or later and are lexical
+
+sub _hex
+ {
+ return CORE::hex($_[0]) unless in_effect(1);
+ my $i = $_[0];
+ $i = '0x'.$i unless $i =~ /^0x/;
+ Math::BigInt->new($i);
+ }
+
+sub _oct
+ {
+ return CORE::oct($_[0]) unless in_effect(1);
+ my $i = $_[0];
+ return Math::BigInt->from_oct($i) if $i =~ /^0[0-7]/;
+ Math::BigInt->new($i);
+ }
+
sub import
{
my $self = shift;
$^H{bigrat} = 1; # we are in effect
+ # for newer Perls always override hex() and oct() with a lexical version:
+ if ($] > 5.009004)
+ {
+ no warnings 'redefine';
+ *CORE::GLOBAL::oct = \&_oct;
+ *CORE::GLOBAL::hex = \&_hex;
+ }
# some defaults
my $lib = ''; my $lib_kind = 'try'; my $upgrade = 'Math::BigFloat';
$trace = 1;
splice @a, $j, 1; $j --;
}
+ elsif ($_[$i] eq 'hex')
+ {
+ splice @a, $j, 1; $j --;
+ no warnings 'redefine';
+ *CORE::GLOBAL::hex = \&bigint::_hex_global;
+ }
+ elsif ($_[$i] eq 'oct')
+ {
+ splice @a, $j, 1; $j --;
+ no warnings 'redefine';
+ *CORE::GLOBAL::oct = \&bigint::_oct_global;
+ }
else
{
die ("unknown option $_[$i]");
}
# Take care of octal/hexadecimal constants
- overload::constant binary => sub { _binary_constant(shift) };
+ overload::constant binary => sub { bigint::_binary_constant(shift) };
# if another big* was already loaded:
my ($package) = caller();
}
}
-sub inf () { Math::BigInt->binf(); }
-sub NaN () { Math::BigInt->bnan(); }
-
1;
__END__
print 1/3,"\n"; # 0.33333...
}
+ # Note that this will make hex() and oct() be globally overriden:
+ use bigrat qw/hex oct/;
+ print hex("0x1234567890123490"),"\n";
+ print oct("01234567890123490"),"\n";
+
=head1 DESCRIPTION
All operators (including basic math operations) are overloaded. Integer and
This will be hopefully fixed soon ;)
+=item hex
+
+Override the build-in hex() method with a version that can handle big
+integers. Note that under Perl v5.9.4 or ealier, this will be global
+and cannot be disabled with "no bigint;".
+
+=item oct
+
+Override the build-in oct() method with a version that can handle big
+integers. Note that under Perl v5.9.4 or ealier, this will be global
+and cannot be disabled with "no bigint;".
+
=item v or version
This prints out the name and version of all modules used and then exits.
=back
+=head1 CAVAETS
+
+=over 2
+
+=item in_effect()
+
+This method only works on Perl v5.9.4 or later.
+
+=item hex()/oct()
+
+C<bigint> overrides these routines with versions that can also handle
+big integer values. Under Perl prior to version v5.9.4, however, this
+will not happen unless you specifically ask for it with the two
+import tags "hex" and "oct" - and then it will be global and cannot be
+disabled inside a scope with "no bigint":
+
+ use bigint qw/hex oct/;
+
+ print hex("0x1234567890123456");
+ {
+ no bigint;
+ print hex("0x1234567890123456");
+ }
+
+The second call to hex() will warn about a non-portable constant.
+
+Compare this to:
+
+ use bigint;
+
+ # will warn only under Perl older than v5.9.4
+ print hex("0x1234567890123456");
+
+=back
+
=head1 EXAMPLES
perl -Mbigrat -le 'print sqrt(33)'