@@ -4338,7 +4338,7 @@ \section{Iterators\label{iterators}}
43384338\method {next()}, then \method {__iter__()} can just return \code {self}:
43394339
43404340\begin {verbatim }
4341- >>> class Reverse:
4341+ class Reverse:
43424342 "Iterator for looping over a sequence backwards"
43434343 def __init__(self, data):
43444344 self.data = data
@@ -4352,8 +4352,8 @@ \section{Iterators\label{iterators}}
43524352 return self.data[self.index]
43534353
43544354>>> for char in Reverse('spam'):
4355- print char
4356-
4355+ ... print char
4356+ ...
43574357m
43584358a
43594359p
@@ -4371,13 +4371,13 @@ \section{Generators\label{generators}}
43714371be trivially easy to create:
43724372
43734373\begin {verbatim }
4374- >>> def reverse(data):
4375- for index in range(len(data)-1, -1, -1):
4376- yield data[index]
4377-
4374+ def reverse(data):
4375+ for index in range(len(data)-1, -1, -1):
4376+ yield data[index]
4377+
43784378>>> for char in reverse('golf'):
4379- print char
4380-
4379+ ... print char
4380+ ...
43814381f
43824382l
43834383o
@@ -4894,7 +4894,7 @@ \section{Multi-threading\label{multi-threading}}
48944894 print 'The main program continues to run'
48954895\end {verbatim }
48964896
4897- The principal challenge of multi-thread applications is coordinating
4897+ The principal challenge of multi-threaded applications is coordinating
48984898threads that share data or other resources. To that end, the threading
48994899module provides a number of synchronization primitives including locks,
49004900events, condition variables, and semaphores.
@@ -4935,7 +4935,7 @@ \section{Logging\label{logging}}
49354935By default, informational and debugging messages are suppressed and the
49364936output is sent to standard error. Other output options include routing
49374937messages through email, datagrams, sockets, or to an HTTP Server. New
4938- filters select different routing based on message priority: DEBUG,
4938+ filters can select different routing based on message priority: DEBUG,
49394939INFO, WARNING, ERROR, and CRITICAL.
49404940
49414941The logging system can be configured directly from Python or can be
@@ -4967,12 +4967,12 @@ \section{Weak References\label{weak-references}}
49674967 ... def __repr__(self):
49684968 ... return str(self.value)
49694969 ...
4970- >>> a = A(10) # create a reference
4970+ >>> a = A(10) # create a reference
49714971 >>> d = weakref.WeakValueDictionary()
49724972 >>> d['primary'] = a # does not create a reference
4973- >>> d['primary'] # fetch the object if it is still alive
4973+ >>> d['primary'] # fetch the object if it is still alive
49744974 10
4975- >>> del a # remove the one reference
4975+ >>> del a # remove the one reference
49764976 >>> gc.collect() # run garbage collection right away
49774977 0
49784978 >>> d['primary'] # entry was automatically removed
@@ -5056,6 +5056,62 @@ \section{Tools for Working with Lists\label{list-tools}}
50565056\end {verbatim }
50575057
50585058
5059+ \section {Tools for Working with Decimal Floating Point\label {decimal-fp } }
5060+
5061+ The \module {decimal} module, offers a \class {Decimal} data type for
5062+ decimal floating point arithmetic. Compared to the built-in \class {float}
5063+ type implemented with binary floating point, the new class is especially
5064+ useful for financial applications and other uses which require exact
5065+ decimal representation, control over precision, control over rounding
5066+ to meet legal or regulatory requirements, tracking of significant
5067+ decimal places, or for applications where the user expects the results
5068+ to match hand calculations done as taught in school.
5069+
5070+ For example, calculating a 5% tax on a 70 cent phone charge gives
5071+ different results in decimal floating point and binary floating point
5072+ with the difference being significant when rounding to the nearest
5073+ cent:
5074+
5075+ \begin {verbatim }
5076+ >>> from decimal import *
5077+ >>> Decimal('0.70') * Decimal('1.05')
5078+ Decimal("0.7350")
5079+ >>> .70 * 1.05
5080+ 0.73499999999999999
5081+ \end {verbatim }
5082+
5083+ Note that the \class {Decimal} result keeps a trailing zero, automatically
5084+ inferring four place significance from two digit mulitiplicands. Decimal
5085+ reproduces mathematics as done by hand and avoids issues that can arise
5086+ when binary floating point cannot exactly represent decimal quantities.
5087+
5088+ Exact representation enables the \class {Decimal} class to perform
5089+ modulo calculations and equality tests that are unsuitable for binary
5090+ floating point:
5091+
5092+ \begin {verbatim }
5093+ >>> Decimal('1.00') % Decimal('.10')
5094+ Decimal("0.00")
5095+ >>> 1.00 % 0.10
5096+ 0.09999999999999995
5097+
5098+ >>> sum([Decimal('0.1')]*10) == Decimal('1.0')
5099+ True
5100+ >>> sum([0.1]*10) == 1.0
5101+ False
5102+ \end {verbatim }
5103+
5104+ The \module {decimal} module also allows arbitrarily large precisions to be
5105+ set for calculation:
5106+
5107+ \begin {verbatim }
5108+ >>> getcontext().prec = 36
5109+ >>> Decimal(1) / Decimal(7)
5110+ Decimal("0.142857142857142857142857142857142857")
5111+ \end {verbatim }
5112+
5113+
5114+
50595115\chapter {What Now? \label {whatNow } }
50605116
50615117Reading this tutorial has probably reinforced your interest in using
0 commit comments