Java - Math nextUp(double x) 方法



描述

Java Math nextUp(double d) 方法返回正無窮大方向上緊鄰 d 的浮點值。此方法在語義上等效於 nextAfter(d, Double.POSITIVE_INFINITY); 但是,nextUp 實現可能比其等效的 nextAfter 呼叫執行得更快。特殊情況 -

  • 如果引數為 NaN,則結果為 NaN。

  • 如果引數為正無窮大,則結果為正無窮大。

  • 如果引數為零,則結果為 Double.MIN_VALUE

宣告

以下是 java.lang.Math.nextUp() 方法的宣告

public static double nextUp(double d)

引數

d - 起始浮點值

返回值

此方法返回更靠近正無窮大的相鄰浮點值。

異常

示例:獲取正雙精度值的下一個值

以下示例演示了對正值使用 Math nextUp() 方法。

package com.tutorialspoint;
public class MathDemo {
   public static void main(String[] args) {

      // get a number
      double x = 154.28764;
   
      // print the next number for x
      System.out.println("Math.nextUp(" + x + ")="
         + Math.nextUp(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果 -

Math.nextUp(154.28764)=154.28764000000004

示例:獲取零雙精度值的下一個值

以下示例演示了對零值使用 Math nextUp() 方法。

package com.tutorialspoint;
public class MathDemo {
   public static void main(String[] args) {

      // get a number
      double x = 0.0;
   
      // print the next number for x
      System.out.println("Math.nextUp(" + x + ")="
         + Math.nextUp(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果 -

Math.nextUp(0.0)=4.9E-324

示例:獲取負雙精度值的下一個值

以下示例演示了對負值使用 Math nextUp() 方法。

package com.tutorialspoint;
public class MathDemo {
   public static void main(String[] args) {

      // get a number
      double x = -154.28764;
   
      // print the next number for x
      System.out.println("Math.nextUp(" + x + ")="
         + Math.nextUp(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果 -

Math.nextUp(-154.28764)=-154.28763999999998
java_lang_math.htm
廣告