[issue7770] sin/cos function in decimal-docs

Johannes Schönberger report at bugs.python.org
Sun Jan 24 14:46:13 CET 2010


Johannes Schönberger <jls at icoost.com> added the comment:

This is the version I would suggest and which is quite accurate unless the numbers get extremely large.

def sin(x):
    """Return the sine of x as measured in radians.

    >>> print sin(Decimal('0.5'))
    0.4794255386042030002732879352
    >>> print sin(0.5)
    0.479425538604
    >>> print sin(0.5+0j)
    (0.479425538604+0j)

    """
    x = x - pi()*int(x / pi())
    getcontext().prec += 2
    i, lasts, s, fact, num, sign = 1, 0, x, 1, x, 1
    while s != lasts:
        lasts = s
        i += 2
        fact *= i * (i-1)
        num *= x * x
        sign *= -1
        s += num / fact * sign
    getcontext().prec -= 2
    return +s

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7770>
_______________________________________


More information about the Python-bugs-list mailing list