Previous section To contents Next section

14.4 Gmp

Gmp is short for GNU Multi-Precision library. It is a set of routines that can manipulate very large numbers. Although much slower than regular integers they are very useful when you need to handle extremely large numbers. Billions and billions as Mr Attenborough would have said..

The Gmp library can handle large integers, floats and rational numbers, but currently Pike only has support for large integers. The others will be added later or when demand arises. Large integers are implemented as objects cloned from Gmp.Mpz.

CLASS
Gmp.mpz - bignum program

DESCRIPTION
Gmp.mpz is a builtin program written in C. It implements large, very large integers. In fact, the only limitation on these integers is the available memory.

The mpz object implements all the normal integer operations. (except xor) There are also some extra operators:

NOTE
This module is only available if libgmp.a was available and found when Pike was compiled.

METHOD
Gmp.mpz.create - initialize a bignum

SYNTAX
object Mpz();
object Mpz(int|object|float i);
object Mpz(string digits, int base);

DESCRIPTION
When cloning an mpz it is by default initialized to zero. However, you can give a second argument to clone to initialize the new object to that value. The argument can be an int, float another mpz object, or a string containing an ascii number. You can also give the number in the string in another base by specifying the base as a second argument. Valid bases are 2-36 and 256.

SEE ALSO
clone

METHOD
Gmp.mpz.powm - raise and modulo

SYNTAX
object powm(int|string|float|object a,int|string|float|object b);

DESCRIPTION
This function returns ( mpz ** a ) % b. For example, Mpz(2)->powm(10,42); would return 16 since 2 to the power of 10 is 1024 and 1024 modulo 42 is 16.

METHOD
Gmp.mpz.sqrt - square root

SYNTAX
object sqrt();

DESCRIPTION
This function returns the truncated integer part of the square root of the value of mpz.

METHOD
Gmp.mpz.probably_prime_p - is this number a prime?

SYNTAX
int probably_prime_p();

DESCRIPTION
This function returns 1 if mpz is a prime, and 0 most of the time if it is not.

METHOD
Gmp.mpz.gcd - greatest common divisor

SYNTAX
object gcd(object|int|float|string arg)

DESCRIPTION
This function returns the greatest common divisor for arg and mpz.

METHOD
Gmp.mpz.cast - cast to other type

SYNTAX
object cast( "string" | "int" | "float" );
(string) mpz
(int) mpz
(float) mpz

DESCRIPTION
This function converts an mpz to a string, int or float. This is necessary when you want to view, store or use the result of an mpz calculation.

SEE ALSO
cast

METHOD
Gmp.mpz.digits - convert mpz to a string

SYNTAX
string digits();
string digits(int|void base);

DESCRIPTION
This function converts an mpz to a string. If a base is given the number will be represented in that base. Valid bases are 2-36 and 256. The default base is 10.

SEE ALSO
Gmp.mpz->cast

METHOD
Gmp.mpz.size - how long is a number

SYNTAX
string size();
string size(int|void base);

DESCRIPTION
This function returns how long the mpz would be represented in the specified base. The default base is 2.

SEE ALSO
Gmp.mpz->digits


Previous section To contents Next section