# 断言错误：断言 0.1 + 0.2 == 0.3

- **ID:** `python/assertionerror-assert-approx-equal`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

浮点运算精度问题导致总和与预期值略有偏差。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |

## 解决方案

1. **Use pytest.approx for approximate comparison** (95% 成功率)
   ```
   assert 0.1 + 0.2 == pytest.approx(0.3)
   ```
2. **Use math.isclose with tolerance** (90% 成功率)
   ```
   import math; assert math.isclose(0.1 + 0.2, 0.3, rel_tol=1e-9)
   ```

## 无效尝试

- **Using round() on both sides** — round() may still produce different results due to floating-point representation. (60% 失败率)
- **Multiplying by a factor to avoid decimals** — Does not eliminate the underlying precision issue; may introduce new errors. (40% 失败率)
