Home » Java programming language

Java Math class signum() with method example

Math class signum() method in Java: Here, we are going to learn about the signum() method of Math class in Java programming language with its syntax and example.
Submitted by Preeti Jain, on September 16, 2019

Math class signum() method

  • signum() method is available in java.lang package.
  • signum() method is used to return the sign function of the given float argument type method. It is an odd mathematical function to extract the sign of the real number.
  • signum() method is static a method, it is accessible with the class name too.
  • signum() method method does not throw any exception.

Syntax:

    public static float signum(float value);
    public static double signum(double value);

Parameter(s):

  • value – represents the float/double floating-point value.

Return value:

The return type of this method is float/double, – it returns the sign function of the given argument.

Note:

  • If we pass "NaN", it returns the same value i.e. ("NaN").
  • If we pass zero (0 or -0), it returns the same value with the same sign.
  • If we pass the value less than 0, it returns the -1.0.
  • If we pass the value greater than 0, it returns the 1.0.

Java program to demonstrate example of signum() method

// Java program to demonstrate the example of 
// signum(float fl) method of Math Class

public class SignumMethod {
    public static void main(String[] args) {
        // Declaring the variables
        float f1 = -0.0f;
        float f2 = 0.0f;
        float f3 = -0.6f;
        float f4 = 2.0f;


        // Here , we will get (-0.0) because we are passing 
        // parameters whose value is (-0.0f)
        System.out.println("Math.signum(f1): " + Math.signum(f1));

        // Here , we will get (0.0) and we are passing 
        // parameters whose value is (0.0f)
        System.out.println("Math.signum(f2): " + Math.signum(f2));

        // Here , we will get (-1.0) and we are passing 
        // parameters whose value is (-0.6f)
        System.out.println("Math.signum(f3): " + Math.signum(f3));

        // Here , we will get (1.0) and we are passing 
        // parameters whose value is (2.0f)
        System.out.println("Math.signum(f4): " + Math.signum(f4));
    }
}

Output

E:\Programs>javac SignumMethod.java
E:\Programs>java SignumMethod
Math.signum(f1): -0.0
Math.signum(f2): 0.0
Math.signum(f3): -1.0
Math.signum(f4): 1.0

Example 2:

// Java program to demonstrate the example of 
// signum(double do) method of Math Class

public class SignumMethod {
    public static void main(String[] args) {
        // Declaring the variables
        double d1 = -0.0f;
        double d2 = 0.0f;
        double d3 = -0.6f;
        double d4 = 2.0f;

        // Here , we will get (-0.0) because we are passing 
        // parameters whose value is (-0.0f)
        System.out.println("Math.signum(d1): " + Math.signum(d1));

        // Here , we will get (0.0) and we are passing 
        // parameters whose value is (0.0f)
        System.out.println("Math.signum(d2): " + Math.signum(d2));

        // Here , we will get (-1.0) and we are passing 
        // parameters whose value is (-0.6f)
        System.out.println("Math.signum(d3): " + Math.signum(d3));

        // Here , we will get (1.0) and we are passing 
        // parameters whose value is (2.0f)
        System.out.println("Math.signum(d4): " + Math.signum(d4));
    }
}

Output

E:\Programs>javac SignumMethod.java
E:\Programs>java SignumMethod
Math.signum(f1): -0.0
Math.signum(f2): 0.0
Math.signum(f3): -1.0
Math.signum(f4): 1.0



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.