Пример #1
0
 def test_gcdext(self):
     assert gcdext(15, 25) == (mpz(5), mpz(2), mpz(-1))
     assert gcdext(323, 340) == gcdext(mpz(323), 340) == gcdext(323, mpz(340)) == gcdext(mpz(323), mpz(340)) == (mpz(17), mpz(-1), mpz(1))
     with pytest.raises(TypeError):
         gcdext(mpq(1.5), 2)
     with pytest.raises(TypeError):
         gcd(3)
Пример #2
0
 def test_next_prime(self):
     assert next_prime(3) == mpz(5)
     assert next_prime(mpz(10000)) == mpz(10007)
     assert next_prime(-4) == mpz(2)
     assert next_prime(453425342532454325324532453245) == mpz(453425342532454325324532453293)
     with pytest.raises(TypeError):
         next_prime(mpq(1, 4))
Пример #3
0
 def test_invert(self):
     assert invert(4, 5) == mpz(4)
     assert invert(3, 10) == mpz(7)
     with pytest.raises(ZeroDivisionError):
         invert(4, 6)
     with pytest.raises(ZeroDivisionError):
         invert(4, 0)
Пример #4
0
 def test_fac(self):
     assert fac(0) == 1
     assert fac(10) == fac(mpz(10)) == mpz(3628800)
     with pytest.raises(ValueError):
         fac(-1)
     with pytest.raises(TypeError):
         fac(45894575342551390123)
Пример #5
0
 def test_rpow_invalid(self):
     with pytest.raises(ValueError) as exc:
         1 ** mpz(-1)
     assert exc.value.args == ('mpz.pow with negative exponent',)
     with pytest.raises(ValueError) as exc:
         1 ** mpz(MAX_UI + 1)
     assert exc.value.args == ('mpz.pow with outragous exponent',)
Пример #6
0
 def test_sinh_cosh(self):
     assert sinh_cosh(0.5) == sinh_cosh(mpfr(0.5)) == (sinh(0.5), cosh(0.5))
     assert sinh_cosh(0) == (mpfr(0.0), mpfr(1.0))
     assert sinh_cosh(mpq(1, 3)) == (sinh(mpq(1, 3)), cosh(mpq(1, 3)))
     assert sinh_cosh(mpz(3)) == (sinh(mpz(3)), cosh(mpz(3)))
     with pytest.raises(TypeError):
         sinh_cosh([])
Пример #7
0
 def test_sinh_cosh(self):
     assert sinh_cosh(0.5) == sinh_cosh(mpfr(0.5)) == (sinh(0.5), cosh(0.5))
     assert sinh_cosh(0) == (mpfr(0.0), mpfr(1.0))
     assert sinh_cosh(mpq(1, 3)) == (sinh(mpq(1, 3)), cosh(mpq(1, 3)))
     assert sinh_cosh(mpz(3)) == (sinh(mpz(3)), cosh(mpz(3)))
     with pytest.raises(TypeError):
         sinh_cosh([])
Пример #8
0
 def test_fac(self):
     assert fac(0) == 1
     assert fac(10) == fac(mpz(10)) == mpz(3628800)
     with pytest.raises(ValueError):
         fac(-1)
     with pytest.raises(TypeError):
         fac(45894575342551390123)
Пример #9
0
 def test_rpow_invalid(self):
     with pytest.raises(ValueError) as exc:
         1**mpz(-1)
     assert exc.value.args == ('mpz.pow with negative exponent', )
     with pytest.raises(ValueError) as exc:
         1**mpz(MAX_UI + 1)
     assert exc.value.args == ('mpz.pow with outragous exponent', )
Пример #10
0
 def test_pow_invalid(self):
     with pytest.raises(TypeError):
         mpz(2)**2.0
     with pytest.raises(TypeError):
         2.0**mpz(2)
     with pytest.raises(TypeError):
         pow(mpz(2), 2, 2.0)
Пример #11
0
 def test_invert(self):
     assert invert(4, 5) == mpz(4)
     assert invert(3, 10) == mpz(7)
     with pytest.raises(ZeroDivisionError):
         invert(4, 6)
     with pytest.raises(ZeroDivisionError):
         invert(4, 0)
Пример #12
0
 def test_pow_invalid(self):
     with pytest.raises(TypeError):
         mpz(2) ** 2.0
     with pytest.raises(TypeError):
         2.0 ** mpz(2)
     with pytest.raises(TypeError):
         pow(mpz(2), 2, 2.0)
Пример #13
0
 def test_next_prime(self):
     assert next_prime(3) == mpz(5)
     assert next_prime(mpz(10000)) == mpz(10007)
     assert next_prime(-4) == mpz(2)
     assert next_prime(453425342532454325324532453245) == mpz(
         453425342532454325324532453293)
     with pytest.raises(TypeError):
         next_prime(mpq(1, 4))
Пример #14
0
 def test_atan2(self):
     assert atan2(1, 2) == atan2(1.0, 2) == atan2(mpfr(1), 2) == mpfr('0.46364760900080609')
     assert atan2(1.5, mpfr(3.1)) == atan2(1.5, 3.1) == mpfr('0.45066132608063364')
     assert atan2(mpq(1, 2), 0.5) == atan2(0.5, mpq(1, 2)) == atan2(0.5, 0.5)
     assert atan2(mpz(3), mpz(2)) == atan2(3, 2) == mpfr('0.98279372324732905')
     with pytest.raises(TypeError):
         atan2(1.4, [])
     with pytest.raises(TypeError):
         atan2([], 1.4)
Пример #15
0
 def test_mod(self, b):
     if b != 0:
         assert mpz(2) % mpz(b) == mpz(2 % b)
         assert mpz(2) % b == mpz(2 % b)
     else:
         with pytest.raises(ZeroDivisionError):
             mpz(2) % mpz(b)
         with pytest.raises(ZeroDivisionError):
             mpz(2) % b
Пример #16
0
 def test_floordiv(self, b):
     if b != 0:
         assert mpz(2) // mpz(b) == mpz(2 // b)
         assert mpz(2) // b == mpz(2 // b)
     else:
         with pytest.raises(ZeroDivisionError):
             mpz(2) // mpz(b)
         with pytest.raises(ZeroDivisionError):
             mpz(2) // b
Пример #17
0
 def test_mod(self, b):
     if b != 0:
         assert mpz(2) % mpz(b) == mpz(2 % b)
         assert mpz(2) % b == mpz(2 % b)
     else:
         with pytest.raises(ZeroDivisionError):
             mpz(2) % mpz(b)
         with pytest.raises(ZeroDivisionError):
             mpz(2) % b
Пример #18
0
 def test_gcdext(self):
     assert gcdext(15, 25) == (mpz(5), mpz(2), mpz(-1))
     assert gcdext(323, 340) == gcdext(mpz(323), 340) == gcdext(
         323, mpz(340)) == gcdext(mpz(323),
                                  mpz(340)) == (mpz(17), mpz(-1), mpz(1))
     with pytest.raises(TypeError):
         gcdext(mpq(1.5), 2)
     with pytest.raises(TypeError):
         gcd(3)
Пример #19
0
 def test_floordiv(self, b):
     if b != 0:
         assert mpz(2) // mpz(b) == mpz(2 // b)
         assert mpz(2) // b == mpz(2 // b)
     else:
         with pytest.raises(ZeroDivisionError):
             mpz(2) // mpz(b)
         with pytest.raises(ZeroDivisionError):
             mpz(2) // b
Пример #20
0
 def test_bincoef(self):
     assert bincoef(1, 4) == mpz(0)
     assert bincoef(19, 3) == mpz(969)
     assert bincoef(mpz(5), 3) == bincoef(5, mpz(3)) == mpz(10)
     with pytest.raises(ValueError):
         bincoef(1, -3)
     with pytest.raises(TypeError):
         bincoef(1, 2534253426342543253426)
     with pytest.raises(TypeError):
         bincoef(1.5, 3)
Пример #21
0
 def test_bincoef(self):
     assert bincoef(1, 4) == mpz(0)
     assert bincoef(19, 3) == mpz(969)
     assert bincoef(mpz(5), 3) == bincoef(5, mpz(3)) == mpz(10)
     with pytest.raises(ValueError):
         bincoef(1, -3)
     with pytest.raises(TypeError):
         bincoef(1, 2534253426342543253426)
     with pytest.raises(TypeError):
         bincoef(1.5, 3)
Пример #22
0
 def test_sin_cos(self):
     assert sin_cos(0.5) == sin_cos(mpfr(0.5)) == (sin(0.5), cos(0.5))
     assert sin_cos(0) == (mpfr(0.0), mpfr(1.0))
     assert sin_cos(mpq(1, 3)) == (sin(mpq(1, 3)), cos(mpq(1, 3)))
     assert sin_cos(mpz(3)) == (sin(mpz(3)), cos(mpz(3)))
     with pytest.raises(TypeError):
         sin_cos([])
     assert sin_cos(0.5+0.7j) == (
         mpc('0.60176007656391672+0.66571982846862043j'),
         mpc('1.1015144315669947-0.36368439983078849j'))
     assert sin_cos(mpc(0.5+0.7j)) == (sin(mpc(0.5+0.7j)), cos(mpc(0.5+0.7j)))
Пример #23
0
 def test_sin_cos(self):
     assert sin_cos(0.5) == sin_cos(mpfr(0.5)) == (sin(0.5), cos(0.5))
     assert sin_cos(0) == (mpfr(0.0), mpfr(1.0))
     assert sin_cos(mpq(1, 3)) == (sin(mpq(1, 3)), cos(mpq(1, 3)))
     assert sin_cos(mpz(3)) == (sin(mpz(3)), cos(mpz(3)))
     with pytest.raises(TypeError):
         sin_cos([])
     assert sin_cos(0.5 + 0.7j) == (
         mpc('0.60176007656391672+0.66571982846862043j'),
         mpc('1.1015144315669947-0.36368439983078849j'))
     assert sin_cos(mpc(0.5 + 0.7j)) == (sin(mpc(0.5 + 0.7j)),
                                         cos(mpc(0.5 + 0.7j)))
Пример #24
0
 def test_init_mpz(self):
     assert mpq(mpz(3), 6) == mpq(1, 2)
     assert mpq(mpz(2), sys.maxsize) == mpq(2, sys.maxsize)
     assert mpq(mpz(4), 2*sys.maxsize) == mpq(2, sys.maxsize)
     assert mpq(mpz(8), 4*sys.maxsize) == mpq(2, sys.maxsize)
     assert mpq(mpz(3), mpz(5)) == mpq(3, 5)
     assert mpq(mpz(3), mpq(5, 7)) == mpq(21, 5)
     assert mpq(mpz(3), 0.5) == mpq(6, 1)
     assert mpq(mpz(3), 0.6) == mpq(27021597764222976,5404319552844595)
Пример #25
0
 def test_atan2(self):
     assert atan2(1, 2) == atan2(1.0, 2) == atan2(
         mpfr(1), 2) == mpfr('0.46364760900080609')
     assert atan2(1.5, mpfr(3.1)) == atan2(
         1.5, 3.1) == mpfr('0.45066132608063364')
     assert atan2(mpq(1, 2), 0.5) == atan2(0.5, mpq(1, 2)) == atan2(
         0.5, 0.5)
     assert atan2(mpz(3), mpz(2)) == atan2(3,
                                           2) == mpfr('0.98279372324732905')
     with pytest.raises(TypeError):
         atan2(1.4, [])
     with pytest.raises(TypeError):
         atan2([], 1.4)
Пример #26
0
 def test_pow_no_mod(self, b):
     if b < 0:
         for exp in [mpz(b), b]:
             with pytest.raises(ValueError) as exc:
                 mpz(2) ** exp
             assert exc.value.args == ('mpz.pow with negative exponent',)
     elif b > MAX_UI:
         for exp in [mpz(b), b]:
             with pytest.raises(ValueError) as exc:
                 mpz(2) ** exp
             assert exc.value.args == ('mpz.pow with outragous exponent',)
     else:
         res = mpz(2 ** b)
         assert mpz(2) ** mpz(b) == res
         assert mpz(2) ** b == res
Пример #27
0
 def test_pow_no_mod(self, b):
     if b < 0:
         for exp in [mpz(b), b]:
             with pytest.raises(ValueError) as exc:
                 mpz(2)**exp
             assert exc.value.args == ('mpz.pow with negative exponent', )
     elif b > MAX_UI:
         for exp in [mpz(b), b]:
             with pytest.raises(ValueError) as exc:
                 mpz(2)**exp
             assert exc.value.args == ('mpz.pow with outragous exponent', )
     else:
         res = mpz(2**b)
         assert mpz(2)**mpz(b) == res
         assert mpz(2)**b == res
Пример #28
0
 def test_pow_int(self):
     assert mpq(2,3) ** 3 == mpq(2,3) ** mpz(3) == mpq(8,27)
     assert mpq(-2,3) ** 2 == mpq(-2,3) ** mpz(2) == mpq(4,9)
     assert mpq(-2,3) ** 3 == mpq(-2,3) ** mpz(3) == mpq(-8,27)
     assert mpq(2,3) ** 0 == mpq(2,3) ** mpz(0) == mpq(0,1) ** 0 == mpq(1,1)
     # XXX These Run out of memory - gmp: overflow in mpz type
     # with pytest.raises(OverflowError):
     #      mpq(2, 3) ** (2*sys.maxsize + 1)
     # with pytest.raises(OverflowError):
     #      mpq(2, 3) ** -(2*sys.maxsize + 1)
     assert mpq(2,3) ** -3 == mpq(27,8)
     assert mpq(-2,3) ** -2 == mpq(9,4)
     assert mpq(-2,3) ** -3 == mpq(-27,8)
     with pytest.raises(ZeroDivisionError):
         mpq(0,1) ** -3
Пример #29
0
 def test_sub(self):
     assert mpq(1, 2) - mpq(1, 3) == mpq(1, 6)
     assert mpq(1, 2) - 2 == mpq(-3, 2)
     assert mpq(1, 2) - sys.maxsize == mpq(1 - 2*sys.maxsize, 2)
     assert mpq(1, 2) - 2*sys.maxsize == mpq(1 - 4*sys.maxsize, 2)
     assert mpq(1, 2) - 3*sys.maxsize == mpq(1 - 6*sys.maxsize, 2)
     assert mpq(1, 2) - mpz(2) == mpq(-3, 2)
Пример #30
0
    def test_is_prime(self):
        assert is_prime(2)
        assert is_prime(-5)
        assert not is_prime(mpz(4))
        assert [x for x in range(901, 1000) if is_prime(x)] == [907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
        assert is_prime(4547337172376300111955330758342147474062293202868155909489)
        assert not is_prime(4547337172376300111955330758342147474062293202868155909393)
        assert is_prime(643808006803554439230129854961492699151386107534013432918073439524138264842370630061369715394739134090922937332590384720397133335969549256322620979036686633213903952966175107096769180017646161851573147596390153)
        assert not is_prime(743808006803554439230129854961492699151386107534013432918073439524138264842370630061369715394739134090922937332590384720397133335969549256322620979036686633213903952966175107096769180017646161851573147596390153)
        assert is_prime(1537381, 1) and not is_prime(1537381, 2)
        assert is_prime(18790021, 2) and not is_prime(18790021, 3)

        # invalid x
        with pytest.raises(TypeError):
            is_prime(3, mpq(2, 1))
        with pytest.raises(TypeError):
            is_prime(mpq(5, 1), 6)
        with pytest.raises(TypeError):
            is_prime(mpfr(1.5))

        # nonpositive n
        with pytest.raises(ValueError):
            is_prime(5, 0)
        with pytest.raises(ValueError):
            is_prime(5, -4)
Пример #31
0
 def test_rpow_mpq(self):
     assert 1 ** mpq(1,1) == mpfr('1.0')
     assert 2**mpq(1,2) == mpz(2) ** mpq(1,2) == mpfr('1.4142135623730951') == mpfr(sqrt(2))
     assert sys.maxsize ** mpq(1,2) == mpfr(sqrt(sys.maxsize))
     assert mpq(2, 3) ** mpq(5,7) == mpfr('0.74854950799570052')
     assert (-2) ** mpq(1,2) == mpfr('nan')
     assert 0 ** mpq(-1,3) == mpfr('inf')
Пример #32
0
 def test_conversions_int(self):
     for n in self.numbers:
         for type_ in [int, long]:
             n1 = type_(n)
             mpz_n = type_(mpz(n))
             assert type(n1) == type(mpz_n)
             assert n1 == mpz_n
Пример #33
0
 def test_conversion_complex(self):
     for n in self.numbers:
         n1 = complex(n)
         mpz_n = complex(mpz(n))
         assert type(n1) == type(mpz_n)
         assert abs(n1.real - mpz_n.real) <= abs(
             n1.real * sys.float_info.epsilon) and n1.imag == mpz_n.imag
Пример #34
0
    def test_is_prime(self):
        assert is_prime(2)
        assert is_prime(-5)
        assert not is_prime(mpz(4))
        assert [x for x in range(901, 1000) if is_prime(x)] == [
            907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991,
            997
        ]
        assert is_prime(
            4547337172376300111955330758342147474062293202868155909489)
        assert not is_prime(
            4547337172376300111955330758342147474062293202868155909393)
        assert is_prime(
            643808006803554439230129854961492699151386107534013432918073439524138264842370630061369715394739134090922937332590384720397133335969549256322620979036686633213903952966175107096769180017646161851573147596390153
        )
        assert not is_prime(
            743808006803554439230129854961492699151386107534013432918073439524138264842370630061369715394739134090922937332590384720397133335969549256322620979036686633213903952966175107096769180017646161851573147596390153
        )
        assert is_prime(1537381, 1) and not is_prime(1537381, 2)
        assert is_prime(18790021, 2) and not is_prime(18790021, 3)

        # invalid x
        with pytest.raises(TypeError):
            is_prime(3, mpq(2, 1))
        with pytest.raises(TypeError):
            is_prime(mpq(5, 1), 6)
        with pytest.raises(TypeError):
            is_prime(mpfr(1.5))

        # nonpositive n
        with pytest.raises(ValueError):
            is_prime(5, 0)
        with pytest.raises(ValueError):
            is_prime(5, -4)
Пример #35
0
 def test_add(self):
     assert mpq(1, 2) + mpq(1, 3) == mpq(5, 6)
     assert mpq(1, 2) + 2 == mpq(5, 2)
     assert mpq(1, 2) + sys.maxsize == mpq(2*sys.maxsize + 1, 2)
     assert mpq(1, 2) + 2*sys.maxsize == mpq(4*sys.maxsize + 1, 2)
     assert mpq(1, 2) + 3*sys.maxsize == mpq(6*sys.maxsize + 1, 2)
     assert mpq(1, 2) + mpz(2) == mpq(5, 2)
Пример #36
0
 def test_rsub(self):
     assert mpq(1, 3) - mpq(1, 2) == mpq(-1, 6)
     assert 2 - mpq(1, 2) == mpq(3, 2)
     assert sys.maxsize - mpq(1, 2) == mpq(2*sys.maxsize -1, 2)
     assert 2*sys.maxsize - mpq(1, 2) == mpq(4*sys.maxsize - 1, 2)
     assert 3*sys.maxsize - mpq(1, 2) == mpq(6*sys.maxsize - 1, 2)
     assert mpz(2) - mpq(1, 2) == mpq(3, 2)
Пример #37
0
 def test_radd(self):
     assert mpq(1, 3) + mpq(1, 2) == mpq(5, 6)
     assert 2 + mpq(1, 2) == mpq(5, 2)
     assert sys.maxsize + mpq(1, 2) == mpq(2*sys.maxsize + 1, 2)
     assert 2*sys.maxsize + mpq(1, 2) == mpq(4*sys.maxsize + 1, 2)
     assert 3*sys.maxsize + mpq(1, 2) == mpq(6*sys.maxsize + 1, 2)
     assert mpz(2) + mpq(1, 2) == mpq(5, 2)
Пример #38
0
 def test_conversions_int(self):
     for n in self.numbers:
         for type_ in [int, long]:
             n1 = type_(n)
             mpz_n = type_(mpz(n))
             assert type(n1) == type(mpz_n)
             assert n1 == mpz_n
Пример #39
0
 def test_pow_big_exp(self):
     with pytest.raises(ValueError):
         mpq(2,3) ** (2 * sys.maxsize + 2)
     with pytest.raises(ValueError):
         mpq(2,3) ** -(2 * sys.maxsize + 2)
     with pytest.raises(ValueError):
         mpq(2,3) ** mpz(2 * sys.maxsize + 2)
Пример #40
0
 def test_lcm(self):
     assert lcm(3, 4) == mpz(12)
     assert lcm(mpz(6), 9) == mpz(18)
     assert lcm(6, mpz(4)) == mpz(12)
     assert lcm(mpz(0), mpz(2)) == mpz(0)
     with pytest.raises(TypeError):
         lcm(mpq(1.5), 2)
     with pytest.raises(TypeError):
         lcm(3)
Пример #41
0
 def test_lcm(self):
     assert lcm(3, 4) == mpz(12)
     assert lcm(mpz(6), 9) == mpz(18)
     assert lcm(6, mpz(4)) == mpz(12)
     assert lcm(mpz(0), mpz(2)) == mpz(0)
     with pytest.raises(TypeError):
         lcm(mpq(1.5), 2)
     with pytest.raises(TypeError):
         lcm(3)
Пример #42
0
 def test_invalid_cmp(self, n):
     with pytest.raises(TypeError):
         mpz(1) > n
     with pytest.raises(TypeError):
         mpz(1) < n
     with pytest.raises(TypeError):
         mpz(1) >= n
     with pytest.raises(TypeError):
         mpz(1) <= n
Пример #43
0
 def test_invalid_cmp(self, n):
     with pytest.raises(TypeError):
         mpz(1) > n
     with pytest.raises(TypeError):
         mpz(1) < n
     with pytest.raises(TypeError):
         mpz(1) >= n
     with pytest.raises(TypeError):
         mpz(1) <= n
Пример #44
0
 def test_str(self):
     n = mpz('123456789abcdef0', 16)
     assert str(n) == '1311768467463790320'
     assert repr(n) == 'mpz(1311768467463790320)'
     assert hex(n) == '0x123456789abcdef0'
     if PY3:
         assert oct(n) == '0o110642547423257157360'
     else:
         assert oct(n) == '0110642547423257157360'
     n = -mpz('123456789abcdef0', 16)
     assert str(n) == '-1311768467463790320'
     assert repr(n) == 'mpz(-1311768467463790320)'
     assert hex(n) == '-0x123456789abcdef0'
     if PY3:
         assert oct(n) == '-0o110642547423257157360'
     else:
         assert oct(n) == '-0110642547423257157360'
Пример #45
0
 def test_rtruediv(self):
     assert 1.5 / mpfr('0.5') == mpfr('3.0')
     assert mpq(3, 2) / mpfr('0.5') == mpfr('3.0')
     assert mpz(3) / mpfr('1.5') == mpfr('2.0')
     assert 3 / mpfr('1.5') == mpfr('2.0')
     assert sys.maxsize / mpfr(1.5e19) == mpfr('0.61489146912365167')
     assert (2 * sys.maxsize) / mpfr(1.5e19) == mpfr('1.2297829382473033')
     assert (3 * sys.maxsize) / mpfr(1.5e19) == mpfr('1.8446744073709551')
Пример #46
0
 def test_rpow(self):
     assert 1.5 ** mpfr('2.5') == mpfr('2.7556759606310752')
     assert mpq(3,2) ** mpfr('2.5') == mpfr('2.7556759606310752')
     assert mpz(3) ** mpfr('2.5') == mpfr('15.588457268119896')
     assert 3 ** mpfr('2.5') == mpfr('15.588457268119896')
     assert sys.maxsize ** mpfr(1.5) == mpfr('2.8011385487393072e+28')
     assert (2 * sys.maxsize) ** mpfr(1.5) == mpfr('7.9228162514264338e+28')
     assert (3 * sys.maxsize) ** mpfr(1.5) == mpfr('1.4555142856368689e+29')
Пример #47
0
 def test_rpow(self):
     assert 1.5**mpfr('2.5') == mpfr('2.7556759606310752')
     assert mpq(3, 2)**mpfr('2.5') == mpfr('2.7556759606310752')
     assert mpz(3)**mpfr('2.5') == mpfr('15.588457268119896')
     assert 3**mpfr('2.5') == mpfr('15.588457268119896')
     assert sys.maxsize**mpfr(1.5) == mpfr('2.8011385487393072e+28')
     assert (2 * sys.maxsize)**mpfr(1.5) == mpfr('7.9228162514264338e+28')
     assert (3 * sys.maxsize)**mpfr(1.5) == mpfr('1.4555142856368689e+29')
Пример #48
0
 def test_rdiv(self):
     assert mpq(1, 3) // mpq(1, 2) == mpq(2, 3)
     assert 2 // mpq(3,2) == mpq(4,3)
     assert sys.maxsize // mpq(1,2) == mpq(2*sys.maxsize, 1)
     assert (2*sys.maxsize) // mpq(2, 1) == mpq(sys.maxsize, 1)
     assert (3*sys.maxsize) // mpq(2, 1) == mpq(3*sys.maxsize, 2)
     assert (4*sys.maxsize) // mpq(2, 1) == mpq(2*sys.maxsize, 1)
     assert mpz(3) // mpq(1, 2) == mpq(6, 1)
Пример #49
0
 def test_rmul(self):
     assert mpq(1, 3) * mpq(1, 2) == mpq(1, 6)
     assert 2 * mpq(1,2) == mpq(1,1)
     assert sys.maxsize * mpq(1,2) == mpq(sys.maxsize, 2)
     assert (2*sys.maxsize) * mpq(1, 2) == mpq(sys.maxsize, 1)
     assert (3*sys.maxsize) * mpq(1, 2) == mpq(3*sys.maxsize, 2)
     assert (4*sys.maxsize) * mpq(1, 2) == mpq(2*sys.maxsize, 1)
     assert mpz(2) * mpq(1, 2) == mpq(1, 1)
Пример #50
0
 def test_str(self):
     n = mpz('123456789abcdef0', 16)
     assert str(n) == '1311768467463790320'
     assert repr(n) == 'mpz(1311768467463790320)'
     assert hex(n) == '0x123456789abcdef0'
     if PY3:
         assert oct(n) == '0o110642547423257157360'
     else:
         assert oct(n) == '0110642547423257157360'
     n = -mpz('123456789abcdef0', 16)
     assert str(n) == '-1311768467463790320'
     assert repr(n) == 'mpz(-1311768467463790320)'
     assert hex(n) == '-0x123456789abcdef0'
     if PY3:
         assert oct(n) == '-0o110642547423257157360'
     else:
         assert oct(n) == '-0110642547423257157360'
Пример #51
0
 def test_mul(self):
     assert mpq(1, 2) * mpq(1, 3) == mpq(1, 6)
     assert mpq(1, 2) * 2 == mpq(1, 1)
     assert mpq(1, 2) * sys.maxsize == mpq(sys.maxsize, 2)
     assert mpq(1, 2) * (2*sys.maxsize) == mpq(sys.maxsize, 1)
     assert mpq(1, 2) * (3*sys.maxsize) == mpq(3*sys.maxsize, 2)
     assert mpq(1, 2) * (4*sys.maxsize) == mpq(2*sys.maxsize, 1)
     assert mpq(1, 2) * mpz(2) == mpq(1, 1)
Пример #52
0
 def test_gcd(self):
     assert gcd(4, 6) == mpz(2)
     assert gcd(5, 0) == mpz(5)
     assert gcd(5, 0) == mpz(5)
     assert gcd(323, 340) == gcd(mpz(323), 340) == gcd(
         323, mpz(340)) == gcd(mpz(323), mpz(340)) == mpz(17)
     with pytest.raises(TypeError):
         gcd(mpq(1.5), 2)
     with pytest.raises(TypeError):
         gcd(3)
Пример #53
0
 def test_get_set_cache(self):
     assert get_cache() == (100, 128)
     set_cache(101, 128)
     assert get_cache() == (101, 128)
     with pytest.raises(ValueError):
         set_cache(-1, 128)
     with pytest.raises(ValueError):
         set_cache(100, -1)
     with pytest.raises(ValueError):
         set_cache(10001, 128)
     with pytest.raises(ValueError):
         set_cache(10001, 16385)
     with pytest.raises(TypeError):
         set_cache(mpz(100), 128)
     with pytest.raises(TypeError):
         set_cache(100, mpz(128))
     # reset the cache paramaters for other tests
     set_cache(100, 128)
Пример #54
0
 def test_isnan(self):
     assert not isnan(mpfr(1.5))
     assert not isnan(mpz(1))
     assert not isnan(mpq(1, 3))
     assert not isnan(mpfr('inf'))
     assert not isnan(mpfr('-inf'))
     assert isnan(mpfr('nan'))
     with pytest.raises(TypeError):
         isnan([])
Пример #55
0
 def test_rsub(self):
     assert 1.5 - mpfr('0.5') == mpfr('1.0')
     assert mpq(3, 2) - mpfr('0.5') == mpfr('1.0')
     assert mpz(1) - mpfr('0.5') == mpfr('0.5')
     assert 1 - mpfr('0.5') == mpfr('0.5')
     assert sys.maxsize - mpfr('1.2e19') == mpfr('-2.7766279631452242e+18')
     assert 2 * sys.maxsize - mpfr('1.2e19') == mpfr(
         '6.4467440737095516e+18')
     assert 3 * sys.maxsize - mpfr('1.2e19') == mpfr(
         '1.5670116110564327e+19')
Пример #56
0
 def test_truediv(self):
     assert mpfr('1.5') / mpfr('0.5') == mpfr('3.0')
     assert mpfr('1.5') / 0.5 == mpfr('3.0')
     assert mpfr('1.5') / mpq(3, 2) == mpfr('1.0')
     assert mpfr('4.5') / mpz(3) == mpfr('1.5')
     assert mpfr('4.5') / 3 == mpfr('1.5')
     assert mpfr('1.5e19') / sys.maxsize == mpfr('1.6263032587282567')
     assert mpfr('1.5e19') / (2 *
                              sys.maxsize) == mpfr('0.81315162936412833')
     assert mpfr('1.5e19') / (3 *
                              sys.maxsize) == mpfr('0.54210108624275222')
Пример #57
0
 def test_add(self):
     assert mpfr('0.5') + mpfr('1.5') == mpfr('2.0')
     assert mpfr('0.5') + 1.5 == mpfr('2.0')
     assert mpfr('0.5') + mpq(3, 2) == mpfr('2.0')
     assert mpfr('0.5') + mpz(1) == mpfr('1.5')
     assert mpfr('0.5') + 1 == mpfr('1.5')
     assert mpfr('1.2e19') + sys.maxsize == mpfr('2.1223372036854776e+19')
     assert mpfr('1.2e19') + 2 * sys.maxsize == mpfr(
         '3.0446744073709552e+19')
     assert mpfr('1.2e19') + 3 * sys.maxsize == mpfr(
         '3.9670116110564327e+19')
Пример #58
0
 def test_mul(self):
     assert mpfr('0.5') * mpfr('1.5') == mpfr('0.75')
     assert mpfr('0.5') * 1.5 == mpfr('0.75')
     assert mpfr('0.5') * mpq(3, 2) == mpfr('0.75')
     assert mpfr('0.5') * mpz(3) == mpfr('1.5')
     assert mpfr('0.5') * 3 == mpfr('1.5')
     assert mpfr('1.5') * sys.maxsize == mpfr('1.3835058055282164e+19')
     assert mpfr('1.5') * (2 *
                           sys.maxsize) == mpfr('2.7670116110564327e+19')
     assert mpfr('1.5') * (3 *
                           sys.maxsize) == mpfr('4.1505174165846491e+19')
Пример #59
0
 def test_sub(self):
     assert mpfr('1.5') - mpfr('0.5') == mpfr('1.0')
     assert mpfr('1.5') - 0.5 == mpfr('1.0')
     assert mpfr('1.5') - mpq(1, 2) == mpfr('1.0')
     assert mpfr('1.5') - mpz(1) == mpfr('0.5')
     assert mpfr('1.5') - 1 == mpfr('0.5')
     assert mpfr('3.2e19') - sys.maxsize == mpfr('2.2776627963145224e+19')
     assert mpfr('3.2e19') - 2 * sys.maxsize == mpfr(
         '1.3553255926290448e+19')
     assert mpfr('3.2e19') - 3 * sys.maxsize == mpfr(
         '4.3298838894356726e+18')
Пример #60
0
 def test_pow(self):
     assert mpfr('2.5')**mpfr('1.5') == mpfr('3.9528470752104741')
     assert mpfr('2.5')**1.5 == mpfr('3.9528470752104741')
     assert mpfr('2.5')**mpq(3, 2) == mpfr('3.9528470752104741')
     assert mpfr('2.5')**mpz(3) == mpfr('15.625')
     assert mpfr('2.5')**3 == mpfr('15.625')
     assert mpfr('1.5')**sys.maxsize == mpfr('inf')
     assert mpfr('1.5')**(2 * sys.maxsize) == mpfr('inf')
     assert mpfr('1.5')**(3 * sys.maxsize) == mpfr('inf')
     assert mpfr('1.0')**sys.maxsize == mpfr('1.0')
     assert mpfr('1.0')**(2 * sys.maxsize) == mpfr('1.0')
     assert mpfr('1.0')**(3 * sys.maxsize) == mpfr('1.0')