📜  Java中的 StrictMath ulp() 方法及示例

📅  最后修改于: 2022-05-13 01:55:24.535000             🧑  作者: Mango

Java中的 StrictMath ulp() 方法及示例

ulp(双数)

ulp(double num)是Java中 StrictMath 类的内置方法,用于获取给定参数的 ulp 的大小。双精度值的ulp是该浮点值与下一个更大的双精度值之间的正距离。
一些特殊情况是:

  • 如果参数为NaN ,则结果为 NaN。
  • 如果参数是正无穷大或负无穷大,则结果是正无穷大。
  • 如果参数为正零或负零,则结果为Double.MIN_VALUE
  • 如果参数为 ±Double.MAX_VALUE,则结果等于2^971

句法:

public static double ulp(double num)

参数:该方法接受一个参数num为 double 类型的要返回其 ulp 的浮点值。
返回值: ulp 方法返回参数的 ulp 的大小
例子:

Input: num = 5.7
Output: 8.881784197001252E-16

Input: num = 0.0
Output: 4.9E-324

下面的程序说明了Java.lang.StrictMath.ulp() 方法:
方案一:

java
// Java program to illustrate the
// java.lang.StrictMath.ulp()
 
import java.lang.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        double num1 = 9.7, num2 = 4.9;
 
        // Get the size of an ulp of the argument
        // using ulp() method
        double ulp_Value = StrictMath.ulp(num1);
 
        // Print the size of the ulp
        System.out.println(" The Size of ulp of "
                           + num1 + " = "
                           + ulp_Value);
 
        // Get the size of an ulp of the argument
        // using ulp() method
        ulp_Value = StrictMath.ulp(num2);
 
        // Print the size of the ulp
        System.out.println(" The Size of ulp of "
                           + num2 + " = "
                           + ulp_Value);
    }
}


java
// Java program to illustrate the
// java.lang.StrictMath.ulp()
 
import java.lang.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        float num1 = 2.7f, num2 = -4.5f;
 
        // Get the size of an ulp of the argument
        // using ulp() method
        float ulp_Value = StrictMath.ulp(num1);
 
        // Print the size of the ulp
        System.out.println(" The Size of ulp of "
                           + num1 + " = "
                           + ulp_Value);
 
        // Get the size of an ulp of the argument
        // using ulp() method
        ulp_Value = StrictMath.ulp(num2);
 
        // Print the size of the ulp
        System.out.println(" The Size of ulp of "
                           + num2 + " = "
                           + ulp_Value);
    }
}


输出:
The Size of ulp of 9.7 = 1.7763568394002505E-15
 The Size of ulp of 4.9 = 8.881784197001252E-16

ulp(浮点数)

ulp(float num)是Java中 StrictMath 类的内置方法,用于获取给定参数的 ulp 的大小。浮点值的ulp是该浮点值与下一个较大的浮点值之间的正距离。
一些特殊情况是:

  • 如果参数为NaN ,则结果为 NaN。
  • 如果参数是正无穷大或负无穷大,则结果是正无穷大。
  • 如果参数为正零或负零,则结果为Float.MIN_VALUE
  • 如果参数为 ±Float.MAX_VALUE,则结果等于2^104

句法:

public static float ulp(float num)

参数:该方法接受一个参数num ,float类型,要返回ulp的浮点值。
返回值: ulp 方法返回参数的 ulp 的大小
例子:

Input: num = 5.7
Output: 8.881784197001252E-16

Input: num = 0.0
Output: 4.9E-324

下面的程序说明了Java.lang.StrictMath.ulp() 方法:
方案一:

Java

// Java program to illustrate the
// java.lang.StrictMath.ulp()
 
import java.lang.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        float num1 = 2.7f, num2 = -4.5f;
 
        // Get the size of an ulp of the argument
        // using ulp() method
        float ulp_Value = StrictMath.ulp(num1);
 
        // Print the size of the ulp
        System.out.println(" The Size of ulp of "
                           + num1 + " = "
                           + ulp_Value);
 
        // Get the size of an ulp of the argument
        // using ulp() method
        ulp_Value = StrictMath.ulp(num2);
 
        // Print the size of the ulp
        System.out.println(" The Size of ulp of "
                           + num2 + " = "
                           + ulp_Value);
    }
}
输出:
The Size of ulp of 2.7 = 2.3841858E-7
 The Size of ulp of -4.5 = 4.7683716E-7