×

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value.

Syntax:

  1. public static double signum(double d)
  2. public static float signum (float d)

Parameters:

The parameter ‘d’ represents the floating-point value whose signum is to be determined.

Return Value:

The signum() method returns the signum function of ‘d’.

  • It returns zero with same sign as argument, if the argument passed is zero.
  • It returns 1.0, if the argument is greater than 0.
  • It returns -1.0, if the argument is less than 0.
  • It returns NaN. If the argument is NaN.

Example 1:

public class JavaMathSignumExample1 {
    static  int i=1;
    public static void main(String[] args) {
        double d=0.1;
        //returns 1.0, if d is greater than 0
        System.out.println(i++ +". Signum Value for "+d+" is : " +Math.signum(d));
    }
}

Output:

Signum Value for 0.1 is : 1.0

Example 2:

public class JavaMathSignumExample2 {
    public static void main(String[] args) {
        double d=-1.1;
        //returns -1.0, if d is less than 0
        System.out.println(" Signum Value for "+d+" is : " +Math.signum(d));
    }
}

Output:

Signum Value for -1.1 is : -1.0

Example 3:

public class JavaMathSignumExample3 {
    public static void main(String[] args) {
        double d=-0d;
        //returns 0 with the same sign as the argument, if d is equal to positive or negative 0
        System.out.println(" Signum Value for "+d+" is : " +Math.signum(d));
    }
}

Output:

Signum Value for -0.0 is : -0.0

Example 4:

public class JavaMathSignumExample4 {
    public static void main(String[] args) {
        double d=Double.NaN;
        //returns NaN, if d is NaN
        System.out.println(" Signum Value for "+d+" is : " +Math.signum(d));
    }
}

Output:

Signum Value for NaN is : NaN

Related Topics

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Java StringReader Class

StreamReader Class technique enables character reading from a string. The java.io package contains this class. A string serves as a source in the character stream. While the Stream class is...

4 minutes read.

Java Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.

How to Convert String to Object in Java

How to Convert String to Object in Java The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to...

3 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

Java Tokens

Classes and methods are included in the Java program. The procedures also provide the expressions and statements required to finish a specific operation. Tokens make up the sentences and expressions...

4 minutes read.

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language. Introduction Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs...

4 minutes read.

Generics in Java

Generics in Java Parameterizedtypes mean generic. Generics allow types (Character, Integer, String, …, etc., as well as user-defined types) to act as parameters to interfaces, classes, and methods. Generics in Java...

9 minutes read.

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.

Sum of digits in string in java

To find the sum of all digits in a string, you need to traverse through the string one by one character; if the character is an integer, you need to...

2 minutes read.

Java List Node

In Java, List Node is the same as the single linked list, which is the collection of nodes. So, we can say, the list nodes are grouped together to get...

8 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

Vectors in Java

Vector Class We may make resizable arrays comparable to the ArrayList class using the Vector class, which implements the List interface. A vector is similar to a dynamic collection that can...

4 minutes read.

Method chaining in Java

Method chaining in Java is the act of calling a series of methods one after the other. The sole distinction between it and function Object () constructor chaining is the difference...

3 minutes read.

Java Char Keyword

The java char keyword is a data type which is defined as character data type.Char keyword belongs to a primitive data type where the data types are classified into primitive...

4 minutes read.