Java signum Function

The Java signum Function is one of the Java Math Library functions, which is to find the Sign of a given expression, i.e., Positive, Negative, or Zero. In this article, we will show how to use Math.signum function in Java Programming language with example.

Java signum Function syntax

The basic syntax of the Math.signum Function in Java Programming language is as shown below:

Math.signum(data_type number);
  • If the number argument is a positive or negative Zero, the Java Math.signum function will return Zero as an output.
  • If the number argument is a Negative Number, the Math.signum function will return the Negative One(-1) as output.
  • When the number argument is a positive Number, Math.signum function will return the Positive One(+1) as the output.
  • If it is not a number, the Math.signum function will return NaN.

The Java Programming provides two different Math.signum functions to return the Sign of a given expression. The below function will accept positive or negative double value as an argument and returns the result

static double signum(double number); //Return type is double

// To use in program: 
Math.signum(double number);

The below function will accept positive or negative float value as an argument and returns the sign of the value

static float signum(float number); //Return Type is float

// To use in program: 
Math.signum(float number);

Java signum Function Example

The Math.signum function returns the Sign of a specified expression. In this Java example, We are going to check the same for different data types and display the output

// Java Math.signum Function
package MathFunctions;

public class SIgnumMethod {
 public static void main(String[] args) {
 double a = 44.5222, b = -698.6674;
 float c = 56.23f, d = - 80.90f; 

 System.out.println("\nSignum Function result of Zero: " + Math.signum(0));
 
 System.out.println("\nSignum Function of result Positive Double Value: " + Math.signum(a));
 System.out.println("Signum Function of result Negative Double Value: " + Math.signum(b));
 
 System.out.println("\nSignum Function of result Positive Float Value: " + Math.signum(c));
 System.out.println("Signum Function of result Negative Float Value: " + Math.signum(d));
 
 double e = Math.signum(10.90 + 15.10 - 20.50 + 1.50);
 System.out.println("\nSignum Function results = " + e);
 }
}
Java signum Function 1

First, We used this Java Function on Zero.

System.out.println("\nSignum Function result of Zero: " + Math.signum(0));

Next, We used the Java Math.signum Function on variable a and b (they belong to double type). It will call the signum method of double type ( static double signum(double number) ) to return the sign value.

System.out.println("\nNextUp result of Positive Doubles: " + Math.nextUp(a));
System.out.println("NextUp result of Negative Double Values: " + Math.nextUp(b));

Next, We used the signum Math function on variable c and d (they belong to float type). The below statements will call the signum method of float type ( static float signum(float number) ) to return the Sign value.

System.out.println("\nSignum Function of result Positive Float Value: " + Math.signum(c));
System.out.println("Signum Function of result Negative Float Value: " + Math.signum(d));

Lastly, variables of type Double was declared, and then we used the Math.signum function directly on expression.

double a = Math.signum(10.90 + 15.10 - 20.50 + 1.50);
System.out.println("Signum Function results = " + a);