PHP | gmp_sign() Function
Last Updated :
14 Apr, 2018
The gmp_sign() is an in-built function in PHP which checks the sign of a given GMP number (GNU Multiple Precision: For large numbers).
Syntax:
gmp_sign($num)
Parameters: This function accepts one GMP number $num as mandatory parameter shown in the above syntax. This parameter can be a GMP object in PHP version 5.6 and later, or we are also allowed to pass a numeric string provided that it is possible to convert that string to a number.
Return Value: The function checks the sign of the given number $num and returns three values depending on the number as described below:
- Returns 1 – $num is positive
- Returns -1 – $num is negative
- Returns 0 – $num is zero
Examples:
Input : $num=9
Output : 1
Input : $num=-8
Output : -1
Input : $num=0
Output : 0
Below programs illustrate the gmp_sign() function:
Program 1: The program below demonstrates the working of gmp_sign() function when GMP number is passed as an argument.
<?php
$num1 = gmp_init( "-101" , 2);
$num2 = gmp_init( "1010" , 2);
$num3 = gmp_init( "0" , 2);
echo gmp_sign( $num1 ). "\n" ;
echo gmp_sign( $num2 ). "\n" ;
echo gmp_sign( $num3 ). "\n" ;
?>
|
Output:
-1
1
0
Program 2: The program below demonstrates the working of gmp_sign() when numeric string is passed as an argument.
<?php
$num1 = -9;
$num2 = 8;
$num3 = 0;
echo gmp_sign( $num1 ). "\n" ;
echo gmp_sign( $num2 ). "\n" ;
echo gmp_sign( $num3 ). "\n" ;
?>
|
Output:
-1
1
0
Reference:
http://php.net/manual/en/function.gmp-sign.php
Share your thoughts in the comments
Please Login to comment...