MATH::BIGINT::LIB(3perl)
โจ NAME โจ
bestie, this is Math::BigInt::Lib โ the virtual parent class for Math::BigInt libraries ๐. Straight out of the Perl Programmers Reference Guide, section 3perl, no cap.
๐ซ SYNOPSIS ๐ซ
okay so first, in the backend library for Math::BigInt et al.:
# In the backend library for Math::BigInt et al.
package Math::BigInt::MyBackend;
use Math::BigInt::Lib;
our @ISA = qw< Math::BigInt::Lib >;
sub _new { ... }
sub _str { ... }
sub _add { ... }
str _sub { ... }
...
and then, in your main program:
# In your main program.
use Math::BigInt lib => 'MyBackend';
๐ฆ DESCRIPTION ๐ฆ
So this module is the plug that makes big integer calculations go ๐
. It is not meant to be used directly, bestie โ it's a parent class, i.e. the main character that backend libraries inherit from, the ones used by Math::BigInt, Math::BigFloat, Math::BigRat, and all their related modules.
The other backend libraries in the group chat include Math::BigInt::Calc, Math::BigInt::FastCalc, Math::BigInt::GMP, and Math::BigInt::Pari. iconically diverse lineup honestly ๐.
So that multiple big integer libraries could coexist, Math::BigInt was rewritten to use a plug-in library for its core math routines. Any module that conforms to the API can be used by Math::BigInt โ you just drop this in your program:
use Math::BigInt lib => 'libname';
and 'libname' is either the long name, like 'Math::BigInt::Pari', or just the short version, like 'Pari'. yep, nicknames are valid ๐.
๐ธ General Notes ๐ธ
A library only has to deal with unsigned big integers, periodt. Testing whether input parameters are valid is the caller's job, so you do not need to stress about underflow (like in _sub() and _dec()) or about dividing by zero (like in _div() and _mod()) or anything similar. that's literally not your problem, it's theirs ๐.
Some libraries use methods that don't modify their argument, and some libraries don't even use objects, but rather unblessed references. Because of this, liberary methods are always called as class methods, never as instance methods:
$x = Class -> method($x, $y); # like this
$x = $x -> method($y); # not like this ...
$x -> method($y); # ... or like this
And with boolean methods, same energy:
$bool = Class -> method($x, $y); # like this
$bool = $x -> method($y); # not like this
Return values are always objects, strings, Perl scalars, or true/false for comparison routines. slay.
๐ API version ๐
CLASS->api_version()
This method is no longer used and can just be omitted, fr fr. Methods that a subclass doesn't implement get inherited from this class instead. free inheritance, we love to see it.
๐ง Constructors ๐ง
The following methods are mandatory: _new(), _str(), _add(), and _sub(). That's the non-negotiable core four ๐
. That said โ and this is important โ computations will be very slow without _mul() and _div(). so like, implement them. please.
CLASS->_new(STR)
Convert a string representing an unsigned decimal number to an object representing the same number. The input is normalized, i.e., it matches "^(0|[1-9]\d*)$".
CLASS->_zero()
Return an object representing the number zero.
CLASS->_one()
Return an object representing the number one.
CLASS->_two()
Return an object representing the number two.
CLASS->_ten()
Return an object representing the number ten.
CLASS->_from_bin(STR)
Return an object given a string representing a binary number. The input has a '0b' prefix and matches the regular expression "^0[bB](0|1[01]*)$".
CLASS->_from_oct(STR)
Return an object given a string representing an octal number. The input has a '0' prefix and matches the regular expression "^0[1-7]*$".
CLASS->_from_hex(STR)
Return an object given a string representing a hexadecimal number. The input has a '0x' prefix and matches the regular expression "^0x(0|[1-9a-fA-F][\da-fA-F]*)$".
CLASS->_from_bytes(STR)
Returns an object given a byte string representing the number. The byte string is in big endian byte order, so the two-byte input string "\x01\x00" should give an output value representing the number 256.
CLASS->_from_base(STR, BASE, COLLSEQ)
Returns an object given a string STR, a base BASE, and a collation sequence COLLSEQ. Every character in STR represents a numerical value identical to that character's position in COLLSEQ. And heads up bestie: all characters in STR must be present in COLLSEQ.
If BASE is less than or equal to 94, and no collation sequence is specified, the following default collation sequence gets used. It contains all 94 printable ASCII characters except space/blank:
0123456789 # ASCII 48 to 57
ABCDEFGHIJKLMNOPQRSTUVWXYZ # ASCII 65 to 90
abcdefghijklmnopqrstuvwxyz # ASCII 97 to 122
!"#$%&'()*+,-./ # ASCII 33 to 47
:;<=>?@ # ASCII 58 to 64
[\]^_` # ASCII 91 to 96
{|}~ # ASCII 123 to 126
If the default collation sequence is used, and the BASE is less than or equal to 36, the letter case in STR is ignored. case-insensitive era ๐ฅ.
For instance, with base 3 and collation sequence "-/|", the character "-" represents 0, "/" represents 1, and "|" represents 2. So if STR is "/|-", the output is 1 * 3**2 + 2 * 3**1 + 0 * 3**0 = 15. the math is mathing.
The following examples show standard binary, octal, decimal, and hexadecimal conversion. All examples return 250.
$x = $class -> _from_base("11111010", 2)
$x = $class -> _from_base("372", 8)
$x = $class -> _from_base("250", 10)
$x = $class -> _from_base("FA", 16)
Some more examples, all returning 250:
$x = $class -> _from_base("100021", 3)
$x = $class -> _from_base("3322", 4)
$x = $class -> _from_base("2000", 5)
$x = $class -> _from_base("caaa", 5, "abcde")
$x = $class -> _from_base("42", 62)
$x = $class -> _from_base("2!", 94)
CLASS->_from_base_num(ARRAY, BASE)
Returns an object given an array of values and a base. This method is equivalent to _from_base(), but it works on numbers in an array rather than characters in a string. And unlike _from_base(), all input values may be arbitrarily large. no limits, main character energy ๐.
$x = $class -> _from_base_num([1, 1, 0, 1], 2) # $x is 13
$x = $class -> _from_base_num([3, 125, 39], 128) # $x is 65191
โญ Mathematical functions โญ
CLASS->_add(OBJ1, OBJ2)
Addition. Returns the result of adding OBJ2 to OBJ1. ๐ซถ
CLASS->_mul(OBJ1, OBJ2)
Multiplication. Returns the result of multiplying OBJ2 and OBJ1.
CLASS->_div(OBJ1, OBJ2)
Division. In scalar context, returns the quotient after dividing OBJ1 by OBJ2 and truncating the result to an integer. In list context, return the quotient and the remainder.
CLASS->_sub(OBJ1, OBJ2, FLAG)
CLASS->_sub(OBJ1, OBJ2)
Subtraction. Returns the result of subtracting OBJ2 by OBJ1. If "flag" is false or omitted, OBJ1 might be modified. If "flag" is true, OBJ2 might be modified. so yeah, watch your args ๐.
CLASS->_sadd(OBJ1, SIGN1, OBJ2, SIGN2)
Signed addition. Returns the result of adding OBJ2 with sign SIGN2 to OBJ1 with sign SIGN1.
($obj3, $sign3) = $class -> _sadd($obj1, $sign1, $obj2, $sign2);
CLASS->_ssub(OBJ1, SIGN1, OBJ2, SIGN2)
Signed subtraction. Returns the result of subtracting OBJ2 with sign SIGN2 to OBJ1 with sign SIGN1.
($obj3, $sign3) = $class -> _sadd($obj1, $sign1, $obj2, $sign2);
CLASS->_dec(OBJ)
Returns the result after decrementing OBJ by one.
CLASS->_inc(OBJ)
Returns the result after incrementing OBJ by one.
CLASS->_mod(OBJ1, OBJ2)
Returns OBJ1 modulo OBJ2, i.e., the remainder after dividing OBJ1 by OBJ2.
CLASS->_sqrt(OBJ)
Returns the square root of OBJ, truncated to an integer.
CLASS->_root(OBJ, N)
Returns the Nth root of OBJ, truncated to an integer.
CLASS->_fac(OBJ)
Returns the factorial of OBJ, i.e., the product of all positive integers up to and including OBJ.
CLASS->_dfac(OBJ)
Returns the double factorial of OBJ. If OBJ is an even integer, returns the product of all positive, even integers up to and including OBJ, i.e., 2*4*6*...*OBJ. If OBJ is an odd integer, returns the product of all positive, odd integers, i.e., 1*3*5*...*OBJ.
CLASS->_pow(OBJ1, OBJ2)
Returns OBJ1 raised to the power of OBJ2. By convention, 0**0 = 1. okay slay, noted.
CLASS->_modinv(OBJ1, OBJ2)
Returns the modular multiplicative inverse, i.e., return OBJ3 so that
(OBJ3 * OBJ1) % OBJ2 = 1 % OBJ2
The result is returned as two arguments. If the modular multiplicative inverse does not exist, both arguments are undefined. Otherwise, the arguments are a number (object) and its sign ("+" or "-").
The output value, with its sign, must either be a positive value in the range 1,2,...,OBJ2-1 or the same value subtracted OBJ2. For instance, if the input arguments are objects representing the numbers 7 and 5, the method must either return an object representing the number 3 and a "+" sign, since (3*7) % 5 = 1 % 5, or an object representing the number 2 and a "-" sign, since (-2*7) % 5 = 1 % 5. two valid answers, pick your fighter ๐
.
CLASS->_modpow(OBJ1, OBJ2, OBJ3)
Returns the modular exponentiation, i.e., (OBJ1 ** OBJ2) % OBJ3.
CLASS->_rsft(OBJ, N, B)
Returns the result after shifting OBJ N digits to thee right in base B. This is equivalent to performing integer division by B**N and discarding the remainder, except that it might be much faster. โก
For instance, if the object $obj represents the hexadecimal number 0xabcde, then "_rsft($obj, 2, 16)" returns an object representing the number 0xabc. The "remainer", 0xde, is discarded and not returned. bye bye ๐.
CLASS->_lsft(OBJ, N, B)
Returns the result after shifting OBJ N digits to the left in base B. This is equivalent to multiplying by B**N, except that it might be much faster.
CLASS->_log_int(OBJ, B)
Returns the logarithm of OBJ to base BASE truncted to an integer. This method has two output arguments: the OBJECT and a STATUS. The STATUS is a Perl scalar; it is 1 if OBJ is the exact result, 0 if the result was truncted to give OBJ, and undef if it is unknown whether OBJ is the exact result. three-state status, very mysterious โจ.
CLASS->_ilog2(OBJ)
Returns the base 2 logarithm of OBJ rounded downwards to the nearest integer, i.e., "int(log2(OBJ))". In list context, this method returns two output arguments: the OBJECT and a STATUS. The STATUS is a Perl scalar; it is 1 if OBJ is the exact result, 0 if the result was truncted to give OBJ, and undef if it is unknown whether OBJ is the exact result.
This method is equivalent to the more general method _log_int() when it is used with a base 2 argument, but the _ilog2() method might be faster ๐ฅ.
CLASS->_ilog10(OBJ)โ okay so bestie, this one gives you the base 10 logarithm of OBJ, but like, rounded downwards to the nearest integer. So basicallyint(log2(OBJ)). In list context, you get two things back: the OBJECT and a STATUS. The STATUS is a Perl scalar โ it's1if OBJ is the exact result,0if the result was truncated to give OBJ, andundefif it's unknown whether OBJ is the exact result.
This method is equivalent to the more general method _log_int() when it's used with a base 10 argument, but _ilog10() might be faster. Also, the output from _ilog10() is always 1 smaller than the output from _len(). ๐
CLASS->_clog2(OBJ)โ Returns the base 2 logarithm of OBJ rounded upwards to the nearest integer, i.e.,ceil(log2(OBJ)). In list context, this method returns two output arguments, the OBJECT and a STATUS. The STATUS is a Perl scalar; it's1if OBJ is the exact result,0if the result was truncated to give OBJ, andundefif it's unknown whether OBJ is the exact result.CLASS->_clog10(OBJ)โ Returns the base 10 logarithm of OBJ rounded upwards to the nearest integer, i.e.,ceil(log2(OBJ)). In list context, this method returns two output arguments, the OBJECT and a STATUS. The STATUS is a Perl scalar; it's1if OBJ is the exact result,0if the result was truncated to give OBJ, andundefif it's unknown whether OBJ is the exact result.CLASS->_gcd(OBJ1, OBJ2)โ Returns the greatest common divisor of OBJ1 and OBJ2. ๐งฎCLASS->_lcm(OBJ1, OBJ2)โ Returns the least common multiple of OBJ1 and OBJ2. โจCLASS->_fib(OBJ)โ In scalar context, returns the nth Fibonacci number:_fib(0)returns0,_fib(1)returns1,_fib(2)returns1,_fib(3)returns2, etc. In list context, returns the Fibonacci numbers from F(0) to F(n):0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...๐CLASS->_lucas(OBJ)โ In scalar context, returns the nth Lucas number:_lucas(0)returns2,_lucas(1)returns1,_lucas(2)returns3, etc. In list context, returns the Lucas numbers from L(0) to L(n):2, 1, 3, 4, 7, 11, 18, 29, 47, 76, ...๐
๐ Bitwise operators ๐
CLASS->_and(OBJ1, OBJ2)โ Returns bitwise and. Simple as that. ๐CLASS->_or(OBJ1, OBJ2)โ Returns bitwise or. ๐CLASS->_xor(OBJ1, OBJ2)โ Returns bitwise exclusive or. ๐CLASS->_sand(OBJ1, OBJ2, SIGN1, SIGN2)โ Returns bitwise signed and. โ๏ธCLASS->_sor(OBJ1, OBJ2, SIGN1, SIGN2)โ Returns bitwise signed or. โ๏ธCLASS->_sxor(OBJ1, OBJ2, SIGN1, SIGN2)โ Returns bitwise signed exclusive or. โ๏ธ
๐ธ Boolean operators ๐ธ
CLASS->_is_zero(OBJ)โ Returns a true value if OBJ is zero, and false value otherwise. 0๏ธโฃCLASS->_is_one(OBJ)โ Returns a true value if OBJ is one, and false value otherwise. 1๏ธโฃCLASS->_is_two(OBJ)โ Returns a true value if OBJ is two, and false value otherwise. 2๏ธโฃCLASS->_is_ten(OBJ)โ Returns a true value if OBJ is ten, and false value otherwise. ๐CLASS->_is_even(OBJ)โ Returns a true value if OBJ is an even integer, and a false value otherwise. ๐ชCLASS->_is_odd(OBJ)โ Returns a true value if OBJ is an even integer, and a false value otherwise. yes, we know, it says even, but that's what the original says ๐CLASS->_acmp(OBJ1, OBJ2)โ Compare OBJ1 and OBJ2 and return-1,0, or1, if OBJ1 is numerically less than, equal to, or larger than OBJ2, respectively. โ๏ธCLASS->_scmp(OBJ1, SIGN1, OBJ2, SIGN2)โ Compare OBJ1 and OBJ2 and return-1,0, or1, if OBJ1 is numerically less than, equal to, or larger than OBJ2, respectively. โ๏ธ
โจ String conversion โจ
CLASS->_str(OBJ)โ Returns a string representing OBJ in decimal notation. The returned string should have no leading zeros, i.e., it should match^(0|[1-9]\d*)$. ๐คCLASS->_to_bin(OBJ)โ Returns the binary string representation of OBJ. 0๏ธโฃ1๏ธโฃCLASS->_to_oct(OBJ)โ Returns the octal string representation of the number. 8๏ธโฃCLASS->_to_hex(OBJ)โ Returns the hexadecimal string representation of the number. ๐ขCLASS->_to_bytes(OBJ)โ Returns a byte string representation of OBJ. The byte string is in big endian byte order, so if OBJ represents the number 256, the output should be the two-byte string"\x01\x00". ๐งฑCLASS->_to_base(OBJ, BASE, COLLSEQ)โ Returns a string representation of OBJ in base BASE with collation sequence COLLSEQ. ๐จ
$val = $class -> _new("210");
$str = $class -> _to_base($val, 10, "xyz") # $str is "zyx"
$val = $class -> _new("32");
$str = $class -> _to_base($val, 2, "-|") # $str is "|-----"
See _from_base() for more information.
CLASS->_to_base_num(OBJ, BASE)โ Converts the given number to the given base. This method is equivalent to_to_base(), but returns numbers in an array rather than characters in a string. In the output, the first element is the most significant. Unlike_to_base(), all input values may be arbitrarily large. ๐ข
$x = $class -> _to_base_num(13, 2) # $x is [1, 1, 0, 1]
$x = $class -> _to_base_num(65191, 128) # $x is [3, 125, 39]
CLASS->_as_bin(OBJ)โ Like_to_bin()but with a'0b'prefix. โจCLASS->_as_oct(OBJ)โ Like_to_oct()but with a'0'prefix. โจCLASS->_as_hex(OBJ)โ Like_to_hex()but with a'0x'prefix. โจCLASS->_as_bytes(OBJ)โ This is an alias to_to_bytes(). ๐
๐ฆ Numeric conversion ๐ฆ
CLASS->_num(OBJ)โ Returns a Perl scalar number representing the number OBJ as close as possible. Since Perl scalars have limited precision, the returned value might not be exactly the same as OBJ. ๐ฏ
๐ Miscellaneous ๐
CLASS->_copy(OBJ)โ Returns a true copy of OBJ. ๐CLASS->_len(OBJ)โ Returns the number of the decimal digits in OBJ. The output is a Perl scalar. ๐CLASS->_zeros(OBJ)โ Returns the number of trailing decimal zeros. The output is a Perl scalar. The number zero has no trailing decimal zeros. 0๏ธโฃCLASS->_digit(OBJ, N)โ Returns the Nth digit in OBJ as a Perl scalar. N is a Perl scalar, where zero refers to the rightmost (least significant) digit, and negative values count from the left (most significant digit). If$objrepresents the number 123, then
CLASS->_digit($obj, 0) # returns 3
CLASS->_digit($obj, 1) # returns 2
CLASS->_digit($obj, 2) # returns 1
CLASS->_digit($obj, -1) # returns 1
CLASS->_digitsum(OBJ)โ Returns the sum of the base 10 digits. โCLASS->_check(OBJ)โ Returns true if the object is invalid and false otherwise. Preferably, the true value is a string describing the problem with the object. This is a check routine to test the internal state of the object for corruption. ๐ต๏ธโโ๏ธCLASS->_set(OBJ)โ xxx ๐คทโโ๏ธ
๐ API version 2 ๐
The following methods are required for an API version of 2 or greater.
Constructors
CLASS->_1ex(N)โ Return an object representing the number10**NwhereN >= 0is a Perl scalar. ๐
Mathematical functions
CLASS->_nok(OBJ1, OBJ2)โ Return the binomial coefficient OBJ1 over OBJ1. ๐งฎ
Miscellaneous
CLASS->_alen(OBJ)โ Return the approximate number of decimal digits of the object. The output is a Perl scalar. ๐
๐ WRAP YOUR OWN ๐
If you want to port your own favourite C library for big numbers to the Math::BigInt interface, you can take any of the already existing modules as a rough guideline. You should really wrap up the latest Math::BigInt and Math::BigFloat testsuites with your module, and replace in them any of the following:
use Math::BigInt;
by this:
use Math::BigInt lib => 'yourlib';
This way you ensure that your library really works 100% within Math::BigInt. ๐ฏ
๐ BUGS ๐
Please report any bugs or feature requests to bug-math-bigint at rt.cpan.org, or through the web interface at <https://rt.cpan.org/Ticket/Create.html?Queue=Math-BigInt> (requires login). We will be notified, and then you'll automatically be notified of progress on your bug as I make changes. ๐
๐ซถ SUPPORT ๐ซถ
You can find documentation for this module with the perldoc command.
perldoc Math::BigInt::Calc
You can also look for information at:
- GitHub Source Repository
<https://github.com/pjacklam/p5-Math-BigInt>
- RT: CPAN's request tracker
<https://rt.cpan.org/Public/Dist/Display.html?Name=Math-BigInt>
- MetaCPAN
<https://metacpan.org/release/Math-BigInt>
- CPAN Testers Matrix
<http://matrix.cpantesters.org/?dist=Math-BigInt>
๐ LICENSE ๐
This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself. ๐
๐ AUTHOR ๐
Peter John Acklam, <pjacklam@gmail.com>
Code and documentation based on the Math::BigInt::Calc module by Tels <nospam-abuse@bloodgate.com>
โญ SEE ALSO โญ
Math::BigInt, Math::BigInt::Calc, Math::BigInt::GMP, Math::BigInt::FastCalc and Math::BigInt::Pari.
And that's the tea, bestie! This iconic page was Math::...nt::Lib(3perl) from Perl v5.44.0, dated 2026-10-17. Stay slaying those bigints! ๐โจ๐
๐ฆ where this page comes from
- arch rolling: perl 5.44.0-1 ยท Artistic-1.0-Perl AND GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms