Monday, December 22, 2025
HomeLanguagesPHP | gmp_div_qr() Function

PHP | gmp_div_qr() Function


The gmp_div_qr() function is an in-built function in PHP which performs the division operation between two GMP numbers (GNU Multiple Precision : For large numbers) and returns the quotient and remainder.
Syntax :

gmp_div_qr($num1, $num2)

Parameters : This function accepts two GMP numbers, $num1 and $num2 as mandatory parameters as shown in the above syntax. These parameters can be GMP objects in PHP version 5.6 and later, or numeric strings can be passed to the function provided that it is possible to convert those strings to numbers.

Return Value : This function returns an array with two components :

  • First being the quotient of the division.
  • Second being the remainder of the division.

Examples:

Input : $num1 = 146, $num2  = 12
Output : Quotient = 12, Remainder = 2
          Array ( [0] => GMP Object ( [num] => 12 ) [1] => GMP Object ( [num] => 2 ) )

Input : $num1 = 189126457831, $num2  = 12098123409
Output : Quotient = 15, Remainder = 7654606696
          Array ( [0] => GMP Object ( [num] => 15) [1] => GMP Object ( [num] => 7654606696 ) )

Below programs will illustrate the use of gmp_div_qr() function.

Program 1 : Program to perform the division of GMP numbers when GMP numbers are passed as arguments.




<?php
// PHP program to perform the division of
// GMP numbers
   
// creating GMP numbers using gmp_init()
$num1 = gmp_init(257);
$num2 = gmp_init(17);
  
// calculates the quotient and remainder
//  when $num1 is divided by num2
  
$res = gmp_div_qr($num1, $num2);
// Printing the Array elements, i.e.
// the quotient and remainder
print_r($res);
?>


Output

Array 
( 
[0] => GMP Object ( [num] => 15 ) 
[1] => GMP Object ( [num] => 2 ) 
)

Program 2 : Program to perform the division of GMP numbers when numeric strings as GMP numbers are passed as arguments.




<?php
// PHP program to perform the division of
// GMP numbers
   
// creating GMP number using gmp_init(
$a = gmp_init("7891267541121");
  
// calculates the quotient when
// $a is divided by 115789034
$res = gmp_div_qr($a, "115789034");
  
// Printing the Array elements, i.e.
// the quotient and remainder
print_r($res);
?>


Output

Array ( 
[0] => GMP Object ( [num] => 68152 ) 
[1] => GMP Object ( [num] => 13295953 ) 
)

Reference : http://php.net/manual/en/function.gmp-div-qr.php

RELATED ARTICLES

Most Popular

Dominic
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6911 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS