Java Math nextAfter() 方法
定义和用法
nextAfter()
方法返回在方向 y
上与数字 x
相邻的浮点数。
如果 y
大于 x
,则它会查找大于 x
的最小可能浮点数。如果 y
小于 x
,则它会查找小于 x
的最大可能浮点数。如果 y
等于 x
,则此方法返回 x
。
相比 float
类型的参数,double
类型的参数的返回值将更接近 x。
实例
在不同方向上为不同的数字查找下一个浮点数:
System.out.println(Math.nextAfter(1, 2)); System.out.println(Math.nextAfter(1, 0)); System.out.println(Math.nextAfter(0.5f, 1.0f)); System.out.println(Math.nextAfter(0.5f, 0.0f));
语法
以下之一:
public static double nextAfter(double x, double y) public static float nextAfter(float x, double y)
参数
参数 | 描述 |
---|---|
x | 必需。起始数字。 |
y | 必需。迈向的方向。 |
技术细节
返回: | 表示从起点沿指定方向的下一个浮点数的 double 或 float 值。 |
---|---|
Java 版本: | 1.6+ |