菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 3 教程
Python3 教程 Python3 简介 Python3 环境搭建 Python3 VScode Python3 基础语法 Python3 基本数据类型 Python3 数据类型转换 Python3 解释器 Python3 注释 Python3 运算符 Python3 数字(Number) Python3 字符串 Python3 列表 Python3 元组 Python3 字典 Python3 集合 Python3 条件控制 Python3 循环语句 Python3 编程第一步 Python3 推导式 Python3 迭代器与生成器 Python3 with Python3 函数 Python3 lambda Python3 装饰器 Python3 数据结构 Python3 模块 Python __name__ Python3 输入和输出 Python3 File Python3 OS Python3 错误和异常 Python3 面向对象 Python3 命名空间/作用域 Python 虚拟环境的创建 Python 类型注解 Python3 标准库概览 Python3 实例 Python 测验

Python3 高级教程

Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQt Python for 循环 Python while 循环
(追記) (追記ここまで)

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 模块

AI 思考中...

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /