.signum()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Oct 31, 2022
Contribute to Docs

The Math.signum() method determines whether a number is positive, negative, zero, or NaN.

Syntax

Math.signum(num)

The .signum() method takes a num parameter and returns a double value that indicates whether num is positive, negative, zero, or NaN.

Some cases to consider include the following:

  • If num is positive, a positive non-zero value is returned.
  • If num is negative, a negative non-zero value is returned.
  • If num is zero, zero is returned.
  • If the value of num is NaN, NaN is returned.

Example

The following example demonstrates the .signum() method and its many use cases:

// Test.java
public class Test {
public static void main(String[] args) {
System.out.println(Math.signum(8.75));
System.out.println(Math.signum(-3.5));
System.out.println(Math.signum(0));
System.out.println(Math.signum(Double.NaN));
}
}

This will produce the following output:

1.0
-1.0
0
NaN

All contributors

Looking to contribute?

Learn Java on Codecademy