def test_fifth(self): "Nearest rounding of 1/5 is upwards." from operator import truediv assert 1 / 5.0 == fpu.up (lambda: truediv(1.0, 5.0)) assert 1 / 5.0 > fpu.down(lambda: truediv(1.0, 5.0)) assert -1 / 5.0 == fpu.down(lambda: truediv(1.0, -5.0)) assert -1 / 5.0 < fpu.up (lambda: truediv(1.0, -5.0))
def test_fourth(self): " 1/4 is exact." from operator import truediv assert 1 / 4.0 == fpu.down(lambda: truediv(1.0, 4.0)) assert 1 / 4.0 == fpu.up (lambda: truediv(1.0, 4.0)) assert -1 / 4.0 == fpu.up (lambda: truediv(1.0, -4.0)) assert -1 / 4.0 == fpu.down(lambda: truediv(1.0, -4.0))
def test_third(self): "Nearest rounding of 1/3 is downwards." from operator import truediv assert 1 / 3.0 == fpu.down(lambda: truediv(1.0, 3.0)) assert 1 / 3.0 < fpu.up (lambda: truediv(1.0, 3.0)) assert -1 / 3.0 == fpu.up (lambda: truediv(1.0, -3.0)) assert -1 / 3.0 > fpu.down(lambda: truediv(1.0, -3.0))
def test_power(self): x = 1 / 3.0 # The cube of one third should depend on the rounding mode assert fpu.down(lambda: x * x * x) < fpu.up(lambda: x * x * x) # But using the built-in power operator, it doesn't necessarily do it # fpu.down(lambda: x ** 3) < fpu.up(lambda: x ** 3)) # So we define an integer power methods that does assert fpu.power_rd( x, 3) < fpu.power_ru( x, 3) assert fpu.power_rd(-x, 3) < fpu.power_ru(-x, 3) assert fpu.power_rd( x, 4) < fpu.power_ru( x, 4) assert fpu.power_rd(-x, 4) < fpu.power_ru(-x, 4) assert (fpu.down(lambda: x * x * x), fpu.up(lambda: x * x * x)) == (fpu.power_rd(x, 3), fpu.power_ru(x, 3))
def test_power(self): self.assertRaises(TypeError, lambda: interval[1, 2] ** (1.3)) self.assertEqual((-interval[1, 2]).inverse(), (-interval[1, 2]) ** -1) self.assertEqual(interval[0, 4], interval[-1, 2] ** 2) self.assertEqual(interval[-27, 8], interval[-3, 2] ** 3) self.assertEqual(interval[-1, 2], (interval[-1,2]**-1)**-1) self.assertEqual(interval([-0.38712442133802405]) ** 3, interval([-0.058016524353106828, -0.058016524353106808])) self.assertEqual( interval[fpu.down(lambda: (1/3.0)*(1/3.0)), fpu.up(lambda: (1/3.0)*(1/3.0))], (interval[1]/3.0) ** 2) self.assertEqual( interval[fpu.down(lambda: (1/3.0)*(1/3.0)*(1/3.0)), fpu.up(lambda: (1/3.0)*(1/3.0)*(1/3.0))], (interval[1]/3.0) ** 3)
def test_power(self): self.assertRaises(TypeError, lambda: interval[1, 2] ** (1.3)) assert (-interval[1, 2]).inverse() == (-interval[1, 2]) ** -1 assert interval[0, 4] == interval[-1, 2] ** 2 assert interval[-27, 8] == interval[-3, 2] ** 3 assert interval[-1, 2] == (interval[-1, 2] ** -1) ** -1 assert interval([-0.38712442133802405]) ** 3 == interval([-0.058016524353106828, -0.058016524353106808]) from operator import truediv assert ( interval[ fpu.down(lambda: truediv(1, 3.0) * truediv(1, 3.0)), fpu.up (lambda: truediv(1, 3.0) * truediv(1, 3.0))] == (interval[1] / 3.0) ** 2) assert ( interval[ fpu.down(lambda: truediv(1, 3.0) * truediv(1, 3.0) * truediv(1, 3.0)), fpu.up(lambda: truediv(1, 3.0) * truediv(1, 3.0) * truediv(1, 3.0))] == (interval[1] / 3.0) ** 3)
def measure(x): from interval import fpu return fpu.up(lambda: sum((c.sup - c.inf for c in x), 0))
def test_fifth(self): "Nearest rounding of 1/5 is upwards." self.assertEqual(1/5.0, fpu.up(lambda: 1.0 / 5.0)) self.assertTrue(1/5.0 > fpu.down(lambda: 1.0 / 5.0)) self.assertEqual(-1/5.0, fpu.down(lambda: 1.0 / -5.0)) self.assertTrue(-1/5.0 < fpu.up(lambda: 1.0 / -5.0))
def test_fourth(self): " 1/4 is exact." self.assertEqual(1/4.0, fpu.down(lambda: 1.0 / 4.0)) self.assertEqual(1/4.0, fpu.up(lambda: 1.0 / 4.0)) self.assertEqual(-1/4.0, fpu.up(lambda: 1.0 / -4.0)) self.assertEqual(-1/4.0, fpu.down(lambda: 1.0 / -4.0))
def test_third(self): "Nearest rounding of 1/3 is downwards." self.assertEqual(1/3.0, fpu.down(lambda: 1.0 / 3.0)) self.assertTrue(1/3.0 < fpu.up(lambda: 1.0 / 3.0)) self.assertEqual(-1/3.0, fpu.up(lambda: 1.0 / -3.0)) self.assertTrue(-1/3.0 > fpu.down(lambda: 1.0 / -3.0))
def measure(x): """See https://github.com/taschini/pyinterval/issues/2""" return int(fpu.up(lambda: sum((c.sup - c.inf for c in x), 0)))