當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。