java.lang.Math.ulp()是一个内置 java方法,该方法返回参数ulp的大小.ulp表示最低精度单位,它计算给定double或float值与double或float值之间的距离下一个更大。参数可以有两种类型:
ulp(float f):它接受浮点类型的输入。
ulp(double d):它接受双精度型输入。
注意:
如果参数为NaN,则输出为NaN。
如果参数为正值或负值double或float值,则ulp(-arg)和ulp(arg)的输出相同。
如果参数为正零或负零,则输出将为Double.MIN_VALUE或Float.MIN_VALUE。
如果参数为正无穷大或负无穷大,则输出为正无穷大。
如果参数为正数或负数Double.MAX_VALUE或Float.MAX_VALUE,则输出为2971对于双类型和2104对于浮点类型。
用法:
public static dataType ulp(dataType g)
参数:
g:argument whose ulp is to be returned.
返回:
This method returns the size of an ulp of the argument.
例:展示java.lang.Math.ulp()方法的用法。
// Java program to demonstrate working
// of java.lang.Math.ulp() method
import java.lang.Math;
class Gfg {
// driver code
public static void main(String args[])
{
double a = 34.543;
// Input positive double value
// Output ulp(a)
System.out.println(Math.ulp(a));
// Input negative double value
// Output ulp(-a)==ulp(a)
System.out.println(Math.ulp(-a));
double b = 0.0 / 0;
// Input NaN, Output Nan
System.out.println(Math.ulp(b));
float c = -0.0f;
// Input negative zero
// Output Float.MIN_VALUE.
System.out.println(Math.ulp(c));
float d = -1.0f / 0;
// Input negative infinity
// Output positive infinity.
System.out.println(Math.ulp(d));
double e = Double.MAX_VALUE;
System.out.println(Math.ulp(e));
}
}
输出:
7.105427357601002E-15
7.105427357601002E-15
NaN
1.4E-45
Infinity
1.9958403095347198E292