MAN page from OpenSuSE perl-Math-BigInt-1.999813-lp152.3.2.noarch.rpm


Section: User Contributed Perl Documentation (3)
Updated: 2018-04-21


Math::BigInt::Lib - virtual parent class for Math::BigInt libraries 


    # 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 { ... }    ...    # In your main program.    use Math::BigInt lib => 'MyBackend';


This module provides support for big integer calculations. It is not intendedto be used directly, but rather as a parent class for backend libraries used byMath::BigInt, Math::BigFloat, Math::BigRat, and related modules.

Other backend libraries include Math::BigInt::Calc, Math::BigInt::FastCalc,Math::BigInt::GMP, and Math::BigInt::Pari.

In order to allow for multiple big integer libraries, Math::BigInt wasrewritten to use a plug-in library for core math routines. Any module whichconforms to the API can be used by Math::BigInt by using this in your program:

        use Math::BigInt lib => 'libname';

'libname' is either the long name, like 'Math::BigInt::Pari', or only the shortversion, like 'Pari'. 

General Notes

A library only needs to deal with unsigned big integers. Testing of inputparameter validity is done by the caller, so there is no need to worry aboutunderflow (e.g., in "_sub()" and "_dec()") or about division by zero (e.g.,in "_div()" and "_mod()")) or similar cases.

Some libraries use methods that don't modify their argument, and some librariesdon't even use objects, but rather unblessed references. Because of this,liberary methods are always called as class methods, not 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

    $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 forcomparison routines.

API version

Return API version as a Perl scalar, 1 for Math::BigInt v1.70, 2 forMath::BigInt v1.83.

This method is no longer used. Methods that are not implemented by a subclasswill be inherited from this class.


The following methods are mandatory: _new(), _str(), _add(), and _sub().However, computations will be very slow without _mul() and _div().

Convert a string representing an unsigned decimal number to an objectrepresenting the same number. The input is normalized, i.e., it matches"^(0|[1-9]\d*)$".
Return an object representing the number zero.
Return an object representing the number one.
Return an object representing the number two.
Return an object representing the number ten.
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]*)$".
Return an object given a string representing an octal number. The input has a'0' prefix and matches the regular expression "^0[1-7]*$".
Return an object given a string representing a hexadecimal number. The inputhas a '0x' prefix and matches the regular expression"^0x(0|[1-9a-fA-F][\da-fA-F]*)$".
Returns an object given a byte string representing the number. The byte stringis in big endian byte order, so the two-byte input string ``\x01\x00'' shouldgive an output value representing the number 256.

Mathematical functions

CLASS->_add(OBJ1, OBJ2)
Returns the result of adding OBJ2 to OBJ1.
CLASS->_mul(OBJ1, OBJ2)
Returns the result of multiplying OBJ2 and OBJ1.
CLASS->_div(OBJ1, OBJ2)
In scalar context, returns the quotient after dividing OBJ1 by OBJ2 andtruncating the result to an integer. In list context, return the quotient andthe remainder.
CLASS->_sub(OBJ1, OBJ2)
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.
Returns the result after decrementing OBJ by one.
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.
Returns the square root of OBJ, truncated to an integer.
CLASS->_root(OBJ, N)
Returns the Nth root of OBJ, truncated to an integer.
Returns the factorial of OBJ, i.e., the product of all positive integers up toand including OBJ.
Returns the double factorial of OBJ. If OBJ is an even integer, returns theproduct 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.
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 inversedoes not exist, both arguments are undefined. Otherwise, the arguments are anumber (object) and its sign (``+'' or ``-'').

The output value, with its sign, must either be a positive value in the range1,2,...,OBJ2-1 or the same value subtracted OBJ2. For instance, if the inputarguments are objects representing the numbers 7 and 5, the method must eitherreturn 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.

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 isequivalent 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.

CLASS->_lsft(OBJ, N, B)
Returns the result after shifting OBJ N digits to the left in base B. This isequivalent 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 methodhas two output arguments, the OBJECT and a STATUS. The STATUS is 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.
CLASS->_gcd(OBJ1, OBJ2)
Returns the greatest common divisor of OBJ1 and OBJ2.
CLASS->_lcm(OBJ1, OBJ2)
Return the least common multiple of OBJ1 and OBJ2.
In scalar context, returns the nth Fibonacci number: _fib(0) returns 0, _fib(1)returns 1, _fib(2) returns 1, _fib(3) returns 2 etc. In list context, returnsthe Fibonacci numbers from F(0) to F(n): 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
In scalar context, returns the nth Lucas number: _lucas(0) returns 2, _lucas(1)returns 1, _lucas(2) returns 3, etc. In list context, returns the Lucas numbersfrom L(0) to L(n): 2, 1, 3, 4, 7, 11, 18, 29,47, 76, ...

Bitwise operators

CLASS->_and(OBJ1, OBJ2)
Returns bitwise and.
CLASS->_or(OBJ1, OBJ2)
Return bitwise or.
CLASS->_xor(OBJ1, OBJ2)
Return bitwise exclusive or.

Boolean operators

Returns a true value if OBJ is zero, and false value otherwise.
Returns a true value if OBJ is one, and false value otherwise.
Returns a true value if OBJ is two, and false value otherwise.
Returns a true value if OBJ is ten, and false value otherwise.
Return a true value if OBJ is an even integer, and a false value otherwise.
Return a true value if OBJ is an even integer, and a false value otherwise.
CLASS->_acmp(OBJ1, OBJ2)
Compare OBJ1 and OBJ2 and return -1, 0, or 1, if OBJ1 is numerically less than,equal to, or larger than OBJ2, respectively.

String conversion

Returns a string representing OBJ in decimal notation. The returned stringshould have no leading zeros, i.e., it should match "^(0|[1-9]\d*)$".
Returns the binary string representation of OBJ.
Returns the octal string representation of the number.
Returns the hexadecimal string representation of the number.
Returns a byte string representation of OBJ. The byte string is in big endianbyte order, so if OBJ represents the number 256, the output should be thetwo-byte string ``\x01\x00''.
Like "_to_bin()" but with a '0b' prefix.
Like "_to_oct()" but with a '0' prefix.
Like "_to_hex()" but with a '0x' prefix.
This is an alias to "_to_bytes()".

Numeric conversion

Returns a Perl scalar number representing the number OBJ as close aspossible. Since Perl scalars have limited precision, the returned value mightnot be exactly the same as OBJ.


Returns a true copy OBJ.
Returns the number of the decimal digits in OBJ. The output is a Perl scalar.
Returns the number of trailing decimal zeros. The output is a Perl scalar. Thenumber zero has no trailing decimal zeros.
CLASS->_digit(OBJ, N)
Returns the Nth digit in OBJ as a Perl scalar. N is a Perl scalar, where zerorefers to the rightmost (least significant) digit, and negative values countfrom the left (most significant digit). If $obj represents 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
Returns true if the object is invalid and false otherwise. Preferably, the truevalue is a string describing the problem with the object. This is a checkroutine to test the internal state of the object for corruption.

API version 2

The following methods are required for an API version of 2 or greater.


Return an object representing the number 10**N where N >= 0 is a Perlscalar.

Mathematical functions

CLASS->_nok(OBJ1, OBJ2)
Return the binomial coefficient OBJ1 over OBJ1.


Return the approximate number of decimal digits of the object. The output is aPerl scalar.

API optional methods

The following methods are optional, and can be defined if the underlying libhas a fast way to do them. If undefined, Math::BigInt will use pure Perl (henceslow) fallback routines to emulate these:

Signed bitwise operators.

CLASS->_signed_or(OBJ1, OBJ2, SIGN1, SIGN2)
Return the signed bitwise or.
CLASS->_signed_and(OBJ1, OBJ2, SIGN1, SIGN2)
Return the signed bitwise and.
CLASS->_signed_xor(OBJ1, OBJ2, SIGN1, SIGN2)
Return the signed bitwise exclusive or.


If you want to port your own favourite C library for big numbers to theMath::BigInt interface, you can take any of the already existing modules as arough guideline. You should really wrap up the latest Math::BigInt andMath::BigFloat testsuites with your module, and replace in them any of thefollowing:

        use Math::BigInt;

by this:

        use Math::BigInt lib => 'yourlib';

This way you ensure that your library really works 100% within Math::BigInt. 


Please report any bugs or feature requests to"bug-math-bigint at", or through the web interface at<>(requires login).We will be notified, and then you'll automatically be notified of progress onyour bug as I make changes. 


You can find documentation for this module with the perldoc command.

    perldoc Math::BigInt::Calc

You can also look for information at:

RT: CPAN's request tracker


AnnoCPAN: Annotated CPAN documentation


CPAN Ratings


Search CPAN


CPAN Testers Matrix


The Bignum mailing list
Post to mailing list

"bignum at"

View mailing list






This program is free software; you may redistribute it and/or modify it underthe same terms as Perl itself. 


Peter John Acklam, <>

Code and documentation based on the Math::BigInt::Calc module by Tels<> 


Math::BigInt, Math::BigInt::Calc, Math::BigInt::GMP,Math::BigInt::FastCalc and Math::BigInt::Pari.



General Notes
API version 2
API optional methods

This document was created byman2html,using the manual pages.