Java signum Function

The Java signum Function is one of the 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 an example.

Java signum 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 Math.signum function will return Zero as an output.
  • If the number argument is a Negative Number, it will return the Negative One(-1) as output.
  • When a number argument is a positive number, it will return the Positive One(+1) as the output.
  • If it is not a number, it will return NaN.

Java Programming provides two different Math.signum functions to return the Sign of a given expression. The below function will accept a 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.

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.

Next, We used the Java Math.signum Function on variables a and b (they belong to double type). It will call the double type ( static double signum(double number) ) method 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 variables c and d (they belong to float type). The below statements will call the float type method ( 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 were declared, and then we used the Math.signum function directly on expression.

double a = Math.signum(10.90 + 15.10 - 20.50 + 1.50);