1b8851fccSafresh1package bigint; 2b8851fccSafresh1 3b8851fccSafresh1use strict; 4b8851fccSafresh1use warnings; 5b8851fccSafresh1 6eac174f2Safresh1use Carp qw< carp croak >; 7eac174f2Safresh1 8*3d61058aSafresh1our $VERSION = '0.67'; 9b8851fccSafresh1 10b8851fccSafresh1use Exporter; 11b8851fccSafresh1our @ISA = qw( Exporter ); 12b8851fccSafresh1our @EXPORT_OK = qw( PI e bpi bexp hex oct ); 13b8851fccSafresh1our @EXPORT = qw( inf NaN ); 14b8851fccSafresh1 15b8851fccSafresh1use overload; 16b8851fccSafresh1 17eac174f2Safresh1my $obj_class = "Math::BigInt"; 18eac174f2Safresh1 19b8851fccSafresh1############################################################################## 20b8851fccSafresh1 21eac174f2Safresh1sub accuracy { 22b8851fccSafresh1 my $self = shift; 23eac174f2Safresh1 $obj_class -> accuracy(@_); 24b8851fccSafresh1} 25b8851fccSafresh1 26eac174f2Safresh1sub precision { 27eac174f2Safresh1 my $self = shift; 28eac174f2Safresh1 $obj_class -> precision(@_); 29b8851fccSafresh1} 30b8851fccSafresh1 31eac174f2Safresh1sub round_mode { 32eac174f2Safresh1 my $self = shift; 33eac174f2Safresh1 $obj_class -> round_mode(@_); 34b8851fccSafresh1} 35b8851fccSafresh1 36eac174f2Safresh1sub div_scale { 37eac174f2Safresh1 my $self = shift; 38eac174f2Safresh1 $obj_class -> div_scale(@_); 39b8851fccSafresh1} 40b8851fccSafresh1 41b8851fccSafresh1sub in_effect { 42b8851fccSafresh1 my $level = shift || 0; 43b8851fccSafresh1 my $hinthash = (caller($level))[10]; 44b8851fccSafresh1 $hinthash->{bigint}; 45b8851fccSafresh1} 46b8851fccSafresh1 47eac174f2Safresh1sub _float_constant { 48eac174f2Safresh1 my $str = shift; 49eac174f2Safresh1 50eac174f2Safresh1 # We can't pass input directly to new() because of the way it handles the 51eac174f2Safresh1 # combination of non-integers with no upgrading. Such cases are by 52eac174f2Safresh1 # Math::BigInt returned as NaN, but we truncate to an integer. 53eac174f2Safresh1 54eac174f2Safresh1 # See if we can convert the input string to a string using a normalized form 55eac174f2Safresh1 # consisting of the significand as a signed integer, the character "e", and 56eac174f2Safresh1 # the exponent as a signed integer, e.g., "+0e+0", "+314e-2", and "-1e+3". 57eac174f2Safresh1 58eac174f2Safresh1 my $nstr; 59eac174f2Safresh1 60eac174f2Safresh1 if ( 61eac174f2Safresh1 # See if it is an octal number. An octal number like '0377' is also 62eac174f2Safresh1 # accepted by the functions parsing decimal and hexadecimal numbers, so 63eac174f2Safresh1 # handle octal numbers before decimal and hexadecimal numbers. 64eac174f2Safresh1 65eac174f2Safresh1 $str =~ /^0(?:[Oo]|_*[0-7])/ and 66eac174f2Safresh1 $nstr = Math::BigInt -> oct_str_to_dec_flt_str($str) 67eac174f2Safresh1 68eac174f2Safresh1 or 69eac174f2Safresh1 70eac174f2Safresh1 # See if it is decimal number. 71eac174f2Safresh1 72eac174f2Safresh1 $nstr = Math::BigInt -> dec_str_to_dec_flt_str($str) 73eac174f2Safresh1 74eac174f2Safresh1 or 75eac174f2Safresh1 76eac174f2Safresh1 # See if it is a hexadecimal number. Every hexadecimal number has a 77eac174f2Safresh1 # prefix, but the functions parsing numbers don't require it, so check 78eac174f2Safresh1 # to see if it actually is a hexadecimal number. 79eac174f2Safresh1 80eac174f2Safresh1 $str =~ /^0[Xx]/ and 81eac174f2Safresh1 $nstr = Math::BigInt -> hex_str_to_dec_flt_str($str) 82eac174f2Safresh1 83eac174f2Safresh1 or 84eac174f2Safresh1 85eac174f2Safresh1 # See if it is a binary numbers. Every binary number has a prefix, but 86eac174f2Safresh1 # the functions parsing numbers don't require it, so check to see if it 87eac174f2Safresh1 # actually is a binary number. 88eac174f2Safresh1 89eac174f2Safresh1 $str =~ /^0[Bb]/ and 90eac174f2Safresh1 $nstr = Math::BigInt -> bin_str_to_dec_flt_str($str)) 91eac174f2Safresh1 { 92eac174f2Safresh1 my $pos = index($nstr, 'e'); 93eac174f2Safresh1 my $expo_sgn = substr($nstr, $pos + 1, 1); 94eac174f2Safresh1 my $sign = substr($nstr, 0, 1); 95eac174f2Safresh1 my $mant = substr($nstr, 1, $pos - 1); 96eac174f2Safresh1 my $mant_len = CORE::length($mant); 97eac174f2Safresh1 my $expo = substr($nstr, $pos + 2); 98eac174f2Safresh1 99eac174f2Safresh1 if ($expo_sgn eq '-') { 100eac174f2Safresh1 if ($mant_len <= $expo) { 101eac174f2Safresh1 return $obj_class -> bzero(); # underflow 102eac174f2Safresh1 } else { 103eac174f2Safresh1 $mant = substr $mant, 0, $mant_len - $expo; # truncate 104eac174f2Safresh1 return $obj_class -> new($sign . $mant); 105eac174f2Safresh1 } 106eac174f2Safresh1 } else { 107eac174f2Safresh1 $mant .= "0" x $expo; # pad with zeros 108eac174f2Safresh1 return $obj_class -> new($sign . $mant); 109eac174f2Safresh1 } 110eac174f2Safresh1 } 111eac174f2Safresh1 112eac174f2Safresh1 # If we get here, there is a bug in the code above this point. 113eac174f2Safresh1 114eac174f2Safresh1 warn "Internal error: unable to handle literal constant '$str'.", 115eac174f2Safresh1 " This is a bug, so please report this to the module author."; 116eac174f2Safresh1 return $obj_class -> bnan(); 117eac174f2Safresh1} 118eac174f2Safresh1 119b8851fccSafresh1############################################################################# 120b8851fccSafresh1# the following two routines are for "use bigint qw/hex oct/;": 121b8851fccSafresh1 122b8851fccSafresh1use constant LEXICAL => $] > 5.009004; 123b8851fccSafresh1 124b8851fccSafresh1# Internal function with the same semantics as CORE::hex(). This function is 125b8851fccSafresh1# not used directly, but rather by other front-end functions. 126b8851fccSafresh1 127b8851fccSafresh1sub _hex_core { 128b8851fccSafresh1 my $str = shift; 129b8851fccSafresh1 130b8851fccSafresh1 # Strip off, clean, and parse as much as we can from the beginning. 131b8851fccSafresh1 132b8851fccSafresh1 my $x; 133b8851fccSafresh1 if ($str =~ s/ ^ ( 0? [xX] )? ( [0-9a-fA-F]* ( _ [0-9a-fA-F]+ )* ) //x) { 134b8851fccSafresh1 my $chrs = $2; 135b8851fccSafresh1 $chrs =~ tr/_//d; 136b8851fccSafresh1 $chrs = '0' unless CORE::length $chrs; 137eac174f2Safresh1 $x = $obj_class -> from_hex($chrs); 138b8851fccSafresh1 } else { 139eac174f2Safresh1 $x = $obj_class -> bzero(); 140b8851fccSafresh1 } 141b8851fccSafresh1 142b8851fccSafresh1 # Warn about trailing garbage. 143b8851fccSafresh1 144b8851fccSafresh1 if (CORE::length($str)) { 145b8851fccSafresh1 require Carp; 146b8851fccSafresh1 Carp::carp(sprintf("Illegal hexadecimal digit '%s' ignored", 147b8851fccSafresh1 substr($str, 0, 1))); 148b8851fccSafresh1 } 149b8851fccSafresh1 150b8851fccSafresh1 return $x; 151b8851fccSafresh1} 152b8851fccSafresh1 153b8851fccSafresh1# Internal function with the same semantics as CORE::oct(). This function is 154b8851fccSafresh1# not used directly, but rather by other front-end functions. 155b8851fccSafresh1 156b8851fccSafresh1sub _oct_core { 157b8851fccSafresh1 my $str = shift; 158b8851fccSafresh1 159b8851fccSafresh1 $str =~ s/^\s*//; 160b8851fccSafresh1 161b8851fccSafresh1 # Hexadecimal input. 162b8851fccSafresh1 163b8851fccSafresh1 return _hex_core($str) if $str =~ /^0?[xX]/; 164b8851fccSafresh1 165b8851fccSafresh1 my $x; 166b8851fccSafresh1 167b8851fccSafresh1 # Binary input. 168b8851fccSafresh1 169b8851fccSafresh1 if ($str =~ /^0?[bB]/) { 170b8851fccSafresh1 171b8851fccSafresh1 # Strip off, clean, and parse as much as we can from the beginning. 172b8851fccSafresh1 173b8851fccSafresh1 if ($str =~ s/ ^ ( 0? [bB] )? ( [01]* ( _ [01]+ )* ) //x) { 174b8851fccSafresh1 my $chrs = $2; 175b8851fccSafresh1 $chrs =~ tr/_//d; 176b8851fccSafresh1 $chrs = '0' unless CORE::length $chrs; 177eac174f2Safresh1 $x = $obj_class -> from_bin($chrs); 178b8851fccSafresh1 } 179b8851fccSafresh1 180b8851fccSafresh1 # Warn about trailing garbage. 181b8851fccSafresh1 182b8851fccSafresh1 if (CORE::length($str)) { 183b8851fccSafresh1 require Carp; 184b8851fccSafresh1 Carp::carp(sprintf("Illegal binary digit '%s' ignored", 185b8851fccSafresh1 substr($str, 0, 1))); 186b8851fccSafresh1 } 187b8851fccSafresh1 188b8851fccSafresh1 return $x; 189b8851fccSafresh1 } 190b8851fccSafresh1 191b8851fccSafresh1 # Octal input. Strip off, clean, and parse as much as we can from the 192b8851fccSafresh1 # beginning. 193b8851fccSafresh1 194eac174f2Safresh1 if ($str =~ s/ ^ ( 0? [oO] )? ( [0-7]* ( _ [0-7]+ )* ) //x) { 195eac174f2Safresh1 my $chrs = $2; 196b8851fccSafresh1 $chrs =~ tr/_//d; 197b8851fccSafresh1 $chrs = '0' unless CORE::length $chrs; 198eac174f2Safresh1 $x = $obj_class -> from_oct($chrs); 199b8851fccSafresh1 } 200b8851fccSafresh1 201eac174f2Safresh1 # Warn about trailing garbage. CORE::oct() only warns about 8 and 9, but it 202eac174f2Safresh1 # is more helpful to warn about all invalid digits. 203b8851fccSafresh1 204b8851fccSafresh1 if (CORE::length($str)) { 205b8851fccSafresh1 require Carp; 206eac174f2Safresh1 Carp::carp(sprintf("Illegal octal digit '%s' ignored", 207eac174f2Safresh1 substr($str, 0, 1))); 208b8851fccSafresh1 } 209b8851fccSafresh1 210b8851fccSafresh1 return $x; 211b8851fccSafresh1} 212b8851fccSafresh1 213b8851fccSafresh1{ 214b8851fccSafresh1 my $proto = LEXICAL ? '_' : ';$'; 215b8851fccSafresh1 eval ' 216b8851fccSafresh1sub hex(' . $proto . ') {' . <<'.'; 217b8851fccSafresh1 my $str = @_ ? $_[0] : $_; 218b8851fccSafresh1 _hex_core($str); 219b8851fccSafresh1} 220b8851fccSafresh1. 221b8851fccSafresh1 222b8851fccSafresh1 eval ' 223b8851fccSafresh1sub oct(' . $proto . ') {' . <<'.'; 224b8851fccSafresh1 my $str = @_ ? $_[0] : $_; 225b8851fccSafresh1 _oct_core($str); 226b8851fccSafresh1} 227b8851fccSafresh1. 228b8851fccSafresh1} 229b8851fccSafresh1 230b8851fccSafresh1############################################################################# 231b8851fccSafresh1# the following two routines are for Perl 5.9.4 or later and are lexical 232b8851fccSafresh1 233b8851fccSafresh1my ($prev_oct, $prev_hex, $overridden); 234b8851fccSafresh1 235b8851fccSafresh1if (LEXICAL) { eval <<'.' } 236b8851fccSafresh1sub _hex(_) { 237b8851fccSafresh1 my $hh = (caller 0)[10]; 238eac174f2Safresh1 return $$hh{bigint} ? bigint::_hex_core($_[0]) 239eac174f2Safresh1 : $$hh{bigfloat} ? bigfloat::_hex_core($_[0]) 240eac174f2Safresh1 : $$hh{bigrat} ? bigrat::_hex_core($_[0]) 241eac174f2Safresh1 : $prev_hex ? &$prev_hex($_[0]) 242eac174f2Safresh1 : CORE::hex($_[0]); 243b8851fccSafresh1} 244b8851fccSafresh1 245b8851fccSafresh1sub _oct(_) { 246b8851fccSafresh1 my $hh = (caller 0)[10]; 247eac174f2Safresh1 return $$hh{bigint} ? bigint::_oct_core($_[0]) 248eac174f2Safresh1 : $$hh{bigfloat} ? bigfloat::_oct_core($_[0]) 249eac174f2Safresh1 : $$hh{bigrat} ? bigrat::_oct_core($_[0]) 250eac174f2Safresh1 : $prev_oct ? &$prev_oct($_[0]) 251eac174f2Safresh1 : CORE::oct($_[0]); 252b8851fccSafresh1} 253b8851fccSafresh1. 254b8851fccSafresh1 255b8851fccSafresh1sub _override { 256b8851fccSafresh1 return if $overridden; 257b8851fccSafresh1 $prev_oct = *CORE::GLOBAL::oct{CODE}; 258b8851fccSafresh1 $prev_hex = *CORE::GLOBAL::hex{CODE}; 259b8851fccSafresh1 no warnings 'redefine'; 260b8851fccSafresh1 *CORE::GLOBAL::oct = \&_oct; 261b8851fccSafresh1 *CORE::GLOBAL::hex = \&_hex; 262eac174f2Safresh1 $overridden = 1; 263eac174f2Safresh1} 264eac174f2Safresh1 265eac174f2Safresh1sub unimport { 266*3d61058aSafresh1 delete $^H{bigint}; # no longer in effect 267eac174f2Safresh1 overload::remove_constant('binary', '', 'float', '', 'integer'); 268b8851fccSafresh1} 269b8851fccSafresh1 270b8851fccSafresh1sub import { 271eac174f2Safresh1 my $class = shift; 272b8851fccSafresh1 273b8851fccSafresh1 $^H{bigint} = 1; # we are in effect 274*3d61058aSafresh1 delete $^H{bigfloat}; 275*3d61058aSafresh1 delete $^H{bigrat}; 276b8851fccSafresh1 277b8851fccSafresh1 # for newer Perls always override hex() and oct() with a lexical version: 278b8851fccSafresh1 if (LEXICAL) { 279b8851fccSafresh1 _override(); 280b8851fccSafresh1 } 281b8851fccSafresh1 282eac174f2Safresh1 my @import = (); 283eac174f2Safresh1 my @a = (); # unrecognized arguments 284eac174f2Safresh1 my $ver; # version? trace? 285eac174f2Safresh1 286eac174f2Safresh1 while (@_) { 287eac174f2Safresh1 my $param = shift; 288eac174f2Safresh1 289eac174f2Safresh1 # Accuracy. 290eac174f2Safresh1 291eac174f2Safresh1 if ($param =~ /^a(ccuracy)?$/) { 292eac174f2Safresh1 push @import, 'accuracy', shift(); 293eac174f2Safresh1 next; 294eac174f2Safresh1 } 295eac174f2Safresh1 296eac174f2Safresh1 # Precision. 297eac174f2Safresh1 298eac174f2Safresh1 if ($param =~ /^p(recision)?$/) { 299eac174f2Safresh1 push @import, 'precision', shift(); 300eac174f2Safresh1 next; 301eac174f2Safresh1 } 302eac174f2Safresh1 303eac174f2Safresh1 # Rounding mode. 304eac174f2Safresh1 305eac174f2Safresh1 if ($param eq 'round_mode') { 306eac174f2Safresh1 push @import, 'round_mode', shift(); 307eac174f2Safresh1 next; 308eac174f2Safresh1 } 309eac174f2Safresh1 310eac174f2Safresh1 # Backend library. 311eac174f2Safresh1 312eac174f2Safresh1 if ($param =~ /^(l|lib|try|only)$/) { 313eac174f2Safresh1 push @import, $param eq 'l' ? 'lib' : $param; 314eac174f2Safresh1 push @import, shift() if @_; 315eac174f2Safresh1 next; 316eac174f2Safresh1 } 317eac174f2Safresh1 318eac174f2Safresh1 if ($param =~ /^(v|version)$/) { 319b8851fccSafresh1 $ver = 1; 320eac174f2Safresh1 next; 321b8851fccSafresh1 } 322b8851fccSafresh1 323eac174f2Safresh1 if ($param =~ /^(t|trace)$/) { 324eac174f2Safresh1 $obj_class .= "::Trace"; 325eac174f2Safresh1 eval "require $obj_class"; 326eac174f2Safresh1 die $@ if $@; 327eac174f2Safresh1 next; 328eac174f2Safresh1 } 329eac174f2Safresh1 330eac174f2Safresh1 if ($param =~ /^(PI|e|bexp|bpi|hex|oct)\z/) { 331eac174f2Safresh1 push @a, $param; 332eac174f2Safresh1 next; 333eac174f2Safresh1 } 334eac174f2Safresh1 335eac174f2Safresh1 croak("Unknown option '$param'"); 336eac174f2Safresh1 } 337eac174f2Safresh1 338eac174f2Safresh1 eval "require $obj_class"; 339eac174f2Safresh1 die $@ if $@; 340eac174f2Safresh1 $obj_class -> import(@import); 341eac174f2Safresh1 342b8851fccSafresh1 if ($ver) { 343eac174f2Safresh1 printf "%-31s v%s\n", $class, $class -> VERSION(); 344eac174f2Safresh1 printf " lib => %-23s v%s\n", 345eac174f2Safresh1 $obj_class -> config("lib"), $obj_class -> config("lib_version"); 346eac174f2Safresh1 printf "%-31s v%s\n", $obj_class, $obj_class -> VERSION(); 347b8851fccSafresh1 exit; 348b8851fccSafresh1 } 349b8851fccSafresh1 350eac174f2Safresh1 $class -> export_to_level(1, $class, @a); # export inf, NaN, etc. 351b8851fccSafresh1 352eac174f2Safresh1 overload::constant 353eac174f2Safresh1 354eac174f2Safresh1 # This takes care each number written as decimal integer and within the 355eac174f2Safresh1 # range of what perl can represent as an integer, e.g., "314", but not 356eac174f2Safresh1 # "3141592653589793238462643383279502884197169399375105820974944592307". 357eac174f2Safresh1 358eac174f2Safresh1 integer => sub { 359eac174f2Safresh1 #printf "Value '%s' handled by the 'integer' sub.\n", $_[0]; 360eac174f2Safresh1 my $str = shift; 361eac174f2Safresh1 return $obj_class -> new($str); 362eac174f2Safresh1 }, 363eac174f2Safresh1 364eac174f2Safresh1 # This takes care of each number written with a decimal point and/or 365eac174f2Safresh1 # using floating point notation, e.g., "3.", "3.0", "3.14e+2" (decimal), 366eac174f2Safresh1 # "0b1.101p+2" (binary), "03.14p+2" and "0o3.14p+2" (octal), and 367eac174f2Safresh1 # "0x3.14p+2" (hexadecimal). 368eac174f2Safresh1 369eac174f2Safresh1 float => sub { 370eac174f2Safresh1 #printf "# Value '%s' handled by the 'float' sub.\n", $_[0]; 371eac174f2Safresh1 _float_constant(shift); 372eac174f2Safresh1 }, 373eac174f2Safresh1 374eac174f2Safresh1 # Take care of each number written as an integer (no decimal point or 375eac174f2Safresh1 # exponent) using binary, octal, or hexadecimal notation, e.g., "0b101" 376eac174f2Safresh1 # (binary), "0314" and "0o314" (octal), and "0x314" (hexadecimal). 377eac174f2Safresh1 378eac174f2Safresh1 binary => sub { 379eac174f2Safresh1 #printf "# Value '%s' handled by the 'binary' sub.\n", $_[0]; 380eac174f2Safresh1 my $str = shift; 381eac174f2Safresh1 return $obj_class -> new($str) if $str =~ /^0[XxBb]/; 382eac174f2Safresh1 $obj_class -> from_oct($str); 383eac174f2Safresh1 }; 384b8851fccSafresh1} 385b8851fccSafresh1 386eac174f2Safresh1sub inf () { $obj_class -> binf(); } 387eac174f2Safresh1sub NaN () { $obj_class -> bnan(); } 388b8851fccSafresh1 389eac174f2Safresh1sub PI () { $obj_class -> new(3); } 390eac174f2Safresh1sub e () { $obj_class -> new(2); } 391eac174f2Safresh1 392eac174f2Safresh1sub bpi ($) { $obj_class -> new(3); } 393eac174f2Safresh1 394b8851fccSafresh1sub bexp ($$) { 395eac174f2Safresh1 my $x = $obj_class -> new(shift); 396eac174f2Safresh1 $x -> bexp(@_); 397b8851fccSafresh1} 398b8851fccSafresh1 399b8851fccSafresh11; 400b8851fccSafresh1 401b8851fccSafresh1__END__ 402b8851fccSafresh1 403b8851fccSafresh1=pod 404b8851fccSafresh1 405b8851fccSafresh1=head1 NAME 406b8851fccSafresh1 407eac174f2Safresh1bigint - transparent big integer support for Perl 408b8851fccSafresh1 409b8851fccSafresh1=head1 SYNOPSIS 410b8851fccSafresh1 411b8851fccSafresh1 use bigint; 412b8851fccSafresh1 413eac174f2Safresh1 $x = 2 + 4.5; # Math::BigInt 6 414eac174f2Safresh1 print 2 ** 512; # Math::BigInt 134...096 415eac174f2Safresh1 print inf + 42; # Math::BigInt inf 416eac174f2Safresh1 print NaN * 7; # Math::BigInt NaN 417eac174f2Safresh1 print hex("0x1234567890123490"); # Perl v5.10.0 or later 418b8851fccSafresh1 419b8851fccSafresh1 { 420b8851fccSafresh1 no bigint; 421eac174f2Safresh1 print 2 ** 256; # a normal Perl scalar now 422b8851fccSafresh1 } 423b8851fccSafresh1 424eac174f2Safresh1 # for older Perls, import into current package: 425b8851fccSafresh1 use bigint qw/hex oct/; 426eac174f2Safresh1 print hex("0x1234567890123490"); 427eac174f2Safresh1 print oct("01234567890123490"); 428b8851fccSafresh1 429b8851fccSafresh1=head1 DESCRIPTION 430b8851fccSafresh1 431eac174f2Safresh1All numeric literal in the given scope are converted to Math::BigInt objects. 432eac174f2Safresh1Numeric literal that represent non-integers are truncated to an integer. All 433eac174f2Safresh1results of expressions are also truncated to integer. 434eac174f2Safresh1 435b8851fccSafresh1All operators (including basic math operations) except the range operator C<..> 436eac174f2Safresh1are overloaded. 437b8851fccSafresh1 438eac174f2Safresh1Unlike the L<integer> pragma, the C<bigint> pragma creates integers that are 439eac174f2Safresh1only limited in their size by the available memory. 440b8851fccSafresh1 441eac174f2Safresh1So, the following: 442eac174f2Safresh1 443eac174f2Safresh1 use bigint; 444eac174f2Safresh1 $x = 1234; 445eac174f2Safresh1 446eac174f2Safresh1creates a Math::BigInt and stores a reference to in $x. This happens 447eac174f2Safresh1transparently and behind your back, so to speak. 448eac174f2Safresh1 449eac174f2Safresh1You can see this with the following: 450eac174f2Safresh1 451eac174f2Safresh1 perl -Mbigint -le 'print ref(1234)' 452eac174f2Safresh1 453eac174f2Safresh1Since numbers are actually objects, you can call all the usual methods from 454eac174f2Safresh1Math::BigFloat on them. This even works to some extent on expressions: 455eac174f2Safresh1 456eac174f2Safresh1 perl -Mbigint -le '$x = 1234; print $x->bdec()' 457eac174f2Safresh1 perl -Mbigint -le 'print 1234->copy()->binc();' 458eac174f2Safresh1 perl -Mbigint -le 'print 1234->copy()->binc->badd(6);' 459eac174f2Safresh1 perl -Mbigint -le 'print +(1234)->copy()->binc()' 460eac174f2Safresh1 461eac174f2Safresh1(Note that print doesn't do what you expect if the expression starts with 462eac174f2Safresh1'(' hence the C<+>) 463eac174f2Safresh1 464eac174f2Safresh1You can even chain the operations together as usual: 465eac174f2Safresh1 466eac174f2Safresh1 perl -Mbigint -le 'print 1234->copy()->binc->badd(6);' 467eac174f2Safresh1 1241 468eac174f2Safresh1 469eac174f2Safresh1Please note the following does not work as expected (prints nothing), since 470eac174f2Safresh1overloading of '..' is not yet possible in Perl (as of v5.8.0): 471eac174f2Safresh1 472eac174f2Safresh1 perl -Mbigint -le 'for (1..2) { print ref($_); }' 473b8851fccSafresh1 474b8851fccSafresh1=head2 use integer vs. use bigint 475b8851fccSafresh1 476eac174f2Safresh1There are some difference between C<use integer> and C<use bigint>. 477eac174f2Safresh1 478eac174f2Safresh1Whereas C<use integer> is limited to what can be handled as a Perl scalar, C<use 479eac174f2Safresh1bigint> can handle arbitrarily large integers. 480eac174f2Safresh1 481eac174f2Safresh1Also, C<use integer> does affect assignments to variables and the return value 482eac174f2Safresh1of some functions. C<use bigint> truncates these results to integer: 483b8851fccSafresh1 484b8851fccSafresh1 # perl -Minteger -wle 'print 3.2' 485b8851fccSafresh1 3.2 486b8851fccSafresh1 # perl -Minteger -wle 'print 3.2 + 0' 487b8851fccSafresh1 3 488b8851fccSafresh1 # perl -Mbigint -wle 'print 3.2' 489b8851fccSafresh1 3 490b8851fccSafresh1 # perl -Mbigint -wle 'print 3.2 + 0' 491b8851fccSafresh1 3 492b8851fccSafresh1 493b8851fccSafresh1 # perl -Mbigint -wle 'print exp(1) + 0' 494b8851fccSafresh1 2 495b8851fccSafresh1 # perl -Mbigint -wle 'print exp(1)' 496b8851fccSafresh1 2 497b8851fccSafresh1 # perl -Minteger -wle 'print exp(1)' 498b8851fccSafresh1 2.71828182845905 499b8851fccSafresh1 # perl -Minteger -wle 'print exp(1) + 0' 500b8851fccSafresh1 2 501b8851fccSafresh1 502eac174f2Safresh1In practice this seldom makes a difference for small integers as B<parts and 503eac174f2Safresh1results> of expressions are truncated anyway, but this can, for instance, affect 504eac174f2Safresh1the return value of subroutines: 505b8851fccSafresh1 506b8851fccSafresh1 sub three_integer { use integer; return 3.2; } 507b8851fccSafresh1 sub three_bigint { use bigint; return 3.2; } 508b8851fccSafresh1 509b8851fccSafresh1 print three_integer(), " ", three_bigint(),"\n"; # prints "3.2 3" 510b8851fccSafresh1 511b8851fccSafresh1=head2 Options 512b8851fccSafresh1 513eac174f2Safresh1C<bigint> recognizes some options that can be passed while loading it via 514eac174f2Safresh1C<use>. The following options exist: 515b8851fccSafresh1 516eac174f2Safresh1=over 4 517b8851fccSafresh1 518b8851fccSafresh1=item a or accuracy 519b8851fccSafresh1 520b8851fccSafresh1This sets the accuracy for all math operations. The argument must be greater 521eac174f2Safresh1than or equal to zero. See Math::BigInt's bround() method for details. 522b8851fccSafresh1 523b8851fccSafresh1 perl -Mbigint=a,2 -le 'print 12345+1' 524b8851fccSafresh1 525b8851fccSafresh1Note that setting precision and accuracy at the same time is not possible. 526b8851fccSafresh1 527b8851fccSafresh1=item p or precision 528b8851fccSafresh1 529b8851fccSafresh1This sets the precision for all math operations. The argument can be any 530eac174f2Safresh1integer. Negative values mean a fixed number of digits after the dot, and are 531eac174f2Safresh1ignored since all operations happen in integer space. A positive value rounds to 532eac174f2Safresh1this digit left from the dot. 0 means round to integer. See Math::BigInt's 533eac174f2Safresh1bfround() method for details. 534b8851fccSafresh1 535eac174f2Safresh1 perl -mbigint=p,5 -le 'print 123456789+123' 536b8851fccSafresh1 537b8851fccSafresh1Note that setting precision and accuracy at the same time is not possible. 538b8851fccSafresh1 539b8851fccSafresh1=item t or trace 540b8851fccSafresh1 541eac174f2Safresh1This enables a trace mode and is primarily for debugging. 542b8851fccSafresh1 543eac174f2Safresh1=item l, lib, try, or only 544b8851fccSafresh1 545b8851fccSafresh1Load a different math lib, see L<Math Library>. 546b8851fccSafresh1 547eac174f2Safresh1 perl -Mbigint=l,GMP -e 'print 2 ** 512' 548b8851fccSafresh1 perl -Mbigint=lib,GMP -e 'print 2 ** 512' 549b8851fccSafresh1 perl -Mbigint=try,GMP -e 'print 2 ** 512' 550b8851fccSafresh1 perl -Mbigint=only,GMP -e 'print 2 ** 512' 551b8851fccSafresh1 552eac174f2Safresh1=item hex 553b8851fccSafresh1 554eac174f2Safresh1Override the built-in hex() method with a version that can handle big numbers. 555eac174f2Safresh1This overrides it by exporting it to the current package. Under Perl v5.10.0 and 556e0680481Safresh1higher, this is not necessary, as hex() is lexically overridden in the current 557e0680481Safresh1scope whenever the C<bigint> pragma is active. 558b8851fccSafresh1 559eac174f2Safresh1=item oct 560eac174f2Safresh1 561eac174f2Safresh1Override the built-in oct() method with a version that can handle big numbers. 562eac174f2Safresh1This overrides it by exporting it to the current package. Under Perl v5.10.0 and 563eac174f2Safresh1higher, this is not so necessary, as oct() is lexically overridden in the 564eac174f2Safresh1current scope whenever the C<bigint> pragma is active. 565b8851fccSafresh1 566b8851fccSafresh1=item v or version 567b8851fccSafresh1 568eac174f2Safresh1this prints out the name and version of the modules and then exits. 569b8851fccSafresh1 570b8851fccSafresh1 perl -Mbigint=v 571b8851fccSafresh1 572b8851fccSafresh1=back 573b8851fccSafresh1 574b8851fccSafresh1=head2 Math Library 575b8851fccSafresh1 576eac174f2Safresh1Math with the numbers is done (by default) by a backend library module called 577eac174f2Safresh1Math::BigInt::Calc. The default is equivalent to saying: 578b8851fccSafresh1 579b8851fccSafresh1 use bigint lib => 'Calc'; 580b8851fccSafresh1 581eac174f2Safresh1you can change this by using: 582b8851fccSafresh1 583eac174f2Safresh1 use bigint lib => 'GMP'; 584b8851fccSafresh1 585eac174f2Safresh1The following would first try to find Math::BigInt::Foo, then Math::BigInt::Bar, 586eac174f2Safresh1and if this also fails, revert to Math::BigInt::Calc: 587b8851fccSafresh1 588b8851fccSafresh1 use bigint lib => 'Foo,Math::BigInt::Bar'; 589b8851fccSafresh1 590eac174f2Safresh1Using c<lib> warns if none of the specified libraries can be found and 591eac174f2Safresh1L<Math::BigInt> fell back to one of the default libraries. To suppress this 592eac174f2Safresh1warning, use c<try> instead: 593b8851fccSafresh1 594eac174f2Safresh1 use bigint try => 'GMP'; 595b8851fccSafresh1 596b8851fccSafresh1If you want the code to die instead of falling back, use C<only> instead: 597b8851fccSafresh1 598eac174f2Safresh1 use bigint only => 'GMP'; 599b8851fccSafresh1 600eac174f2Safresh1Please see the respective module documentation for further details. 601b8851fccSafresh1 602b8851fccSafresh1=head2 Method calls 603b8851fccSafresh1 604eac174f2Safresh1Since all numbers are now objects, you can use all methods that are part of the 605eac174f2Safresh1Math::BigInt API. 606b8851fccSafresh1 607b8851fccSafresh1But a warning is in order. When using the following to make a copy of a number, 608b8851fccSafresh1only a shallow copy will be made. 609b8851fccSafresh1 610b8851fccSafresh1 $x = 9; $y = $x; 611b8851fccSafresh1 $x = $y = 7; 612b8851fccSafresh1 613eac174f2Safresh1Using the copy or the original with overloaded math is okay, e.g., the following 614eac174f2Safresh1work: 615b8851fccSafresh1 616b8851fccSafresh1 $x = 9; $y = $x; 617b8851fccSafresh1 print $x + 1, " ", $y,"\n"; # prints 10 9 618b8851fccSafresh1 619eac174f2Safresh1but calling any method that modifies the number directly will result in B<both> 620eac174f2Safresh1the original and the copy being destroyed: 621b8851fccSafresh1 622b8851fccSafresh1 $x = 9; $y = $x; 623b8851fccSafresh1 print $x->badd(1), " ", $y,"\n"; # prints 10 10 624b8851fccSafresh1 625b8851fccSafresh1 $x = 9; $y = $x; 626b8851fccSafresh1 print $x->binc(1), " ", $y,"\n"; # prints 10 10 627b8851fccSafresh1 628b8851fccSafresh1 $x = 9; $y = $x; 629b8851fccSafresh1 print $x->bmul(2), " ", $y,"\n"; # prints 18 18 630b8851fccSafresh1 631b8851fccSafresh1Using methods that do not modify, but test that the contents works: 632b8851fccSafresh1 633b8851fccSafresh1 $x = 9; $y = $x; 634b8851fccSafresh1 $z = 9 if $x->is_zero(); # works fine 635b8851fccSafresh1 636eac174f2Safresh1See the documentation about the copy constructor and C<=> in overload, as well 637eac174f2Safresh1as the documentation in Math::BigInt for further details. 638b8851fccSafresh1 639b8851fccSafresh1=head2 Methods 640b8851fccSafresh1 641eac174f2Safresh1=over 4 642b8851fccSafresh1 643b8851fccSafresh1=item inf() 644b8851fccSafresh1 645b8851fccSafresh1A shortcut to return Math::BigInt->binf(). Useful because Perl does not always 646b8851fccSafresh1handle bareword C<inf> properly. 647b8851fccSafresh1 648b8851fccSafresh1=item NaN() 649b8851fccSafresh1 650b8851fccSafresh1A shortcut to return Math::BigInt->bnan(). Useful because Perl does not always 651b8851fccSafresh1handle bareword C<NaN> properly. 652b8851fccSafresh1 653b8851fccSafresh1=item e 654b8851fccSafresh1 655b8851fccSafresh1 # perl -Mbigint=e -wle 'print e' 656b8851fccSafresh1 657eac174f2Safresh1Returns Euler's number C<e>, aka exp(1). Note that under C<bigint>, this is 658eac174f2Safresh1truncated to an integer, i.e., 2. 659b8851fccSafresh1 660b8851fccSafresh1=item PI 661b8851fccSafresh1 662b8851fccSafresh1 # perl -Mbigint=PI -wle 'print PI' 663b8851fccSafresh1 664eac174f2Safresh1Returns PI. Note that under C<bigint>, this is truncated to an integer, i.e., 3. 665b8851fccSafresh1 666b8851fccSafresh1=item bexp() 667b8851fccSafresh1 668b8851fccSafresh1 bexp($power, $accuracy); 669b8851fccSafresh1 670eac174f2Safresh1Returns Euler's number C<e> raised to the appropriate power, to the wanted 671eac174f2Safresh1accuracy. 672b8851fccSafresh1 673eac174f2Safresh1Note that under C<bigint>, the result is truncated to an integer. 674b8851fccSafresh1 675b8851fccSafresh1Example: 676b8851fccSafresh1 677b8851fccSafresh1 # perl -Mbigint=bexp -wle 'print bexp(1,80)' 678b8851fccSafresh1 679b8851fccSafresh1=item bpi() 680b8851fccSafresh1 681b8851fccSafresh1 bpi($accuracy); 682b8851fccSafresh1 683eac174f2Safresh1Returns PI to the wanted accuracy. Note that under C<bigint>, this is truncated 684eac174f2Safresh1to an integer, i.e., 3. 685b8851fccSafresh1 686b8851fccSafresh1Example: 687b8851fccSafresh1 688b8851fccSafresh1 # perl -Mbigint=bpi -wle 'print bpi(80)' 689b8851fccSafresh1 690eac174f2Safresh1=item accuracy() 691b8851fccSafresh1 692eac174f2Safresh1Set or get the accuracy. 693eac174f2Safresh1 694eac174f2Safresh1=item precision() 695eac174f2Safresh1 696eac174f2Safresh1Set or get the precision. 697eac174f2Safresh1 698eac174f2Safresh1=item round_mode() 699eac174f2Safresh1 700eac174f2Safresh1Set or get the rounding mode. 701eac174f2Safresh1 702eac174f2Safresh1=item div_scale() 703eac174f2Safresh1 704eac174f2Safresh1Set or get the division scale. 705b8851fccSafresh1 706b8851fccSafresh1=item in_effect() 707b8851fccSafresh1 708b8851fccSafresh1 use bigint; 709b8851fccSafresh1 710b8851fccSafresh1 print "in effect\n" if bigint::in_effect; # true 711b8851fccSafresh1 { 712b8851fccSafresh1 no bigint; 713b8851fccSafresh1 print "in effect\n" if bigint::in_effect; # false 714b8851fccSafresh1 } 715b8851fccSafresh1 716b8851fccSafresh1Returns true or false if C<bigint> is in effect in the current scope. 717b8851fccSafresh1 718b8851fccSafresh1This method only works on Perl v5.9.4 or later. 719b8851fccSafresh1 720b8851fccSafresh1=back 721b8851fccSafresh1 722b8851fccSafresh1=head1 CAVEATS 723b8851fccSafresh1 724eac174f2Safresh1=over 4 725eac174f2Safresh1 726eac174f2Safresh1=item Hexadecimal, octal, and binary floating point literals 727eac174f2Safresh1 728eac174f2Safresh1Perl (and this module) accepts hexadecimal, octal, and binary floating point 729eac174f2Safresh1literals, but use them with care with Perl versions before v5.32.0, because some 730eac174f2Safresh1versions of Perl silently give the wrong result. 731b8851fccSafresh1 732b8851fccSafresh1=item Operator vs literal overloading 733b8851fccSafresh1 734eac174f2Safresh1C<bigint> works by overloading handling of integer and floating point literals, 735eac174f2Safresh1converting them to L<Math::BigInt> objects. 736b8851fccSafresh1 737eac174f2Safresh1This means that arithmetic involving only string values or string literals are 738eac174f2Safresh1performed using Perl's built-in operators. 739b8851fccSafresh1 740b8851fccSafresh1For example: 741b8851fccSafresh1 742eac174f2Safresh1 use bigint; 743b8851fccSafresh1 my $x = "900000000000000009"; 744b8851fccSafresh1 my $y = "900000000000000007"; 745b8851fccSafresh1 print $x - $y; 746b8851fccSafresh1 747eac174f2Safresh1outputs C<0> on default 32-bit builds, since C<bigint> never sees the string 748eac174f2Safresh1literals. To ensure the expression is all treated as C<Math::BigInt> objects, 749eac174f2Safresh1use a literal number in the expression: 750b8851fccSafresh1 751b8851fccSafresh1 print +(0+$x) - $y; 752b8851fccSafresh1 753eac174f2Safresh1=item Ranges 754b8851fccSafresh1 755eac174f2Safresh1Perl does not allow overloading of ranges, so you can neither safely use ranges 756eac174f2Safresh1with C<bigint> endpoints, nor is the iterator variable a C<Math::BigInt>. 757b8851fccSafresh1 758b8851fccSafresh1 use 5.010; 759b8851fccSafresh1 for my $i (12..13) { 760b8851fccSafresh1 for my $j (20..21) { 761b8851fccSafresh1 say $i ** $j; # produces a floating-point number, 762eac174f2Safresh1 # not an object 763b8851fccSafresh1 } 764b8851fccSafresh1 } 765b8851fccSafresh1 766b8851fccSafresh1=item in_effect() 767b8851fccSafresh1 768b8851fccSafresh1This method only works on Perl v5.9.4 or later. 769b8851fccSafresh1 770b8851fccSafresh1=item hex()/oct() 771b8851fccSafresh1 772eac174f2Safresh1C<bigint> overrides these routines with versions that can also handle big 773eac174f2Safresh1integer values. Under Perl prior to version v5.9.4, however, this will not 774eac174f2Safresh1happen unless you specifically ask for it with the two import tags "hex" and 775eac174f2Safresh1"oct" - and then it will be global and cannot be disabled inside a scope with 776eac174f2Safresh1C<no bigint>: 777b8851fccSafresh1 778b8851fccSafresh1 use bigint qw/hex oct/; 779b8851fccSafresh1 780b8851fccSafresh1 print hex("0x1234567890123456"); 781b8851fccSafresh1 { 782b8851fccSafresh1 no bigint; 783b8851fccSafresh1 print hex("0x1234567890123456"); 784b8851fccSafresh1 } 785b8851fccSafresh1 786b8851fccSafresh1The second call to hex() will warn about a non-portable constant. 787b8851fccSafresh1 788b8851fccSafresh1Compare this to: 789b8851fccSafresh1 790b8851fccSafresh1 use bigint; 791b8851fccSafresh1 792b8851fccSafresh1 # will warn only under Perl older than v5.9.4 793b8851fccSafresh1 print hex("0x1234567890123456"); 794b8851fccSafresh1 795b8851fccSafresh1=back 796b8851fccSafresh1 797b8851fccSafresh1=head1 EXAMPLES 798b8851fccSafresh1 799b8851fccSafresh1Some cool command line examples to impress the Python crowd ;) You might want 800eac174f2Safresh1to compare them to the results under -Mbigfloat or -Mbigrat: 801b8851fccSafresh1 802b8851fccSafresh1 perl -Mbigint -le 'print sqrt(33)' 803e0680481Safresh1 perl -Mbigint -le 'print 2**255' 804e0680481Safresh1 perl -Mbigint -le 'print 4.5+2**255' 805b8851fccSafresh1 perl -Mbigint -le 'print 123->is_odd()' 806eac174f2Safresh1 perl -Mbigint=l,GMP -le 'print 7 ** 7777' 807b8851fccSafresh1 8089f11ffb7Safresh1=head1 BUGS 8099f11ffb7Safresh1 810eac174f2Safresh1Please report any bugs or feature requests to 811eac174f2Safresh1C<bug-bignum at rt.cpan.org>, or through the web interface at 812eac174f2Safresh1L<https://rt.cpan.org/Ticket/Create.html?Queue=bignum> (requires login). 813eac174f2Safresh1We will be notified, and then you'll automatically be notified of 814eac174f2Safresh1progress on your bug as I make changes. 8159f11ffb7Safresh1 8169f11ffb7Safresh1=head1 SUPPORT 8179f11ffb7Safresh1 8189f11ffb7Safresh1You can find documentation for this module with the perldoc command. 8199f11ffb7Safresh1 8209f11ffb7Safresh1 perldoc bigint 8219f11ffb7Safresh1 822eac174f2Safresh1You can also look for information at: 8239f11ffb7Safresh1 824eac174f2Safresh1=over 4 825eac174f2Safresh1 826eac174f2Safresh1=item * GitHub 827eac174f2Safresh1 828eac174f2Safresh1L<https://github.com/pjacklam/p5-bignum> 829eac174f2Safresh1 830eac174f2Safresh1=item * RT: CPAN's request tracker 831eac174f2Safresh1 832eac174f2Safresh1L<https://rt.cpan.org/Dist/Display.html?Name=bignum> 833eac174f2Safresh1 834eac174f2Safresh1=item * MetaCPAN 835eac174f2Safresh1 836eac174f2Safresh1L<https://metacpan.org/release/bignum> 837eac174f2Safresh1 838eac174f2Safresh1=item * CPAN Testers Matrix 839eac174f2Safresh1 840eac174f2Safresh1L<http://matrix.cpantesters.org/?dist=bignum> 841eac174f2Safresh1 842eac174f2Safresh1=back 8439f11ffb7Safresh1 844b8851fccSafresh1=head1 LICENSE 845b8851fccSafresh1 846b8851fccSafresh1This program is free software; you may redistribute it and/or modify it under 847b8851fccSafresh1the same terms as Perl itself. 848b8851fccSafresh1 849b8851fccSafresh1=head1 SEE ALSO 850b8851fccSafresh1 8519f11ffb7Safresh1L<bignum> and L<bigrat>. 852b8851fccSafresh1 8539f11ffb7Safresh1L<Math::BigInt>, L<Math::BigFloat>, L<Math::BigRat> and L<Math::Big> as well as 8549f11ffb7Safresh1L<Math::BigInt::FastCalc>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>. 855b8851fccSafresh1 856b8851fccSafresh1=head1 AUTHORS 857b8851fccSafresh1 8589f11ffb7Safresh1=over 4 8599f11ffb7Safresh1 8609f11ffb7Safresh1=item * 8619f11ffb7Safresh1 862b8851fccSafresh1(C) by Tels L<http://bloodgate.com/> in early 2002 - 2007. 863b8851fccSafresh1 8649f11ffb7Safresh1=item * 8659f11ffb7Safresh1 866eac174f2Safresh1Maintained by Peter John Acklam E<lt>pjacklam@gmail.comE<gt>, 2014-. 8679f11ffb7Safresh1 8689f11ffb7Safresh1=back 8699f11ffb7Safresh1 870b8851fccSafresh1=cut 871