How to Get the Floating-point Value Adjacent to the Given Number in Java


In this example, we will learn how to get the floating-point value adjacent to the given number in the direction of positive infinity with Math.nextUp(). In other words, the output value will be greater than the input value by the slightest amount.

Source Code

package com.beginner.examples;

public class NextUpExample {

    public static void main(String[] args){
    	//return the floating-point value adjacent to the given number in the direction of positive infinity.
    	double a = -10.8;
    	double b = 0;
    	double c = 2.01;
    	System.out.println("nextUp of " + a + " is: " + Math.nextUp(a));
        System.out.println("nextUp of " + b + " is: " + Math.nextUp(b));
        System.out.println("nextUp of " + c + " is: " + Math.nextUp(c));
    }
}

Output:

nextUp of -10.8 is: -10.799999999999999
nextUp of 0.0 is: 4.9E-324
nextUp of 2.01 is: 2.0100000000000002
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments