# AssertionError: assert 0.30000000000000004 == 0.3
 +  其中 0.30000000000000004 = 0.1 + 0.2

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

## 根因

浮点运算会产生小的表示误差。浮点数直接相等比较不可靠；pytest 提供了 pytest.approx 用于此场景。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## 解决方案

1. **** (98% 成功率)
   ```
   Use pytest.approx:
  from pytest import approx
  assert 0.1 + 0.2 == approx(0.3)
  assert result == approx(expected, rel=1e-6, abs=1e-9)
   ```
2. **** (95% 成功率)
   ```
   For numpy arrays:
  import numpy as np
  np.testing.assert_allclose(a, b, rtol=1e-6)
   ```

## 无效尝试

- **** — Works for this case but fails for very large/small magnitudes and hides genuine precision bugs. (55% 失败率)
- **** — Default rel_tol=1e-9 may be too strict for accumulated errors or too loose for scientific tests. (50% 失败率)
