Close

Java - Math.nextAfter() Examples

Java Java API 


Class:

java.lang.Math

java.lang.Objectjava.lang.Objectjava.lang.Mathjava.lang.MathLogicBig

Methods:

public static double nextAfter(double start,
                               double direction)

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.



public static float nextAfter(float start,
                              double direction)

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.


Examples


package com.logicbig.example.math;

public class NextAfterExample {

public static void main(String... args) {
findNextAfter(41.23, 15.15);
findNextAfter(0.0, 0.0);
findNextAfter(0.0 / 0, -1.12);
findNextAfter(12.7, Double.POSITIVE_INFINITY);
findNextAfter(Double.MIN_VALUE, Double.NEGATIVE_INFINITY);
findNextAfter(Double.MAX_VALUE, Double.POSITIVE_INFINITY);
}

private static void findNextAfter(double x, double y) {
double nextAfter = Math.nextAfter(x, y);
System.out.printf("Math.nextAfter(%s,%s) = %s%n", x, y, nextAfter);
}
}

Output

Math.nextAfter(41.23,15.15) = 41.22999999999999
Math.nextAfter(0.0,0.0) = 0.0
Math.nextAfter(NaN,-1.12) = NaN
Math.nextAfter(12.7,Infinity) = 12.700000000000001
Math.nextAfter(4.9E-324,-Infinity) = 0.0
Math.nextAfter(1.7976931348623157E308,Infinity) = Infinity




package com.logicbig.example.math;

public class NextAfterExample2 {

public static void main(String... args) {
findNextAfter(12.75f, 45.13);
findNextAfter(0.0f, 0.0);
findNextAfter(0.0f / 0, -2.75);
findNextAfter(12.7f, Double.POSITIVE_INFINITY);
findNextAfter(Float.MIN_VALUE, Double.NEGATIVE_INFINITY);
findNextAfter(Float.MAX_VALUE, Double.POSITIVE_INFINITY);
}

private static void findNextAfter(float x, double y) {
float nextAfter = Math.nextAfter(x, y);
System.out.printf("Math.nextAfter(%s,%s) = %s%n", x, y, nextAfter);
}
}

Output

Math.nextAfter(12.75,45.13) = 12.750001
Math.nextAfter(0.0,0.0) = 0.0
Math.nextAfter(NaN,-2.75) = NaN
Math.nextAfter(12.7,Infinity) = 12.700001
Math.nextAfter(1.4E-45,-Infinity) = 0.0
Math.nextAfter(3.4028235E38,Infinity) = Infinity




See Also