Commits

Thomas Kluyver committed c7ec9cf

Use numpy.testing.assert_almost_equal in math_test, to handle limitations of
floating point accuracy.

Comments (0)

Files changed (1)

test/math_test.py

 from random import random
 import gc
 
+# Some vector calculations are slightly inaccurate due to limitations of
+# floating point maths. This function allows an error of 10^-7 by default.
+from numpy.testing import assert_almost_equal
+
 class Vector2TypeTest(unittest.TestCase):
     def setUp(self):
 #        gc.collect()
         self.assertEqual(self.v1.cross(self.v1), 0)
 
     def test_dot(self):
-        self.assertEqual(self.v1.dot(self.v2),
-                         self.v1.x * self.v2.x + self.v1.y * self.v2.y)
-        self.assertEqual(self.v1.dot(self.l2),
-                         self.v1.x * self.l2[0] + self.v1.y * self.l2[1])
-        self.assertEqual(self.v1.dot(self.t2),
-                         self.v1.x * self.t2[0] + self.v1.y * self.t2[1])
+        assert_almost_equal(self.v1.dot(self.v2),
+                            self.v1.x * self.v2.x + self.v1.y * self.v2.y)
+        assert_almost_equal(self.v1.dot(self.l2),
+                            self.v1.x * self.l2[0] + self.v1.y * self.l2[1])
+        assert_almost_equal(self.v1.dot(self.t2),
+                            self.v1.x * self.t2[0] + self.v1.y * self.t2[1])
         self.assertEqual(self.v1.dot(self.v2), self.v2.dot(self.v1))
         self.assertEqual(self.v1.dot(self.v2), self.v1 * self.v2)
 
     def test_distance_to(self):
         diff = self.v1 - self.v2
         self.assertEqual(self.e1.distance_to(self.e2), math.sqrt(2))
-        self.assertEqual(self.v1.distance_to(self.v2),
-                         math.sqrt(diff.x * diff.x + diff.y * diff.y))
+        assert_almost_equal(self.v1.distance_to(self.v2),
+                            math.sqrt(diff.x * diff.x + diff.y * diff.y))
         self.assertEqual(self.v1.distance_to(self.v1), 0)
         self.assertEqual(self.v1.distance_to(self.v2),
                          self.v2.distance_to(self.v1))
     def test_distance_squared_to(self):
         diff = self.v1 - self.v2
         self.assertEqual(self.e1.distance_squared_to(self.e2), 2)
-        self.assertEqual(self.v1.distance_squared_to(self.v2),
-                         diff.x * diff.x + diff.y * diff.y)
+        assert_almost_equal(self.v1.distance_squared_to(self.v2),
+                            diff.x * diff.x + diff.y * diff.y)
         self.assertEqual(self.v1.distance_squared_to(self.v1), 0)
         self.assertEqual(self.v1.distance_squared_to(self.v2),
                          self.v2.distance_squared_to(self.v1))
     def test_distance_squared_to(self):
         diff = self.v1 - self.v2
         self.assertEqual(self.e1.distance_squared_to(self.e2), 2)
-        self.assertEqual(self.v1.distance_squared_to(self.v2),
-                         diff.x * diff.x + diff.y * diff.y + diff.z * diff.z)
+        assert_almost_equal(self.v1.distance_squared_to(self.v2),
+                            diff.x * diff.x + diff.y * diff.y + diff.z * diff.z)
         self.assertEqual(self.v1.distance_squared_to(self.v1), 0)
         self.assertEqual(self.v1.distance_squared_to(self.v2),
                          self.v2.distance_squared_to(self.v1))