Perl 5 version 8.0 documentation

Math::BigInt::Calc

NAME

Math::BigInt::Calc - Pure Perl module to support Math::BigInt

SYNOPSIS

Provides support for big integer calculations. Not intended to be used by other modules (except Math::BigInt::Cached). Other modules which sport the same functions can also be used to support Math::Bigint, like Math::BigInt::Pari.

DESCRIPTION

In order to allow for multiple big integer libraries, Math::BigInt was rewritten to use library modules for core math routines. Any module which follows the same API as this can be used instead by using the following:

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

'libname' is either the long name ('Math::BigInt::Pari'), or only the short version like 'Pari'.

EXPORT

The following functions MUST be defined in order to support the use by Math::BigInt:

  1. _new(string) return ref to new object from ref to decimal string
  2. _zero() return a new object with value 0
  3. _one() return a new object with value 1
  4. _str(obj) return ref to a string representing the object
  5. _num(obj) returns a Perl integer/floating point number
  6. NOTE: because of Perl numeric notation defaults,
  7. the _num'ified obj may lose accuracy due to
  8. machine-dependend floating point size limitations
  9. _add(obj,obj) Simple addition of two objects
  10. _mul(obj,obj) Multiplication of two objects
  11. _div(obj,obj) Division of the 1st object by the 2nd
  12. In list context, returns (result,remainder).
  13. NOTE: this is integer math, so no
  14. fractional part will be returned.
  15. _sub(obj,obj) Simple subtraction of 1 object from another
  16. a third, optional parameter indicates that the params
  17. are swapped. In this case, the first param needs to
  18. be preserved, while you can destroy the second.
  19. sub (x,y,1) => return x - y and keep x intact!
  20. _dec(obj) decrement object by one (input is garant. to be > 0)
  21. _inc(obj) increment object by one
  22. _acmp(obj,obj) <=> operator for objects (return -1, 0 or 1)
  23. _len(obj) returns count of the decimal digits of the object
  24. _digit(obj,n) returns the n'th decimal digit of object
  25. _is_one(obj) return true if argument is +1
  26. _is_zero(obj) return true if argument is 0
  27. _is_even(obj) return true if argument is even (0,2,4,6..)
  28. _is_odd(obj) return true if argument is odd (1,3,5,7..)
  29. _copy return a ref to a true copy of the object
  30. _check(obj) check whether internal representation is still intact
  31. return 0 for ok, otherwise error message as string

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

  1. _from_hex(str) return ref to new object from ref to hexadecimal string
  2. _from_bin(str) return ref to new object from ref to binary string
  3. _as_hex(str) return ref to scalar string containing the value as
  4. unsigned hex string, with the '0x' prepended.
  5. Leading zeros must be stripped.
  6. _as_bin(str) Like as_hex, only as binary string containing only
  7. zeros and ones. Leading zeros must be stripped and a
  8. '0b' must be prepended.
  9. _rsft(obj,N,B) shift object in base B by N 'digits' right
  10. For unsupported bases B, return undef to signal failure
  11. _lsft(obj,N,B) shift object in base B by N 'digits' left
  12. For unsupported bases B, return undef to signal failure
  13. _xor(obj1,obj2) XOR (bit-wise) object 1 with object 2
  14. Note: XOR, AND and OR pad with zeros if size mismatches
  15. _and(obj1,obj2) AND (bit-wise) object 1 with object 2
  16. _or(obj1,obj2) OR (bit-wise) object 1 with object 2
  17. _mod(obj,obj) Return remainder of div of the 1st by the 2nd object
  18. _sqrt(obj) return the square root of object (truncate to int)
  19. _fac(obj) return factorial of object 1 (1*2*3*4..)
  20. _pow(obj,obj) return object 1 to the power of object 2
  21. _gcd(obj,obj) return Greatest Common Divisor of two objects
  22. _zeros(obj) return number of trailing decimal zeros
  23. _modinv return inverse modulus
  24. _modpow return modulus of power ($x ** $y) % $z

Input strings come in as unsigned but with prefix (i.e. as '123', '0xabc' or '0b1101').

Testing of input parameter validity is done by the caller, so you need not worry about underflow (f.i. in _sub() , _dec() ) nor about division by zero or similar cases.

The first parameter can be modified, that includes the possibility that you return a reference to a completely different object instead. Although keeping the reference and just changing it's contents is prefered over creating and returning a different reference.

Return values are always references to objects or strings. Exceptions are _lsft() and _rsft() , which return undef if they can not shift the argument. This is used to delegate shifting of bases different than the one you can support back to Math::BigInt, which will use some generic code to calculate the result.

WRAP YOUR OWN

If you want to port your own favourite c-lib 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 BigInt and BigFloat testsuites with your module, and replace in them any of the following:

  1. use Math::BigInt;

by this:

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

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

LICENSE

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

AUTHORS

Original math code by Mark Biggar, rewritten by Tels http://bloodgate.com/ in late 2000, 2001. Seperated from BigInt and shaped API with the help of John Peacock.

SEE ALSO

Math::BigInt, Math::BigFloat, Math::BigInt::BitVect, Math::BigInt::GMP, Math::BigInt::Cached and Math::BigInt::Pari.