
PHP gmp_divexact() Function
As of my last update in September 2021, there is no built-in gmp_divexact()
function in PHP. However, PHP provides the gmp_div()
function, which can be used to perform exact integer division between two GMP (GNU Multiple Precision) numbers.
Here’s the syntax of the gmp_div()
function:
GMP gmp_div ( GMP $num1 , GMP $num2 [, int $round = GMP_ROUND_ZERO ] )
Parameters:
$num1
: The GMP number (numerator) to be divided.$num2
: The GMP number (denominator) by which$num1
is divided.$round
(optional): Specifies the rounding method. It can take one of the following constants:GMP_ROUND_ZERO
: The result is rounded towards zero.GMP_ROUND_PLUSINF
: The result is rounded towards positive infinity.GMP_ROUND_MINUSINF
: The result is rounded towards negative infinity.
Return value:
- The result of the division as a new GMP number.
If you want to ensure that the division is exact (meaning there is no remainder), you can check if the remainder is zero after using the gmp_div()
function.
Here’s an example of performing exact division using gmp_div()
:
$numerator = gmp_init('24');
$denominator = gmp_init('6');
$result = gmp_div($numerator, $denominator);
$remainder = gmp_mod($numerator, $denominator);
if (gmp_cmp($remainder, 0) === 0) {
echo "Division is exact. Quotient: " . gmp_strval($result);
} else {
echo "Division is not exact. Quotient: " . gmp_strval($result) . ", Remainder: " . gmp_strval($remainder);
}
In this example, the division of 24 by 6 is exact, so the output will be:
Division is exact. Quotient: 4
Remember to ensure that the GMP extension is enabled in your PHP configuration to use these functions. Additionally, please note that the availability of functions may change in newer PHP versions, so always refer to the PHP manual for the specific version you are using.