def test_invalid_cmp(self): with pytest.raises(ValueError): int(mpfr('inf')) with pytest.raises(ValueError): int(mpfr('-inf')) with pytest.raises(ValueError): int(mpfr('nan'))
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([])
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([])
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)
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([])
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)))
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)))
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)
def test_invalid_prec(self): assert mpfr(1.5, 0) == mpfr('1.5') # Zero precision uses default with pytest.raises(ValueError): mpfr(1.5, 1) assert mpfr(1.5, 2) == mpfr('1.5',2) with pytest.raises(ValueError): mpfr(1.5, 10**20)
def test_invalid_prec(self): assert mpfr(1.5, 0) == mpfr('1.5') # Zero precision uses default with pytest.raises(ValueError): mpfr(1.5, 1) assert mpfr(1.5, 2) == mpfr('1.5', 2) with pytest.raises(ValueError): mpfr(1.5, 10**20)
def __pow__(self, other, modulo=None): if modulo is not None: raise TypeError("mpq.pow() no modulo allowed") if isinstance(other, mpq): # XXX Optimize return self ** gmpy_cffi.mpfr(other) elif isinstance(other, (mpz, int, long)): other = int(other) if 0 <= other <= MAX_UI: res = _new_mpq() gmp.mpz_pow_ui( gmp.mpq_numref(res), gmp.mpq_numref(self._mpq), other) gmp.mpz_pow_ui( gmp.mpq_denref(res), gmp.mpq_denref(self._mpq), other) return mpq._from_c_mpq(res) elif -MAX_UI <= other < 0: if self == 0: raise ZeroDivisionError( "mpq.pow() 0 base to negative exponent") res = _new_mpq() gmp.mpz_pow_ui( gmp.mpq_numref(res), gmp.mpq_denref(self._mpq), -other) gmp.mpz_pow_ui( gmp.mpq_denref(res), gmp.mpq_numref(self._mpq), -other) # For Example mpq(-1,1)**-1 == mpq(1, -1) -> mpq(1, 1) gmp.mpq_canonicalize(res) return mpq._from_c_mpq(res) else: raise ValueError('mpz.pow with outragous exponent') else: return NotImplemented
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)
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)
def test_repr(self): assert repr(mpfr(1.5)) == "mpfr('1.5')" assert repr(mpfr(-1.4)) == "mpfr('-1.3999999999999999')" assert repr(mpfr(2.5, 2)) == "mpfr('2.0',2)" assert repr(mpfr(2.5, 10)) == "mpfr('2.5',10)" assert repr(mpfr(2.5, 99)) == "mpfr('2.5',99)" assert repr(mpfr('nan')) == "mpfr('nan')" assert repr(mpfr('+inf')) == "mpfr('inf')" assert repr(mpfr('-inf')) == "mpfr('-inf')"
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')
def test_invalid_cmp(self, n): with pytest.raises(TypeError): mpfr('1.5') > n with pytest.raises(TypeError): mpfr('1.5') < n # with pytest.raises(TypeError): # mpfr('1.5') == n with pytest.raises(TypeError): mpfr('1.5') >= n with pytest.raises(TypeError): mpfr('1.5') <= n
def test_int(self): assert int(mpfr(0.0)) == 0 assert int(mpfr(1.4)) == 1 assert int(mpfr(1.6)) == 2 assert int(mpfr(-1.4)) == -1 assert int(mpfr(-1.6)) == -2 # Increased precision is required to get the exact result assert int(mpfr(sys.maxsize, 100)) == sys.maxsize assert int(mpfr(2 * sys.maxsize, 100)) == 2 * sys.maxsize assert int(mpfr(3 * sys.maxsize, 100)) == 3 * sys.maxsize
def test_int(self): assert int(mpfr(0.0)) == 0 assert int(mpfr(1.4)) == 1 assert int(mpfr(1.6)) == 2 assert int(mpfr(-1.4)) == -1 assert int(mpfr(-1.6)) == -2 # Increased precision is required to get the exact result assert int(mpfr(sys.maxsize, 100)) == sys.maxsize assert int(mpfr(2*sys.maxsize, 100)) == 2*sys.maxsize assert int(mpfr(3*sys.maxsize, 100)) == 3*sys.maxsize
def test_str(self): assert str(mpfr(1.5)) == '1.5' assert str(mpfr(-1.4)) == '-1.3999999999999999' assert str(mpfr(1.3, 21)) == '1.3000002' assert str(mpfr(1.3, 100)) == '1.3000000000000000444089209850063' assert str(mpfr('nan')) == 'nan' assert str(mpfr('+inf')) == 'inf' assert str(mpfr('-inf')) == '-inf'
def test_ceil(self): assert math.ceil(mpfr(1.5)) == 2.0 assert math.ceil(mpfr(-1.5)) == -1.0 if PY3: with pytest.raises(OverflowError): math.ceil(mpfr('inf')) with pytest.raises(OverflowError): math.ceil(mpfr('-inf')) with pytest.raises(ValueError): math.floor(mpfr('nan')) else: assert math.ceil(mpfr('inf')) == float('inf') assert math.ceil(mpfr('-inf')) == float('-inf') assert math.isnan(math.ceil(mpfr('nan')))
def test_rtruediv(self): assert (2.1+0.4j) / mpc('1.5+3.2j') == mpc('0.35468374699759808-0.48999199359487589j') assert 0.3 / mpc('1.2+3.1j') == mpc('0.032579185520361986-0.084162895927601802j') assert mpq(2,3) / mpc('1.4+0.4j') == mpc('0.44025157232704404-0.12578616352201258j') assert mpfr('0.3') / mpc('1.2+3.1j') == mpc('0.032579185520361986-0.084162895927601802j') assert 3 / mpc('1.3+0.7j') == mpc('1.7889908256880733-0.96330275229357787j') assert mpz(3) / mpc('1.3+0.7j') == mpc('1.7889908256880733-0.96330275229357787j') assert (-1) / mpc(0.4+0.6j) == mpc('-0.76923076923076927+1.1538461538461537j') assert sys.maxsize / mpc(1.3e19+1.6e19j) == mpc('0.28212667406849901-0.34723282962276802j') assert (2*sys.maxsize) / mpc(1.3e19+1.6e19j) == mpc('0.56425334813699801-0.69446565924553605j') assert (3*sys.maxsize) / mpc(1.3e19+1.6e19j) == mpc('0.84638002220549713-1.0416984888683041j') with pytest.raises(TypeError): [] / mpc('1.5+0.5j')
def test_rsub(self): assert mpfr('1.5') - mpc('0.5') == mpc('1.0') assert (1.5 + 0.5j) - mpc('0.2+0.3j') == mpc('1.3+0.2j') assert 1.5 - mpc('0.5') == mpc('1.0') assert mpq(3, 2) - mpc('0.5') == mpc('1.0') assert mpz(1) - mpc('0.5') == mpc('0.5') assert 1 - mpc('0.5') == mpc('0.5') assert (-1) - mpc('0.5') == mpc('-1.5') assert sys.maxsize - mpc('1.2e19') == mpc('-2.7766279631452242e+18') assert 2 * sys.maxsize - mpc('1.2e19') == mpc('6.4467440737095516e+18') assert 3 * sys.maxsize - mpc('1.2e19') == mpc('1.5670116110564327e+19') with pytest.raises(TypeError): [] - mpc('1.5+0.5j')
def test_rpow(self): assert 2 ** mpc('1.2+0.7j') == mpc('2.0322318210346983+1.0714781699435463j') assert (-3) ** mpc('1.2+0.7j') == mpc('-0.07152774910532736-0.40824064717539998j') assert mpz(2) ** mpc('1.2+0.7j') == mpc('2.0322318210346983+1.0714781699435463j') assert mpq(2,3) ** mpc('1.2+0.7j') == mpc('0.59014364683348408-0.17214537996429355j') assert mpfr(1.3) ** mpc('1.2+0.7j') == mpc('1.3469959279701931+0.25020189563974787j') assert 1.3 ** mpc('1.2+0.7j') == mpc('1.3469959279701931+0.25020189563974787j') assert (1.1+0.8j) ** mpc('1.2+0.7j') == mpc('0.52663021989956504+0.76824606795641459j') assert sys.maxsize ** mpc('1.5+0.3j') == mpc('2.4110001396293724e+28+1.4259928106202001e+28j') assert (2*sys.maxsize) ** mpc('1.5+0.3j') == mpc('5.8397586579879863e+28+5.354272702274433e+28j') assert (3*sys.maxsize) ** mpc('1.5+0.3j') == mpc('9.4555004785752363e+28+1.1065518255778351e+29j') with pytest.raises(TypeError): [] ** mpc('1.5+0.5j')
def test_rsub(self): assert mpfr('1.5') - mpc('0.5') == mpc('1.0') assert (1.5+0.5j) - mpc('0.2+0.3j') == mpc('1.3+0.2j') assert 1.5 - mpc('0.5') == mpc('1.0') assert mpq(3,2) - mpc('0.5') == mpc('1.0') assert mpz(1) - mpc('0.5') == mpc('0.5') assert 1 - mpc('0.5') == mpc('0.5') assert (-1) - mpc('0.5') == mpc('-1.5') assert sys.maxsize - mpc('1.2e19') == mpc('-2.7766279631452242e+18') assert 2*sys.maxsize - mpc('1.2e19') == mpc('6.4467440737095516e+18') assert 3*sys.maxsize - mpc('1.2e19') == mpc('1.5670116110564327e+19') with pytest.raises(TypeError): [] - mpc('1.5+0.5j')
def test_truediv(self): assert mpc(1,3) / mpc(2,5) == mpc('0.58620689655172409+0.034482758620689655j') assert mpc('1.5+3.2j') / (2.1+0.4j) == mpc('0.96936542669584247+1.3391684901531729j') assert mpc('1.5+1.2j') / 0.3 == mpc('5.0+4.0j') assert mpc('1.5+1.2j') / mpfr('0.3') == mpc('5.0+4.0j') assert mpc('1.5+3.0j') / 3 == mpc('0.5+1.0j') assert mpc('1.5+3.0j') / mpz(3) == mpc('0.5+1.0j') assert mpc('1.5+0.6j') / mpq(7,5) == mpc('1.0714285714285714+0.4285714285714286j') assert mpc('4.4+1.7j') / (-2) == mpc('-2.2000000000000002-0.84999999999999998j') assert mpc(1.4e+19-1.2e+19j) / sys.maxsize == mpc('1.5178830414797062-1.3010426069826053j') assert mpc(1.4e+19-1.2e+19j) / (2*sys.maxsize) == mpc('0.7589415207398531-0.65052130349130266j') assert mpc(1.4e+19-1.2e+19j) / (3*sys.maxsize) == mpc('0.50596101382656877-0.43368086899420177j') with pytest.raises(TypeError): mpc('1.5+0.5j') / []
def test_mul(self): assert mpc('0.5+0.1j') * mpc('0.5+0.2j') == mpc('0.23+0.15000000000000002j') assert mpc('0.5+0.1j') * (0.5+0.2j) == mpc('0.23+0.15000000000000002j') assert mpc('0.5+0.3j') * mpfr('1.5') == mpc('0.75+0.44999999999999996j') assert mpc('0.5+0.3j') * 1.5 == mpc('0.75+0.44999999999999996j') assert mpc('0.5+0.3j') * mpq(3,2) == mpc('0.75+0.44999999999999996j') assert mpc('0.5+0.3j') * mpz(3) == mpc('1.5+0.89999999999999991j') assert mpc('0.5+0.3j') * 3 == mpc('1.5+0.89999999999999991j') assert mpc('1.5') * sys.maxsize == mpc('1.3835058055282164e+19') assert mpc('1.5') * (2 * sys.maxsize) == mpc('2.7670116110564327e+19') assert mpc('1.5') * (3 * sys.maxsize) == mpc('4.1505174165846491e+19') with pytest.raises(TypeError): mpc('1.5+0.5j') * [] assert 3 * mpc('1.5+0.5j') == mpc('4.5+1.5j')
def test_sub(self): assert mpc('1.5+0.5j') - mpc('0.2+0.3j') == mpc('1.3+0.2j') assert mpc('1.5+0.5j') - (0.2 + 0.3j) == mpc('1.3+0.2j') assert mpc('1.5+0.5j') - mpfr('0.2') == mpc('1.3+0.5j') assert mpc('1.5+0.5j') - mpq(3, 2) == mpc('0.0+0.5j') assert mpc('1.5+0.5j') - mpz(2) == mpc('-0.5+0.5j') assert mpc('1.5+0.5j') - 1.5 == mpc('0.0+0.5j') assert mpc('1.5+0.5j') - 1 == mpc('0.5+0.5j') assert mpc('1.5+0.5j') - (-1) == mpc('2.5+0.5j') assert mpc('3.2e19') - sys.maxsize == mpc('2.2776627963145224e+19') assert mpc('3.2e19') - 2 * sys.maxsize == mpc('1.3553255926290448e+19') assert mpc('3.2e19') - 3 * sys.maxsize == mpc('4.3298838894356726e+18') with pytest.raises(TypeError): assert mpc('1.5+0.5j') - []
def test_add(self): assert mpc('1.5+0.5j') + mpc('0.2+0.3j') == mpc('1.7+0.8j') assert mpc('1.5+0.5j') + (0.2 + 0.3j) == mpc('1.7+0.8j') assert mpc('1.5+0.5j') + mpfr('1.5') == mpc('3.0+0.5j') assert mpc('1.5+0.5j') + mpq(3, 2) == mpc('3.0+0.5j') assert mpc('1.5+0.5j') + mpz(2) == mpc('3.5+0.5j') assert mpc('1.5+0.5j') + 1.5 == mpc('3.0+0.5j') assert mpc('1.5+0.5j') + 1 == mpc('2.5+0.5j') assert mpc('1.5+0.5j') + (-1) == mpc('0.5+0.5j') assert mpc('1.2e19') + sys.maxsize == mpc('2.1223372036854776e19') assert mpc('1.2e19') + 2 * sys.maxsize == mpc('3.0446744073709552e+19') assert mpc('1.2e19') + 3 * sys.maxsize == mpc('3.9670116110564327e+19') with pytest.raises(TypeError): assert mpc('1.5+0.5j') + [] assert 1.5 + mpc('1.5+0.5j') == mpc('3.0+0.5j')
def test_pow(self): assert mpc('1.2+3.1j') ** mpc('0.7+0.3j') == mpc('0.58374557428865026+1.5076769261293019j') assert mpc('1.2+3.1j') ** (0.7+0.3j) == mpc('0.58374557428865026+1.5076769261293019j') assert mpc('1.3+2.7j') ** mpq(2,3) == mpc('1.523607752011799+1.4138443176037268j') assert mpc('1.2+2.7j') ** mpfr(1.35) == mpc('0.063993805808622087+4.3165511702888386j') assert mpc('1.2+2.7j') ** 1.35 == mpc('0.063993805808622087+4.3165511702888386j') assert mpc('1.2+2.7j') ** 15 == mpc('112131.44281004601-11417890.819726286j') assert mpc('1.2+2.7j') ** mpz(15) == mpc('112131.44281004601-11417890.819726286j') assert mpc('1.2+2.7j') ** (-1) == mpc('0.1374570446735395-0.30927835051546393j') assert mpc('0.0+1.0j') ** sys.maxsize == mpc('-0.0-1.0j') assert mpc('0.0+1.0j') ** (2*sys.maxsize) == mpc('-1.0+0.0j') assert mpc('0.0+1.0j') ** (3*sys.maxsize) == mpc('0.0+1.0j') with pytest.raises(TypeError): mpc('1.5+0.5j') ** []
def test_add(self): assert mpc('1.5+0.5j') + mpc('0.2+0.3j') == mpc('1.7+0.8j') assert mpc('1.5+0.5j') + (0.2+0.3j) == mpc('1.7+0.8j') assert mpc('1.5+0.5j') + mpfr('1.5') == mpc('3.0+0.5j') assert mpc('1.5+0.5j') + mpq(3, 2) == mpc('3.0+0.5j') assert mpc('1.5+0.5j') + mpz(2) == mpc('3.5+0.5j') assert mpc('1.5+0.5j') + 1.5 == mpc('3.0+0.5j') assert mpc('1.5+0.5j') + 1 == mpc('2.5+0.5j') assert mpc('1.5+0.5j') + (-1) == mpc('0.5+0.5j') assert mpc('1.2e19') + sys.maxsize == mpc('2.1223372036854776e19') assert mpc('1.2e19') + 2*sys.maxsize == mpc('3.0446744073709552e+19') assert mpc('1.2e19') + 3*sys.maxsize == mpc('3.9670116110564327e+19') with pytest.raises(TypeError): assert mpc('1.5+0.5j') + [] assert 1.5 + mpc('1.5+0.5j') == mpc('3.0+0.5j')
def test_sub(self): assert mpc('1.5+0.5j') - mpc('0.2+0.3j') == mpc('1.3+0.2j') assert mpc('1.5+0.5j') - (0.2+0.3j) == mpc('1.3+0.2j') assert mpc('1.5+0.5j') - mpfr('0.2') == mpc('1.3+0.5j') assert mpc('1.5+0.5j') - mpq(3, 2) == mpc('0.0+0.5j') assert mpc('1.5+0.5j') - mpz(2) == mpc('-0.5+0.5j') assert mpc('1.5+0.5j') - 1.5 == mpc('0.0+0.5j') assert mpc('1.5+0.5j') - 1 == mpc('0.5+0.5j') assert mpc('1.5+0.5j') - (-1) == mpc('2.5+0.5j') assert mpc('3.2e19') - sys.maxsize == mpc('2.2776627963145224e+19') assert mpc('3.2e19') - 2*sys.maxsize == mpc('1.3553255926290448e+19') assert mpc('3.2e19') - 3*sys.maxsize == mpc('4.3298838894356726e+18') with pytest.raises(TypeError): assert mpc('1.5+0.5j') - []
def test_mul(self): assert mpc('0.5+0.1j') * mpc('0.5+0.2j') == mpc( '0.23+0.15000000000000002j') assert mpc('0.5+0.1j') * (0.5 + 0.2j) == mpc('0.23+0.15000000000000002j') assert mpc('0.5+0.3j') * mpfr('1.5') == mpc( '0.75+0.44999999999999996j') assert mpc('0.5+0.3j') * 1.5 == mpc('0.75+0.44999999999999996j') assert mpc('0.5+0.3j') * mpq(3, 2) == mpc('0.75+0.44999999999999996j') assert mpc('0.5+0.3j') * mpz(3) == mpc('1.5+0.89999999999999991j') assert mpc('0.5+0.3j') * 3 == mpc('1.5+0.89999999999999991j') assert mpc('1.5') * sys.maxsize == mpc('1.3835058055282164e+19') assert mpc('1.5') * (2 * sys.maxsize) == mpc('2.7670116110564327e+19') assert mpc('1.5') * (3 * sys.maxsize) == mpc('4.1505174165846491e+19') with pytest.raises(TypeError): mpc('1.5+0.5j') * [] assert 3 * mpc('1.5+0.5j') == mpc('4.5+1.5j')
def test_truediv(self): assert mpc(1, 3) / mpc( 2, 5) == mpc('0.58620689655172409+0.034482758620689655j') assert mpc('1.5+3.2j') / (2.1 + 0.4j) == mpc( '0.96936542669584247+1.3391684901531729j') assert mpc('1.5+1.2j') / 0.3 == mpc('5.0+4.0j') assert mpc('1.5+1.2j') / mpfr('0.3') == mpc('5.0+4.0j') assert mpc('1.5+3.0j') / 3 == mpc('0.5+1.0j') assert mpc('1.5+3.0j') / mpz(3) == mpc('0.5+1.0j') assert mpc('1.5+0.6j') / mpq( 7, 5) == mpc('1.0714285714285714+0.4285714285714286j') assert mpc('4.4+1.7j') / (-2) == mpc( '-2.2000000000000002-0.84999999999999998j') assert mpc(1.4e+19 - 1.2e+19j) / sys.maxsize == mpc( '1.5178830414797062-1.3010426069826053j') assert mpc(1.4e+19 - 1.2e+19j) / (2 * sys.maxsize) == mpc( '0.7589415207398531-0.65052130349130266j') assert mpc(1.4e+19 - 1.2e+19j) / (3 * sys.maxsize) == mpc( '0.50596101382656877-0.43368086899420177j') with pytest.raises(TypeError): mpc('1.5+0.5j') / []
def test_rtruediv(self): assert (2.1 + 0.4j) / mpc('1.5+3.2j') == mpc( '0.35468374699759808-0.48999199359487589j') assert 0.3 / mpc('1.2+3.1j') == mpc( '0.032579185520361986-0.084162895927601802j') assert mpq(2, 3) / mpc('1.4+0.4j') == mpc( '0.44025157232704404-0.12578616352201258j') assert mpfr('0.3') / mpc('1.2+3.1j') == mpc( '0.032579185520361986-0.084162895927601802j') assert 3 / mpc('1.3+0.7j') == mpc( '1.7889908256880733-0.96330275229357787j') assert mpz(3) / mpc('1.3+0.7j') == mpc( '1.7889908256880733-0.96330275229357787j') assert (-1) / mpc(0.4 + 0.6j) == mpc( '-0.76923076923076927+1.1538461538461537j') assert sys.maxsize / mpc(1.3e19 + 1.6e19j) == mpc( '0.28212667406849901-0.34723282962276802j') assert (2 * sys.maxsize) / mpc(1.3e19 + 1.6e19j) == mpc( '0.56425334813699801-0.69446565924553605j') assert (3 * sys.maxsize) / mpc(1.3e19 + 1.6e19j) == mpc( '0.84638002220549713-1.0416984888683041j') with pytest.raises(TypeError): [] / mpc('1.5+0.5j')
def test_pow(self): assert mpc('1.2+3.1j')**mpc('0.7+0.3j') == mpc( '0.58374557428865026+1.5076769261293019j') assert mpc('1.2+3.1j')**( 0.7 + 0.3j) == mpc('0.58374557428865026+1.5076769261293019j') assert mpc('1.3+2.7j')**mpq( 2, 3) == mpc('1.523607752011799+1.4138443176037268j') assert mpc('1.2+2.7j')**mpfr(1.35) == mpc( '0.063993805808622087+4.3165511702888386j') assert mpc('1.2+2.7j')**1.35 == mpc( '0.063993805808622087+4.3165511702888386j') assert mpc('1.2+2.7j')**15 == mpc( '112131.44281004601-11417890.819726286j') assert mpc('1.2+2.7j')**mpz(15) == mpc( '112131.44281004601-11417890.819726286j') assert mpc('1.2+2.7j')**( -1) == mpc('0.1374570446735395-0.30927835051546393j') assert mpc('0.0+1.0j')**sys.maxsize == mpc('-0.0-1.0j') assert mpc('0.0+1.0j')**(2 * sys.maxsize) == mpc('-1.0+0.0j') assert mpc('0.0+1.0j')**(3 * sys.maxsize) == mpc('0.0+1.0j') with pytest.raises(TypeError): mpc('1.5+0.5j')**[]
def test_rpow(self): assert 2**mpc('1.2+0.7j') == mpc( '2.0322318210346983+1.0714781699435463j') assert (-3)**mpc('1.2+0.7j') == mpc( '-0.07152774910532736-0.40824064717539998j') assert mpz(2)**mpc('1.2+0.7j') == mpc( '2.0322318210346983+1.0714781699435463j') assert mpq(2, 3)**mpc('1.2+0.7j') == mpc( '0.59014364683348408-0.17214537996429355j') assert mpfr(1.3)**mpc('1.2+0.7j') == mpc( '1.3469959279701931+0.25020189563974787j') assert 1.3**mpc('1.2+0.7j') == mpc( '1.3469959279701931+0.25020189563974787j') assert (1.1 + 0.8j)**mpc('1.2+0.7j') == mpc( '0.52663021989956504+0.76824606795641459j') assert sys.maxsize**mpc('1.5+0.3j') == mpc( '2.4110001396293724e+28+1.4259928106202001e+28j') assert (2 * sys.maxsize)**mpc('1.5+0.3j') == mpc( '5.8397586579879863e+28+5.354272702274433e+28j') assert (3 * sys.maxsize)**mpc('1.5+0.3j') == mpc( '9.4555004785752363e+28+1.1065518255778351e+29j') with pytest.raises(TypeError): []**mpc('1.5+0.5j')
def test_tan(self): assert tan(0.5) == mpfr('0.54630248984379048') assert tan(0.5+0.7j) == mpc('0.31267491960977917+0.70760291160255884j')
def test_invalid_nargs(self): with pytest.raises(TypeError): mpfr(1, 2, 3, 4) with pytest.raises(TypeError): # String expected mpfr(1, 2, 3)
def test_init_mpfr(self): assert mpfr(mpfr('1.5')) == mpfr('1.5')
def test_init_mpq(self): assert mpfr(mpq(2, 3)) == mpfr('0.66666666666666663') assert mpfr(mpq(2, 5), 80) == mpfr('0.40000000000000000000000008', 80)
def test_init_mpz(self, n): assert mpfr(mpz(n)) == mpfr(n)
def test_init_int(self, n): assert mpfr(int(n)) == mpfr(n)
def test_hash(self, n): assert hash(mpfr(n)) == hash(n)
def test_hash_special(self): assert hash(mpfr()) == hash(mpfr(0.0, 100)) == 0 assert hash(mpfr('inf')) == hash(float('inf')) assert hash(mpfr('-inf')) == hash(float('-inf')) assert hash(mpfr('nan')) == hash(float('nan'))
def test_csc(self): assert csc(0.5) == mpfr('2.0858296429334882')
def test_cot(self): assert cot(0.5) == mpfr('1.830487721712452')
def test_atan(self): assert atan(0.5) == mpfr('0.46364760900080609') assert atan(0.5+0.7j) == mpc('0.65821413412081531+0.55575811532302299j')
def test_init_invalid(self, n): with pytest.raises(TypeError): mpfr(n) with pytest.raises(TypeError): mpfr(1, n) with pytest.raises(TypeError): mpfr(n, 0) with pytest.raises(TypeError): mpfr(n, 53) with pytest.raises(TypeError): mpfr('1', n) with pytest.raises(TypeError): mpfr('1', n, 10) with pytest.raises(TypeError): mpfr('1', 53, n) with pytest.raises(TypeError): mpfr('1', 0, n) with pytest.raises(TypeError): mpfr(n, 0, 10) with pytest.raises(TypeError): mpfr(n, 53, 10)
def test_acos(self): assert acos(0.5) == mpfr('1.0471975511965979') assert acos(0.5+0.7j) == mpc('1.1619717971477033-0.70341059318814581j')
def test_sec(self): assert sec(0.5) == mpfr('1.139493927324549')
def __rpow__(self, other, modulo=None): if modulo is not None: raise TypeError("mpq.pow() no modulo allowed") return other ** gmpy_cffi.mpfr(self)
def test_asin(self): assert asin(0.5) == mpfr('0.52359877559829893') assert asin(0.5+0.7j) == mpc('0.40882452964719346+0.70341059318814581j')