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;
}
Answer from assylias on Stack Overflow
🌐
Tutorjoes
tutorjoes.in › Java_example_programs › find_the_sign_of_given_number_using_library_method_in_java
Write a Java program to Find the sign of given number using library method
import java.util.*; public class FindSign_GivenNumber { public static void main(String[] args) { Scanner input = new Scanner(System.in); double num = 0; System.out.print("Enter The Number : "); num = input.nextDouble(); double res = Math.signum(num); if(res == 0) { System.out.print("Number is Zero"); } else if (res == -1) { System.out.print("Number is Negative"); } else if (res == 1) { System.out.print("Number is Positive"); } else { System.out.print("Number is NaN"); } } } ... Accept two integers and return true if the either one is 15 or if their sum or difference is 15 in Java · Count the Number of prime numbers less than given positive number in Java
🌐
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.
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.

🌐
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!
🌐
Java2s
java2s.com › Tutorials › Java › Data_Types › How_to_get_the_sign_of_a_Java_Integer.htm
How to get the sign of a Java Integer
public class Main { public static void main(String[] args) { /*from ja va 2s . c o m*/ System.out.println(Integer.signum(10)); } }
🌐
Java2s
java2s.com › Book › Java › 0060__Development › Get_the_sign_of_an_Integer.htm
Get the sign of an Integer « Integer « Numbers « Development « Java Book
Get the sign of an Integer · signum(int i) Returns · public · class · Main { · static · main(String[] args) { System.out.println(Integer.signum(10)); } } · The output: · java2s.com | Contact Us | Privacy Policy
🌐
Javatpoint
javatpoint.com › java-integer-signum-method
Java Integer signum() method with Examples
Java Integer signum() Method - The signum() method of Java Integer class returns the signum function of the specified int value in the method argument. The signum function can be computed as:
🌐
Tutorialspoint
tutorialspoint.com › java › lang › math_signum_float.htm
Java - Math signum(float x) method
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get float numbers float x = 0.0f; // find the signum of the value System.out.println("Math.signum(" + x + ")=" + Math.signum(x)); } } Let us compile and run the above program, this will produce the following result − · Math.signum(0.0)=0.0 · java_lang_math.htm ·
🌐
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.
Find elsewhere
🌐
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); } }
🌐
Blogger
self-learning-java-tutorial.blogspot.com › 2022 › 08 › hive-signn-return-sign-of-number.html
Programming for beginners: Hive: sign(n): Return the sign of a number
August 23, 2022 - If the number is -ve, it return -1.0, for positive it return 1.0, 0.0 otherwise. hive> SELECT sign(10); OK _c0 1.0 Time taken: 0.05 seconds, Fetched: 1 row(s) hive> ; hive> SELECT sign(-10); OK _c0 -1.0 Time taken: 0.041 seconds, Fetched: 1 row(s) hive> ; hive> SELECT sign(0); OK _c0 0.0 Time taken: 0.041 seconds, Fetched: 1 row(s) ... My Blog is selected as one of the Top 40 Java Blogs by FeedSpot.
🌐
Baeldung
baeldung.com › home › java › java numbers › check if a number is positive or negative in java
Check if a Number Is Positive or Negative in Java | Baeldung
January 8, 2024 - Of course, the same logic works if we can change the Integer parameter to Long, Float, or Double. We’ve seen how to check if a number is positive or negative using the < and the > operators. Alternatively, we can use the signum() method to get ...
🌐
Blogger
javarevisited.blogspot.com › 2013 › 01 › how-to-check-if-number-is-positive-or-negative-java-example.html
How to check if a Number is Positive or Negative in Java? Solution Example
All these methods assume that you ... on the positive side. Convert any number into String and get the first character, if it's equals to "-" then it's a negative number otherwise it's a positive one....
🌐
Tutorialspoint
tutorialspoint.com › java › lang › math_signum_double.htm
Java - Math signum(double x) method
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get double numbers double x = 0.0; // find the signum of the value System.out.println("Math.signum(" + x + ")=" + Math.signum(x)); } } Let us compile and run the above program, this will produce the following result − · Math.signum(0.0)=0.0 · java_lang_math.htm ·
🌐
Coderanch
coderanch.com › t › 639978 › java › comparing-sign-number-java
comparing sign of a number in java (Beginning Java forum at Coderanch)
September 23, 2014 - I had a slightly more general method, but I just changed it as to show an alternative (albeit one that is far less simple): applicable, say, The strange thing is: in one minute we write a classic Java solution, but we're willing to spend a whole evening coming up with an equal stream solution, feeling proud too. Remarkable. Greetz, Piet · There are three kinds of actuaries: those who can count, and those who can't. ... sai rama krishna wrote:both zeros is not a concern for me now. Thank you Actually, I suggest you think about that carefully before you answer categorically. The title of your thread was "comparing · sign of a number in java", and signum() treats 0 differently from any other value.
🌐
Educative
educative.io › answers › what-is-mathsignum-in-java
What is Math.signum in Java?
Not a Number. ... In the code below, we calculate the signum for four different values with the help of the signum() function.
🌐
Tutorial Gateway
tutorialgateway.org › java-signum-function
Java signum Function
March 28, 2025 - static float signum(float number); //Return Type is float // To use in program: Math.signum(float number); The Math.signum function returns the Sign of a specified expression. In this Java example, We are going to check the same for different ...
🌐
GeeksforGeeks
geeksforgeeks.org › java-signum-method-examples
Java signum() method with Examples | GeeksforGeeks
April 9, 2018 - If the argument passed is less than zero, then the signum() function will return -1.0. Note: If the argument passed is NaN, then the result is NaN. If the argument passed is positive zero or negative zero then the result will be same as that ...
🌐
Blogger
grammarofdev.blogspot.com › 2016 › 12 › efficient-way-to-find-sign-of-number-in.html
How-To:: Efficient way to find the sign of a number in Java
http://www.java2novice.com/java-fundamentals/math/signum/ http://stackoverflow.com/questions/13988805/fastest-way-to-get-sign-in-java