title: what is a better way to check if two complex numbers are identical in python?
body: <p>While testing mpi4py's <code>comm.reduce()</code> and <code>comm.Reduce()</code> methods in python 2.7.3 I encountered the following behaviour:</p>
  
  <ul>
  <li><p>sometimes subtracting two complex numbers (type 'numpy.complex128', which are the output of some parallel calculation) that appear identical when printed on the screen produces a non-zero result</p></li>
  <li><p>comparing them with <code>==</code> occasionally yields <code>False</code>.</p></li>
  </ul>
  
  <p>Example:</p>
  
  <pre><code>print z1, z2, z1-z2
  (0.268870295763-0.268490433604j) (0.268870295763-0.268490433604j) 0j
  print z1 == z2
  True
  </code></pre>
  
  <p>but then</p>
  
  <pre><code>print z1, z2, z1-z2
  (0.226804302192-0.242683516175j) (0.226804302192-0.242683516175j) (-2.77555756156e-17+5.55111512313e-17j)
  print z1 == z2
  False
  </code></pre>
  
  <p>I figured this had something to do with the finite precision of floats, so I resorted to just checking whether the difference <code>abs(z1-z2)</code> is bigger than 1e-16 (it never was - which is what one would expect if <code>reduce()</code> and <code>Reduce()</code> are equivalent). (EDIT: this is actually not a good way to check for equality. See here: <a href="https://stackoverflow.com/questions/5595425/what-is-the-best-way-to-compare-floats-for-almost-equality-in-python">What is the best way to compare floats for almost-equality in Python?</a>)</p>
  
  <p>I was wondering if there's a more straightforward way to compare complex numbers in python.</p>
  
  <p>Also, why does this behaviour arise? After all, a float (and as far as I know a complex is basically a tuple of two floats) is stored on the machine in binary, as a sequence of bits. Isn't it true that if the two numbers are represented by the same sequence in binary, the difference should be zero and the comparison with <code>==</code> should yield <code>True</code>?</p>
  
  <p>EDIT: OK, I found this <a href="https://stackoverflow.com/questions/5595425/what-is-the-best-way-to-compare-floats-for-almost-equality-in-python">What is the best way to compare floats for almost-equality in Python?</a>, which basically boils down to the same thing.</p>
  
  <p>But then the last part of the question remains: Why do floats work like that if in binary they are all basically represented by integers?</p>