Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
division
divide
communitycreator

What is gmp_div_q in PHP?

Hammad Nasir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

gmp_div_q is a function in PHP that divides two numbers passed in as arguments.

Syntax

gmp_div_q(GMP|int|string $number1, GMP|int|string $number2, int $rounding_mode = GMP_ROUND_ZERO): GMP

Parameters

  • number1: The number that is going to be divided.

  • number2: The number that number1 is being divided by.

number1 and number2 can be a GMPGNU Multiple Precision Arithmetic Library object, integer, or numeric string.

  • rounding_mode: The rounded result is defined by the rounding_mode, which can take the values listed below:

    • GMP_ROUND_ZERO: The result is truncated towards 0.
    • GMP_ROUND_PLUSINF: The result is rounded towards positive infinity.
    • GMP_ROUND_MINUSINF: The result is rounded towards negative infinity.

Return value

The function returns a number that is the quotient when number1 is divided by number2. The value returned is of type GMP number.

Code

<?php
$val1 = gmp_div_q("10", "5");
echo gmp_strval($val1) . "\n";
$val2 = gmp_div_q("1", "4");
echo gmp_strval($val2) . "\n";
$val3 = gmp_div_q("1", "3", GMP_ROUND_PLUSINF);
echo gmp_strval($val3) . "\n";
$val4 = gmp_div_q("-1", "4", GMP_ROUND_PLUSINF);
echo gmp_strval($val4) . "\n";
$val5 = gmp_div_q("-1", "4", GMP_ROUND_MINUSINF);
echo gmp_strval($val5) . "\n";
?>

Expected output

2
0
1
0
-1

RELATED TAGS

php
division
divide
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring