Java.lang.StrictMath.ulp() 方法
描述
java.lang.StrictMath.ulp(float f) 方法返回参数的 ulp 的大小。 浮点值的 ulp 是该浮点值与幅度下一个更大的浮点值之间的正距离。 请注意,对于非 NaN x,ulp(-x) == ulp(x)。包括这些情况 −
- 如果参数为 NaN,则结果为 NaN。
- 如果参数为正无穷或负无穷,则结果为正无穷。
- 如果参数为正零或负零,则结果为 Float.MIN_VALUE。
- 如果参数是 ±Float.MAX_VALUE,则结果等于 2104。
声明
以下是 java.lang.StrictMath.ulp() 方法的声明。
public static float ulp(float f)
参数
f − 这是要返回 ulp 的浮点值。
返回值
此方法返回参数的 ulp 的大小。
异常
NA
示例
下面的例子展示了 java.lang.StrictMath.ulp() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class StrictMathDemo {
public static void main(String[] args) {
float f1 = 3.71f , f2 = -4.3f, f3 = 0.0f;
// returns the size of an ulp of the argument
float ulpValue = StrictMath.ulp(f1);
System.out.println("Size of ulp of " + f1 + " : " + ulpValue);
ulpValue = StrictMath.ulp(f2);
System.out.println("Size of ulp of " + f2 + " : " + ulpValue);
ulpValue = StrictMath.ulp(f3);
System.out.println("Size of ulp of " + f3 + " : " + ulpValue);
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Size of ulp of 3.71 = 2.3841858E-7 Size of ulp of -4.3 = 4.7683716E-7 Size of ulp of 0.0 = 1.4E-45

