Java - Math ulp(double x) 方法



描述

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

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

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

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

  • 如果引數是 Double.MAX_VALUE,則結果等於 2971

宣告

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

public static double ulp(double d)

引數

d - 要返回其 ulp 的浮點值

返回值

此方法返回引數的 ulp 的大小

異常

從正雙精度值獲取 ulp 示例

以下示例顯示了 Math ulp() 方法在正雙精度值上的用法。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = 45.0;

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

輸出

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

Math.ulp(45.0)=7.105427357601002E-15

從負雙精度值獲取 ulp 示例

以下示例顯示了 Math ulp() 方法用於獲取負雙精度值的用法。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = -45.0;

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

輸出

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

Math.ulp(-45.0)=7.105427357601002E-15

從零雙精度值獲取 ulp 示例

以下示例顯示了 Math ulp() 方法用於獲取零雙精度值的用法。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = 0.0;

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

輸出

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

Math.ulp(0.0)=4.9E-324
java_lang_math.htm
廣告