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

2021年3月18日16:35:14 发表评论 723 次浏览

gmp_div_r()函数是PHP中的内置函数, 可在两个GMP编号之间执行除法运算

(GNU多重精度:适用于大数)

并返回余数。

句法 :

gmp_div_r($num1, $num2)

参数:此函数接受两个GMP数字$ num1和$ num2作为强制参数, 如上面的语法所示。这些参数可以是PHP 5.6和更高版本中的GMP对象, 或者可以将数字字符串传递给函数, 前提是可以将这些字符串转换为数字。

返回值:该函数返回GMP编号, 该编号是除法的余数。

例子:

Input : $num1 = 146, $num2  = 12
Output : 2

Input : $num1 = "189126457831", $num2  = "12098123409"
Output :  7654606696

下面的程序将说明gmp_div_r()函数的用法。

程序1:当GMP数字作为参数传递时执行GMP数字除法的程序。

<?php
// PHP program to perform the division of
// GMP numbers
    
// creating GMP numbers using gmp_init()
$num1 = gmp_init(257);
$num2 = gmp_init(17);
   
// caluates the remainder when 
//  $num1 is divided by num2
  
$res = gmp_div_r( $num1 , $num2 );
// Display the remainder
echo $res ;
?>

输出如下

2

程序2:将数字字符串作为GMP数字作为参数传递时执行GMP数字除法的程序。

<?php
// PHP program to perform the division of
// GMP numbers
    
// creating GMP number using gmp_init()
$a = gmp_init( "7891267541121" );
   
// calculates the remainder when 
// $a is divided by 115789034
$res = gmp_div_r( $a , 115789034);
  
// Display the remainder
echo $res ;
?>

输出如下

13295953

参考:http://php.net/manual/en/function.gmp-div-r.php


木子山

发表评论

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