本文目录导读:

- 避免浮点数:使用
decimal.Decimal - 大整数除法:使用
Fraction或Decimal - 高精度科学计算:使用
mpmath库 - 金融计算:严格使用
Decimal - 比较运算:避免直接比较浮点数
- 不要用 或 后转浮点
- 常见陷阱总结
- 总结选择
在Python中处理大数运算时,精度差主要来源于浮点数(float) 的有限精度,Python的整数(int)是任意精度的,不会丢失精度,但一旦涉及浮点数、分数或除法,就可能出现精度问题。
以下是几个常见场景及其解决方案:
避免浮点数:使用 decimal.Decimal
浮点数在内存中以二进制存储,很多十进制小数无法精确表示(如 1)。
# 问题:浮点数精度差
print(0.1 + 0.2) # 输出 0.30000000000000004
# 解决:使用 decimal 模块
from decimal import Decimal, getcontext
getcontext().prec = 100 # 设置全局精度
a = Decimal('0.1')
b = Decimal('0.2')
print(a + b) # 输出 0.3(精确)
大整数除法:使用 Fraction 或 Decimal
整数除法 会返回浮点数,可能导致大数精度丢失。
# 问题:大整数除法返回浮点数
result = 123456789012345678901234567890 / 3
print(result) # 输出 4.115226300411526e+28(精度丢失)
# 方法1:使用 Decimal 保留精度
from decimal import Decimal
result = Decimal('123456789012345678901234567890') / Decimal('3')
print(result) # 输出 41152263004115263004115263030
# 方法2:使用 Fraction 精确分数
from fractions import Fraction
frac = Fraction(123456789012345678901234567890, 3)
print(frac) # 输出 41152263004115263004115263030
print(float(frac)) # 转为浮点数时注意:会丢失精度
高精度科学计算:使用 mpmath 库
mpmath 提供任意精度的浮点数和复数运算。
# 安装:pip install mpmath
from mpmath import mp
# 设置精度(1000 位小数)
mp.dps = 1000
# 高精度计算
a = mp.mpf('1.2345678901234567890')
b = mp.mpf('2.3456789012345678901')
result = a * b
mp.nstr(result, 50) # 显示前50位
金融计算:严格使用 Decimal
金融场景中,精度至关重要(如货币计算)。
from decimal import Decimal, ROUND_HALF_UP
# 银行家舍入(四舍五入)
amount = Decimal('99.99')
tax_rate = Decimal('0.08')
total = amount * tax_rate
# 保留两位小数,四舍五入
total = total.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
print(total) # 输出 8.00
比较运算:避免直接比较浮点数
# 不要这样
if 0.1 + 0.2 == 0.3:
print("相等")
# 使用 Decimal 比较
from decimal import Decimal
if Decimal('0.1') + Decimal('0.2') == Decimal('0.3'):
print("相等") # 会输出
不要用 或 后转浮点
# 错误做法
big_int = 10**30
result = big_int // 3 # 整数除法没问题
float_result = float(big_int // 3) # 但转成 float 会丢失精度!
# 正确做法
from decimal import Decimal
result = Decimal(str(big_int)) / Decimal('3')
常见陷阱总结
| 操作 | 风险 | 推荐做法 |
|---|---|---|
a / b 计算大整数 |
返回 float,丢失精度 | 用 Decimal 或 Fraction |
1 + 0.2 |
二进制无法精确表示 | 用 Decimal('0.1') 等 |
float(10**100) |
超出 float 范围变成 inf |
保持为 int 或 Decimal |
| 高次幂运算 | 结果很大时转 float 溢出 | 用 mpmath |
| 金融计算使用 float | 累积误差 | 始终用 Decimal |
总结选择
- 整数运算:用
int(Python 自动大数支持) - 精确小数运算(金融等):用
decimal.Decimal - 精确分数运算:用
fractions.Fraction - 任意精度浮点运算(科学计算):用
mpmath - 普通浮点运算(对精度不敏感):用
float(但应避免比较和累加)
如果需要更具体的示例或代码实现,请告诉我具体的应用场景。