title: Appending of decimals in doubles in C# that I didn't ask for
body: <p>A program I was writing had a weird output, which I boiled down to the behavior of the following code:</p>
  
  <pre><code>class Program
  {
      public static void Main()
      {
          double test1 = 0.1;
          double test2 = Math.Pow(10, -4);
          double test3 = 0.1+Math.Pow(10, -4);
      }
   }
  </code></pre>
  
  <p>When I check the values of <code>test1</code>, <code>test2</code>, and <code>test3</code> in debug mode, the values of the first two variables are</p>
  
  <p><code>test1 = 0.1</code></p>
  
  <p><code>test2 = 0.0001</code></p>
  
  <p>which is what I assigned them to be, but their sum becomes</p>
  
  <p><code>test3 = 0.10010000000000001</code>.</p>
  
  <p>There appears to be an error of 10^{-17}. My questions are:</p>
  
  <ul>
  <li>Why does this happen?</li>
  <li>Can I do anything to prevent it?</li>
  </ul>
  
  <p>Thanks.</p>