title: C# Getting strange results for a simple math operations
body: <p>I have noticed for some strange behavior while adding two doubles, sometimes it works correct, and sometimes not!</p>
  
  <p>Here is the first example:</p>
  
  <pre><code>double num1 = 0.1 + 0.7; //Guess the result?
  </code></pre>
  
  <p>Easy - 0.8 !!! or not?</p>
  
  <p>Look at the strange result:</p>
  
  <p><img src="https://i.stack.imgur.com/RB0a2.png" alt="enter image description here"></p>
  
  <p>And guess what, the if statement is going inside the else block, and prints the <code>num1</code> - and no, it doesn't prints 0.799999999999993, it prints 0.8.</p>
  
  <p>So I have gone one more step forward and tried this code:</p>
  
  <pre><code>if (0.1 + 0.7 == 0.8) //Returns false ??
  {
      Console.WriteLine("Correct");
  }
  </code></pre>
  
  <p>OK, strange, but now I found the correct way, it should use f (float). As I remember double has many spaces, so it can contain higher numbers, maybe this is the cause.</p>
  
  <pre><code>float num2 = 0.1f + 0.7f;
  
  if (num2 == 0.8f) //Perfect - finally works !!!
  {
      Console.WriteLine("Correct");
  }
  else
  {
      Console.WriteLine(num2);
  }
  </code></pre>
  
  <p>But now, I try this - and again it returns false, why?</p>
  
  <pre><code>if (0.1f + 0.7f == 0.8f) //Returns false :(
  {
      Console.WriteLine("Correct");
  }
  </code></pre>
  
  <p>The watch results when debugging it:</p>
  
  <p><img src="https://i.stack.imgur.com/Q6jef.png" alt="enter image description here"></p>
  
  <p>Can someone explain me what's wrong here? Are those bugs?</p>
  
  <p>Thanks in advance.</p>