PHP如何使用gmp_divexact()函数?用法示例

2021年3月30日15:43:18 发表评论 971 次浏览

gmp_divexact()是PHP中的内置函数, 用于检查GMP编号(GNU多重精度:适用于大数)是否可以完全被另一个GMP数字整除。如果发生这种情况, 则函数将返回准确的结果, 否则将返回其他任何不相关的GMP编号

语法如下:

gmp_divexact($num, $divisor)

参数:此功能接受两个GMP编号, $ num1, $ num2作为上述语法中所示的强制性参数。这些参数可以是PHP 5.6和更高版本中的GMP对象, 或者也可以传递数字字符串, 以便可以将这些字符串转换为数字。

返回值:该函数使用快速除法算法, 并检查除法是否可行, 从而将除法结果作为GMP编号返回。

例子:

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

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

下面的程序说明了PHP中的gmp_divexact()函数:

程序1:当数字字符串作为GMP数字作为参数传递时, 对GMP数字执行"精确除法"算法的程序。

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

输出如下:

4

程式2:当GMP数字作为参数传递时, 对GMP数字执行"精确除法"算法的程序。

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

输出如下:

3

程式3:当GMP数字作为参数传递时, 对GMP数字执行"精确除法"算法的程序。

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

输出如下:

7905747460161236409

参考:

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


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: