Python math.ulp() 函数

Python math 模块 Python math 模块


在浮点数计算中,理解浮点数的精度限制非常重要。ULP(Unit in the Last Place)是衡量浮点数精度的基本单位。

math.ulp() 是 Python 3.9 引入的函数,用于返回浮点数的最小有效浮点单位(ULP),即从该数到下一个可表示浮点数之间的距离。

单词释义ulp 是 "Unit in the Last Place" 的缩写,意为"最后一位的单位"。


基本语法与参数

语法格式

import math

math.ulp(x)

参数说明

  • x: 浮点数

返回值

返回 x 的 ULP 值,即 x 与下一个可表示浮点数之间的差


实例

示例 1:基础用法

实例

import math

print("1.0 的 ULP:", math.ulp(1.0))
print("2.0 的 ULP:", math.ulp(2.0))
print("100.0 的 ULP:", math.ulp(100.0))
print("0.0 的 ULP:", math.ulp(0.0))
print("最小正数:", math.ulp(float.min)

运行结果:

1.0 的 ULP: 2.220446049250313e-16
2.0 的 ULP: 4.440892098500626e-16
100.0 的 ULP: 2.8421709430404007e-14
0.0 的 ULP: 5e-324

示例 2:不同数值的 ULP

实例

import math

values = [0.1, 0.5, 1.0, 1.5, 2.0, 10.0, 100.0, 1000.0]
print("不同数值的 ULP:")
for x in values:
    print(f"  ULP({x}) = {math.ulp(x)}")

运行结果:

不同数值的 ULP:
  ULP(0.1) = 1.3877787807814457e-17
  ULP(0.5) = 1.3877787807814457e-16
  ULP(1.0) = 2.220446049250313e-16
  ULP(1.5) = 2.220446049250313e-16
  ULP(2.0) = 4.440892098500626e-16
  U浮点 10.0: 2.220446049250313e-15
  ULP(100.0) = 2.8421709430404007e-14
  ULP(1000.0) = 2.2737367544323206e-13

示例 3:特殊值

实例

import math

print("无穷大:", math.ulp(math.inf))
print("最大有限数:", math.ulp(sys.float_info.max))
print("负数:", math.ulp(-1.0))
print("次正规数:", math.ulp(1e-310))

运行结果:

无穷大: inf
最大有限数: inf
负数: 2.220446049250313e-16
次正规数: 5e-324

示例 4:浮点数精度检测

实例

import math

def check_precision(x):
    """检测浮点数精度"""
    ulp = math.ulp(x)
    relative_error = ulp / x
    print(f"x = {x}")
    print(f"  ULP = {ulp}")
    print(f"  相对误差 ≈ {relative_error:.2e}")
    print(f"  有效位数 ≈ {-math.log2(relative_error):.1f}")
    print()

check_precision(1.0)
check_precision(1000000.0)

运行结果:

x = 1.0
  ULP = 2.220446049250313e-16
  相对误差 ≈ 2.22e-16
  有效位数 ≈ 52.0

x = 1000000.0
  ULP = 1.907344663e-13
  相对误差 ≈ 1.91e-13
  有效位数 ≈ 52.0

示例 5:与 nextafter 结合

实例

import math

# 验证:nextafter(x, inf) - x = ULP(x)
x = 1.0
next_val = math.nextafter(x, math.inf)
ulp_calc = math.ulp(x)
direct_diff = next_val - x

print(f"x = {x}")
print(f"nextafter(x, inf) - x = {direct_diff}")
print(f"math.ulp(x) = {ulp_calc}")
print(f"两者相等: {direct_diff == ulp_calc}")

运行结果:

x = 1.0
nextafter(x, inf) - x = 2.220446049250313e-16
math.ulp(x) = 2.220 double 000001e-16
两者相等: True

说明:math.ulp(x) 等价于 |nextafter(x, inf) - x|


应用场景

  • 数值算法的误差分析
  • 浮点数精度检测
  • 科学计算中的容差设置
  • 比较浮点数时确定合适的 epsilon

注意事项

  • Python 3.9+ 才有此函数
  • ULP 值随数值增大而增大
  • inf 的 ULP 也是 inf

Python math 模块 Python math 模块