Java signum() method with Examples

The java.lang.Math.signum() returns the Sign function of a value passed to it as argument. The signum() function returns the following values depending on the argument passed to it:

  • If the argument passed is greater than zero, then the signum() function will return 1.0.
  • If the argument passed is equal to zero, then the signum() function will return 0.
  • If the argument passed is less than zero, then the signum() function will return -1.0.

Note: If the argument passed is NaN, then the result is NaN. If the argument passed is positive zero or negative zero then the result will be same as that of the argument.

Syntax:



public static double signum(double d)
Parameter :
a : the value whose signum function is to be returned.
Return :
This method returns the signum function value of 
the argument passed to it.

Example 1 : To show working of java.lang.Math.signum() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working
// of java.lang.Math.signum() method
import java.lang.Math;
  
class Gfg {
  
    // driver code
    public static void main(String args[])
    {
        // when the argument is greater than zero
        double a = 30;
        System.out.println(Math.signum(a));
  
        // when the argument is equals to zero
        a = 0;
        System.out.println(Math.signum(a));
  
        // when the argument is less than zero
        a = -30;
        System.out.println(Math.signum(a));
    }
}

chevron_right


Output:

1.0
0
-1.0

Example 2 : To show working of java.lang.Math.signum() method when argument is NaN.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working
// of java.lang.Math.signum() method
import java.lang.Math; // importing java.lang package
  
public class GFG {
    public static void main(String[] args)
    {
  
        double nan = Double.NaN;
        double result;
  
        // Here argument is NaN, output will be NaN
        result = Math.signum(nan);
        System.out.println(result);
  
        // Here argument is positive zero
        result = Math.signum(0);
        System.out.println(result);
  
        // Here argument is negative zero
        result = Math.signum(-0);
        System.out.println(result);
    }
}

chevron_right


Output:

NaN
0.0
0.0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.