Home »
Java programming language
Java StrictMath signum() Method with Example
StrictMath Class signum() method: Here, we are going to learn about the signum() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on January 05, 2020
StrictMath Class signum() method
Syntax:
public static float signum(float fl);
public static double signum(double d);
- signum() Method is available in java.lang package.
- signum(float fl) Method is used to return the signum function of the given float argument type method. This is an odd mathematical function to extract the sign of the real number.
- signum(double d) Method is used to return the signum function of the given double argument type. This is an odd mathematical function to extract the sign of the real number.
- These methods don't throw an exception.
- These are static methods, it is accessible with the class name and, if we try to access these methods with the class object then we will not get any error.
Parameter(s):
- float / double – represents the value whose singnum function to be found.
Return value:
The return type of the method is float / double, it returns the signum function of the given argument.
Note:
- If we pass NaN, the method returns the same value (i.e. NaN).
- If we pass zero, the method returns the same value (o.e. 0) with the same sign.
- If we pass the value which is less than 0, the method returns -1.0.
- If we pass the value which is greater than 0, the method returns 1.0.
Example:
// Java program to demonstrate the example
// of signum() method of StrictMath class
public class Signum {
public static void main(String[] args) {
// variable declarations
float f1 = -0.0f;
float f2 = 0.0f;
float f3 = -0.6f;
float f4 = 2.0f;
double d1 = -0.0;
double d2 = 0.0;
double d3 = -0.6;
double d4 = 2.0;
System.out.println("signum(float fl): ");
// Here, we will get (-0.0) because we are passing
// parameter whose value is (-0.0f)
System.out.println("StrictMath.signum(f1): " + StrictMath.signum(f1));
// Here, we will get (0.0) and we are passing
// parameter whose value is (0.0f)
System.out.println("StrictMath.signum(f2): " + StrictMath.signum(f2));
// Here, we will get (-1.0) and we are passing
// parameter whose value is (-0.6f)
System.out.println("StrictMath.signum( f3): " + StrictMath.signum(f3));
// Here, we will get (1.0) and we are passing
// parameter whose value is (2.0f)
System.out.println("StrictMath.signum( f4): " + StrictMath.signum(f4));
System.out.println();
System.out.println("signum(double d): ");
// Here, we will get (-0.0) because we are passing
// parameter whose value is (-0.0)
System.out.println("StrictMath.signum(d1): " + StrictMath.signum(d1));
// Here, we will get (0.0) and we are passing
// parameter whose value is (0.0)
System.out.println("StrictMath.signum(d2): " + StrictMath.signum(d2));
// Here, we will get (-1.0) and we are passing
// parameter whose value is (-0.6)
System.out.println("StrictMath.signum(d3): " + StrictMath.signum(d3));
// Here, we will get (1.0) and we are passing
// parameter whose value is (2.0)
System.out.println("StrictMath.signum(d4): " + StrictMath.signum(d4));
}
}
Output
signum(float fl):
StrictMath.signum(f1): -0.0
StrictMath.signum(f2): 0.0
StrictMath.signum( f3): -1.0
StrictMath.signum( f4): 1.0
signum(double d):
StrictMath.signum(d1): -0.0
StrictMath.signum(d2): 0.0
StrictMath.signum(d3): -1.0
StrictMath.signum(d4): 1.0