Java - StrictMath nextUp(double x) 方法



描述

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

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

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

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

宣告

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

public static double nextUp(double d)

引數

d - 起始浮點值

返回值

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

異常

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

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

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

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

輸出

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

StrictMath.nextUp(154.28764)=154.28764000000004

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

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

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

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

輸出

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

StrictMath.nextUp(0.0)=4.9E-324

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

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

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

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

輸出

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

StrictMath.nextUp(-154.28764)=-154.28763999999998
java_lang_strictmath.htm
廣告

© . All rights reserved.