# 断言错误：assert 0.1 + 0.2 == 0.3（pytest.approx 使用不正确）

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

## 根因

浮点运算导致精度问题；直接使用 '==' 会失败，并且必须将 pytest.approx 应用于两侧或作为上下文使用。

## 版本兼容性

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

## 解决方案

1. **Use pytest.approx to compare floating point values within tolerance.** (95% 成功率)
   ```
   assert 0.1 + 0.2 == pytest.approx(0.3)
   ```
2. **Specify relative tolerance using pytest.approx with rel parameter.** (90% 成功率)
   ```
   assert result == pytest.approx(expected, rel=1e-9)
   ```

## 无效尝试

- **Using round() on both values to compare** — Rounding can still introduce errors if the rounding precision is not chosen carefully. (60% 失败率)
- **Comparing with == after converting to Decimal** — Decimal conversion may not be accurate if the original values are floats. (50% 失败率)
