Java Math signum() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 5+

Definition

In Java, Math.signum(d) returns the sign of a number: -1.0 if it is negative, 1.0 if it is positive, and zero if it is zero.

It answers "which direction?" without caring "how far?". That makes it handy for trends (up or down), movement toward a target one step at a time, and turning a difference into a simple direction.

A static method of the java.lang.Math class, added in Java 5 (JDK 1.5). Call it on the class name, as in Math.signum(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass a double or a float; the result has the same type.

Java
double s1 = Math.signum(-42.7);       // -1.0
double s2 = Math.signum(0.003);       // 1.0
float s3 = Math.signum(0.0f);         // 0.0
int step = (int) Math.signum(target - pos);   // -1, 0, or 1
CallWhat it does
Math.signum(-8.5)Negative: returns -1.0.
Math.signum(3.2f)float overload: returns 1.0f.
Math.signum(-0.0)Returns -0.0 (the zero keeps its sign).
(int) Math.signum(a - b)Direction from b to a as an int.

There is no int overload. An int argument widens to float, so cast the result back with (int). Integer.signum(int) is an integer-only alternative that returns an int.

Library declarations
Java
// java.lang.Math
public static double signum(double d)
public static float signum(float f)

Parameters

ParameterDescription
d / fThe number whose sign you want.

Return Value

Return ValueDescription
double / float-1.0 for negative input, 1.0 for positive input, the argument itself for 0.0, -0.0, and NaN.

Compare with Similar Methods

Other ways to work with signs:

MethodHow it differs
abs(x)The size without the sign; abs(x) * signum(x) rebuilds x.
copySign(m, s)Gives m the sign of s.
Integer.signum(i)For int: returns the int -1, 0, or 1.
Double.compare(x, 0.0)Negative, zero, or positive int; orders -0.0 before 0.0.

Java Examples

Example 1 — Label Price Changes as Up, Down, or Flat

Turn daily price changes into a direction word.

Java
public class MathSignumTrend {
    public static void main(String[] args) {
        double[] changes = {2.75, -0.5, 0.0};
        for (double c : changes) {
            double s = Math.signum(c);
            String word = s > 0 ? "up" : (s < 0 ? "down" : "flat");
            System.out.println(c + " -> " + s + " (" + word + ")");
        }
    }
}

How It Works

  1. Only the direction survives. A rise of 2.75 and a rise of 0.01 would both give 1.0; the size of the change is thrown away.
  2. Three possible results. -0.5 gives -1.0 and 0.0 gives 0.0, so a simple pair of comparisons picks the right word.
  3. Double output. The sign values print as 1.0, -1.0, and 0.0 because signum(double) returns a double.

Example 2 — Move One Step at a Time Toward a Target

A game piece walks along a line until it reaches its target, one square per move.

Java
public class MathSignumStep {
    public static void main(String[] args) {
        int position = 2;
        int target = -1;
        while (position != target) {
            position += (int) Math.signum(target - position);
            System.out.println("moved to " + position);
        }
    }
}

How It Works

  1. Difference to direction. target - position is -3; its sign, -1, says "move left" no matter how far away the target is.
  2. int widens to float. There is no signum(int), so Java calls signum(float); the (int) cast turns -1.0f back into the step -1.
  3. Stops exactly. Each move shrinks the distance by one, so after three moves position equals target and the loop ends.

Example 3 — Negative Zero, NaN, Infinity, and float

The special inputs, plus the float overload.

Java
public class MathSignumSpecial {
    public static void main(String[] args) {
        System.out.println("tiny negative: " + Math.signum(-1e-300));
        System.out.println("-0.0:          " + Math.signum(-0.0));
        System.out.println("NaN:           " + Math.signum(Double.NaN));
        System.out.println("Infinity:      " + Math.signum(Double.POSITIVE_INFINITY));
        System.out.println("float -3.5f:   " + Math.signum(-3.5f));
    }
}

How It Works

  1. Size is irrelevant. -1e-300 is extremely close to zero but still negative, so the sign is -1.0.
  2. Unchanged inputs. For -0.0 and NaN the method returns its argument, so the minus sign on zero is kept.
  3. Infinity and float. Positive infinity is simply positive (1.0), and the float argument -3.5f uses the float overload, which prints -1.0.

Frequently Asked Questions

It returns -1.0 for negative numbers, 1.0 for positive numbers, and the argument itself for zero, negative zero, and NaN.
Because its parameters are float and double. Cast the result with (int), or use Integer.signum for int values, which returns an int.
It is -0.0. Zeros are returned unchanged, including the sign of negative zero.
Yes. The int is widened to float, so the float version runs and returns -1.0f, 0.0f, or 1.0f.
Java 5 (JDK 1.5).

Next: sin()

Sine of an angle given in radians.

sin() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful