How to Get the Floating-point Number Adjacent to The First Argument in Java


In this example, we will use Math.nextAfter() to get the floating-point number adjacent to the first argument in the direction of the second argument.

Source Code

package com.beginner.examples;

public class NextAferExample {

    public static void main(String[] args){
    	//return the floating-point number adjacent to the first argument in the direction of the second argument.
    	double a = -10.8;
    	int d1 = 5;
    	double b = 0;
    	int d2 = 6;
    	double c = 2.01;
    	int d3 = 7;
    	System.out.println("nextAfter of " + a + " in the direction of " + d1 + " is: " + Math.nextAfter(a, d1));
        System.out.println("nextAfter of " + b + " in the direction of " + d2 + " is: " + Math.nextAfter(b, d2));
        System.out.println("nextAfter of " + c + " in the direction of " + d3 + " is: " + Math.nextAfter(c, d3));
    }
}

Output:

nextAfter of -10.8 in the direction of 5 is: -10.799999999999999
nextAfter of 0.0 in the direction of 6 is: 4.9E-324
nextAfter of 2.01 in the direction of 7 is: 2.0100000000000002
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments