Example #1
0
    def test_add(self):
        f1 = Float(5.1)
        f2 = Float(10.1)
        assert f1.arith_add(f2).floatval == 15.2

        n0 = Number(1)
        n1 = Number(2)
        assert n0.arith_add(n1).num == 3

        n2 = Number(2)
        f3 = Float(3.2)
        assert n2.arith_add(f3).floatval == 5.2
        assert f3.arith_add(n2).floatval == 5.2

        b1 = BigInt(rbigint.fromdecimalstr('50000000000000000000000'))
        b2 = BigInt(rbigint.fromdecimalstr('10000000000000000000001'))
        assert b1.arith_add(b2).value.str() == '60000000000000000000001'

        n3 = Number(sys.maxint)
        assert n3.arith_add(n0).value.str() == str(sys.maxint + 1)

        b = BigInt(rbigint.fromdecimalstr('100000000000000000000000000000'))
        f = Float(1.5)
        assert b.arith_add(f).floatval == 100000000000000000000000000001.5
        assert f.arith_add(b).floatval == 100000000000000000000000000001.5

        assert b.arith_add(n0).value.tofloat() == 100000000000000000000000000001.0
        assert n0.arith_add(b).value.tofloat() == 100000000000000000000000000001.0
Example #2
0
 def test_min(self):
     assert Number(5).arith_min(Number(1)).num == 1
     assert Float(-1.32).arith_min(Float(4.5)).floatval == -1.32
     assert BigInt(rbigint.fromdecimalstr('111111111111111111111111111')).arith_min(BigInt(rbigint.fromdecimalstr('222222222222222222222222222222'))).value.str() == '111111111111111111111111111'
     assert Number(-1000).arith_min(BigInt(rbigint.fromint(-1001))).num == -1001
     assert BigInt(rbigint.fromint(-1001)).arith_min(Number(-1000)).num == -1001
     assert BigInt(rbigint.fromdecimalstr('10000')).arith_min(Float(20000)).floatval == 10000.0
     assert Float(20000).arith_min(BigInt(rbigint.fromdecimalstr('10000'))).floatval == 10000.0
Example #3
0
    def test_power(self):
        assert Number(5).arith_pow(Number(2)).num == 25
        assert Float(2.3).arith_pow(Float(3.1)).floatval == 13.223800591254721
        assert BigInt(rbigint.fromdecimalstr('1000000')).arith_pow(Number(4)).value.str() == '1000000000000000000000000'
        assert Float(10.0).arith_pow(BigInt(rbigint.fromdecimalstr('10'))).floatval == 10000000000.0
        assert BigInt(rbigint.fromdecimalstr('10')).arith_pow(Float(10.0)).floatval == 10000000000.0

        assert BigInt(rbigint.fromdecimalstr('1000000000000000')).arith_pow(Number(0)).num == 1
        assert Float(10000000000).arith_pow(BigInt(rbigint.fromdecimalstr('0'))).floatval == 1.0
Example #4
0
 def test_float_misc(self):
     assert Float(7.4).arith_round().num == 7.0
     assert Float(7.5).arith_round().num == 8.0
     assert Float(7.4).arith_floor().num == 7.0
     assert Float(7.9).arith_floor().num == 7.0
     assert Float(7.4).arith_ceiling().num == 8.0
     assert Float(7.4).arith_float_fractional_part().floatval == 7.4 - 7
     assert Float(7.4).arith_float_integer_part().num == 7.0
Example #5
0
 def test_mul(self):
     assert Number(5).arith_mul(Number(100)).num == 500
     assert Number(5).arith_mul(BigInt(rbigint.fromdecimalstr('1000000000000000000000000000000'))).value.tofloat() == 5000000000000000000000000000000.0
     assert Number(-10).arith_mul(Float(-7.3)).floatval == 73
     assert BigInt(rbigint.fromdecimalstr('-1000000000000000000000000000')).arith_mul(BigInt(rbigint.fromdecimalstr('100000000000000000000'))).value.str() == '-100000000000000000000000000000000000000000000000'
     assert Float(6.7).arith_mul(Float(-2.4)).floatval == -16.08
     assert Float(6.7).arith_mul(BigInt(rbigint.fromdecimalstr('100000000000000000000000000000000000'))).floatval == 670000000000000000000000000000000000.0
     assert BigInt(rbigint.fromdecimalstr('100000000000000000000000000000000000')).arith_mul(Float(6.7)).floatval == 670000000000000000000000000000000000.0
     assert Number(2).arith_mul(Float(2.5)).floatval == 5
     assert Float(2.5).arith_mul(Number(2)).floatval == 5
Example #6
0
    def test_sub(self):
        n1 = Number(5)
        n2 = Number(10)
        assert n1.arith_sub(n2).num == -5
        assert n2.arith_sub(n1).num == 5

        f1 = Float(10.5)
        f2 = Float(30.6)
        assert f1.arith_sub(f2).floatval == -20.1
        assert f2.arith_sub(f1).floatval == 20.1

        b1 = BigInt(rbigint.fromdecimalstr('10000000000000000000000000000000000000'))
        b2 = BigInt(rbigint.fromdecimalstr('20000000000000000000000000000000000000'))
        assert b1.arith_sub(b2).value.tofloat() == -10000000000000000000000000000000000000.0
        assert b2.arith_sub(b1).value.tofloat() == 10000000000000000000000000000000000000.0
        assert BigInt(rbigint.fromdecimalstr('100000000000000000000')).arith_sub(Number(1)).value.str() == '99999999999999999999'
        assert Number(5).arith_sub(BigInt(rbigint.fromdecimalstr('5'))).num == 0
        assert BigInt(rbigint.fromdecimalstr('1000000000000000')).arith_sub(Float(500000000000000)).floatval == 500000000000000.0
        assert Float(2000000000000000).arith_sub(BigInt(rbigint.fromdecimalstr('1000000000000000'))).floatval == 1000000000000000.0
Example #7
0
    def test_div(self):
        assert Number(5).arith_div(Number(2)).num == 2
        assert Number(15).arith_div(Number(5)).num == 3
        assert Number(5).arith_div(Float(2.5)).floatval == 2.0
        assert Float(2.5).arith_div(Number(5)).floatval == 0.5
        assert Float(-10).arith_div(Float(2.5)).floatval == -4.0
        assert BigInt(rbigint.fromdecimalstr('50000000000000000')).arith_div(BigInt(rbigint.fromdecimalstr('25000000000000000'))).num == 2
        assert BigInt(rbigint.fromdecimalstr('100000000000000000000')).arith_div(Float(100000000000000000000.0)).floatval == 1.0
        assert Float(100000000000000000000).arith_div(BigInt(rbigint.fromdecimalstr('100000000000000000000'))).floatval == 1.0
        assert Number(5).arith_div(BigInt(rbigint.fromdecimalstr('5'))).num == 1
        assert BigInt(rbigint.fromdecimalstr('5')).arith_div(Number(5)).num == 1

        py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr(\'1\')).arith_div(BigInt(rbigint.fromdecimalstr(\'0\')))')
        py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr(\'1\')).arith_div(Number(0))')
        py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr(\'1\')).arith_div(Float(0))')
        py.test.raises(error.CatchableError, 'Float(1).arith_div(Number(0))')
        py.test.raises(error.CatchableError, 'Number(1).arith_div(Number(0))')
        py.test.raises(error.CatchableError, 'Number(1).arith_div(Float(0))')
Example #8
0
 def _get_list(self, i):
     from prolog.interpreter.term import Float
     assert i == 0
     return Float(self.vals_flo_0)
Example #9
0
 def test_abs(self):
     assert Number(-345345345).arith_abs().num == 345345345
     assert Float(345345.435).arith_abs().floatval == 345345.435
     assert Float(-345345.435).arith_abs().floatval == 345345.435
     assert BigInt(rbigint.fromdecimalstr('-123123123123123123123123123')).arith_abs().value.str() == '123123123123123123123123123'