Java - Math nextUp(float x) 方法



描述

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

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

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

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

宣告

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

public static float nextUp(float d)

引數

d − 起始浮點值

返回值

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

異常

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

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

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

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

輸出

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

Math.nextUp(154.28764)=154.28766

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

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

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

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

輸出

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

Math.nextUp(0.0)=1.4E-45

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

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

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

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

輸出

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

Math.nextUp(-154.28764)=-154.28763
java_lang_math.htm
廣告
© . All rights reserved.