PHP | gmp_divexact() Function

The gmp_divexact() is a built-in function in PHP which is used to check whether a GMP number(GNU Multiple Precision : For large numbers) is exactly divisible by another GMP number or not. If it so happens then function returns the exact result else any other irrelevant GMP number.

Syntax:

gmp_divexact($num, $divisor)

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

Return Value: This function uses fast division algorithm and checks if the division is possible or not and thus returns the result of division as a GMP number.

Examples:



Input : gmp_divexact("15", "5")
Output : 3

Input : gmp_divexact("13", "3")
Output : 12297829382473034415

Below programs illustrate the gmp_divexact() function in PHP:

Program 1: Program to perform “exact division” algorithm on GMP numbers when numeric strings as GMP numbers are passed as arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to perform "exact division" of
// GMP numbers passed as arguments 
  
// strings as GMP numbers
$num = "12";
$divisor = "3";
  
// calculate the correct result
// if division possible
$res = gmp_divexact($num, $divisor);
  
echo $res;
?>

chevron_right


Output:

4

Program 2: Program to perform “exact division” algorithm on GMP numbers when GMP numbers are passed as arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to perform "exact division" of
// GMP numbers passed as arguments 
  
// creating GMP numbers using gmp_init()
$num = gmp_init(15);
$divisor = gmp_init(5);
  
// calculate the correct result
// if division is possible
$res = gmp_divexact($num, $divisor);
  
echo $res;
?>

chevron_right


Output:

3

Program 3: Program to perform “exact division” algorithm on GMP numbers when GMP numbers are passed as arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to perform "exact division" of
// GMP numbers passed as arguments 
  
// creating GMP numbers using gmp_init()
$num = gmp_init(15);
$divisor = gmp_init(7);
  
// calculate the correct result
// if division is possible
$res = gmp_divexact($num, $divisor);
  
echo $res;
?>

chevron_right


Output:

7905747460161236409

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.