nextAfter() double in Java

nextAfter(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.nextAfter(double start, double direction)

This method takes two arguments of type double as its parameters. This method returns the floating-point number adjacent to the first argument in the direction of the second argument. If both arguments compare as equal the second argument is returned.

Special cases:

1. If either argument is a NaN, then NaN is returned.

2. If both arguments are signed zeros, the direction is returned unchanged.

3. If the start is ±Double.MIN_VALUE and direction have a value such that the result should have a smaller magnitude, than zero with the same sign as the start is returned.

4. If the start is infinite and direction has a value such that the result should have a smaller magnitude, Double.MAX_VALUE with the same sign as the start is returned.

5. If start is equal to ± Double.MAX_VALUE and direction have a value such that the result should have a larger magnitude, an infinity with the same sign as the start is returned.

Parameters: Two parameters are required for this method.

start: starting floating-point value.

direction: value indicating which of start's neighbors or start should be returned

Returns: The floating-point number adjacent to start in the direction of the direction.

For Example:

Math.nextAfter(199, 999) = > It returns 199.00000000000003

Approach

Java

public class NextAfterDouble {
    public static void main(String[] args) {

        double start = 199, direction = 999;
        System.out.println(Math.nextAfter(start, direction));

    }
}

Output:

199.00000000000003

No comments:

Post a Comment