Java Math signum() Method
The signum() method of Java Math class returns the signum function of the value.
Syntax:
- public static double signum(double d)
- public static float signum (float d)
Parameters:
The parameter ‘d’ represents the floating-point value whose signum is to be determined.
Return Value:
The signum() method returns the signum function of ‘d’.
- It returns zero with same sign as argument, if the argument passed is zero.
- It returns 1.0, if the argument is greater than 0.
- It returns -1.0, if the argument is less than 0.
- It returns NaN. If the argument is NaN.
Example 1:
public class JavaMathSignumExample1 { static int i=1; public static void main(String[] args) { double d=0.1; //returns 1.0, if d is greater than 0 System.out.println(i++ +". Signum Value for "+d+" is : " +Math.signum(d)); } }
Output:
Signum Value for 0.1 is : 1.0
Example 2:
public class JavaMathSignumExample2 { public static void main(String[] args) { double d=-1.1; //returns -1.0, if d is less than 0 System.out.println(" Signum Value for "+d+" is : " +Math.signum(d)); } }
Output:
Signum Value for -1.1 is : -1.0
Example 3:
public class JavaMathSignumExample3 { public static void main(String[] args) { double d=-0d; //returns 0 with the same sign as the argument, if d is equal to positive or negative 0 System.out.println(" Signum Value for "+d+" is : " +Math.signum(d)); } }
Output:
Signum Value for -0.0 is : -0.0
Example 4:
public class JavaMathSignumExample4 { public static void main(String[] args) { double d=Double.NaN; //returns NaN, if d is NaN System.out.println(" Signum Value for "+d+" is : " +Math.signum(d)); } }
Output:
Signum Value for NaN is : NaN