Cover Image for PHP gmp_div_q() Function
205 views

PHP gmp_div_q() Function

As of my last update in September 2021, there is no built-in gmp_div_q() function in PHP. However, there is a gmp_div_qr() function, which calculates the quotient and remainder of the division of two GMP numbers. GMP (GNU Multiple Precision) is an extension in PHP that allows working with arbitrary precision numbers.

The gmp_div_qr() function divides a GMP number (num) by another GMP number (den) and returns an array containing the quotient and remainder as GMP numbers.

Here’s the syntax of the gmp_div_qr() function:

array gmp_div_qr ( GMP $num , GMP $den [, int $round = GMP_ROUND_ZERO ] )

Parameters:

  • $num: The GMP number (numerator) to be divided.
  • $den: The GMP number (denominator) by which $num 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:

  • An array containing two elements:
  • The first element is the quotient as a GMP number.
  • The second element is the remainder as a GMP number.

Here’s a simple example demonstrating the usage of gmp_div_qr():

$numerator = gmp_init('12345678901234567890');
$denominator = gmp_init('12345');

$result = gmp_div_qr($numerator, $denominator);

$quotient = $result[0];
$remainder = $result[1];

echo "Quotient: " . gmp_strval($quotient) . PHP_EOL;
echo "Remainder: " . gmp_strval($remainder) . PHP_EOL;

Remember to make sure that the GMP extension is enabled in your PHP configuration. The gmp_div_qr() function won’t work without it. Also, please note that the availability of functions may change in newer versions of PHP, so always consult the PHP manual for the version you are using.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS