Java - StrictMath nextUp(float x) 方法



描述

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

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

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

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

宣告

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

public static float nextUp(float d)

引數

d − 起始浮點值

返回值

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

異常

示例:獲取正浮點值的下一個值

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

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

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

輸出

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

StrictMath.nextUp(154.28764)=154.28766

示例:獲取零浮點值的下一個值

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

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

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

輸出

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

StrictMath.nextUp(0.0)=1.4E-45

示例:獲取負浮點值的下一個值

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

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

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

輸出

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

StrictMath.nextUp(-154.28764)=-154.28763
java_lang_strictmath.htm
廣告
© . All rights reserved.