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

gvanrossum@users.sourceforge.net gvanrossum at users.sourceforge.net
Wed May 18 00:15:52 CEST 2005


Update of /cvsroot/python/python/nondist/peps
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5548

Modified Files:
	pep-0343.txt 
Log Message:
Elaborate Decimal context example, thanks to Michael Chermside.


Index: pep-0343.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/peps/pep-0343.txt,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- pep-0343.txt	16 May 2005 13:42:52 -0000	1.11
+++ pep-0343.txt	17 May 2005 22:15:50 -0000	1.12
@@ -388,9 +388,33 @@
        by default all signals are blocked.  The implementation is left
        as an exercise to the reader.
 
-    8. Another use for this feature is the Decimal context.  It's left
-       as an exercise for the reader.  (Mail it to me if you'd like to
-       see it here.)
+    8. Another use for this feature is the Decimal context.  Here's a
+       simple example, after one posted by Michael Chermside:
+
+        import decimal
+
+        @do_template
+        def with_extra_precision(places=2):
+            c = decimal.getcontext()
+            saved_prec = c.prec
+            c.prec += places
+            yield None
+            c.prec = saved_prec
+
+       Sample usage (adapted from the Python Library Reference):
+
+        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
 
 References
 



More information about the Python-checkins mailing list