Open In App

PHP | gmp_divexact() Function

Last Updated : 20 Apr, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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.




<?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;
?>


Output:

4

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




<?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;
?>


Output:

3

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




<?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;
?>


Output:

7905747460161236409

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads