{
  "id": "python/pytest-assertion-approx-float",
  "signature": "AssertionError: assert 0.30000000000000004 == 0.3\nE    +  where 0.30000000000000004 = 0.1 + 0.2",
  "signature_zh": "AssertionError: assert 0.30000000000000004 == 0.3\nE    +  其中 0.30000000000000004 = 0.1 + 0.2",
  "regex": "AssertionError:\\ assert\\ 0\\.30000000000000004\\ ==\\ 0\\.3\\\nE\\ \\ \\ \\ \\+\\ \\ where\\ 0\\.30000000000000004\\ =\\ 0\\.1\\ \\+\\ 0\\.2",
  "domain": "python",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "Binary floating-point representation cannot represent 0.3 exactly; direct equality on floats is unreliable.",
  "root_cause_type": "generic",
  "root_cause_zh": "二进制浮点表示无法精确表示 0.3；直接对浮点数使用相等比较不可靠。",
  "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 some values but fails for very large or very small magnitudes; not a general solution.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "String representations vary across Python versions and locales; brittle.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.98,
      "how": "import pytest\n\ndef test_sum():\n    assert 0.1 + 0.2 == pytest.approx(0.3)\n    assert [0.1, 0.2] == pytest.approx([0.1, 0.2])",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "import math\n\ndef test_sum():\n    assert math.isclose(0.1 + 0.2, 0.3, rel_tol=1e-9, abs_tol=1e-12)",
      "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.9,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-02-03",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}