# 断言错误：断言 0.1 + 0.2 == 0.3；0.30000000000000004 != 0.3

- **ID:** `python/pytest-assert-approx-equality`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

浮点算术精度问题导致在将计算出的浮点数与预期值进行比较时，相等性断言失败。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Use pytest.approx: assert 0.1 + 0.2 == pytest.approx(0.3, rel=1e-9) or abs=1e-12
   ```
2. **** (90% 成功率)
   ```
   Use math.isclose: assert math.isclose(0.1 + 0.2, 0.3, rel_tol=1e-9)
   ```

## 无效尝试

- **** — Rounding the result to a fixed number of decimal places may work for some cases but can mask precision errors in edge cases. (50% 失败率)
- **** — Using assertAlmostEqual with a default tolerance may still fail if the tolerance is too small. (40% 失败率)
