×

Java Math nextUp() Method

The nextUp() method of Math class returns the floating-point number adjacent to the argument in direction of the positive infinity.

Syntax:

  1. public static double nextUp (double d)
  2. public static float nextUp (float f)

Parameters:

The parameters ‘d’ and ‘f’ represent the starting floating-point values.

Return Value:

The nextUp () method returns the floating-point number which is closer to positive infinity.

Special cases of the nextUp () method are as follows:

  • It returns NaN, is either argument passed is NaN.
  • It returns positive infinity, if the argument is positive infinity.
  • It returns Double.MIN_VALUE, is the argument is zero.

Example 1:

public class JavaMathNextUpExample1 {
    public static void main(String[] args) {
        double d = 2d;
        // returns the floating-point number which is closer to positive infinity
        System.out.println("Floating-point number : "+Math.nextUp(d));
    }
}

Output:

Floating-point number : 2.0000000000000004

Example 2:

public class JavaMathNextUpExample2 {
    public static void main(String[] args) {
        Float f = Float.NEGATIVE_INFINITY;
        // returns the floating-point number which is closer to positive infinity
        System.out.println("Floating-point number : "+Math.nextUp(f));
    }
}

Output:

Floating-point number : -3.4028235E38

Example 3:

public class JavaMathNextUpExample3 {
    public static void main(String[] args) {
        Double d = Double.POSITIVE_INFINITY;
        //returns positive infinity, if the argument is positive infinity
        System.out.println("Floating-point number : "+Math.nextUp(d));
    }
}

Output:

Floating-point number : Infinity

Example 4:

public class JavaMathNextUpExample4 {
    public static void main(String[] args) {
        Double d = Double.NaN;
        //returns NaN, is the argument is NaN.
        System.out.println("Floating-point number : "+Math.nextUp(d));
    }
}

Output:

Floating-point number : NaN

Example 5:

public class JavaMathNextUpExample5 {
    public static void main(String[] args) {
        Double d = 0.0d;
        //returns Double.MIN_VALUE, is the argument is zero.
        System.out.println("Floating-point number : "+Math.nextUp(d));
    }
}

Output:

Floating-point number : 4.9E-324

Related Topics

Bin Packing Problem Java

Assigning some items with known weights to bins with consistent capacities is necessary for the bin packing problem. The goal is to use the smallest possible boxes while ensuring everything is put...

6 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Stone Game in Java

In this tutorial, we will learn to design a stone game in Java. First of all, we will understand what is this game all about. We will grasp it through...

9 minutes read.

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.

XOR of Array Elements Except Itself in Java

A lot of the biggest IT organisations, including The Google, Amazon, TCS, and The Accenture, etc., question about this issue in their interview processes. By addressing the issue, one hopes to assess...

5 minutes read.

Java Hello World

Let’s start by writing a simple program that prints “Hello World” to the output window. Write the program into any text editor or IDE (Eclipse, Netbeans, etc.) and save the file with the...

2 minutes read.

Best Java IDE

Applications for desktop, workplace, smartphone, and the internet can be created using Java, one of the most popular programming languages. Java will undoubtedly be a popular programming language for so...

5 minutes read.

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.

Java Worker Class

What is a Worker? A service which executes Work - flow and Activities is referred to as a Worker. On user-controlled hosts, workers are defined and put into action. The Worker...

3 minutes read.

Bifunction in java 8

A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function...

4 minutes read.

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

Java Rename File

Renaming a file is the process of changing its name. Using the renameTo() function of the Java File class, renaming operations are possible. A file can be renamed using Java's renameTo()...

3 minutes read.

Java Transient

Java Transient In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the data of the instance as well as the...

3 minutes read.

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

Association in Java

In Java, association refers to the link between two classes established by their objects. One-to-one, one-to-many, and many-to-many connections are managed via association. The Association defines the multiplicity between objects...

5 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.