{
  "id": "python/pytest-assertion-approx-strict",
  "signature": "AssertionError: assert 0.30000000000000004 == 0.3\n +  where 0.30000000000000004 = 0.1 + 0.2",
  "signature_zh": "AssertionError: assert 0.30000000000000004 == 0.3\n +  其中 0.30000000000000004 = 0.1 + 0.2",
  "regex": "AssertionError:\\ assert\\ 0\\.30000000000000004\\ ==\\ 0\\.3\\\n\\ \\+\\ \\ where\\ 0\\.30000000000000004\\ =\\ 0\\.1\\ \\+\\ 0\\.2",
  "domain": "python",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "Floating-point arithmetic produces small representation errors. Direct equality on floats is unreliable; pytest provides pytest.approx for this purpose.",
  "root_cause_type": "generic",
  "root_cause_zh": "浮点运算会产生小的表示误差。浮点数直接相等比较不可靠；pytest 提供了 pytest.approx 用于此场景。",
  "versions": [
    {
      "version": "7.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Works for this case but fails for very large/small magnitudes and hides genuine precision bugs.",
      "fail_rate": 0.55,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Default rel_tol=1e-9 may be too strict for accumulated errors or too loose for scientific tests.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.98,
      "how": "Use pytest.approx:\n  from pytest import approx\n  assert 0.1 + 0.2 == approx(0.3)\n  assert result == approx(expected, rel=1e-6, abs=1e-9)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "For numpy arrays:\n  import numpy as np\n  np.testing.assert_allclose(a, b, rtol=1e-6)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.92,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-01-08",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}