title: Python floating point error that has left me puzzled
body: <p>I just recently ran into a problem where I needed to append numbers to a list only if they weren't in the list already, and then I had to run those numbers through a comparison later on. The problem arises in floating point arithmetic errors. To illustrate what is basically happening in my code:</p>
  
  <pre><code>_list = [5.333333333333333, 6.666666666666667, ...]
  number = some_calculation()
  if number not in _list:
      _list.append(number) #note that I can't use a set to remove
                           #duplicates because order needs to be maintained
  new_list = []
  for num in _list:
      if some_comparison(num): #note that I can't combine 'some_comparison' with the
          new_list.append(num) #above check to see if the item is already in the list
  </code></pre>
  
  <p>The problem is that <code>some_calculation()</code> would sometimes generate an inexact number, such as 5.333333333333332, which is, as far as my calculations need to go, the same as the first element in <code>_list</code> in this example. The solution I had in mind was to simply round all the numbers generated to 9 or so decimal places. This worked for a short amount of time, until I realized that <code>some_comparison</code> compares <code>num</code> against, again, an inexact calculation. Even if I didn't round the numbers in <code>_list</code>, <code>some_comparison</code> would still return an inexact value and thus would evaluate to False. </p>
  
  <p>I am absolutely puzzled. I've never had to worry about floating point errors so this problem is quite infuriating. Does anyone have any ideas for solutions?</p>
  
  <p>NOTE: I would post the actual code, but it's very convoluted and requires 7 or 8 different functions and classes I made specifically for this purpose, and reposting them here would be a hassle.</p>