Java - StrictMath ulp(double x) 方法



描述

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

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

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

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

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

宣告

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

public static double ulp(double d)

引數

d - 需要返回其 ulp 的浮點值

返回值

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

異常

示例:獲取正雙精度值的 ulp

以下示例顯示了對正雙精度值使用 StrictMath ulp() 方法。

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

      // get a double number
      double x = 45.0;

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

輸出

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

StrictMath.ulp(45.0)=7.105427357601002E-15

示例:獲取負雙精度值的 ulp

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

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

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

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

輸出

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

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

示例:獲取零雙精度值的 ulp

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

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

      // get a double number
      double x = 0.0;

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

輸出

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

StrictMath.ulp(0.0)=4.9E-324
java_lang_strictmath.htm
廣告