Monday, September 23, 2024
Google search engine
HomeLanguagesPHP | gmp_divexact() Function

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.




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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments