Math::BigInt - Arbitrary size integer/float math package
| Use Case | Command | Description |
|---|---|---|
| Create big integer | $x = Math::BigInt->new('12345678901234567890') | Arbitrary precision integer from string |
| Force bigints on constants | use Math::BigInt ':constant'; | All numeric literals become Math::BigInt objects |
| Arithmetic (overloaded) | $x + $y; $x * $y; $x ** $y; | Standard operators work with big integers |
| Modular exponentiation | $num->bmodpow($exp, $mod) | Fast exponentiation modulo $mod |
| Factorial | $x->bfac() | Compute factorial of $x |
| Greatest common divisor | $x->bgcd($y) | GCD of two big integers |
| Bitwise operations | $x->band($y); $x->bior($y); $x->bxor($y) | AND, OR, XOR for bigints |
| Random access digit | $x->digit($n) | Get the nth digit from the right |
| Convert to string | $x->bstr() | Decimal string, possibly zero padded |
use Math::BigInt;
# or make it faster with huge numbers: install (optional)
# Math::BigInt::GMP and always use (it falls back to
# pure Perl if the GMP library is not installed):
# (See also the MATH LIBRARY section!)
# to warn if Math::BigInt::GMP cannot be found, use
use Math::BigInt lib => 'GMP';
# to suppress the warning if Math::BigInt::GMP cannot be found, use
# use Math::BigInt try => 'GMP';
# to die if Math::BigInt::GMP cannot be found, use
# use Math::BigInt only => 'GMP';
my $str = '1234567890';
my @values = (64, 74, 18);
my $n = 1; my $sign = '-';
… (full synopsis continues below with method calls – preserved exactly)
# Configuration methods (may be used as class methods and instance methods)
Math::BigInt->accuracy(); # get class accuracy
Math::BigInt->accuracy($n); # set class accuracy
Math::BigInt->precision(); # get class precision
Math::BigInt->precision($n); # set class precision
Math::BigInt->round_mode(); # get class rounding mode
Math::BigInt->round_mode($m); # set global round mode, must be one of
# 'even', 'odd', '+inf', '-inf', 'zero',
# 'trunc', or 'common'
Math::BigInt->config(); # return hash with configuration
# Constructor methods (when the class methods below are used as instance
# methods, the value is assigned the invocand)
$x = Math::BigInt->new($str); # defaults to 0
$x = Math::BigInt->new('0x123'); # from hexadecimal
$x = Math::BigInt->new('0b101'); # from binary
$x = Math::BigInt->from_hex('cafe'); # from hexadecimal
$x = Math::BigInt->from_oct('377'); # from octal
$x = Math::BigInt->from_bin('1101'); # from binary
$x = Math::BigInt->from_base('why', 36); # from any base
$x = Math::BigInt->from_base_num([1, 0], 2); # from any base
$x = Math::BigInt->bzero(); # create a +0
$x = Math::BigInt->bone(); # create a +1
$x = Math::BigInt->bone('-'); # create a -1
$x = Math::BigInt->binf(); # create a +inf
$x = Math::BigInt->binf('-'); # create a -inf
$x = Math::BigInt->bnan(); # create a Not-A-Number
$x = Math::BigInt->bpi(); # returns pi
$y = $x->copy(); # make a copy (unlike $y = $x)
$y = $x->as_int(); # return as a Math::BigInt
… (all remaining synopsis code remains in
blocks exactly as provided – see full perldoc)
📝 DESCRIPTION
Math::BigInt provides support for arbitrary precision integers. Overloading is also provided for Perl operators.
🔢 Input
Input values to these routines may be any scalar number or string that looks like a number and represents an integer. … (full details preserved)
Input string Resulting value
123 123
1.23e2 123
12300e-2 123
67_538_754 67538754
-4_5_6.7_8_9e+0_1_0 -4567890000000
0x13a 314
0x13ap0 314
0x1.3ap+8 314
0x0.00013ap+24 314
0x13a000p-12 314
0o472 314
0o1.164p+8 314
0o0.0001164p+20 314
0o1164000p-10 314
0472 472 Note!
01.164p+8 314
00.0001164p+20 314
01164000p-10 314
0b100111010 314
0b1.0011101p+8 314
0b0.00010011101p+12 314
0b100111010000p-3 314
… (Output, Methods, etc. all kept with emoji-enhanced headings)
⚙️ METHODS
🔧 Configuration methods
- Each method accepts three additional parameters $A, $P, $R (accuracy, precision, round_mode).
🎯 accuracy()
Math::BigInt->accuracy(5); # set class accuracy
$x->accuracy(5); # set instance accuracy
$A = Math::BigInt->accuracy(); # get class accuracy
$A = $x->accuracy(); # get instance accuracy
📏 precision()
Math::BigInt->precision(-2); # set class precision
$x->precision(-2); # set instance precision
$P = Math::BigInt->precision(); # get class precision
$P = $x->precision(); # get instance precision
… (all methods listed similarly with appropriate emojis, code blocks preserved exactly)
🏆 EXAMPLES
use Math::BigInt;
sub bigint { Math::BigInt->new(shift); }
$x = Math::BigInt->bstr("1234") # string "1234"
$x = "$x"; # same as bstr()
$x = Math::BigInt->bneg("1234"); # Math::BigInt "-1234"
$x = Math::BigInt->babs("-12345"); # Math::BigInt "12345"
$x = Math::BigInt->bnorm("-0.00"); # Math::BigInt "0"
$x = bigint(1) + bigint(2); # Math::BigInt "3"
$x = bigint(1) + "2"; # ditto ("2" becomes a Math::BigInt)
$x = bigint(1); # Math::BigInt "1"
$x = $x + 5 / 2; # Math::BigInt "3"
$x = $x ** 3; # Math::BigInt "27"
$x *= 2; # Math::BigInt "54"
$x = Math::BigInt->new(0); # Math::BigInt "0"
$x--; # Math::BigInt "-1"
$x = Math::BigInt->badd(4,5) # Math::BigInt "9"
print $x->bsstr(); # 9e+0
🔣 NUMERIC LITERALS
After use Math::BigInt ':constant' all numeric literals in the given scope are converted to Math::BigInt objects.
… (full text preserved, code examples in
)
⚡ PERFORMANCE
Using the form $x += $y; etc over $x = $x + $y is faster, since a copy of $x must be made in the second case. …
🧬 SUBCLASSING
… (preserved)
📈 UPGRADING
… (preserved)
📦 EXPORTS
Math::BigInt exports nothing by default, but can export the following methods: bgcd, blcm.
⚠️ CAVEATS
- Comparing numbers as strings: …
- int(): …
- Modifying and =: …
- Overloading -$x: …
- Mixing different object types: …
🐛 BUGS
Please report bugs to bug-math-bigint at rt.cpan.org.
🛟 SUPPORT
- GitHub: https://github.com/pjacklam/p5-Math-BigInt
- RT: https://rt.cpan.org/…
- MetaCPAN: https://metacpan.org/…
📜 LICENSE
This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
🔍 SEE ALSO
Math::BigFloat, Math::BigRat, Math::BigInt::FastCalc, Math::BigInt::GMP, Math::BigInt::Pari.
✍️ AUTHORS
- Mark Biggar, overloaded interface by Ilya Zakharevich, 1996-2001.
- Completely rewritten by Tels, 2001-2008.
- Florian Ragwitz, 2010.
- Peter John Acklam, 2011-.
Generated by phpman v4.9.29 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-21 00:46 @2600:1f28:365:80b0:d8a5:c3c6:bf94:28c0
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format