Java.lang Package Classes

Java Math - nextAfter() Method



The java.lang.Math.nextAfter() method returns the floating-point number adjacent to the first argument in the direction of the second argument. If both arguments compare as equal a value equivalent to the second argument is returned. In special cases it returns the following:

  • If either argument is a NaN, then NaN is returned.
  • If both arguments are signed zeros, a value equivalent to direction is returned.
  • If start is infinite and direction has a value such that the result should have a smaller magnitude, Float.MAX_VALUE with the same sign as start is returned.
  • If start is ±Float.MIN_VALUE and direction has a value such that the result should have a smaller magnitude, then a zero with the same sign as start is returned.
  • If start is equal to ±Float.MAX_VALUE and direction has a value such that the result should have a larger magnitude, an infinity with same sign as start is returned.

Syntax

public static float nextAfter(float start,
                              double direction)

Parameters

start Specify starting floating-point value.
direction Specify value for direction.

Return Value

Returns the floating-point number adjacent to the first argument in the direction of the second argument.

Exception

NA.

Example:

In the example below, nextAfter() method returns the floating-point number adjacent to the first argument in the direction of the second argument.

import java.lang.*;

public class MyClass {
 public static void main(String[] args) {
  System.out.println(Math.nextAfter(2.55f, 4)); 
  System.out.println(Math.nextAfter(10.1f, 3));   
 }
}

The output of the above code will be:

2.5500002
10.099999

❮ Java.lang - Math