ulp通信
数学类ulp()方法 (Math class ulp() method)
ulp() method is available in java.lang package.
ulp()方法在java.lang包中可用。
ulp() method is used to return the size of a ulp of the given argument, where, a ulp of the given value is the positive distance between floating-point value and the value next larger in magnitude.
ulp()方法用于返回给定参数的ulp的大小,其中,给定值的ulp是浮点值与下一个幅度较大的值之间的正距离。
ulp() method is a static method, it is accessible with the class name too.
ulp()方法是静态方法,也可以使用类名进行访问。
ulp() method does not throw any exception.
ulp()方法不会引发任何异常。
Syntax:
句法:
public static float ulp(float value);
public static double ulp(double value);
Parameter(s):
参数:
value – represents the float/double floating-point value whose ulp is to be returned.
value –表示要返回其ulp的浮点/双浮点值。
Return value:
返回值:
The return type of this method is float/double – it returns the size of a ulp.
此方法的返回类型为float / double-返回ulp的大小。
Note:
注意:
If we pass "NaN", it returns the same value (i.e. "NaN").
如果我们传递“ NaN”,它将返回相同的值(即“ NaN”)。
If we pass an infinity (+ve or –ve), it returns the infinity.
如果我们传递无穷大(+ ve或–ve),它将返回无穷大。
If we pass a zero (0 or -0), it returns the "Float.MIN_VALUE" / "Double.MIN_VALUE".
如果我们传递零(0或-0),它将返回“ Float.MIN_VALUE” /“ Double.MIN_VALUE”。
If we pass "Float.MAX_VALUE", it returns the 2^104 and in the case of "Double.MAX_VALUE", it returns the 2^971.
如果我们传递“ Float.MAX_VALUE”,则返回2 ^ 104;对于“ Double.MAX_VALUE”,则返回2 ^ 971。
Java程序演示ulp()方法的示例 (Java program to demonstrate example of ulp() method)
// Java program to demonstrate the example of
// ulp(float fl) method of Math Class
public class UlpFloatTypeMethod {
public static void main(String[] args) {
// declaring the variables
float f1 = 0.0f;
float f2 = -0.0f;
float f3 = 7.0f / 0.0f;
float f4 = -7.0f / 0.0f;
float f5 = 1285.45f;
// Display the values
System.out.println("f1: " + f1);
System.out.println("f2: " + f2);
System.out.println("f3: " + f3);
System.out.println("f4: " + f4);
System.out.println("f5: " + f5);
// Here , we will get (Float.MIN_VALUE) because
// we are passing parameter (0.0)
System.out.println("Math.ulp(f1): " + Math.ulp(f1));
// Here , we will get (Float.MIN_VALUE) because
// we are passing parameter (-0.0)
System.out.println("Math.ulp(f2): " + Math.ulp(f2));
// Here , we will get (Infinity) because
// we are passing parameter (7.0/0.0)
System.out.println("Math.ulp(f3): " + Math.ulp(f3));
// Here , we will get (Infinity) because
// we are passing parameter (-7.0/0.0)
System.out.println("Math.ulp(f4): " + Math.ulp(f4));
// Here , we will get (2 raised to the power of 104)
// because we are passing parameter (1285.45)
System.out.println("Math.ulp(f5): " + Math.ulp(f5));
}
}
Output
输出量
E:\Programs>javac UlpFloatTypeMethod.java
E:\Programs>java UlpFloatTypeMethod
f1: 0.0
f2: -0.0
f3: Infinity
f4: -Infinity
f5: 1285.45
Math.ulp(f1): 1.4E-45
Math.ulp(f2): 1.4E-45
Math.ulp(f3): Infinity
Math.ulp(f4): Infinity
Math.ulp(f5): 1.2207031E-4
Example 2:
范例2:
// Java program to demonstrate the example of
// ulp(float fl) method of Math Class
public class UlpFloatTypeMethod {
public static void main(String[] args) {
// declaring the variables
double d1 = 0.0;
double d2 = -0.0;
double d3 = 7.0 / 0.0;
double d4 = -7.0f / 0.0;
double d5 = 1285.45;
// Display the values
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);
System.out.println("d3: " + d3);
System.out.println("d4: " + d4);
System.out.println("d5: " + d5);
// Here , we will get (Float.MIN_VALUE) because
// we are passing parameter (0.0)
System.out.println("Math.ulp(d1): " + Math.ulp(d1));
// Here , we will get (Float.MIN_VALUE) because
// we are passing parameter (-0.0)
System.out.println("Math.ulp(d2): " + Math.ulp(d2));
// Here , we will get (Infinity) because
// we are passing parameter (7.0/0.0)
System.out.println("Math.ulp(d3): " + Math.ulp(d3));
// Here , we will get (Infinity) because
// we are passing parameter (-7.0/0.0)
System.out.println("Math.ulp(d4): " + Math.ulp(d4));
// Here , we will get (2 raised to the power of 971)
// because we are passing parameter (1285.45)
System.out.println("Math.ulp(d5): " + Math.ulp(d5));
}
}
Output
输出量
E:\Programs>javac UlpMethod.java
E:\Programs>java UlpMethod
d1: 0.0
d2: -0.0
d3: Infinity
d4: -Infinity
d5: 1285.45
Math.ulp(d1): 4.9E-324
Math.ulp(d2): 4.9E-324
Math.ulp(d3): Infinity
Math.ulp(d4): Infinity
Math.ulp(d5): 2.2737367544323206E-13
翻译自: https://www.includehelp.com/java/math-class-ulp-method-with-example.aspx
ulp通信