PHP如何使用gmp_cmp()函数?代码实例

2021年3月23日15:18:23 发表评论 671 次浏览

gmp_cmp()是PHP中的内置函数, 用于比较两个GMP编号(GNU多重精度:适用于大数)。

语法如下:

gmp_cmp($num1, $num2)

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

返回值:如果$ num1> $ num2, 则函数返回" 1", 如果$ num1等于$ num2, 则函数返回" 0", 如果$ num1 <$ num2, 则返回-1。

例子:

Input : gmp_cmp("1234", "1236")
Output : -1

Input : gmp_cmp("3569", "3569")
Output : 0

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

程序1:程序将两个GMP数字作为数字字符串传递时进行比较。

<?php
// PHP program to compare two
// GMP numbers passed as arguments 
   
// strings as GMP numbers 
$num1 = "12356" ;
$num2 = "12356" ;
  
// compares the two numbers and
// gives the result "0" as both are equal 
$res = gmp_cmp( $num1 , $num2 );
  
echo $res ;
  
?>

输出如下:

0

程式2:程序将两个GMP编号作为GMP编号作为参数传递时进行比较。

<?php
  
// PHP program to compare two
// GMP numbers passed as arguments 
  
// creating GMP numbers using gmp_init()
$num1 = gmp_init(12355);
$num2 = gmp_init(12356);
  
// compares these two numbers and
// gives the result "-1" as $num1 < $num2
$res = gmp_cmp( $num1 , $num2 );
  
echo $res ;
?>

输出如下:

-1

参考:

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


木子山

发表评论

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