{"id":2299,"date":"2025-09-22T11:34:16","date_gmt":"2025-09-22T11:34:16","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=2299"},"modified":"2026-02-05T11:59:12","modified_gmt":"2026-02-05T11:59:12","slug":"floating-point-arithmetic-inaccurate-results-python","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/floating-point-arithmetic-inaccurate-results-python\/","title":{"rendered":"Why does Floating-point Arithmetic Sometimes give Inaccurate Results in Python?"},"content":{"rendered":"\n<p>At times simple arithmetic equations like 0.1+0.2 can give answers like 0.30000000000000004 instead of 0.3, in Python. Is Python broken then? No, this is not Python&#8217;s fault, it is the side effect of how floating numbers are represented in computers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Description of the Problem<\/h2>\n\n\n\n<p>You\u2019re doing basic math in Python, but the results don\u2019t make sense:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(0.1 + 0.2)  # Expected: 0.3, Actual: 0.30000000000000004<\/code><\/pre>\n\n\n\n<p><strong>Or:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(1.2 - 1.0)  # Expected: 0.2, Actual: 0.19999999999999996<\/code><\/pre>\n\n\n\n<p>It seems like Python is doing something wrong, but it&#8217;s not just Python. This issue also occurs in most modern programming languages, including JavaScript, Java, and C.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why This Happens<\/h2>\n\n\n\n<p>Computers represent floating-point numbers in binary code. Most decimal fractions are difficult to be represented in a binary format. This results in small rounding errors.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The number 0.1 can\u2019t be precisely represented in binary floating-point format.<\/li>\n\n\n\n<li>So, 0.1 + 0.2 ends up being something like 0.30000000000000004.<br><\/li>\n<\/ul>\n\n\n\n<p>This issue is part of the IEEE 754 standard used by almost all programming languages for floating-point representation. It\u2019s not a bug in Python, it&#8217;s just how computers work at the binary level.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Resolve the Issue<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Use round() for Display or Comparison<\/h3>\n\n\n\n<p>When displaying results or comparing float values, use round():<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>result = 0.1 + 0.2\nprint(round(result, 2))  # 0.3<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Use math.isclose() for Comparisons<\/h3>\n\n\n\n<p><strong>Instead of doing:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if result == 0.3:<\/code><\/pre>\n\n\n\n<p><strong>Do this:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import math\n\nif math.isclose(result, 0.3, rel_tol=1e-9):\n    print(\"Close enough!\")<\/code><\/pre>\n\n\n\n<p>This is especially helpful in unit tests or financial applications where precision matters.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Use decimal.Decimal for High-Precision Arithmetic<\/h3>\n\n\n\n<p>Python\u2019s decimal module allows decimal arithmetic with better precision and control:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from decimal import Decimal\n\na = Decimal('0.1')\nb = Decimal('0.2')\nprint(a + b)  # 0.3<\/code><\/pre>\n\n\n\n<p>This is particularly useful for financial calculations where exact decimal representation is critical.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Use fractions.Fraction for Exact Rational Numbers<\/h3>\n\n\n\n<p>You can also use the fractions module when you want exact representations of fractions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from fractions import Fraction\n\nx = Fraction(1, 10)\ny = Fraction(2, 10)\nprint(x + y)  # 3\/10<\/code><\/pre>\n\n\n\n<p>This avoids all floating-point inaccuracies entirely but may not be suitable for large-scale computations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Floating-Point Arithmetic Best Practices In Python<\/h2>\n\n\n\n<p>Floating-point inaccuracies are a result of binary representation, not a Python-specific issue. While such differences can be neglected in most cases, they can cause serious distrup they can cause serious concerns in comparisons or important applications.<\/p>\n\n\n\n<p><strong>Best Practices:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use round() for formatting float values<\/li>\n\n\n\n<li>Use math.isclose() for safe float comparisons<\/li>\n\n\n\n<li>Use decimal.Decimal for financial or high-precision applications<\/li>\n\n\n\n<li>Use fractions.Fraction if exact arithmetic with ratios is required<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Python isn&#8217;t broken, floating-point math just has its quirks due to binary representation. If you&#8217;re working in finance, data science, or anything where precision matters, it\u2019s smart to handle float operations carefully. And if you&#8217;re looking to build accurate and reliable Python-based solutions, it&#8217;s a good idea to <a href=\"https:\/\/www.cmarix.com\/hire-python-developers.html\" data-type=\"link\" data-id=\"https:\/\/www.cmarix.com\/hire-python-developers.html\">hire Python developers<\/a> who understand these nuances.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At times simple arithmetic equations like 0.1+0.2 can give answers like 0.30000000000000004 instead of 0.3, in Python. Is Python broken then? No, this is not Python&#8217;s fault, it is the side effect of how floating numbers are represented in computers. Description of the Problem You\u2019re doing basic math in Python, but the results don\u2019t make [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2301,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[163,3],"tags":[],"class_list":["post-2299","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=2299"}],"version-history":[{"count":2,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2299\/revisions"}],"predecessor-version":[{"id":2303,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2299\/revisions\/2303"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/2301"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=2299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=2299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=2299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}