<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sneha Makwana</title>
    <description>The latest articles on DEV Community by Sneha Makwana (@sneha_makwana_c7d78ac74df).</description>
    <link>https://dev.to/sneha_makwana_c7d78ac74df</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3976572%2F6f60e4ac-6129-4769-900a-bce565eb5f9a.png</url>
      <title>DEV Community: Sneha Makwana</title>
      <link>https://dev.to/sneha_makwana_c7d78ac74df</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sneha_makwana_c7d78ac74df"/>
    <language>en</language>
    <item>
      <title>Your code is correct. But!! Your computer is lying..🤯</title>
      <dc:creator>Sneha Makwana</dc:creator>
      <pubDate>Thu, 20 Aug 2026 21:18:25 +0000</pubDate>
      <link>https://dev.to/sneha_makwana_c7d78ac74df/your-code-is-correct-but-your-computer-is-lying-391o</link>
      <guid>https://dev.to/sneha_makwana_c7d78ac74df/your-code-is-correct-but-your-computer-is-lying-391o</guid>
      <description>&lt;p&gt;Why! Why!&lt;/p&gt;

&lt;p&gt;Have you ever seen something in your code that made your brain go, "Wait.....WHAT? while a hundred possible explanations started running through your head? Is my computer a GHOST? 🤯&lt;/p&gt;

&lt;p&gt;I recently got this feeling. I wrote something that look completely reasonable. Yeah! I am a coder, having a very good experience. But, once in a blue moon, I got something weird things that blown my mind and just hate the computer system with why! why!&lt;/p&gt;

&lt;p&gt;I wrote this on my computer when I was just playing with numbers in java.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;/&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Java&lt;/span&gt;
&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My brain expected&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;0.3&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My computer had other plans..&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;0.30000000000000004&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For a second, I thought...&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Okay, What did I break? (baffled programmer in skibidi) Hahaha!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Nothing.&lt;br&gt;
My code was correct.&lt;br&gt;
The computer wasn't broken either.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem was my assumption about what 0.1 actually means to a computer.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Computers don't really understand 0.1&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Humans use decimal numbers:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;0.1&lt;br&gt;
0.2&lt;br&gt;
0.5&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Computers work with binary:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;0s and 1s&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And here's the annoying part:&lt;/p&gt;

&lt;p&gt;Some decimal fractions simply &lt;strong&gt;cannot be represented exactly in binary.&lt;/strong&gt;&lt;br&gt;
It's a bit like trying to represent 1/3 using only two decimal places.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;0.33&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's close.&lt;br&gt;
But it's not actually 1/3.&lt;/p&gt;

&lt;p&gt;Floating-point numbers have a simmilar problem.&lt;br&gt;
So, when you write&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;/&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Java&lt;/span&gt;
&lt;span class="mf"&gt;0.1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the computer stores the closest representable binary value, not necessarily the exact mathematical value that you had in your head and Java print that value quite different than expectations.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;So... is the computer or programming language lying?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Not really.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It's being painfully honest.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The lie happened earlier. &lt;br&gt;
We assumed...&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0.1 in our program = exactly 0.1 in the computer.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's the trap.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And this isn't just a funny Java trick&lt;/strong&gt;&lt;br&gt;
This matters in real software.&lt;/p&gt;

&lt;p&gt;Imagine you are building:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a banking system&lt;/li&gt;
&lt;li&gt;a payment application&lt;/li&gt;
&lt;li&gt;an inventory system&lt;/li&gt;
&lt;li&gt;a billing platform&lt;/li&gt;
&lt;li&gt;a trading system&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Would you want this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;/&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Java&lt;/span&gt;

&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maybe not.&lt;/p&gt;

&lt;p&gt;For financial calculations, using floating-point numbers without understanding their limitations can cause very real problems.&lt;/p&gt;

&lt;p&gt;That's why developers often use decimal-oriented representations such as Java's:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&amp;lt;/&amp;gt; Java&lt;br&gt;
BigDecimal&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When exact decimal arithmatic matters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The bigger lession&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This isn't really story about 01 + 0.2.&lt;br&gt;
It's about something much bigger:&lt;/p&gt;

&lt;p&gt;Your program can be completely correct and still produce a result you didn't expect. (Usually this happens in a critical situations like pick time of project delivery... Haha!)&lt;/p&gt;

&lt;p&gt;Because software doesn't operate on our assumptions.&lt;/p&gt;

&lt;p&gt;It operates on:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;representations -&amp;gt; rules -&amp;gt; limitations -&amp;gt; hardware.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And sometimes the weirdest bugs aren't caused by bad code. Is it so?** (Comment with 'Your situation' If I hit a pitch here.)**&lt;/p&gt;

&lt;p&gt;They're caused by a perfectly reasonable human assumtopn about how computers work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One last question please!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What's a computer behavior that looked completely wrong to you the first time you encountered it?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Floating point? Time zones? null? Threads? Memory? stuck at production release? Something else?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Drop it in the comments.&lt;br&gt;
Curious and I'm collecting examples for the next "Wait... why does the computer do THAT?" Am I special? 😄&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>computerscience</category>
      <category>whywhy</category>
    </item>
  </channel>
</rss>
