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_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_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_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_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_init_zero_den2(self, n): with pytest.raises(ZeroDivisionError): mpq(n, 0) with pytest.raises(ZeroDivisionError): mpq(n, 0.0) with pytest.raises(ZeroDivisionError): mpq(n, mpz(0)) with pytest.raises(ZeroDivisionError): mpq(n, mpq(0,1))
def test_zerodiv(self): with pytest.raises(ZeroDivisionError): mpq(1, 2) // mpq(0, 1) with pytest.raises(ZeroDivisionError): mpq(1, 2) // mpz(0) with pytest.raises(ZeroDivisionError): mpq(1, 2) // 0 with pytest.raises(ZeroDivisionError): mpq(1, 2) // long(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)
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_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))
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)
def test_invalid_type_math(self, n): with pytest.raises(TypeError): mpq(1, 2) > n with pytest.raises(TypeError): mpq(1, 2) < n with pytest.raises(TypeError): mpq(1, 2) >= n with pytest.raises(TypeError): mpq(1, 2) <= n
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')
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')
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')
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')
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))
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')
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)
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')
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')
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')
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')
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_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)
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_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)
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)
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')
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')
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')
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')
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')
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')
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')
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)
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_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_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_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_q(self): with pytest.raises(TypeError): mpc('0.5') > mpq(1, 2) with pytest.raises(TypeError): mpc('0.5') < mpq(1, 2) with pytest.raises(TypeError): mpc('0.5') >= mpq(1, 2) with pytest.raises(TypeError): mpc('0.5') <= mpq(1, 2) assert mpc('0.5') == mpq(1, 2) assert mpc('0.4') != mpq(1, 2) assert mpc('0.5+0.25j') != mpq(1, 2)
def test_q(self): with pytest.raises(TypeError): mpc('0.5') > mpq(1,2) with pytest.raises(TypeError): mpc('0.5') < mpq(1,2) with pytest.raises(TypeError): mpc('0.5') >= mpq(1,2) with pytest.raises(TypeError): mpc('0.5') <= mpq(1,2) assert mpc('0.5') == mpq(1,2) assert mpc('0.4') != mpq(1,2) assert mpc('0.5+0.25j') != mpq(1,2)
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_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_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_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_init_mpq(self): assert mpfr(mpq(2, 3)) == mpfr('0.66666666666666663') assert mpfr(mpq(2, 5), 80) == mpfr('0.40000000000000000000000008', 80)
class TestCmp(object): @pytest.mark.parametrize(('a', 'b'), [(mpfr(1.5), 1.4), (mpfr(1.1), 1), (mpfr(1.1), mpz(1)), (mpfr(1.6), mpq(3, 2)), (mpfr('1.0e19'), sys.maxsize), (mpfr('2.0e19'), 2 * sys.maxsize), (mpfr('3.0e19'), 3 * sys.maxsize)]) def test_gt(self, a, b): assert a > b @pytest.mark.parametrize(('a', 'b'), [(mpfr(1.4), 1.5), (mpfr(0.9), 1), (mpfr(0.9), mpz(1)), (mpfr(1.4), mpq(3, 2)), (mpfr('0.5e19'), sys.maxsize), (mpfr('1.5e19'), 2 * sys.maxsize), (mpfr('2.0e19'), 3 * sys.maxsize)]) def test_lt(self, a, b): assert a < b @pytest.mark.parametrize(('a', 'b'), [(mpfr(1.5), 1.5), (mpfr(1.0), 1), (mpfr(1.0), mpz(1)), (mpfr(1.5), mpq(3, 2))]) def test_eq(self, a, b): assert a == b def test_ge(self): assert mpfr(1.5) >= mpfr(1.5) assert mpfr(1.5) >= mpfr(1.4) def test_le(self): assert mpfr(1.5) <= mpfr(1.5) assert mpfr(1.5) <= mpfr(1.6) def test_ne(self): assert mpfr(1.4) != mpfr(1.5) assert mpfr(1.5) != mpfr(-1.5) @pytest.mark.xfail("sys.version.startswith('2.')", reason="python2 comparison") @pytest.mark.parametrize('n', invalids) 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 # with pytest.raises(TypeError): # mpfr('1.5') != 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')) @pytest.mark.parametrize('n', small_floats + large_floats) def test_hash(self, n): assert hash(mpfr(n)) == hash(n)
def test_divzero(self): assert mpfr(1.0) / mpq(0, 1) == mpfr('inf') assert mpfr(1.0) / mpz(0) == mpfr('inf') assert mpfr(1.0) / 0 == mpfr('inf') assert mpfr(1.0) / 0.0 == mpfr('inf') assert 1 / mpfr(0.0) == mpfr('inf')
def test_init_q3(self): x = mpc(mpq(1, 2), mpq(1, 4), 110) assert x == 0.5 + 0.25j assert x.precision == (110, 110)