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.
Syntax :
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.
Examples:
Input: (Long) 2731766 Output: 1 Input: (Long) -233611 Output: -1 Input: (Long) 0 Output: 0
Below programs illustrate the Java.lang.Long.signum() Method:
Program 1:
// Java program to illustrate the // Java.lang.Long.signum() Method import java.lang.*; public class Geeks { public static void main(String[] args) { // It will return 1 as long value is greater than 1 System.out.println(Long.signum(36565531)); // It will return -1 as long value is less than 1 System.out.println(Long.signum(-628127)); // Returns 0 as long value is equal to 0 System.out.println(Long.signum(0)); } } |
1 -1 0
Program 2: For decimal value and string.
// Java program to illustrate the // Java.lang.Long.signum() Method import java.lang.*; public class Geeks { public static void main(String[] args) { // It will return compile time error System.out.println(Long.signum(36565.531)); // It will return compile time error System.out.println(Long.signum("628127")); } } |
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
Recommended Posts:
- Java Guava | LongMath.divide(long, long, RoundingMode) method with Examples
- Java Guava | LongMath.checkedAdd(long a, long b) method with Examples
- Java Guava | Longs.checkedSubtract(long a, long b) method with Examples
- Java Guava | Longs.indexOf(long[] array, long[] target) method with Examples
- Java Guava | Longs.indexOf(long[] array, long target) method with Examples
- Integer signum() Method in Java
- BigDecimal signum() Method in Java
- StrictMath signum() Method in Java
- BigInteger signum() Method in Java
- Java signum() method with Examples
- Duration ofSeconds(long, long) method in Java with Examples
- Java Math subtractExact(long x, long y) method
- Java Math addExact(long x, long y) method
- Java Guava | gcd(long a, long b) of LongMath Class with Examples
- Java Guava | mean(long x, long y) of LongMath Class with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



