title: Unittest (sometimes) fails because floating-point imprecision
body: <p>I have a class <em>Vector</em> that represents a point in 3-dimensional space. This vector has a method <code>normalize(self, length = 1)</code> which scales the vector down/up to be <code>length == vec.normalize(length).length</code>.</p>
  
  <p>The unittest for this method <strong>sometimes</strong> fails because of the imprecision of floating-point numbers. My question is, how can I make sure this test does not fail when the methods are implemented correctly? Is it possible to do it <strong><em>without</em></strong> testing for an approximate value?</p>
  
  <p><br/></p>
  
  <hr>
  
  <p><strong>Additional information</strong>:</p>
  
  <pre><code>    def testNormalize(self):
          vec = Vector(random.random(), random.random(), random.random())
          self.assertEqual(vec.normalize(5).length, 5)
  </code></pre>
  
  <p>This <strong><em>sometimes</em></strong> results in either <code>AssertionError: 4.999999999999999 != 5</code> or <code>AssertionError: 5.000000000000001 != 5</code>.</p>
  
  <p><em>Note</em>: I am aware that the floating-point issue may be in the <code>Vector.length</code> property or in <code>Vector.normalize()</code>.</p>