def testFixedDisplay3(self): "fixed display < precision rounds properly" V.ArithmeticClass( Options(dict(arithmetic='fixed', precision=6, display=6))) self.assertEqual(str(F(20) / F(3)), '6.666666') V.ArithmeticClass( Options(dict(arithmetic='fixed', precision=7, display=6))) self.assertEqual(str(F(20) / F(3)), '6.666667')
def testG0(self): "basic equalities" f1 = F(1) f2 = F(2) f3 = F(3) f7 = F(7) g1 = G(1) g2 = G(2) g3 = G(3) g7 = G(7) self.assertEqual(str(f1), str(g1)) self.assertEqual(str(f1/f3), str(g1/g3)) self.assertEqual(str(f2/f3), str(g2/g3)) self.assertEqual(F.mul(f2, f3, round='down'), G.mul(g2, g3, round='down')) self.assertEqual(F.mul(f2, f3, round='up'), G.mul(g2, g3, round='up')) self.assertEqual(F.div(f2, f3, round='down'), G.div(g2, g3, round='down')) self.assertEqual(F.div(f2, f3, round='up'), G.div(g2, g3, round='up')) self.assertEqual(F.muldiv(f2, f3, f7, round='down'), G.muldiv(g2, g3, g7, round='down')) self.assertEqual(F.muldiv(f2, f3, f7, round='up'), G.muldiv(g2, g3, g7, round='up'))
def testFixed6(self): "simple assertions" self.assertEqual(self.A.name, 'fixed') # Fixed.name self.assertEqual(self.A.exact, False) # Fixed.exact self.assertEqual(self.A.precision, self.p) # Fixed.precision self.assertEqual(self.A._Fixed__scale, 10**self.p) # Fixed.__scale self.assertEqual(self.A(0)._value, 0) # 0 self.assertEqual(self.A(1)._value, 10**self.p) # 1 self.assertEqual(self.A(100)._value, 100*10**self.p) # 100 self.assertEqual((self.A(1)/self.A(10))._value, 10**(self.p-1)) # 1/10 x = self.A(1) y = self.A(2) self.assertEqual(-x, x-y) self.assertEqual(+x, y-x) self.assertEqual(abs(x-y), x) self.assertEqual(y/1, y/x) self.assertRaises(ValueError, self.A.mul, x, y, 'bad') self.assertRaises(ValueError, self.A.muldiv, x, y, y, 'bad') f13 = F(1)/F(3) f15 = F(1)/F(5) f17 = F(1)/F(7) self.assertTrue(F.muldiv(f13, f15, f17, round='down') > f13*f15/f17) self.assertTrue(F.muldiv(f13, f15, f17, round='up') > F.muldiv(f13, f15, f17, round='down')) self.assertEqual(str(f13*f15/f17), '0.466662') self.assertEqual(str(F.muldiv(f13, f15, f17, round='down')), '0.466666') self.assertEqual(str(F.muldiv(f13, f15, f17, round='up')), '0.466667') self.assertTrue(x != y) self.assertEqual(str(x/y), '0.500000')
def testRoundNone(self): "round= must be present" self.assertRaises(ValueError, F.div, F(1), F(3))
def testRoundBad(self): "round= must be up or down" self.assertRaises(ValueError, F.div, F(1), F(3), round='bad')
def testRoundUpExact(self): "up is bigger by an epsilon but not if exact result" self.assertEqual(F.div(F(2), F(4), round='up')._value, 500)
def testRoundDownExact(self): "no rounding if exact result" self.assertEqual(F.div(F(2), F(4), round='down')._value, 500)
def testRoundUp(self): "up is bigger by an epsilon" self.assertEqual(F.div(F(1), F(3), round='up')._value, 334)
def testRoundDown(self): "down is same as default" self.assertEqual(F.div(F(1), F(3), round='down')._value, 333)
def testRoundFloor(self): "default rounding is truncation/floor" self.assertEqual((F(1)/F(3))._value, 333)
def testFixed0(self): "simple assertions" self.assertEqual(self.A.name, 'integer') # Fixed.name x = F(1) self.assertEqual(str(x), '1')