signum
function?Any real number has a magnitude and a sign associated with it. A real number’s sign, also known as the signum, is -1 for
The signum()
method in the Math
class in Java works as follows:
Returns 1
if the number passed is greater than zero.
Returns 0
if the number passed is equal to zero.
Returns -1
if the number passed is less than zero.
Returns NaN
if the number passed is NaN
public static double signum(double d)
double d
: the value whose signum
is to be returned.
public static double signum(double d)
public static float signum(float f)
In the code below, we calculate the signum for four different values with the help of the signum()
function.
public class Main {public static void main(String[] args) {System.out.println("Signum for -1000 is " + Math.signum(-1000));System.out.println("Signum for 0 is " + Math.signum(0));System.out.println("Signum for 1000 is " + Math.signum(1000));System.out.println("Signum for NaN is " + Math.signum(Double.NaN));}}