PHP gmp_div_qr()函数用法详细介绍

2021年3月13日16:39:02 发表评论 570 次浏览

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

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

并返回商和余数。

句法 :

gmp_div_qr($num1, $num2)

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

返回值:此函数返回一个包含两个成分的数组:

  • 首先是商师。
  • 第二个是余师。

例子:

Input : $num1 = 146, $num2  = 12
Output : Quotient = 12, Remainder = 2
          Array ( [0] => GMP Object ( [num] => 12 ) [1] => GMP Object ( [num] => 2 ) )

Input : $num1 = 189126457831, $num2  = 12098123409
Output : Quotient = 15, Remainder = 7654606696
          Array ( [0] => GMP Object ( [num] => 15) [1] => GMP Object ( [num] => 7654606696 ) )

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

程序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);
  
// calculates the quotient and remainder
//  when $num1 is divided by num2
  
$res = gmp_div_qr( $num1 , $num2 );
// Printing the Array elements, i.e.
// the quotient and remainder
print_r( $res );
?>

输出如下

Array 
( 
[0] => GMP Object ( [num] => 15 ) 
[1] => GMP Object ( [num] => 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 quotient when
// $a is divided by 115789034
$res = gmp_div_qr( $a , "115789034" );
  
// Printing the Array elements, i.e.
// the quotient and remainder
print_r( $res );
?>

输出如下

Array ( 
[0] => GMP Object ( [num] => 68152 ) 
[1] => GMP Object ( [num] => 13295953 ) 
)

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


木子山

发表评论

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