gmp_sign()是PHP中的一个内置函数,用于检查给定gmp编号的符号( 多精度 :对于大数字)。
null
语法:
gmp_sign($num)
参数: 此函数接受一个GMP编号 $num 作为上述语法中显示的必需参数。该参数可以是PHP 5.6版及更高版本中的GMP对象,也可以传递数字字符串,前提是可以将该字符串转换为数字。
返回值: 该函数检查给定数字的符号 $num 并根据数字返回三个值,如下所述:
- 返回1 –$num为正
- 返回-1 -$num是负数
- 返回0 –$num为零
例如:
Input : $num=9 Output : 1 Input : $num=-8 Output : -1 Input : $num=0 Output : 0
下面的程序演示了gmp_sign()函数:
项目1: 下面的程序演示了当gmp编号作为参数传递时,gmp_sign()函数的工作原理。
<?php // PHP program to check the sign // of a number // GMP arguments // negative $num1 = gmp_init( "-101" , 2); // positive $num2 = gmp_init( "1010" , 2); // zero $num3 = gmp_init( "0" , 2); // prints -1 as negative echo gmp_sign( $num1 ). "" ; // prints +1 as negative echo gmp_sign( $num2 ). "" ; // prints 0 as 0 echo gmp_sign( $num3 ). "" ; ?> |
输出:
-1 1 0
项目2: 下面的程序演示了当数字字符串作为参数传递时,gmp_sign()的工作原理。
<?php // PHP program to check the sign // of a number // numeric arguments // negative $num1 = -9; // positive $num2 = 8; // zero $num3 = 0; // prints -1 as negative echo gmp_sign( $num1 ). "" ; // prints +1 as negative echo gmp_sign( $num2 ). "" ; // prints 0 as 0 echo gmp_sign( $num3 ). "" ; ?> |
输出:
-1 1 0
参考: http://php.net/manual/en/function.gmp-sign.php
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END