XXXVI. GMP functions
These functions allow you to work with arbitrary-length integers
using GNU MP library. In order to have these
functions available, you must compile PHP with
GMP support by using the --with-gmp option.
You can download the GMP library from http://www.swox.com/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.
Nota:
Most GMP functions accept GMP number arguments, defined as
resource below. However, most of these
functions will accept also numeric and string arguments, given
it's possible to convert the latter to number. Also, if there's
faster function that can operate on integer arguments, it would
be used instead of slower function when supplied arguments are
integers. This is done transparently, so the bootom line is that
you can use integers in every function that expects GMP
number. See also gmp_init() function.
Ejemplo 1. Factorial function using GMP <?php
function fact ($x) {
if ($x <= 1)
return 1;
else
return gmp_mul ($x, fact ($x-1));
}
print gmp_strval (fact (1000)) . "\n";
?> |
|
This will calculate factiorial of 1000 (pretty big number)
very fast.