当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Math.nextUp()用法及代码示例


  • java.lang.Math.nextUp() 是 java 中内置的数学方法。它返回在正无穷大方向上与用户指定参数 (d) 相邻的浮点值。此方法等效于 nextAfter(d, Double.POSITIVE_INFINITY) 方法。
  • nextUp 的实现可能比等效的 nextAfter 调用运行得更快。

用法:

public static double nextUp(double a)
public static float nextUp(float a)

参数

a = starting floating-point value

返回:

This method returns the adjacent floating-point value closer to positive infinity.
  • 如果参数是 NaN,则此方法将返回 NaN。
    • 如果参数为零并且我们正在处理 double,则此方法将返回 Double.MIN_VALUE。
    • 如果参数为零并且我们正在处理浮点数,则此方法将返回 Float.MIN_VALUE。
    • 如果参数是 Positive Infinity,则此方法将返回 Positive Infinity。

例子1

public class NextUpExample1
{
    public static void main(String[] args) 
    {
         float x = 0.0f / 0;
        // Input NaN, Output NaN
        System.out.println(Math.nextUp(x));
    }
}

输出:

NaN

例子2

public class NextUpExample2
{
    public static void main(String[] args) 
    {
        double x = 744.93;
        // Input double value, Output adjacent floating-point
        System.out.println(Math.nextUp(x));
    }
}

输出:

744.9300000000001

例子3

public class NextUpExample3 
{
    public static void main(String[] args) 
    {
        float a = 328.7f;
        // Input float value, Output adjacent floating-point
        System.out.println(Math.nextUp(a));
    }
}

输出:

328.70004

示例 4

public class NextUpExample4 
{
    public static void main(String[] args) 
    {
         float x = 0.0f;
        // Input zero, Output Float.MIN_VALUE for float
        System.out.println(Math.nextUp(x));
    }
}

输出:

1.4E-45

例 5

public class NextUpExample5
{
    public static void main(String[] args) 
    {
        double b = 0.0;
        // Input zero, Output Double.MIN_VALUE for double
        System.out.println(Math.nextUp(b));
    }
}

输出:

4.9E-324






相关用法


注:本文由纯净天空筛选整理自 Java Math.nextUp() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。