GMP functionsGMP
These functions allow you to work with arbitrary-length integers
using the GNU MP library. In order to have these
functions available, you must compile PHP with
GMP support by using the option.
You can download the GMP library from &url.gmp;. This site also has the
GMP manual available.
You will need GMP version 2 or better to use these functions. Some
functions may require more recent version of the GMP library.
These functions have been added in PHP 4.0.4.
Most GMP functions accept GMP number arguments, defined as
resource below. However, most of these
functions will also accept numeric and string arguments, given
that it is possible to convert the latter to a number. Also,
if there is a faster function that can operate on integer arguments,
it would be used instead of the slower function when the supplied arguments are
integers. This is done transparently, so the bottom line is that
you can use integers in every function that expects GMP
number. See also the gmp_init function.
If you want to explicitely specify a large integer,
specify it as a string. If you don't do that, PHP will
interpret the integer-literal first, possibly resulting
in loss of precision, even before GMP
comes into play.
Factorial function using GMP
]]>
This will calculate factiorial of 1000 (pretty big number)
very fast.
gmp_initCreate GMP numberDescriptionresource gmp_initmixed number
Creates a GMP number from an integer or string. String
representation can be decimal or hexadecimal. In the latter case,
the string should start with 0x.
Creating GMP number
]]>
It is not necessary to call this function if you want to use
integer or string in place of GMP number in GMP functions, like
gmp_add. Function arguments are
automatically converted to GMP numbers, if such conversion is
possible and needed, using the same rules as
gmp_init.
gmp_intvalConvert GMP number to integerDescriptionint gmp_intvalresource gmpnumber
This function allows to convert GMP number to integer.
This function returns a useful result only if the number actually
fits the PHP integer (i.e., signed long type). If you want just
to print the GMP number, use gmp_strval.
gmp_strvalConvert GMP number to stringDescriptionstring gmp_strvalresource gmpnumberint
base
Convert GMP number to string representation in base
base. The default base is 10. Allowed
values for the base are from 2 to 36.
Converting a GMP number to a string
]]>
gmp_addAdd numbersDescriptionresource gmp_addresource aresource b
Add two GMP numbers. The result will be a GMP number representing
the sum of the arguments.
gmp_subSubtract numbersDescriptionresource gmp_subresource aresource b
Subtracts b from a
and returns the result.
gmp_mulMultiply numbersDescriptionresource gmp_mulresource aresource b
Multiplies a by b
and returns the result.
gmp_div_qDivide numbersDescriptionresource gmp_div_qresource aresource bint
round
Divides a by b and
returns the integer result. The result rounding is defined by the
round, which can have the following
values:
GMP_ROUND_ZERO: The result is truncated
towards 0.
GMP_ROUND_PLUSINF: The result is
rounded towards +infinity.
GMP_ROUND_MINUSINF: The result is
rounded towards -infinity.
This function can also be called as gmp_div.
See also gmp_div_r,
gmp_div_qrgmp_div_rRemainder of the division of numbersDescriptionresource gmp_div_rresource nresource dint
round
Calculates remainder of the integer division of
n by d. The
remainder has the sign of the n argument,
if not zero.
See the gmp_div_q function for description
of the round argument.
See also gmp_div_q,
gmp_div_qrgmp_div_qrDivide numbers and get quotient and remainderDescriptionarray gmp_div_qrresource nresource dint
round
The function divides n by
d and returns array, with the first
element being [n/d] (the integer result of the
division) and the second being (n - [n/d] * d)
(the remainder of the division).
See the gmp_div_q function for description
of the round argument.
Division of GMP numbers
]]>
See also gmp_div_q,
gmp_div_r.
gmp_divDivide numbersDescriptionresource gmp_divresource aresource b
This function is an alias to gmp_div_q.
gmp_modModulo operationDescriptionresource gmp_modresource nresource d
Calculates n modulo
d. The result is always non-negative, the
sign of d is ignored.
gmp_divexactExact division of numbersDescriptionresource gmp_divexactresource nresource d
Divides n by d,
using fast "exact division" algorithm. This function produces
correct results only when it is known in advance that
d divides n.
gmp_cmpCompare numbersDescriptionint gmp_cmpresource aresource b
Returns a positive value if a > b, zero if
a = b and negative value if a <
b.
gmp_negNegate numberDescriptionresource gmp_negresource a
Returns -a.
gmp_com
Calculates one's complement of a
Descriptionresource gmp_comresource a
&warn.undocumented.func;
gmp_absAbsolute valueDescriptionresource gmp_absresource a
Returns absolute value of a.
gmp_signSign of numberDescriptionint gmp_signresource a
Return sign of a - 1 if
a is positive and -1 if it's negative.
gmp_factFactorialDescriptionresource gmp_factint a
Calculates factorial (a!) of a.
gmp_sqrtSquare rootDescriptionresource gmp_sqrtresource a
Calculates square root of a.
gmp_sqrtrmSquare root with remainderDescriptionarray gmp_sqrtrmresource a
Returns array where first element is the integer square root of
a (see also
gmp_sqrt), and the second is the remainder
(i.e., the difference between a and the
first element squared).
gmp_perfect_squarePerfect square checkDescriptionbool gmp_perfect_squareresource a
Returns &true; if a is a perfect square,
&false; otherwise.
See also: gmp_sqrt,
gmp_sqrtrm.
gmp_powRaise number into powerDescriptionresource gmp_powresource baseint exp
Raise base into power
exp. The case of 0^0 yields
1. exp cannot be negative.
gmp_powmRaise number into power with moduloDescriptionresource gmp_powmresource baseresource expresource mod
Calculate (base raised into power
exp) modulo mod. If
exp is negative, result is undefined.
gmp_prob_primeCheck if number is "probably prime"Descriptionint gmp_prob_primeresource aint reps
If this function returns 0, a is
definitely not prime. If it returns 1, then
a is "probably" prime. If it returns 2,
then a is surely prime. Reasonable values
of reps vary from 5 to 10 (default being
10); a higher value lowers the probability for a non-prime to
pass as a "probable" prime.
The function uses Miller-Rabin's probabilistic test.
gmp_gcdCalculate GCDDescriptionresource gmp_gcdresource aresource b
Calculate greatest common divisor of a and
b. The result is always positive even if
either of, or both, input operands are negative.
gmp_gcdextCalculate GCD and multipliersDescriptionarray gmp_gcdextresource aresource b
Calculates g, s, and t, such that a*s + b*t = g =
gcd(a,b), where gcd is the greatest common divisor. Returns
an array with respective elements g, s and t.
gmp_invertInverse by moduloDescriptionresource gmp_invertresource aresource b
Computes the inverse of a modulo
b. Returns &false; if an inverse does not
exist.
gmp_legendreLegendre symbolDescriptionint gmp_legendreresource aresource p
Compute the
Legendre symbol of a and
p. p should be odd
and must be positive.
gmp_jacobiJacobi symbolDescriptionint gmp_jacobiresource aresource p
Computes
Jacobi symbol of a and
p. p should be odd
and must be positive.
gmp_randomRandom numberDescriptionresource gmp_randomint limiter
Generate a random number. The number will be up to
limiter words long. If
limiter is negative, negative numbers are
generated.
gmp_andLogical ANDDescriptionresource gmp_andresource aresource b
Calculates logical AND of two GMP numbers.
gmp_orLogical ORDescriptionresource gmp_orresource aresource b
Calculates logical inclusive OR of two GMP numbers.
gmp_xorLogical XORDescriptionresource gmp_xorresource aresource b
Calculates logical exclusive OR (XOR) of two GMP numbers.
gmp_setbitSet bitDescriptionresource gmp_setbitresource &aint indexbool
set_clear
Sets bit index in
a. set_clear
defines if the bit is set to 0 or 1. By default the bit is set to
1.
gmp_clrbitClear bitDescriptionresource gmp_clrbitresource &aint index
Clears (sets to 0) bit index in
a.
gmp_scan0Scan for 0Descriptionint gmp_scan0resource aint start
Scans a, starting with bit
start, towards more significant bits,
until the first clear bit is found. Returns the index of the
found bit.
gmp_scan1Scan for 1Descriptionint gmp_scan1resource aint start
Scans a, starting with bit
start, towards more significant bits,
until the first set bit is found. Returns the index of the found
bit.
gmp_popcountPopulation countDescriptionint gmp_popcountresource a
Return the population count of a.
gmp_hamdistHamming distanceDescriptionint gmp_hamdistresource aresource b
Returns the hamming distance between a and
b. Both operands should be non-negative.