def testAbsolute(self): x = random.uniform(-23, 42) for tol in (1e-13, 1e-12, 1e-10, 1e-5): # Test error < tol. self.assertTrue(approx_equal(x, x+tol/2, tol=tol, rel=None)) self.assertTrue(approx_equal(x, x-tol/2, tol=tol, rel=None)) # Test error > tol. self.assertFalse(approx_equal(x, x+tol*2, tol=tol, rel=None)) self.assertFalse(approx_equal(x, x-tol*2, tol=tol, rel=None))
def testUnequal(self): for _ in range(20): a = b = random.uniform(-1000, 1000) while b == a: b = random.uniform(-1000, 1000) assert a != b self.assertFalse(approx_equal(a, b)) self.assertFalse(approx_equal(a, b, tol=None)) self.assertFalse(approx_equal(a, b, rel=None)) self.assertFalse(approx_equal(a, b, tol=None, rel=None))
def testRelative(self): for x in (1e-10, 1.1, 123.456, 1.23456e18, -17.98): for rel in (1e-2, 1e-4, 1e-7, 1e-9): # Test error < rel. delta = x*rel/2 self.assertTrue(approx_equal(x, x+delta, tol=None, rel=rel)) self.assertTrue(approx_equal(x, x+delta, tol=None, rel=rel)) # Test error > rel. delta = x*rel*2 self.assertFalse(approx_equal(x, x+delta, tol=None, rel=rel)) self.assertFalse(approx_equal(x, x+delta, tol=None, rel=rel))
def testEqual(self): for x in (-123.456, -1.1, 0.0, 0.5, 1.9, 23.42, 1.2e68, -1, 0, 1): self.assertTrue(approx_equal(x, x)) self.assertTrue(approx_equal(x, x, tol=None)) self.assertTrue(approx_equal(x, x, rel=None)) self.assertTrue(approx_equal(x, x, tol=None, rel=None))