gmp_sign()是PHP中的內置函數,用於檢查給定GMP編號的符號(GNU多重精度:用於大數)。
用法:
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)."\n";
// prints +1 as negative
echo gmp_sign($num2)."\n";
// prints 0 as 0
echo gmp_sign($num3)."\n";
?>
輸出:
-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)."\n";
// prints +1 as negative
echo gmp_sign($num2)."\n";
// prints 0 as 0
echo gmp_sign($num3)."\n";
?>
輸出:
-1 1 0
參考:
http://php.net/manual/en/function.gmp-sign.php
相關用法
- PHP pi( )用法及代碼示例
- p5.js second()用法及代碼示例
- PHP Ds\Map get()用法及代碼示例
- p5.js value()用法及代碼示例
- p5.js str()用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
- p5.js int()用法及代碼示例
- p5.js max()用法及代碼示例
- PHP tan( )用法及代碼示例
- CSS hsl()用法及代碼示例
- d3.js d3.max()用法及代碼示例
- p5.js nfp()用法及代碼示例
注:本文由純淨天空篩選整理自ChetnaAgarwal大神的英文原創作品 PHP | gmp_sign() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。