Java.lang.StrictMath.nextAfter() 方法
描述
java.lang.StrictMath.nextAfter(double start, double direction) 方法返回第二个参数方向上与第一个参数相邻的浮点数。 如果两个参数比较相等,则返回第二个参数。它包括这些情况 −
- 如果任一参数是 NaN,则返回 NaN。
- 如果两个参数都是有符号的零,direction 原样返回。
- 如果 start 是 ±Double.MIN_VALUE 并且 direction 的值应使结果的幅度更小,则返回与 start 符号相同的零。
- 如果 start 是无限的,并且 direction 的值使得结果的大小应该更小,则返回与 start 符号相同的 Double.MAX_VALUE。
- 如果 start 等于 ±Double.MAX_VALUE 并且方向的值使得结果应该具有更大的量级,则返回与 start 符号相同的无穷大。
声明
以下是 java.lang.StrictMath.nextAfter() 方法的声明。
public static double nextAfter(double start, double direction)
参数
start − 这是起始浮点值
direction − 这是指示应该返回 start 的哪个邻居或 start 的值
返回值
该方法返回与开始方向相邻的浮点数。
异常
NA
示例
下面的例子展示了 java.lang.StrictMath.nextAfter() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class StrictMathDemo {
public static void main(String[] args) {
double d1 = 102.2d, d2 = 0.0d;
/* returns the floating-point number adjacent to the first argument in the
direction of the second argument */
double retval = StrictMath.nextAfter(d1, 9.2d);
System.out.println("NextAfter = " + retval);
/* returns the floating-point number adjacent to the first argument in the
direction of the second argument */
retval = StrictMath.nextAfter(d2, 9.2d);
System.out.println("NextAfter = " + retval);
// returns 0 if both arguments is zero
retval = StrictMath.nextAfter(d2, 0.0d);
System.out.println("NextAfter = " + retval);
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
NextAfter = 102.19999999999999 NextAfter = 4.9E-324 NextAfter = 0.0

