AppDividend
Latest Code Tutorials

# Java Math signum() Function: The Complete Guide

Java Math signum() function is used to return a signum function value for any passed value(double or float).

In mathematics,

1. The signum function value for any positive number is 1.
2. The signum function value for any negative number is -1.
3. The signum function value for zero is 0.

## Java Math signum()

Java Math signum() is a built-in function that returns the sign function of a value passed to it as an argument. If an argument passed is greater than zero, the signum() function will return 1.0. If an argument passed equals zero, the signum() function will return 0.

### Syntax

```public static double signum(double x)
public static float signum(float x)
```

### Parameter(s)

The variable whose signum value is to be returned.

### Return Value

• If the passed value is positive, returns 1.0;
• If the passed value is negative, returns -1.0;
• If the passed value is zero, returns 0.0;

See the following figure.

#### Note

1. If an argument is NaN, then this method returns NaN.
2. If an argument is positive, zero, or negative, this method returns the same pass.

Consider the following examples.

#### Example1.java: The following example demonstrates the use of this method.

```public class Example1 {
public static void main(String[] args) {
double a = 2.0;
double b = -2.0;
double c = 0.0;

System.out.println(Math.signum(a));
System.out.println(Math.signum(b));
System.out.println(Math.signum(c));

float x = 2.0f;
float y = -2.0f;
float z = 0.0f;

System.out.println(Math.signum(x));
System.out.println(Math.signum(y));
System.out.println(Math.signum(z));
}
}
```

#### Output

```->javac Example1.java
->java Example1
1.0
-1.0
0.0
1.0
-1.0
0.0
```

#### Example2.java: The following example demonstrates the situation when the argument is NaN.

```public class Example2 {
public static void main(String[] args) {
double a = Double.NaN;
System.out.println(Math.signum(a));
}
}
```

#### Output

```->javac Example2.java
->java Example2
NaN
```

#### Example3.java: The following example demonstrates the situation of passing negative or positive zero.

```public class Example3 {
public static void main(String[] args) {
double a = 0.0;
double b = -0.0;

System.out.println(Math.signum(a));
System.out.println(Math.signum(b));

}
}
```

#### Output

```->javac Example3.java
->java Example3
0.0
-0.0```