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