🌐
W3Schools
w3schools.com › java › ref_math_signum.asp
Java Math signum() Method
The signum() method returns the sign of a number. A number's sign tells whether it is positive or negative.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Math.html
Math (Java Platform SE 8 )
July 21, 2026 - Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.
🌐
GeeksforGeeks
geeksforgeeks.org › java-signum-method-examples
Java signum() method with Examples | GeeksforGeeks
April 9, 2018 - The java.lang.Math.signum() returns the Sign function of a value passed to it as argument. The signum() function returns the following values depending on the argument passed to it: If the argument passed is greater than zero, then the signum() ...
🌐
Codecademy
codecademy.com › docs › java › math methods › .signum()
Java | Math Methods | .signum() | Codecademy
October 31, 2022 - The Math.signum() method determines whether a number is positive, negative, zero, or NaN. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-math-signum-method
Java Math signum() Method - GeeksforGeeks
May 13, 2025 - Example 2: In this example, we will see how signum() method handles special cases like NaN, positive zero and negative zero. Java ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › integer-signum-method-in-java
Integer signum() Method in Java - GeeksforGeeks
August 19, 2021 - The signum function is an odd mathematical function that extracts the sign of a real number. The signum function is also known as sign function. The Integer.signum() method of java.lang returns the signum function of the specified integer value.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › integer_signum.htm
Java - Integer signum() method
This method returns the signum function of the specified int value.It is -1 if the specified value is negative, 0 if the specified value is zero and 1 if the specified value is positive.
🌐
Javatpoint
javatpoint.com › java-math-signum-method
Java Math.signum() Method with Examples - Javatpoint
Java Math.signum() Method with Examples on abs(), min(), max(), avg(), round(), ceil(), floor(), pow(), sqrt(), sin(), cos(), tan(), exp() etc.
Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com › java › lang › math_signum_float.htm
Java - Math signum(float x) method
The Java Math signum(float f) returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.Special Cases − Following is the declaration for
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-signum-method-in-java
BigDecimal signum() Method in Java - GeeksforGeeks
December 4, 2018 - public int signum() Parameters: This method does not accepts any parameter. Return value: This method can return three types of values: -1 if this BigDecimal < 0 · 0 if this BigDecimal = 0 · 1 if this BigDecimal is > 0 Below program illustrates the working of the above mentioned method: Program 1: Java ·
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-math-signum-method
Java Math.signum() Method
Java Math.signum() method returns the signum function of passed argument. If the argument is zero, it returns zero. If argument is negative, it returns -1.0. If argument is positive, it returns 1.0.
🌐
Tutorial Gateway
tutorialgateway.org › java-signum-function
Java signum Function
March 28, 2025 - The Java signum Function is one of the Math Library functions, which is to find the Sign of a given expression, i.e., Positive, Negative, or Zero.
🌐
Apache Commons
commons.apache.org › proper › commons-math › javadocs › api-3.6.1 › org › apache › commons › math3 › analysis › function › Signum.html
Signum (Apache Commons Math 3.6.1 API)
java.lang.Object · org.apache.commons.math3.analysis.function.Signum · All Implemented Interfaces: UnivariateFunction · public class Signum extends Object implements UnivariateFunction · signum function. Since: 3.0 · clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, ...
Top answer
1 of 3
89

Any reasons why you don't simply use:

int sign = (int) Math.signum(a); //1 cast for floating-points, 2 for Integer types

Additionally most Number implementations have a signum method taking a primitive of that type and returning an int, so you can avoid casting for extra performance.

int sign1 = Integer.signum(12); //no casting
int sign2 = Long.signum(-24l); //no casting

It will return +1 / 0 / -1 and it has been optimized to deliver a good performance.

For reference, you can have a look at the implementation in openJDK. The relevant bits are:

public static float signum(float f) {
    return (f == 0.0f || isNaN(f)) ? f : copySign(1.0f, f);
}

public static boolean isNaN(float f) {
    return (f != f);
}

public static float copySign(float magnitude, float sign) {
    return rawCopySign(magnitude, (isNaN(sign) ? 1.0f : sign));
}

public static float rawCopySign(float magnitude, float sign) {
    return Float.intBitsToFloat((Float.floatToRawIntBits(sign)
            & (FloatConsts.SIGN_BIT_MASK))
            | (Float.floatToRawIntBits(magnitude)
            & (FloatConsts.EXP_BIT_MASK
            | FloatConsts.SIGNIF_BIT_MASK)));
}

static class FloatConsts {
    public static final int SIGN_BIT_MASK = -2147483648;
    public static final int EXP_BIT_MASK = 2139095040;
    public static final int SIGNIF_BIT_MASK = 8388607;
}
2 of 3
10

If you just want the IEEE 754 sign bit from the float value you can use:

/**
 * Gets the sign bit of a floating point value
 */
public static int signBit(float f) {
    return (Float.floatToIntBits(f)>>>31);
}

This is very fast and has the advantage of no branches. I think it is the fastest you can get on the JVM.

But make sure it is what you want! Especially watch out for the special cases, e.g. NaN can technically have either a 0 or 1 sign bit.

🌐
Tutorialspoint
tutorialspoint.com › java › lang › math_signum_double.htm
Java - Math signum(double x) method
The Java Math signum(double d) returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.Special Cases − Following is the declaration for
🌐
Educative
educative.io › answers › what-is-mathsignum-in-java
What is Math.signum in Java?
The signum() method in the Math class in Java works as follows: Returns 1 if the number passed is greater than zero. Returns 0 if the number passed is equal to zero. Returns -1 if the number passed is less than zero.
🌐
Kodejava
kodejava.org › how-do-i-get-signum-function-of-a-number
How do I get signum function of a number? - Learn Java by Examples
May 27, 2023 - // * -1.0 if value is less than zero. double sign1 = Math.signum(zero); double sign2 = Math.signum(negative); double sign3 = Math.signum(positive); // For floating-point value float sign4 = Math.signum(zero.floatValue()); float sign5 = Math.signum(negative.floatValue()); float sign6 = Math.signum(positive.floatValue()); System.out.println("In double:"); System.out.println("Signum of " + zero + " is " + sign1); System.out.println("Signum of " + negative + " is " + sign2); System.out.println("Signum of " + positive + " is " + sign3); System.out.println("In float:"); System.out.println("Signum of " + zero + " is " + sign4); System.out.println("Signum of " + negative + " is " + sign5); System.out.println("Signum of " + positive + " is " + sign6); } }
🌐
Educative
educative.io › answers › what-is-the-bigdecimalsignum-method-in-java
What is the BigDecimal.signum() method in Java?
The signum method of the BigDecimal class retrieves the signum function of a BigDecimal value.
Top answer
1 of 2
13

You can use it this way:

Integer.signum(int i) and Long.signum(long l)

Link to javadoc: https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#signum-int-

2 of 2
2

Just an addendum of some implementation details of that :

public static int signum(int i) {
    // HD, Section 2-7
    return (i >> 31) | (-i >>> 31);
}

Integer::signum says : I'll give you -1 if the number is negative, 0 if number is zero and 1 if number is positive. This is fairly trivial via some nested if/else for example.

Instead JDK uses a solution that is a bit more fancy. (x >> 31) | (-x >>> 31). Looks easy right? the first part : x >> 31 is signed shift to the right; it's called signed because it keeps the sign after the shift.

Suppose we live in a world where 4 bits numbers exist only (for simplicity).

We have 0100 (+4), a single shift 0100 >> 1 will make it 0010 (+2). Now, if our number is 1100 (-4; the first bit is the sign), a shift signed to the right : 1100 >> 1 is 1110 (-2). Do a division, but keep the sign.

Thus if we shift 31 times, we throw away the last 31 bits of the number, move the bit for the sign in the least significant position and keep the original sign. Or in simple words take the 31 bit, put it into 0 position and throw away everything else.

 0 00000 ..... 11111
 x --------------->0 // x is kept
        ignore

The second part -x >>> 31 is a unsigned shift, meaning the sign is not kept when we shift.

For example 0100 >>> 1 (+4) will give you 0010 (+2). Nothing is really different so far from the signed shift and the example above. The interesting part comes when numbers are negative:

1100 (-4) and we try to shift it once : 1100 >>> 1, because the sign is not kept, we put zero in the most significant bit and move to the right, thus we get : 0110 (+6!).

In reality, taking 32 bits into the picture. -4 == 1111...111100 and we shift it to the right: sign is zero, everything else is moved to the right, thus: 0111...11110 or Integer.MAX_VALUE - 1.

System.out.println(-4 >>> 1);
System.out.println(Integer.MAX_VALUE - 1);

Thus the part x >>> 31 will move the sign bit into the least significant position, zeroing everything else. No matter the number you give it, you will always get 1 or 0.

1 00000 ..... 11111
x --------------->1 // x is "zeroed also"
       ignore

And the addition of -x to that x >>> 31 is simply done so that | would work correctly satisfy our needed result.