[Python-Dev] [Python-checkins] python/nondist/peps pep-0343.txt, 1.11, 1.12

Raymond Hettinger python at rcn.com
Wed May 18 05:47:39 CEST 2005


> +        def sin(x):
> +            "Return the sine of x as measured in radians."
> +            do with_extra_precision():
> +                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
> +                return +s

One more change:  The final "return +s" should be unindented.  It should
be at the same level as the "do with_extra_precision()".  The purpose of
the "+s" is to force the result to be rounded back to the *original*
precision.

This nuance is likely to be the bane of folks who shift back and forth
between different levels of precision.  The following example shows the
kind of oddity that can arise when working with quantities that have not
been rounded to the current precision:

>>> from decimal import getcontext, Decimal as D
>>> getcontext().prec = 3
>>> D('3.104') + D('2.104')
Decimal("5.21")
>>> D('3.104') + D('0.000') + D('2.104')
Decimal("5.20")



Raymond


More information about the Python-Dev mailing list