Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

gmp_div_qr

Description

The gmp_div_qr of GMP for PHP divides numbers and gets the quotient and remainder.

Syntax

gmp_div_qr(
    GMP|int|string $num1,
    GMP|int|string $num2,
    int $rounding_mode = GMP_ROUND_ZERO
): array

Parameters

num1

The number being divided.

A GMP object, an int or a numeric string.

num2

The number that num1 is being divided by.

A GMP object, an int or a numeric string.

rounding_mode

The result rounding is defined by the rounding_mode:

ConstantDescription
GMP_ROUND_ZEROThe result is truncated towards 0.
GMP_ROUND_PLUSINFThe result is rounded towards +infinity.
GMP_ROUND_MINUSINFThe result is rounded towards -infinity.

Return

Returns an 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).

Examples

1 · num1 num2 · GMP

<?

$num1 = 2;
$num2 = 3;

$num1 = gmp_init($num1);
$num2 = gmp_init($num2);

$return = gmp_div_qr($num1, $num2);

var_dump($return);

?>
array(2) {
  [0]=>
  object(GMP)#3 (1) {
    ["num"]=>
    string(1) "0"
  }
  [1]=>
  object(GMP)#4 (1) {
    ["num"]=>
    string(1) "2"
  }
}

2 · num1 num2 · int

<?

$num1 = 2;
$num2 = 3;

$return = gmp_div_qr($num1, $num2);

var_dump($return);

?>
array(2) {
  [0]=>
  object(GMP)#1 (1) {
    ["num"]=>
    string(1) "0"
  }
  [1]=>
  object(GMP)#2 (1) {
    ["num"]=>
    string(1) "2"
  }
}

3 · num1 num2 · string

<?

$num1 = "2";
$num2 = "3";

$return = gmp_div_qr($num1, $num2);

var_dump($return);

?>
array(2) {
  [0]=>
  object(GMP)#1 (1) {
    ["num"]=>
    string(1) "0"
  }
  [1]=>
  object(GMP)#2 (1) {
    ["num"]=>
    string(1) "2"
  }
}

4 · rounding_mode · GMP_ROUND_ZERO

<?

$num1 = 2;
$num2 = 3;
$rounding_mode = GMP_ROUND_ZERO;

$return = gmp_div_qr($num1, $num2, $rounding_mode);

var_dump($return);

?>
array(2) {
  [0]=>
  object(GMP)#1 (1) {
    ["num"]=>
    string(1) "0"
  }
  [1]=>
  object(GMP)#2 (1) {
    ["num"]=>
    string(1) "2"
  }
}

5 · rounding_mode · GMP_ROUND_PLUSINF

<?

$num1 = 2;
$num2 = 3;
$rounding_mode = GMP_ROUND_PLUSINF;

$return = gmp_div_qr($num1, $num2, $rounding_mode);

var_dump($return);

?>
array(2) {
  [0]=>
  object(GMP)#1 (1) {
    ["num"]=>
    string(1) "1"
  }
  [1]=>
  object(GMP)#2 (1) {
    ["num"]=>
    string(2) "-1"
  }
}

6 · rounding_mode · GMP_ROUND_MINUSINF

<?

$num1 = -2;
$num2 = 3;
$rounding_mode = GMP_ROUND_MINUSINF;

$return = gmp_div_qr($num1, $num2, $rounding_mode);

var_dump($return);

?>
array(2) {
  [0]=>
  object(GMP)#1 (1) {
    ["num"]=>
    string(2) "-1"
  }
  [1]=>
  object(GMP)#2 (1) {
    ["num"]=>
    string(1) "1"
  }
}
HomeMenu