Long signum() Method in Java
The signum function also known as sign function is an odd mathematical function that extracts the sign of a real number.
The java.lang.Long.signum() method is used to get the signum function of the specified long value. For a positive value, a negative value and zero, the method returns 1, -1 and 0 respectively.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public static int signum(long num)
Parameters: The method accepts one parameter num of long type on which the signum operation is to be performed.
Return Value: The method returns the signum function of the specified long value. If the specified value is:
- Negative, the method returns -1.
- Zero, the method returns 0.
- Positive, the method returns 1.
Input: (Long) 2731766 Output: 1 Input: (Long) -233611 Output: -1 Input: (Long) 0 Output: 0
Below programs illustrate the Java.lang.Long.signum() Method:
1 -1 0
Program 2: For decimal value and string.
prog.java:10: error: incompatible types: possible lossy conversion from double to long System.out.println(Long.signum(36565.531)); ^ prog.java:13: error: incompatible types: String cannot be converted to long System.out.println(Long.signum("628127")); ^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 2 errors