The short answer is Decimal.cos () and Decimal.sin () can be implemented from the implementation of Decimal.exp (), breaking all even members into cos () and all odd members into sin () and alternating each sign between positive and negative in both series. The loop does not require any changes that calculate only N terms based on the configured precision (Decimal.getcontext (). Prec).
Long answer - Python decimal.Decimal supports the exp () function, which takes only a real number argument (unlike exp () in the R language) and calculates an infinite series only up to the number of terms based on the configured precision (decimal.Decimal.getcontext () . Prec).
Currently, even expressions calculate cosh (), and odd terms calculate sinh (). Their sum is returned as the result of exp (). If the sign of each member has been changed to alternate between positive and negative in each row, a series of even members will calculate cos (), and a series of odd members will calculate sin ().
Also, like R, this change may allow Decimal.exp () to support complex arguments, so exp (1j * x) can return Decimal.cos (x) + 1j * Decimal.sin (x).
source share