> + 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 _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com