當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Math.ulp()用法及代碼示例

java.lang.Math.ulp() 返回參數的 ulp 的大小。 ulp 是最後一個單位。 float 或 double 值的 ulp 是給定值與下一個較大的值之間的正距離。

注意:對於非 NaN x,ulp(-x) == ulp(x)。

用法

public static double ulp(double x)
public static float ulp(float x)

參數

x = a floating-point value whose ulp is to be returned

返回

It returns the size of an ulp of the argument.
  • 如果參數為正或負雙精度或浮點值,則此方法將返回輸出。
  • 如果參數是正無窮大或負無窮大,此方法將返回正無窮大。
  • 如果參數為正或負零,則此方法將返回 Double.MIN_VALUE。
  • 如果參數是 Ä…Double.MAX_VALUE,此方法將返回等於 2971 的結果。
  • 如果參數是 Ä…Float.MAX_VALUE,此方法將返回等於 2104 的結果。
  • 如果參數為 NaN,則此方法將返回 NaN。

例子1

public class UlpExample1
{
    public static void main(String[] args) 
    {
        double a = 8.1;
        // Input positive double value, Output ulp(a)
        System.out.println(Math.ulp(a));
    }
}

輸出:

1.7763568394002505E-15

例子2

public class UlpExample2
{
    public static void main(String[] args) 
    {
        double a = -37.1;
        // Input negative double value, Output ulp(-a)==ulp(a)
        System.out.println(Math.ulp(a));
    }
}

輸出:

7.105427357601002E-15

例子3

public class UlpExample3
{
    public static void main(String[] args) 
    {
        float a = -1.0f / 0;
        // Input negative infinity, Output positive infinity
        System.out.println(Math.ulp(a));
    }
}

輸出:

Infinity

示例 4

public class UlpExample4
{
    public static void main(String[] args) 
    {
        double a = 0.0;
        // Input zero, Output Double.MIN_VALUE
        System.out.println(Math.ulp(a));
    }
}

輸出:

4.9E-324

例 5

public class UlpExample5
{
    public static void main(String[] args) 
    {
        double a = Double.MAX_VALUE ;
        // Input Double.MAX_VALUE, Output 2 power of 971
        System.out.println(Math.ulp(a));
    }
}

輸出:

1.9958403095347198E292

例 6

public class UlpExample6
{
    public static void main(String[] args) 
    {
        float a = Float.MAX_VALUE ;
        // Input Float.MAX_VALUE, Output 2 power of 104
        System.out.println(Math.ulp(a));
    }
}

輸出:

2.028241E31






相關用法


注:本文由純淨天空篩選整理自 Java Math.ulp() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。