title: Why is the built-in python sum function behaving like this?
body: <p>I am trying to write a program that determines the pearson correlation coefficient with population standard deviation in python. I thought this would be pretty trivial until I got to the part where I was summing (yi -  μy)*(xi -  μx). Here is my full code:</p>
  
  <pre><code>def r(x, y):
      mx, my = sum(x) / len(x), sum(y) / len(y)
      sdx, sdy = (sum([(xi-mx)**2 for xi in x]) / len(x))**0.5, (sum([(yi- 
      my)**2 for yi in y]) / (len(y)))**0.5
      res = ((sum([(xi-mx)*(yi-my) for xi in x for yi in y]))/(len(x)*sdx*sdy))**0.5
      return res
  </code></pre>
  
  <p>I noticed the result was super small, so I checked out the sum of (xi-mx):</p>
  
  <pre><code>sum([(xi-mx) for xi in x])
  </code></pre>
  
  <p>and the result was -9.769962616701378e-15. Here are the values in the list:</p>
  
  <pre><code>print([(xi-mx) for xi in x])
  [3.2699999999999987, 3.0699999999999994, 1.2699999999999987, 1.0699999999999985, 0.9699999999999989, 0.2699999999999987, -0.7300000000000013, -1.7300000000000013, -2.7300000000000013, -4.730000000000001]
  </code></pre>
  
  <p>Can anyone explain why python is behaving so strangely with this? </p>