[Python-checkins] python/dist/src/Doc/tut tut.tex,1.234,1.235

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Thu Jul 1 08:56:57 EDT 2004


Update of /cvsroot/python/python/dist/src/Doc/tut
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17562

Modified Files:
	tut.tex 
Log Message:
* Fix typos.
* Format an example so that the identation is more obvious.
* Add a section on the decimal module to the Brief Tour Part II.



Index: tut.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/tut/tut.tex,v
retrieving revision 1.234
retrieving revision 1.235
diff -C2 -d -r1.234 -r1.235
*** tut.tex	3 Jun 2004 17:19:25 -0000	1.234
--- tut.tex	1 Jul 2004 12:56:54 -0000	1.235
***************
*** 4339,4343 ****
  
  \begin{verbatim}
! >>> class Reverse:
      "Iterator for looping over a sequence backwards"
      def __init__(self, data):
--- 4339,4343 ----
  
  \begin{verbatim}
! class Reverse:
      "Iterator for looping over a sequence backwards"
      def __init__(self, data):
***************
*** 4353,4358 ****
  
  >>> for char in Reverse('spam'):
! 	print char
! 
  m
  a
--- 4353,4358 ----
  
  >>> for char in Reverse('spam'):
! ...     print char
! ...
  m
  a
***************
*** 4372,4382 ****
  
  \begin{verbatim}
! >>> def reverse(data):
!         for index in range(len(data)-1, -1, -1):
!             yield data[index]
! 		
  >>> for char in reverse('golf'):
!         print char
! 
  f
  l
--- 4372,4382 ----
  
  \begin{verbatim}
! def reverse(data):
!     for index in range(len(data)-1, -1, -1):
!         yield data[index]
! 	
  >>> for char in reverse('golf'):
! ...     print char
! ...
  f
  l
***************
*** 4895,4899 ****
  \end{verbatim}
  
! The principal challenge of multi-thread applications is coordinating
  threads that share data or other resources.  To that end, the threading
  module provides a number of synchronization primitives including locks,
--- 4895,4899 ----
  \end{verbatim}
  
! The principal challenge of multi-threaded applications is coordinating
  threads that share data or other resources.  To that end, the threading
  module provides a number of synchronization primitives including locks,
***************
*** 4936,4940 ****
  output is sent to standard error.  Other output options include routing
  messages through email, datagrams, sockets, or to an HTTP Server.  New
! filters select different routing based on message priority:  DEBUG,
  INFO, WARNING, ERROR, and CRITICAL.
  
--- 4936,4940 ----
  output is sent to standard error.  Other output options include routing
  messages through email, datagrams, sockets, or to an HTTP Server.  New
! filters can select different routing based on message priority:  DEBUG,
  INFO, WARNING, ERROR, and CRITICAL.
  
***************
*** 4968,4977 ****
      ...             return str(self.value)
      ...
!     >>> a = A(10)		    # create a reference
      >>> d = weakref.WeakValueDictionary()
      >>> d['primary'] = a            # does not create a reference
!     >>> d['primary']		    # fetch the object if it is still alive
      10
!     >>> del a			    # remove the one reference
      >>> gc.collect()                # run garbage collection right away
      0
--- 4968,4977 ----
      ...             return str(self.value)
      ...
!     >>> a = A(10)                   # create a reference
      >>> d = weakref.WeakValueDictionary()
      >>> d['primary'] = a            # does not create a reference
!     >>> d['primary']                # fetch the object if it is still alive
      10
!     >>> del a                       # remove the one reference
      >>> gc.collect()                # run garbage collection right away
      0
***************
*** 5057,5060 ****
--- 5057,5116 ----
  
  
+ \section{Tools for Working with Decimal Floating Point\label{decimal-fp}}
+ 
+ The \module{decimal} module, offers a \class{Decimal} data type for
+ decimal floating point arithmetic.  Compared to the built-in \class{float}
+ type implemented with binary floating point, the new class is especially
+ useful for financial applications and other uses which require exact
+ decimal representation, control over precision, control over rounding
+ to meet legal or regulatory requirements, tracking of significant
+ decimal places, or for applications where the user expects the results
+ to match hand calculations done as taught in school.
+ 
+ For example, calculating a 5% tax on a 70 cent phone charge gives
+ different results in decimal floating point and binary floating point
+ with the difference being significant when rounding to the nearest
+ cent:
+ 
+ \begin{verbatim}
+ >>> from decimal import *       
+ >>> Decimal('0.70') * Decimal('1.05')
+ Decimal("0.7350")
+ >>> .70 * 1.05
+ 0.73499999999999999       
+ \end{verbatim}
+ 
+ Note that the \class{Decimal} result keeps a trailing zero, automatically
+ inferring four place significance from two digit mulitiplicands.  Decimal
+ reproduces mathematics as done by hand and avoids issues that can arise
+ when binary floating point cannot exactly represent decimal quantities.
+ 
+ Exact representation enables the \class{Decimal} class to perform
+ modulo calculations and equality tests that are unsuitable for binary
+ floating point:
+ 
+ \begin{verbatim}
+ >>> Decimal('1.00') % Decimal('.10')
+ Decimal("0.00")
+ >>> 1.00 % 0.10
+ 0.09999999999999995
+        
+ >>> sum([Decimal('0.1')]*10) == Decimal('1.0')
+ True
+ >>> sum([0.1]*10) == 1.0
+ False      
+ \end{verbatim}
+ 
+ The \module{decimal} module also allows arbitrarily large precisions to be
+ set for calculation:
+ 
+ \begin{verbatim}
+ >>> getcontext().prec = 36
+ >>> Decimal(1) / Decimal(7)
+ Decimal("0.142857142857142857142857142857142857")
+ \end{verbatim}
+ 
+ 
+ 
  \chapter{What Now? \label{whatNow}}
  




More information about the Python-checkins mailing list