Java signum() method with Examples


The java.lang.Math.signum(float f) returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.

Let us now see an example −

Example

import java.security.*;
import java.util.*;
public class Main {
   public static void main(String[] argv) {
      // get two float numbers
      float x = 50.14f;
      float y = -4f;
      // call signum for both floats and print the result
      System.out.println("Math.signum(" + x + ")=" + Math.signum(x));
      System.out.println("Math.signum(" + y + ")=" + Math.signum(y));
   }
}

Output

Math.signum(50.14)=1.0
Math.signum(-4.0)=-1.0

Let us now see another example −

Example

import java.security.*;
import java.util.*;
public class Main {
   public static void main(String[] argv) {
      double a = 5;
      double nanVal = Double.NaN;
      System.out.println(Math.signum(a));
      a = -10;
      System.out.println(Math.signum(a));
      a = 0;
      System.out.println(Math.signum(a));
      System.out.println(Math.signum(nanVal));
   }
}

Output

1.0
-1.0
0.0
NaN

Updated on: 23-Sep-2019

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements