🌐
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.
🌐
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.
🌐
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.
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.

🌐
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.
🌐
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
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_signum.htm
Java.math.BigDecimal.signum() Method
The following example shows the usage of math.BigDecimal.signum() method. package com.tutorialspoint; import java.math.*; public class BigDecimalDemo { public static void main(String[] args) { // create 3 BigDecimal objects BigDecimal bg1, bg2, bg3; // create 3 int objects int i1, i2, i3; bg1 = new BigDecimal("123"); bg2 = new BigDecimal("0"); bg3 = new BigDecimal("-12"); // assign the signum values of bg1,bg2,bg3 to i1,i2,i3 respectively i1 = bg1.signum(); i2 = bg2.signum(); i3 = bg3.signum(); String str1 = "The Result of Signum function on " + bg1 + " is " + i1; String str2 = "The Result of Signum function on " + bg2 + " is " + i2; String str3 = "The Result of Signum function on " + bg3 + " is " + i3; // print i1,i2,i3 values System.out.println( str1 ); System.out.println( str2 ); System.out.println( str3 ); } }