nextAfter()方法语法:
public static double nextAfter(double starts , double directions); public static float nextAfter(float starts , double directions);
nextAfter()方法在java.lang包中可用。
nextAfter(double starts,double direction)方法用于在第二个参数(directions)的方向上返回与第一个参数(starts)相邻的double浮点数。
nextAfter(float starts,double direction)方法用于在第二个参数(directions)的方向上返回与第一个参数(starts)相邻的float型浮点数。
这些方法不会引发异常。这些是静态方法,可以使用类名进行访问,如果尝试使用类对象访问这些方法,则不会出现任何错误。
参数:
start –表示float或double类型的初始或起始浮点值。
Directions –它代表一个值,该值指示返回给定的第一个参数邻居(start的邻居)或start。
返回值:
该方法的返回类型为float / double-返回第二个参数方向上与开始位置相邻的float类型浮点数。
注意:
如果我们在两个参数中传递相同的值,则该方法将返回相同的值。
如果我们将Float.MIN_VALUE / Double.MIN_VALUE作为第一个参数传递,而第二个参数包含另一个值,则该方法将返回较小的值。
如果我们传递无穷大作为第一个参数,而第二个参数包含另一个值,则该方法将返回带有第一个参数符号的Float.MAX_VALUE / Double.MAX_VALUE。
如果我们将Float.MAX_VALUE / Double.MAX_VALUE作为第一个参数传递,而第二个参数包含另一个值,则该方法将返回带有第一个参数符号的最大值。
示例
//Java程序演示示例
//nextAfter()StrictMath类的方法
public class NextAfter {
public static void main(String[] args) {
//变量声明
double d1 = -2.6;
double d2 = 0.0;
double d3 = -0.6;
double d4 = 7.0 / 0.0;
float f1 = -2.6f;
float f2 = 0.0f;
double d5 = -7.0 / 0.0;
//显示d1,d2,d3和d4的先前值
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);
System.out.println("d3: " + d3);
System.out.println("d4: " + d4);
//显示f1,f2和d5的先前值
System.out.println("f1: " + f1);
System.out.println("f2: " + f2);
System.out.println("d5: " + d5);
System.out.println("nextAfter(double, double): ");
//在这里,我们得到(-2.5(大约)),因为我们是
//传递参数,其值为(-2.6,0.0)
System.out.println("StrictMath.nextAfter (d1,d2): " + StrictMath.nextAfter(d1, d2));
//在这里,我们将得到(-4.9(大约)),我们是
//传递参数,其值为(0.0,-2.6)
System.out.println("StrictMath.nextAfter (d2,d1): " + StrictMath.nextAfter(d2, d1));
//在这里,我们将得到(Double.MAX_VALUE),我们是
//传递参数的值为(7.0 / 0.0,0.0)
System.out.println("StrictMath.nextAfter (d4,d2): " + StrictMath.nextAfter(d4, d2));
//在这里,我们将得到(最大值),并且我们
//传递参数,其值为(0.0,7.0 / 0.0)
System.out.println("StrictMath.nextAfter (d2,d4): " + StrictMath.nextAfter(d2, d4));
System.out.println();
System.out.println("nextAfter(float, double): ");
//在这里,我们得到(-2.5(大约)),因为我们是
//传递参数的值为(-2.6f,0.0)
System.out.println("StrictMath. nextAfter (f1,d3): " + StrictMath.nextAfter(f1, d3));
//在这里,我们将得到(Float.MAX_VALUE),并且我们
//传递参数的值为(0.0f,-7.0 / 0.0)
System.out.println("StrictMath. nextAfter(f2,d5): " + StrictMath.nextAfter(f2, d5));
//在这里,我们将得到(-2.5(大约)),我们是
//传递参数的值为(-2.6f,0.0)
System.out.println("StrictMath. nextAfter(f1,d2): " + StrictMath.nextAfter(f1, d2));
//在这里,我们将得到(最小值),并且我们是
//传递参数的值为(0.0f,-7.0 / 0.0)
System.out.println("StrictMath. nextAfter(f2,d5): " + StrictMath.nextAfter(f2, d5));
}
}输出结果
d1: -2.6 d2: 0.0 d3: -0.6 d4: Infinity f1: -2.6 f2: 0.0 d5: -Infinity nextAfter(double, double): StrictMath.nextAfter (d1,d2): -2.5999999999999996 StrictMath.nextAfter (d2,d1): -4.9E-324 StrictMath.nextAfter (d4,d2): 1.7976931348623157E308 StrictMath.nextAfter (d2,d4): 4.9E-324 nextAfter(float, double): StrictMath. nextAfter (f1,d3): -2.5999997 StrictMath. nextAfter(f2,d5): -1.4E-45 StrictMath. nextAfter(f1,d2): -2.5999997 StrictMath. nextAfter(f2,d5): -1.4E-45