MPFR Functionality ================== >>> import gmpy2 >>> import math >>> from gmpy2 import mpfr, mpq, mpz >>> from fractions import Fraction as F >>> from decimal import Decimal as D >>> def _cmp(x,y): ... return x.as_integer_ratio() == y.as_integer_ratio() ... Test trigonometric operations ============================= Note: gmpy2 returns InvalidOperationError whereas the math module returns ValueError. The test of acos verifies that InvalidOperationError is sub- classed from ValueError. Note: The math module assumes non-signaling NaNs; math.acos(float("nan")) does not raise an exception. But math.acos(float("inf")) and math.acos(float("-inf")) do raise exceptions. MPFR treats NaN as a signaling NaN and raises an exception if trap_invalid is True. Test acos --------- >>> gmpy2.set_context(gmpy2.context()) >>> _cmp(gmpy2.acos(mpfr("0.2")), math.acos(0.2)) True >>> gmpy2.acos() Traceback (most recent call last): File "", line 1, in TypeError: acos() takes exactly one argument (0 given) >>> gmpy2.acos("a") Traceback (most recent call last): File "", line 1, in TypeError: acos() argument type not supported >>> gmpy2.acos(0,0) Traceback (most recent call last): File "", line 1, in TypeError: acos() takes exactly one argument (2 given) >>> gmpy2.acos(0) mpfr('1.5707963267948966') >>> gmpy2.acos(mpz(0)) mpfr('1.5707963267948966') >>> gmpy2.acos(mpq(1,2)) mpfr('1.0471975511965979') >>> gmpy2.acos(F(1,2)) mpfr('1.0471975511965979') >>> gmpy2.acos(D("0.5")) mpfr('1.0471975511965979') >>> gmpy2.acos(mpfr("nan")) mpfr('nan') >>> gmpy2.acos(mpfr("inf")) mpfr('nan') >>> gmpy2.acos(mpfr("-inf")) mpfr('nan') >>> gmpy2.set_context(gmpy2.context(trap_invalid=True)) >>> gmpy2.acos(mpfr("nan")) Traceback (most recent call last): File "", line 1, in InvalidOperationError: 'mpfr' invalid operation in acos() >>> gmpy2.acos(mpfr("inf")) Traceback (most recent call last): File "", line 1, in InvalidOperationError: 'mpfr' invalid operation in acos() >>> gmpy2.acos(mpfr("-inf")) Traceback (most recent call last): File "", line 1, in InvalidOperationError: 'mpfr' invalid operation in acos() >>> gmpy2.set_context(gmpy2.context(precision=100)) >>> gmpy2.acos(mpfr("0.2")) mpfr('1.3694384060045658277761961394221',100) >>> gmpy2.get_context() context(precision=100, real_prec=Default, imag_prec=Default, round=RoundToNearest, real_round=Default, imag_round=Default, emax=1073741823, emin=-1073741823, subnormalize=False, trap_underflow=False, underflow=False, trap_overflow=False, overflow=False, trap_inexact=False, inexact=True, trap_invalid=False, invalid=False, trap_erange=False, erange=False, trap_divzero=False, divzero=False, trap_expbound=False, allow_complex=False) Test asin --------- >>> gmpy2.set_context(gmpy2.context()) >>> _cmp(gmpy2.asin(mpfr("0.2")), math.asin(0.2)) True >>> gmpy2.asin() Traceback (most recent call last): File "", line 1, in TypeError: asin() takes exactly one argument (0 given) >>> gmpy2.asin("a") Traceback (most recent call last): File "", line 1, in TypeError: asin() argument type not supported >>> gmpy2.asin(0,0) Traceback (most recent call last): File "", line 1, in TypeError: asin() takes exactly one argument (2 given) >>> gmpy2.asin(0) mpfr('0.0') >>> gmpy2.asin(mpz(0)) mpfr('0.0') >>> gmpy2.asin(mpq(1,2)) mpfr('0.52359877559829893') >>> gmpy2.asin(F(1,2)) mpfr('0.52359877559829893') >>> gmpy2.asin(D("0.5")) mpfr('0.52359877559829893') >>> gmpy2.asin(mpfr("nan")) mpfr('nan') >>> gmpy2.asin(mpfr("inf")) mpfr('nan') >>> gmpy2.asin(mpfr("-inf")) mpfr('nan') >>> gmpy2.set_context(gmpy2.context(trap_invalid=True)) >>> gmpy2.asin(mpfr("nan")) Traceback (most recent call last): File "", line 1, in InvalidOperationError: 'mpfr' invalid operation in asin() >>> gmpy2.asin(mpfr("inf")) Traceback (most recent call last): File "", line 1, in InvalidOperationError: 'mpfr' invalid operation in asin() >>> gmpy2.asin(mpfr("-inf")) Traceback (most recent call last): File "", line 1, in InvalidOperationError: 'mpfr' invalid operation in asin() >>> gmpy2.set_context(gmpy2.context(precision=100)) >>> gmpy2.asin(mpfr("0.2")) mpfr('0.20135792079033079145512555221757',100) >>> gmpy2.get_context() context(precision=100, real_prec=Default, imag_prec=Default, round=RoundToNearest, real_round=Default, imag_round=Default, emax=1073741823, emin=-1073741823, subnormalize=False, trap_underflow=False, underflow=False, trap_overflow=False, overflow=False, trap_inexact=False, inexact=True, trap_invalid=False, invalid=False, trap_erange=False, erange=False, trap_divzero=False, divzero=False, trap_expbound=False, allow_complex=False) Test atan --------- >>> gmpy2.set_context(gmpy2.context()) >>> _cmp(gmpy2.atan(mpfr("0.2")), math.atan(0.2)) True >>> _cmp(gmpy2.atan(mpfr("100")), math.atan(100)) True >>> gmpy2.atan() Traceback (most recent call last): File "", line 1, in TypeError: atan() takes exactly one argument (0 given) >>> gmpy2.atan("a") Traceback (most recent call last): File "", line 1, in TypeError: atan() argument type not supported >>> gmpy2.atan(0,0) Traceback (most recent call last): File "", line 1, in TypeError: atan() takes exactly one argument (2 given) >>> gmpy2.atan(0) mpfr('0.0') >>> gmpy2.atan(mpz(0)) mpfr('0.0') >>> gmpy2.atan(mpq(1,2)) mpfr('0.46364760900080609') >>> gmpy2.atan(F(1,2)) mpfr('0.46364760900080609') >>> gmpy2.atan(D("0.5")) mpfr('0.46364760900080609') >>> gmpy2.atan(mpfr("nan")) mpfr('nan') >>> gmpy2.atan(mpfr("inf")) mpfr('1.5707963267948966') >>> gmpy2.atan(mpfr("-inf")) mpfr('-1.5707963267948966') >>> gmpy2.set_context(gmpy2.context(trap_invalid=True)) >>> gmpy2.atan(mpfr("nan")) Traceback (most recent call last): File "", line 1, in InvalidOperationError: 'mpfr' invalid operation in atan() >>> gmpy2.set_context(gmpy2.context(precision=100)) >>> gmpy2.atan(mpfr("0.2")) mpfr('0.19739555984988075837004976519484',100) >>> gmpy2.get_context() context(precision=100, real_prec=Default, imag_prec=Default, round=RoundToNearest, real_round=Default, imag_round=Default, emax=1073741823, emin=-1073741823, subnormalize=False, trap_underflow=False, underflow=False, trap_overflow=False, overflow=False, trap_inexact=False, inexact=True, trap_invalid=False, invalid=False, trap_erange=False, erange=False, trap_divzero=False, divzero=False, trap_expbound=False, allow_complex=False) Test atan2 ---------- >>> gmpy2.set_context(gmpy2.context()) >>> _cmp(gmpy2.atan2(1,2), math.atan2(1,2)) True >>> _cmp(gmpy2.atan2(-1,2), math.atan2(-1,2)) True >>> _cmp(gmpy2.atan2(1,-2), math.atan2(1,-2)) True >>> _cmp(gmpy2.atan2(-1,-2), math.atan2(-1,-2)) True >>> _cmp(gmpy2.atan2(float("0"),float("0")), math.atan2(float("0"),float("0"))) True >>> _cmp(gmpy2.atan2(float("-0"),float("0")), math.atan2(float("-0"),float("0"))) True >>> _cmp(gmpy2.atan2(float("0"),float("-0")), math.atan2(float("0"),float("-0"))) True >>> _cmp(gmpy2.atan2(float("-0"),float("-0")), math.atan2(float("-0"),float("-0"))) True >>> _cmp(gmpy2.atan2(float("inf"),float("inf")), math.atan2(float("inf"),float("inf"))) True >>> _cmp(gmpy2.atan2(float("-inf"),float("inf")), math.atan2(float("-inf"),float("inf"))) True >>> _cmp(gmpy2.atan2(float("inf"),float("-inf")), math.atan2(float("inf"),float("-inf"))) True >>> _cmp(gmpy2.atan2(float("-inf"),float("-inf")), math.atan2(float("-inf"),float("-inf"))) True Test cos -------- >>> gmpy2.set_context(gmpy2.context()) >>> _cmp(gmpy2.cos(mpfr("0.2")), math.cos(0.2)) True >>> _cmp(gmpy2.cos(mpfr("20")), math.cos(20)) True >>> _cmp(gmpy2.cos(mpfr("2000")), math.cos(2000)) True >>> gmpy2.cos() Traceback (most recent call last): File "", line 1, in TypeError: cos() takes exactly one argument (0 given) >>> gmpy2.cos("a") Traceback (most recent call last): File "", line 1, in TypeError: cos() argument type not supported >>> gmpy2.cos(0,0) Traceback (most recent call last): File "", line 1, in TypeError: cos() takes exactly one argument (2 given) >>> gmpy2.cos(0) mpfr('1.0') >>> gmpy2.cos(mpz(0)) mpfr('1.0') >>> gmpy2.cos(mpq(1,2)) mpfr('0.87758256189037276') >>> gmpy2.cos(F(1,2)) mpfr('0.87758256189037276') >>> gmpy2.cos(D("0.5")) mpfr('0.87758256189037276') >>> gmpy2.cos(mpfr("nan")) mpfr('nan') >>> gmpy2.cos(mpfr("inf")) mpfr('nan') >>> gmpy2.cos(mpfr("-inf")) mpfr('nan') >>> gmpy2.set_context(gmpy2.context(trap_invalid=True)) >>> gmpy2.cos(mpfr("nan")) Traceback (most recent call last): File "", line 1, in InvalidOperationError: 'mpfr' invalid operation in cos() >>> gmpy2.cos(mpfr("inf")) Traceback (most recent call last): File "", line 1, in InvalidOperationError: 'mpfr' invalid operation in cos() >>> gmpy2.cos(mpfr("-inf")) Traceback (most recent call last): File "", line 1, in InvalidOperationError: 'mpfr' invalid operation in cos() >>> gmpy2.set_context(gmpy2.context(precision=100)) >>> gmpy2.cos(mpfr("0.2")) mpfr('0.98006657784124163112419651674809',100) >>> gmpy2.get_context() context(precision=100, real_prec=Default, imag_prec=Default, round=RoundToNearest, real_round=Default, imag_round=Default, emax=1073741823, emin=-1073741823, subnormalize=False, trap_underflow=False, underflow=False, trap_overflow=False, overflow=False, trap_inexact=False, inexact=True, trap_invalid=False, invalid=False, trap_erange=False, erange=False, trap_divzero=False, divzero=False, trap_expbound=False, allow_complex=False) Test cot -------- >>> gmpy2.set_context(gmpy2.context()) >>> _cmp(gmpy2.cot(mpfr("0.2")), 1/math.tan(0.2)) False >>> gmpy2.cot(mpfr("0.2")).as_integer_ratio() == (mpz(173569956714485), mpz(35184372088832)) True >>> (1/math.tan(0.2)).as_integer_ratio() == (5554238614863519, 1125899906842624) True >>> _cmp(gmpy2.cot(gmpy2.const_pi()), 1/math.tan(math.pi)) True >>> gmpy2.cot(1) mpfr('0.64209261593433076') >>> gmpy2.cot(float('0')) mpfr('inf') >>> gmpy2.cot(float('-0')) mpfr('-inf') >>> gmpy2.cot(mpfr('0')) mpfr('inf') >>> gmpy2.cot(mpfr('-0')) mpfr('-inf') Test csc -------- Test degrees ------------ Test radians ------------- Test sec -------- Test sin -------- Test sin_cos ------------ Test tan -------- Test acosh ---------- Test asinh ---------- Test atanh ---------- Test cosh --------- Test coth --------- Test csch --------- Test degrees ------------ Test radians ------------- Test sech --------- Test sinh --------- Test sinh_cosh -------------- Test tanh ---------