Java - StrictMath ulp(float x) 方法



描述

Java StrictMath ulp(float f) 方法返回引數的 ulp 大小。浮點值的 ulp 是該浮點值與下一個更大數量級的浮點值之間的正距離。請注意,對於非 NaN 的 x,ulp(-x) == ulp(x)。特殊情況:

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

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

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

  • 如果引數是 Float.MAX_VALUE,則結果等於 2104

宣告

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

public static float ulp(float f)

引數

f − 將返回其 ulp 的浮點值

返回值

此方法返回引數的 ulp 大小

異常

示例:獲取正浮點值的 ulp

以下示例演示了對正浮點值使用 StrictMath ulp() 方法。

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

      // get a float number
      float x = 45.0f;

      // print the radian for this float
      System.out.println("StrictMath.ulp(" + x + ")=" + StrictMath.ulp(x));
   }
}

輸出

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

StrictMath.ulp(45.0)=3.8146973E-6

示例:獲取負浮點值的 ulp

以下示例演示瞭如何使用 StrictMath ulp() 方法來獲取負浮點值的 ulp。

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

      // get a float number
      float x = -45.0f;

      // print the radian for this float
      System.out.println("StrictMath.ulp(" + x + ")=" + StrictMath.ulp(x));
   }
}

輸出

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

StrictMath.ulp(-45.0)=3.8146973E-6

示例:獲取零浮點值的 ulp

以下示例演示瞭如何使用 StrictMath ulp() 方法來獲取零浮點值的 ulp。

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

      // get a float number
      float x = 0.0f;

      // print the radian for this float
      System.out.println("StrictMath.ulp(" + x + ")=" + StrictMath.ulp(x));
   }
}

輸出

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

StrictMath.ulp(0.0)=1.4E-45
java_lang_strictmath.htm
廣告