Java - Math ulp(float x) 方法



描述

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

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

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

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

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

宣告

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

public static float ulp(float f)

引數

f − 將返回其 ulp 的浮點值

返回值

此方法返回引數的 ulp 大小

異常

從正浮點值獲取 ulp 示例

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

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

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

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

輸出

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

Math.ulp(45.0)=3.8146973E-6

從負浮點值獲取 ulp 示例

以下示例演示了使用 Math ulp() 方法為負浮點值獲取值。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

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

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

輸出

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

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

從零浮點值獲取 ulp 示例

以下示例演示了使用 Math ulp() 方法獲取零浮點值的 ulp 值。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

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

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

輸出

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

Math.ulp(0.0)=1.4E-45
java_lang_math.htm
廣告
© . All rights reserved.