Math::BigInt::Lib - virtual parent class for Math::BigInt libraries |
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 intended to be used directly, but rather as a parent class for backend libraries used by Math::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 was rewritten to use a plug-in library for core math routines. Any module which conforms 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 short version, like 'Pari'.
A library only needs to deal with unsigned big integers. Testing of input
parameter validity is done by the caller, so there is no need to worry about
underflow (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 libraries don'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 for comparison routines.
api_version()
This method is no longer used. Methods that are not implemented by a subclass will 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().
_new(STR)
^(0|[1-9]\d*)$
.
_zero()
_one()
_two()
_ten()
_from_bin(STR)
^0[bB](0|1[01]*)$
.
_from_oct(STR)
^0[1-7]*$
.
_from_hex(STR)
^0x(0|[1-9a-fA-F][\da-fA-F]*)$
.
_from_bytes(STR)
If BASE is less than or equal to 62, and a collation sequence is not specified, a default collation sequence consisting of the 62 characters 0..9, A..Z, and a..z is used. If the default collation sequence is used, and the BASE is less than or equal to 36, the letter case in STR is ignored.
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 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, "012") $x = $class -> _from_base("3322", 4, "0123") $x = $class -> _from_base("2000", 5, "01234") $x = $class -> _from_base("caaa", 5, "abcde")
flag
is false or omitted,
OBJ1 might be modified. If flag
is true, OBJ2 might be modified.
_dec(OBJ)
_inc(OBJ)
_sqrt(OBJ)
_fac(OBJ)
_dfac(OBJ)
(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.
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.
_fib(OBJ)
_fib(0)
returns 0, _fib(1)
returns 1, _fib(2)
returns 1, _fib(3)
returns 2 etc. In list context, returns
the Fibonacci numbers from F(0)
to F(n): 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
_lucas(OBJ)
_lucas(0)
returns 2, _lucas(1)
returns 1, _lucas(2)
returns 3, etc. In list context, returns the Lucas numbers
from L(0)
to L(n): 2, 1, 3, 4, 7, 11, 18, 29,47, 76, ...
_is_zero(OBJ)
_is_one(OBJ)
_is_two(OBJ)
_is_ten(OBJ)
_is_even(OBJ)
_is_odd(OBJ)
_str(OBJ)
^(0|[1-9]\d*)$
.
_to_bin(OBJ)
_to_oct(OBJ)
_to_hex(OBJ)
_to_bytes(OBJ)
$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.
_as_bin(OBJ)
_to_bin()
but with a '0b' prefix.
_as_oct(OBJ)
_to_oct()
but with a '0' prefix.
_as_hex(OBJ)
_to_hex()
but with a '0x' prefix.
_as_bytes(OBJ)
_to_bytes()
.
_num(OBJ)
_copy(OBJ)
_len(OBJ)
_zeros(OBJ)
CLASS->_digit($obj, 0) # returns 3 CLASS->_digit($obj, 1) # returns 2 CLASS->_digit($obj, 2) # returns 1 CLASS->_digit($obj, -1) # returns 1
_check(OBJ)
_set(OBJ)
The following methods are required for an API version of 2 or greater.
_1ex(N)
_alen(OBJ)
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.
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.
You can find documentation for this module with the perldoc command.
perldoc Math::BigInt::Calc
You can also look for information at:
bignum at lists.scsys.co.uk
This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
Peter John Acklam, <pjacklam@online.no>
Code and documentation based on the Math::BigInt::Calc module by Tels <nospam-abuse@bloodgate.com>
the Math::BigInt manpage, the Math::BigInt::Calc manpage, the Math::BigInt::GMP manpage, the Math::BigInt::FastCalc manpage and the Math::BigInt::Pari manpage.
Math::BigInt::Lib - virtual parent class for Math::BigInt libraries |