×

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

Applet Program in Java

Applet Program in Java An applet is a program that can be embedded in a web page. Applets programs are run by a web browser. It mainly works on the client-side....

3 minutes read.

JRE (Java Runtime Environment)

JRE is an installation package that provides an environment to run the Java program on any Operating System. It does not deal with the development process of any application. It is a part...

2 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

Java String subSequence() method

This method returns a new character sequence i.e. subsequence of current sequence Syntax: public CharSequence subSequence(int beginIndex, int endIndex) Parameter: beginIndex ? begin index, inclusive. endIndex ? end index, exclusive. Return: specified subsequnce Throws: It throws IndexOutOfBoundsException...

1 minute read.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 minutes read.

Davis Staircase Problem in Java

Davis has several stairs in his home and prefers to ascend one, two, or three steps at a time. As a highly clever youngster, he thinks about how many ways...

3 minutes read.

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the...

5 minutes read.

Creation of Multi Thread in java

What are threads in Java? We can use threads to facilitate parallel processing. Threads are helpful when you wish to execute several pieces of code concurrently. A thread is a small process...

3 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 callable example

What is callable in Java? Callable is an interface that is present in Java. There are two methods for constructing threads: one is to inherit the Thread class, while the other...

3 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

Why are generics used in Java

Java has a feature called generics that allows you to make a class, interface, and function accepting any (reference) type as a parameter. In other words, it is the idea...

4 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.

Dining Philosophers problem in Java

The Problem of the Dining Philosophers illustrates a concurrency issue involving the distribution of scarce resources among conflicting processes. Problem Statement Imagine a dining table with a circle in the middle and five...

3 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.

Beautiful Array in Java

In this section, we will be acknowledged about the beautiful array in Java, its characteristics, how it is achieved. What are the types of approaches and what are the tools...

8 minutes read.

ArrayList vs Vector in Java

ArrayList Vs. Vector in Java In Java, the two classes ArrayList and Vector both are associated with Java Collections Framework. Both classes implement java.util.List interface. Even so, these classes have noticeable...

4 minutes read.

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures. Example: If we want to know about the age of a person based on their date of birth,...

3 minutes read.