title: Strange behaviour with floats and string conversion
body: <p>I've typed this into python shell:</p>
  
  <pre><code>&gt;&gt;&gt; 0.1*0.1
  0.010000000000000002
  </code></pre>
  
  <p>I expected that 0.1*0.1 is not 0.01, because I know that 0.1 in base 10 is periodic in base 2.</p>
  
  <pre><code>&gt;&gt;&gt; len(str(0.1*0.1))
  4
  </code></pre>
  
  <p>I expected to get 20 as I've seen 20 characters above. Why do I get 4?</p>
  
  <pre><code>&gt;&gt;&gt; str(0.1*0.1)
  '0.01'
  </code></pre>
  
  <p>Ok, this explains why I <code>len</code> gives me 4, but why does <code>str</code> return <code>'0.01'</code>?</p>
  
  <pre><code>&gt;&gt;&gt; repr(0.1*0.1)
  '0.010000000000000002'
  </code></pre>
  
  <p>Why does <code>str</code> round but <code>repr</code> not? (I have read <a href="https://stackoverflow.com/a/2626364/562769">this answer</a>, but I would like to know how they have decided when <code>str</code> rounds a float and when it doesn't)</p>
  
  <pre><code>&gt;&gt;&gt; str(0.01) == str(0.0100000000001)
  False
  &gt;&gt;&gt; str(0.01) == str(0.01000000000001)
  True
  </code></pre>
  
  <p>So it seems to be a problem with the accuracy of floats. I thought Python would use IEEE 754 single precicion floats. So I've checked it like this:</p>
  
  <pre><code>#include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt; // printf
  
  union myUnion {
      uint32_t i; // unsigned integer 32-bit type (on every machine)
      float f;    // a type you want to play with
  };
  
  int main() {
      union myUnion testVar;
      testVar.f = 0.01000000000001f;
      printf("%f\n", testVar.f);
  
      testVar.f = 0.01000000000000002f;
      printf("%f\n", testVar.f);
  
      testVar.f = 0.01f*0.01f;
      printf("%f\n", testVar.f);
  }
  </code></pre>
  
  <p>I got:</p>
  
  <pre><code>0.010000
  0.010000
  0.000100
  </code></pre>
  
  <p>Python gives me:</p>
  
  <pre><code>&gt;&gt;&gt; 0.01000000000001
  0.010000000000009999
  &gt;&gt;&gt; 0.01000000000000002
  0.010000000000000019
  &gt;&gt;&gt; 0.01*0.01
  0.0001
  </code></pre>
  
  <p>Why does Python give me these results?</p>
  
  <p>(I use Python 2.6.5. If you know of differences in the Python versions, I would also be interested in them.)</p>