
153 views
PHP GMP Functions Detail Reference
As of my last update in September 2021, here is a detailed reference for some of the GMP (GNU Multiple Precision) functions in PHP. GMP is an extension that provides support for arbitrary precision arithmetic, allowing you to work with very large integers and perform various mathematical operations with high precision.
To use GMP functions, make sure you have the GMP extension enabled in your PHP configuration.
gmp_init()
– Create GMP number
GMP gmp_init ( mixed $number [, int $base = 0 ] )
$number
: The number to convert to a GMP resource. It can be an integer, a string representing an integer, or a numeric string in base$base
.$base
: The base of the number given in$number
. If$base
is 0 (default), it will be determined automatically based on the input.
gmp_add()
– Add two GMP numbers
GMP gmp_add ( GMP $num1 , GMP $num2 )
$num1
: The first GMP number.$num2
: The second GMP number.- Returns the result of the addition as a new GMP number.
gmp_sub()
– Subtract two GMP numbers
GMP gmp_sub ( GMP $num1 , GMP $num2 )
$num1
: The GMP number to subtract from.$num2
: The GMP number to subtract.- Returns the result of the subtraction as a new GMP number.
gmp_mul()
– Multiply two GMP numbers
GMP gmp_mul ( GMP $num1 , GMP $num2 )
$num1
: The first GMP number.$num2
: The second GMP number.- Returns the result of the multiplication as a new GMP number.
gmp_div_qr()
– Divide two GMP numbers and get quotient and remainder
array gmp_div_qr ( GMP $num , GMP $den )
$num
: The GMP number (numerator) to be divided.$den
: The GMP number (denominator) by which$num
is divided.- Returns an array containing the quotient and remainder as GMP numbers.
gmp_pow()
– Raise a GMP number to a given power
GMP gmp_pow ( GMP $base , int $exponent )
$base
: The GMP number to be raised to the power.$exponent
: The exponent (power) to raise the$base
.- Returns the result of raising
$base
to the power of$exponent
as a new GMP number.
gmp_cmp()
– Compare two GMP numbers
int gmp_cmp ( GMP $num1 , GMP $num2 )
$num1
: The first GMP number.$num2
: The second GMP number.- Returns 0 if the two GMP numbers are equal, 1 if
$num1
is greater, or -1 if$num1
is smaller.
gmp_strval()
– Convert GMP number to string
string gmp_strval ( GMP $num [, int $base = 10 ] )
$num
: The GMP number to convert to a string.$base
: The base to use for representing the number. Default is 10 (decimal).- Returns the string representation of the GMP number.
gmp_import()
– Import a binary string as a GMP number
GMP gmp_import ( string $data [, int $word_size = 1 [, int $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN ]] )
$data
: The binary string data to import.$word_size
: The number of bytes per word. Default is 1.$options
: A bitmask of options. Default isGMP_MSW_FIRST | GMP_NATIVE_ENDIAN
.- Returns the GMP number created from the binary string.
These are just some of the commonly used GMP functions in PHP. The GMP extension provides many more functions for working with arbitrary precision numbers and performing various mathematical operations. For a complete list of GMP functions and more details, you can refer to the PHP manual: https://www.php.net/manual/en/book.gmp.php.