def testReprFixed0(self): "repr is the underlying _value" A = V.ArithmeticClass( Options(dict(arithmetic='fixed', precision=self.p))) self.assertEqual(repr(A(1)), '1') A = V.ArithmeticClass(Options(dict(arithmetic='integer'))) self.assertEqual(repr(A(1)), '1')
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 testBigDisplay(self): "display is limited to precision+guard" p = 5 g = 6 d = p + g + 1 A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=p, guard=g, display=d))) self.assertEqual(A.display, p+g) self.assertEqual(str(A(20)/A(3)), '6.66666_666666')
def testUnaryOps(self): "test unary + and -" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=4))) x = A(1) y = A(2) self.assertEqual(x-y, -x) self.assertEqual(y-x, +x) self.assertEqual(abs(x-y), x)
def testMulDiv(self): "guarded muldiv" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9))) f13 = A(1)/A(3) f15 = A(1)/A(5) f17 = A(1)/A(7) self.assertEqual(A.muldiv(f13, f15, f17), f13*f15/f17) self.assertEqual(str(f13*f15/f17), '0.466666667') self.assertEqual(str(A.muldiv(f13, f15, f17)), '0.466666667')
def testMulDiv0(self): "guarded muldiv with g=0" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9, guard=0))) f13 = A(1)/A(3) f15 = A(1)/A(5) f17 = A(1)/A(7) self.assertEqual(A.muldiv(f13, f15, f17)._value, (f13*f15/f17)._value+5) self.assertEqual(A.muldiv(f13, f15, f17, round='down')._value, (f13*f15/f17)._value+5) self.assertEqual(A.muldiv(f13, f15, f17, round='up')._value, (f13*f15/f17)._value+6) self.assertEqual(str(f13*f15/f17), '0.466666664') self.assertEqual(str(A.muldiv(f13, f15, f17)), '0.466666669')
def testNoHash(self): "test guarded/int" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9))) x = A(2) self.assertRaises(NotImplementedError, hash, x)
def testMulInt(self): "test guarded*int" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9))) x = A(2) y = A(3) self.assertEqual(x*y, x*3)
def testFixedDisplay2(self): "fixed display != precision gets a mention in info" V.ArithmeticClass(Options(dict(arithmetic='fixed', precision=6, display=5))) self.assertTrue(F.info.find('display') > 0)
def testValueInitRational(self): "class Rational if arithmetic=rational" self.assertEqual(V.ArithmeticClass(Options(dict(arithmetic='rational'))), R)
def testValueInitRationalDefault(self): "default class Guarded" self.assertEqual(V.ArithmeticClass(Options(dict(precision=8))), G)
def testDisplay(self): "display defaults to precision" p = 5 A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=p))) self.assertEqual(A.display, p) self.assertEqual(str(A(20)/A(3)), '6.66667')
def testGeps(self): "geps is a function of guard" p = 9 g = p A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=p))) self.assertEqual(A._Guarded__geps, 10**g/2)
def testScale(self): "scale is a function of precision and guard" p = 9 g = p A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=p))) self.assertEqual(A._Guarded__scale, 10**(p+g))
def testExact(self): "guarded is exact" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9))) self.assertEqual(A.exact, True)
def setUp(self): "initialize fixed class" V.ArithmeticClass(Options(dict(arithmetic='fixed', precision=self.p, guard=self.g)))
def setUp(self): "initialize fixed point 0 places" self.A = V.ArithmeticClass(Options(dict(arithmetic='fixed', precision=self.p, guard=self.g)))
def testSetvalFixed6(self): "set a fixed value directly" A = V.ArithmeticClass(Options(dict(arithmetic='fixed', precision=6))) f = A(123456, True) self.assertEqual(repr(A(f)), 'Fixed(123456,True)') self.assertEqual(A(1000000, True), A(1))
def testRepr(self): "repr is the underlying _value" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9))) self.assertEqual(repr(A(1)), 'Guarded(1000000000000000000,True)')
def testNe(self): "not equal" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9))) self.assertFalse(A(1)!=A(1))
def testFixedInteger(self): "fixed precision 0 means integer" V.ArithmeticClass(Options(dict(arithmetic='fixed', precision=0))) self.assertEqual(F.tag(), 'integer')
def testValueInitFixed(self): "class Fixed if arithmetic=fixed" self.assertEqual(V.ArithmeticClass(Options(dict(arithmetic='fixed', precision=8))), F)
def testDivInt(self): "test guarded/int" A = V.ArithmeticClass(Options(dict(arithmetic='guarded', precision=9))) x = A(2) y = A(3) self.assertEqual(x/y, x/3)
def testFixedIntegerP0(self): "fixed=integer yields precision 0" V.ArithmeticClass(Options(dict(arithmetic='integer'))) self.assertEqual(F.precision, 0)
def testFixedDisplay1(self): "fixed display must be <= precision" V.ArithmeticClass(Options(dict(arithmetic='fixed', precision=6, display=7))) self.assertEqual(F.display, 6) self.assertTrue(F.info.find('display') < 0)