What is Math.signum in Java?
What is the signum function?
Any real number has a magnitude and a sign associated with it. A real number’s sign, also known as the signum, is -1 for
Return type
The signum() method in the Math class in Java works as follows:
-
Returns
1if the number passed is greater than zero. -
Returns
0if the number passed is equal to zero. -
Returns
-1if the number passed is less than zero. -
Returns
NaNif the number passed is .NaNNot a Number
Method signature
public static double signum(double d)
Parameters
double d: the value whose signum is to be returned.
Overloaded methods
-
public static double signum(double d) -
public static float signum(float f)
Code
In the code below, we calculate the signum for four different values with the help of the signum() function.
public class Main {public static void main(String[] args) {System.out.println("Signum for -1000 is " + Math.signum(-1000));System.out.println("Signum for 0 is " + Math.signum(0));System.out.println("Signum for 1000 is " + Math.signum(1000));System.out.println("Signum for NaN is " + Math.signum(Double.NaN));}}