MPFR Functionality
==================

>>> import gmpy2
>>> from gmpy2 import mpfr, mpq, mpz
>>> from fractions import Fraction as F
>>> import sys

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())
>>> gmpy2.acos(mpfr("0.2")).as_integer_ratio()
(mpz(6167402294989009), mpz(4503599627370496))
>>> gmpy2.acos()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: acos() takes exactly one argument (0 given)
>>> gmpy2.acos("a")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: acos() argument type not supported
>>> gmpy2.acos(0,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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(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 "<stdin>", line 1, in <module>
InvalidOperationError: invalid operation
>>> gmpy2.acos(mpfr("inf"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
InvalidOperationError: invalid operation
>>> gmpy2.acos(mpfr("-inf"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
InvalidOperationError: invalid operation
>>> 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,
        allow_complex=False,
        rational_division=False)

Test asin
---------

>>> gmpy2.set_context(gmpy2.context())
>>> gmpy2.asin(mpfr("0.2")).as_integer_ratio()
(mpz(7254683656315453), mpz(36028797018963968))
>>> gmpy2.asin()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: asin() takes exactly one argument (0 given)
>>> gmpy2.asin("a")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: asin() argument type not supported
>>> gmpy2.asin(0,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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(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 "<stdin>", line 1, in <module>
InvalidOperationError: invalid operation
>>> gmpy2.asin(mpfr("inf"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
InvalidOperationError: invalid operation
>>> gmpy2.asin(mpfr("-inf"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
InvalidOperationError: invalid operation
>>> 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,
        allow_complex=False,
        rational_division=False)

Test atan
---------

>>> gmpy2.set_context(gmpy2.context())
>>> gmpy2.atan(mpfr("0.2")).as_integer_ratio()
(mpz(1777981139569027), mpz(9007199254740992))
>>> gmpy2.atan(mpfr("100")).as_integer_ratio()
(mpz(3514601628432273), mpz(2251799813685248))
>>> gmpy2.atan()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: atan() takes exactly one argument (0 given)
>>> gmpy2.atan("a")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: atan() argument type not supported
>>> gmpy2.atan(0,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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(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 "<stdin>", line 1, in <module>
InvalidOperationError: invalid operation
>>> 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,
        allow_complex=False,
        rational_division=False)

Test atan2
----------

>>> gmpy2.set_context(gmpy2.context())
>>> gmpy2.atan2(1,2).as_integer_ratio()
(mpz(8352332796509007), mpz(18014398509481984))
>>> gmpy2.atan2(-1,2).as_integer_ratio()
(mpz(-8352332796509007), mpz(18014398509481984))
>>> gmpy2.atan2(1,-2).as_integer_ratio()
(mpz(3015098076232407), mpz(1125899906842624))
>>> gmpy2.atan2(-1,-2).as_integer_ratio()
(mpz(-3015098076232407), mpz(1125899906842624))
>>> gmpy2.atan2(float("0"),float("0")).as_integer_ratio()
(mpz(0), mpz(1))
>>> gmpy2.atan2(float("-0"),float("0")).as_integer_ratio()
(mpz(0), mpz(1))
>>> gmpy2.atan2(float("0"),float("-0")).as_integer_ratio()
(mpz(884279719003555), mpz(281474976710656))
>>> gmpy2.atan2(float("-0"),float("-0")).as_integer_ratio()
(mpz(-884279719003555), mpz(281474976710656))
>>> gmpy2.atan2(float("inf"),float("inf")).as_integer_ratio()
(mpz(884279719003555), mpz(1125899906842624))
>>> gmpy2.atan2(float("-inf"),float("inf")).as_integer_ratio()
(mpz(-884279719003555), mpz(1125899906842624))
>>> gmpy2.atan2(float("inf"),float("-inf")).as_integer_ratio()
(mpz(2652839157010665), mpz(1125899906842624))
>>> gmpy2.atan2(float("-inf"),float("-inf")).as_integer_ratio()
(mpz(-2652839157010665), mpz(1125899906842624))

Test cos
--------

>>> gmpy2.set_context(gmpy2.context())
>>> gmpy2.cos(mpfr("0.2")).as_integer_ratio()
(mpz(4413827474764093), mpz(4503599627370496))
>>> gmpy2.cos(mpfr("20")).as_integer_ratio() == (mpz(7351352886077503), mpz(18014398509481984)) or (sys.platform == 'win32')
True
>>> gmpy2.cos(mpfr("2000")).as_integer_ratio()
(mpz(-3309781376808469), mpz(9007199254740992))
>>> gmpy2.cos()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cos() takes exactly one argument (0 given)
>>> gmpy2.cos("a")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cos() argument type not supported
>>> gmpy2.cos(0,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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(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 "<stdin>", line 1, in <module>
InvalidOperationError: invalid operation
>>> gmpy2.cos(mpfr("inf"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
InvalidOperationError: invalid operation
>>> gmpy2.cos(mpfr("-inf"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
InvalidOperationError: invalid operation
>>> 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,
        allow_complex=False,
        rational_division=False)

Test cot
--------

>>> gmpy2.set_context(gmpy2.context())
>>> gmpy2.cot(mpfr("0.2")).as_integer_ratio()
(mpz(173569956714485), mpz(35184372088832))
>>> gmpy2.cot(gmpy2.const_pi()).as_integer_ratio()
(mpz(-8165619676597685), mpz(1))
>>> 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
--------

>>> r2 = mpfr('5.6')
>>> gmpy2.csc(r2)
mpfr('-1.5841166632383596')

Test sec
--------

>>> gmpy2.sec(r2)
mpfr('1.2893811186238056')

Test sin
--------

>>> r = mpfr(5.6)
>>> gmpy2.sin(r)
mpfr('-0.63126663787232162')
>>> gmpy2.sin(r) == gmpy2.sin(5.6)
True

Test sin_cos
------------

>>> gmpy2.sin_cos(r)
(mpfr('-0.63126663787232162'), mpfr('0.77556587851024961'))
>>> gmpy2.sin_cos(r) == gmpy2.sin_cos(5.6)
True
>>> gmpy2.sin_cos(r) == (gmpy2.sin(r), gmpy2.cos(r))
True

Test tan
--------

>>> gmpy2.tan(r)
mpfr('-0.8139432836897027')

Test acosh
----------

>>> gmpy2.acosh(r)
mpfr('2.4078447868719399')

Test asinh
----------

>>> gmpy2.asinh(r)
mpfr('2.4237920435875173')

Test atanh
----------

>>> gmpy2.atanh(mpfr(0.365))
mpfr('0.38264235436318422')
>>> gmpy2.atanh(mpfr(0.365)) == gmpy2.atanh(0.365)
True

Test cosh
---------

>>> gmpy2.cosh(r)
mpfr('135.2150526449345')
>>> gmpy2.cosh(r) == gmpy2.cosh(5.6)
True

Test coth
---------

>>> gmpy2.coth(r)
mpfr('1.0000273487661038')

Test csch
---------

>>> gmpy2.csch(r)
mpfr('0.0073958285649757295')

>>> gmpy2.csch(r)
mpfr('0.0073958285649757295')

Test degrees
------------
>>> rad = mpfr(1.57)
>>> ctx = gmpy2.get_context()
>>> ctx.degrees(rad)
mpfr('89.954373835539243')
>>> gmpy2.degrees(rad)
mpfr('89.954373835539243')
>>> gmpy2.degrees(1)
mpfr('57.295779513082323')

Test radians
-------------

>>> deg = mpfr(90)
>>> ctx.radians(deg)
mpfr('1.5707963267948966')
>>> gmpy2.radians(deg)
mpfr('1.5707963267948966')
>>> gmpy2.radians(45)
mpfr('0.78539816339744828')
>>> gmpy2.radians(mpz(20))
mpfr('0.3490658503988659')
>>> gmpy2.radians(mpfr('inf'))
mpfr('inf')
>>> gmpy2.radians(mpfr('nan'))
mpfr('nan')

Test sech
---------

>>> gmpy2.sech(r)
mpfr('0.0073956263037217584')

Test sinh
---------

>>> gmpy2.sinh(r)
mpfr('135.21135478121803')
>>> gmpy2.sinh(r) == gmpy2.sinh(5.6)
True

Test sinh_cosh
--------------

>>> gmpy2.sinh_cosh(r)
(mpfr('135.21135478121803'), mpfr('135.2150526449345'))
>>> gmpy2.sinh_cosh(r) == gmpy2.sinh_cosh(5.6)
True
>>> gmpy2.sinh_cosh(r) == (gmpy2.sinh(r), gmpy2.cosh(r))
True

Test tanh
---------

>>> gmpy2.tanh(r)
mpfr('0.99997265198183083')
