<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dhanashree Takate on Medium]]></title>
        <description><![CDATA[Stories by Dhanashree Takate on Medium]]></description>
        <link>https://medium.com/@dhanashreetakate1402?source=rss-42c82801a556------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*yXkUpuw-FbasxJVl</url>
            <title>Stories by Dhanashree Takate on Medium</title>
            <link>https://medium.com/@dhanashreetakate1402?source=rss-42c82801a556------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 14:25:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhanashreetakate1402/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[WHY 0.1 + 0.2 IS NOT EXACTLY 0.3 IN JAVA :]]></title>
            <link>https://medium.com/@dhanashreetakate1402/why-0-1-0-2-is-not-exactly-0-3-in-java-48d7d7520e04?source=rss-42c82801a556------2</link>
            <guid isPermaLink="false">https://medium.com/p/48d7d7520e04</guid>
            <dc:creator><![CDATA[Dhanashree Takate]]></dc:creator>
            <pubDate>Sat, 29 Aug 2026 17:51:10 GMT</pubDate>
            <atom:updated>2026-08-29T17:51:10.898Z</atom:updated>
            <content:encoded><![CDATA[<h3>If you’ve ever run this in Java:</h3><pre>System.out.println(0.1 + 0.2);</pre><p>You probably expected 0.3 Instead, you got:</p><pre>0.30000000000000004</pre><p>This isn’t a Java bug. It’s not a JVM quirk either. It’s a direct consequence of how computers represent decimal numbers in binary and it happens in almost every mainstream programming language, from Python to JavaScript to C++. Let’s break down exactly why.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1imMJdTWAFq5c6xQZfKJjQ.png" /></figure><h3><strong>The Core Problem: Binary Can’t Represent Most Decimals Exactly.</strong></h3><p>Computers store floating-point numbers in <strong>binary (base 2)</strong>, not decimal (base 10). This matters because some numbers that look perfectly clean in base 10 turn into infinitely repeating fractions in base 2.</p><p>A simple human analogy: in base 10, we can’t represent 1/3 exactly it becomes 0.3333… forever. The value isn&#39;t flawed, but the base we&#39;re using can&#39;t express it cleanly.</p><p>The same thing happens to 0.1 and 0.2 in binary. Neither number has a finite binary representation. Written out, 0.1 in binary looks like:</p><pre>0.0001100110011001100110011001100110011001100110011...  (repeating forever)</pre><p>Since computers can’t store infinite digits, the value gets <strong>rounded</strong> to fit within the available bits.</p><h3>How Java Stores Floating-Point Numbers ?</h3><p>By default, Java represents 0.1 and 0.2 as double a 64-bit floating-point type defined by the <strong>IEEE 754</strong> standard.</p><p>That gives about 15–17 significant decimal digits of precision plenty for most calculations, but not infinite. So when Java stores 0.1, it doesn&#39;t store exactly 0.1. It stores the closest possible 64-bit binary approximation of 0.1.</p><p>The same happens with 0.2 When you add these two already rounding approximations together, the tiny rounding errors combine, and the result is a number that&#39;s extremely close to 0.3 but not identical to the binary representation of 0.3 itself.</p><p>That leftover sliver of error is what shows up as 0.300000000004.</p><h3>How to Handle This Correctly in Java :</h3><p><strong>1.Use a bigdecimal for exact decimal value</strong></p><pre>import java.math.BigDecimal;<br><br>BigDecimal a = new BigDecimal(&quot;0.1&quot;);<br>BigDecimal b = new BigDecimal(&quot;0.2&quot;);<br>BigDecimal sum = a.add(b);<br><br>System.out.println(sum); // 0.3</pre><p>Bigdecimal stores numbers as exact decimal values rather than binary approximations, which is why it&#39;s the standard choice for financial and monetary calculations in Java. Always construct it from a string, not a double otherwise you just import the same rounding error.</p><h4>2.Compare doubles with a tolerance,not ==</h4><pre>double result = 0.1 + 0.2;<br>double expected = 0.3;<br>double epsilon = 1e-9;<br><br>boolean isEqual = Math.abs(result - expected) &lt; epsilon;<br>System.out.println(isEqual); // true</pre><h4>3. Use integers for money when possible</h4><p>Instead of storing $19.99 as a double, store it as 1999(cents) as a long or int This sidesteps floating-point rounding entirely.</p><p>0.1 + 0.2 != 0.3 in Java not because of a flaw in the language, but because binary floating-point numbers as defined by the IEEE 754 standard cannot exactly represent most decimal fractions. Understanding this helps you know when floating-point math is good enough (most of the time) and when you need a precise alternative like bigdecimal or integer-based arithmetic.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48d7d7520e04" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>