×

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

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

Upcasting and Downcasting in Java

Type casting in Java is an important and very interesting topic to deal with. But here upcasting and downcasting is somewhat related to typecasting. In normal typecasting, we convert from...

6 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

Java Strings

String class is the most used class in Java programming language. The string is the sequence of characters, which is treated as objects in Java. Creating String objects We create String objects...

5 minutes read.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

2 minutes read.

Java SE vs EE

Java : Java is an independent platform. It works on any kind of operating system. We use java to develop and to focus on large or major projects. The goal of...

3 minutes read.

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.

Java Framework List

The framework is the programs which are written in Java. Frameworks in Java are used to create web applications. The code which can reuse can act as a reference for...

6 minutes read.

Block Swap Algorithm for array rotation in Java

An array and r, the rotation factor by which the array must be rotated, are both provided to us. We must then return the rotated array. A well-known and popular method is...

4 minutes read.

How to Convert String to double in Java

How to Convert String to double in java It is used if we have to perform mathematical operations on the string that contains a double number. When we get data from...

3 minutes read.

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 minutes read.

History and Evolution of Java

Java is invented by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Java is related to C++, which is inherited from...

3 minutes read.

Java Math nextAfter() Method

The nextAfter() method of Math class returns the floating-point value adjacent to the first argument in direction of the second argument. Syntax: public static double nextAfter (double start, double direction)public static float...

2 minutes read.

Java Integer numberOfTrailingZeros() method

The numberOfTrailingZeros()  method of Java Integer class returns the total number of zero bits following the lowest-order one-bit in the 2’s complement binary representation of the specified int value. Syntax public static...

1 minute read.

Zig Zag star and Number Pattern in Java

We covered many Java pattern applications in the preceding part. We will write Java applications for zigzag star and number patterns in this part. Printing Zig Zag Number Pattern Steps Print one...

3 minutes read.

Command Class in Java

We use the command class to run commands against the database. The Command class may find a set of parameter objects for use in sending values to a stored procedure...

3 minutes read.

Java vs Go

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

Synchronized Keyword in Java

Synchronization is the process of limiting access to a shared resource or data to a single thread at a given moment in time. This aids in shielding the data from...

6 minutes read.