[Python-checkins] python/dist/src/Doc/lib libdecimal.tex,1.11,1.12
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Sun Jul 11 14:40:22 CEST 2004
Update of /cvsroot/python/python/dist/src/Doc/lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11727
Modified Files:
libdecimal.tex
Log Message:
Minor improvements, fixups and wording changes everywhere.
Index: libdecimal.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/lib/libdecimal.tex,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** libdecimal.tex 10 Jul 2004 14:14:37 -0000 1.11
--- libdecimal.tex 11 Jul 2004 12:40:19 -0000 1.12
***************
*** 22,26 ****
\item Decimal numbers can be represented exactly. In contrast, numbers like
! \constant{1.1} do not have an exact representations in binary floating point.
End users typically wound not expect \constant{1.1} to display as
\constant{1.1000000000000001} as it does with binary floating point.
--- 22,26 ----
\item Decimal numbers can be represented exactly. In contrast, numbers like
! \constant{1.1} do not have an exact representation in binary floating point.
End users typically wound not expect \constant{1.1} to display as
\constant{1.1000000000000001} as it does with binary floating point.
***************
*** 71,82 ****
The context for arithmetic is an environment specifying precision, rounding
! rules, limits on exponents, flags that indicate the results of operations,
! and trap enablers which determine whether signals are to be treated as
exceptions. Rounding options include \constant{ROUND_CEILING},
\constant{ROUND_DOWN}, \constant{ROUND_FLOOR}, \constant{ROUND_HALF_DOWN},
\constant{ROUND_HALF_EVEN}, \constant{ROUND_HALF_UP}, and \constant{ROUND_UP}.
! Signals are types of information that arise during the course of a
! computation. Depending on the needs of the application, some signals may be
ignored, considered as informational, or treated as exceptions. The signals in
the decimal module are: \constant{Clamped}, \constant{InvalidOperation},
--- 71,82 ----
The context for arithmetic is an environment specifying precision, rounding
! rules, limits on exponents, flags indicating the results of operations,
! and trap enablers which determine whether signals are treated as
exceptions. Rounding options include \constant{ROUND_CEILING},
\constant{ROUND_DOWN}, \constant{ROUND_FLOOR}, \constant{ROUND_HALF_DOWN},
\constant{ROUND_HALF_EVEN}, \constant{ROUND_HALF_UP}, and \constant{ROUND_UP}.
! Signals are groups of exceptional conditions arising during the course of
! computation. Depending on the needs of the application, signals may be
ignored, considered as informational, or treated as exceptions. The signals in
the decimal module are: \constant{Clamped}, \constant{InvalidOperation},
***************
*** 105,111 ****
\subsection{Quick-start Tutorial \label{decimal-tutorial}}
! The normal start to using decimals is to import the module, and then use
! \function{getcontext()} to view the context and, if necessary, set the context
! precision, rounding, or trap enablers:
\begin{verbatim}
--- 105,111 ----
\subsection{Quick-start Tutorial \label{decimal-tutorial}}
! The usual start to using decimals is importing the module, viewing the current
! context with \function{getcontext()} and, if necessary, setting new values
! for precision, rounding, or enabled traps:
\begin{verbatim}
***************
*** 113,128 ****
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,
! capitals=1, flags=[], traps=[])
! >>> getcontext().prec = 7
\end{verbatim}
Decimal instances can be constructed from integers, strings or tuples. To
create a Decimal from a \class{float}, first convert it to a string. This
serves as an explicit reminder of the details of the conversion (including
! representation error). Malformed strings signal \constant{InvalidOperation}
! and return a special kind of Decimal called a \constant{NaN} which stands for
! ``Not a number''. Positive and negative \constant{Infinity} is yet another
! special kind of Decimal.
\begin{verbatim}
--- 113,129 ----
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,
! capitals=1, flags=[], traps=[Overflow, InvalidOperation,
! DivisionByZero])
! >>> getcontext().prec = 7 # Set a new precision
\end{verbatim}
+
Decimal instances can be constructed from integers, strings or tuples. To
create a Decimal from a \class{float}, first convert it to a string. This
serves as an explicit reminder of the details of the conversion (including
! representation error). Decimal numbers include special values such as
! \constant{NaN} which stands for ``Not a number'', positive and negative
! \constant{Infinity}, and \constant{-0}.
\begin{verbatim}
***************
*** 141,152 ****
\end{verbatim}
! Creating decimals is unaffected by context precision. Their level of
! significance is completely determined by the number of digits input. It is
! the arithmetic operations that are governed by context.
\begin{verbatim}
>>> getcontext().prec = 6
- >>> Decimal('3.0000')
- Decimal("3.0000")
>>> Decimal('3.0')
Decimal("3.0")
--- 142,152 ----
\end{verbatim}
!
! The significance of a new Decimal is determined solely by the number
! of digits input. Context precision and rounding only come into play during
! arithmetic operations.
\begin{verbatim}
>>> getcontext().prec = 6
>>> Decimal('3.0')
Decimal("3.0")
***************
*** 160,163 ****
--- 160,164 ----
\end{verbatim}
+
Decimals interact well with much of the rest of python. Here is a small
decimal floating point flying circus:
***************
*** 191,198 ****
\end{verbatim}
! The \function{getcontext()} function accesses the current context. This one
! context is sufficient for many applications; however, for more advanced work,
! multiple contexts can be created using the Context() constructor. To make a
! new context active, use the \function{setcontext()} function.
In accordance with the standard, the \module{Decimal} module provides two
--- 192,213 ----
\end{verbatim}
! The \method{quantize()} method rounds a number to a fixed exponent. This
! method is useful for monetary applications that often round results to a fixed
! number of places:
!
! \begin{verbatim}
! >>> Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_DOWN)
! Decimal("7.32")
! >>> Decimal('7.325').quantize(Decimal('1.'), rounding=ROUND_UP)
! Decimal("8")
! \end{verbatim}
!
! As shown above, the \function{getcontext()} function accesses the current
! context and allows the settings to be changed. This approach meets the
! needs of most applications.
!
! For more advanced work, it may be useful to create alternate contexts using
! the Context() constructor. To make an alternate active, use the
! \function{setcontext()} function.
In accordance with the standard, the \module{Decimal} module provides two
***************
*** 206,215 ****
Context(prec=60, rounding=ROUND_HALF_DOWN, Emin=-999999999, Emax=999999999,
capitals=1, flags=[], traps=[])
- >>> ExtendedContext
- Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,
- capitals=1, flags=[], traps=[])
>>> setcontext(myothercontext)
>>> Decimal(1) / Decimal(7)
Decimal("0.142857142857142857142857142857142857142857142857142857142857")
>>> setcontext(ExtendedContext)
>>> Decimal(1) / Decimal(7)
--- 221,231 ----
Context(prec=60, rounding=ROUND_HALF_DOWN, Emin=-999999999, Emax=999999999,
capitals=1, flags=[], traps=[])
>>> setcontext(myothercontext)
>>> Decimal(1) / Decimal(7)
Decimal("0.142857142857142857142857142857142857142857142857142857142857")
+
+ >>> ExtendedContext
+ Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,
+ capitals=1, flags=[], traps=[])
>>> setcontext(ExtendedContext)
>>> Decimal(1) / Decimal(7)
***************
*** 217,220 ****
--- 233,237 ----
>>> Decimal(42) / Decimal(0)
Decimal("Infinity")
+
>>> setcontext(BasicContext)
>>> Decimal(42) / Decimal(0)
***************
*** 225,236 ****
\end{verbatim}
! Besides using contexts to control precision, rounding, and trapping signals,
! they can be used to monitor flags which give information collected during
! computation. The flags remain set until explicitly cleared, so it is best to
! clear the flags before each set of monitored computations by using the
! \method{clear_flags()} method.
\begin{verbatim}
>>> setcontext(ExtendedContext)
>>> Decimal(355) / Decimal(113)
Decimal("3.14159292")
--- 242,254 ----
\end{verbatim}
!
! Contexts also have signal flags for monitoring exceptional conditions
! encountered during computations. The flags remain set until explicitly
! cleared, so it is best to clear the flags before each set of monitored
! computations by using the \method{clear_flags()} method.
\begin{verbatim}
>>> setcontext(ExtendedContext)
+ >>> getcontext().clear_flags()
>>> Decimal(355) / Decimal(113)
Decimal("3.14159292")
***************
*** 240,247 ****
\end{verbatim}
! The \var{flags} entry shows that the rational approximation to
! \constant{Pi} was rounded (digits beyond the context precision were thrown
! away) and that the result is inexact (some of the discarded digits were
! non-zero).
Individual traps are set using the dictionary in the \member{traps}
--- 258,264 ----
\end{verbatim}
! The \var{flags} entry shows that the rational approximation to \constant{Pi}
! was rounded (digits beyond the context precision were thrown away) and that
! the result is inexact (some of the discarded digits were non-zero).
Individual traps are set using the dictionary in the \member{traps}
***************
*** 260,283 ****
\end{verbatim}
! To turn all the traps on or off all at once, use a loop. Also, the
! \method{dict.update()} method is useful for changing a handfull of values.
!
! \begin{verbatim}
! >>> getcontext.clear_flags()
! >>> for sig in getcontext().traps:
! ... getcontext().traps[sig] = 1
!
! >>> getcontext().traps.update({Rounded:0, Inexact:0, Subnormal:0})
! >>> getcontext()
! Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,
! capitals=1, flags=[], traps=[Clamped, Underflow,
! InvalidOperation, DivisionByZero, Overflow])
! \end{verbatim}
!
! Applications typically set the context once at the beginning of a program
! and no further changes are needed. For many applications, the data resides
! in a resource external to the program and is converted to \class{Decimal} with
! a single cast inside a loop. Afterwards, decimals are as easily manipulated
! as other Python numeric types.
--- 277,285 ----
\end{verbatim}
! Most programs adjust the current context only once, at the beginning of the
! program. And, in many applications, data is converted to \class{Decimal} with
! a single cast inside a loop. With context set and decimals created, the bulk
! of the program manipulates the data no differently than with other Python
! numeric types.
***************
*** 309,326 ****
If \var{value} is a \class{tuple}, it should have three components,
a sign (\constant{0} for positive or \constant{1} for negative),
! a \class{tuple} of digits, and an exponent represented as an integer.
! For example, \samp{Decimal((0, (1, 4, 1, 4), -3))} returns
! \code{Decimal("1.414")}.
! The supplied \var{context} or, if not specified, the current context
! governs only the handling of malformed strings not conforming to the
! numeric string syntax. If the context traps \constant{InvalidOperation},
! an exception is raised; otherwise, the constructor returns a new Decimal
! with the value of \constant{NaN}.
! The context serves no other purpose. The number of significant digits
! recorded is determined solely by the \var{value} and the \var{context}
! precision is not a factor. For example, \samp{Decimal("3.0000")} records
! all four zeroes even if the context precision is only three.
Once constructed, \class{Decimal} objects are immutable.
--- 311,326 ----
If \var{value} is a \class{tuple}, it should have three components,
a sign (\constant{0} for positive or \constant{1} for negative),
! a \class{tuple} of digits, and an integer exponent. For example,
! \samp{Decimal((0, (1, 4, 1, 4), -3))} returns \code{Decimal("1.414")}.
! The \var{context} precision does not affect how many digits are stored.
! That is determined exclusively by the number of digits in \var{value}. For
! example, \samp{Decimal("3.00000")} records all five zeroes even if the
! context precision is only three.
! The purpose of the \var{context} argument is determining what to do if
! \var{value} is a malformed string. If the context traps
! \constant{InvalidOperation}, an exception is raised; otherwise, the
! constructor returns a new Decimal with the value of \constant{NaN}.
Once constructed, \class{Decimal} objects are immutable.
***************
*** 335,345 ****
In addition to the standard numeric properties, decimal floating point objects
! have a number of more specialized methods:
\begin{methoddesc}{adjusted}{}
Return the adjusted exponent after shifting out the coefficient's rightmost
digits until only the lead digit remains: \code{Decimal("321e+5").adjusted()}
! returns seven. Used for determining the place value of the most significant
! digit.
\end{methoddesc}
--- 335,345 ----
In addition to the standard numeric properties, decimal floating point objects
! also have a number of specialized methods:
\begin{methoddesc}{adjusted}{}
Return the adjusted exponent after shifting out the coefficient's rightmost
digits until only the lead digit remains: \code{Decimal("321e+5").adjusted()}
! returns seven. Used for determining the position of the most significant
! digit with respect to the decimal point.
\end{methoddesc}
***************
*** 390,394 ****
\begin{methoddesc}{remainder_near}{other\optional{, context}}
! Computed the modulo as either a positive or negative value depending
on which is closest to zero. For instance,
\samp{Decimal(10).remainder_near(6)} returns \code{Decimal("-2")}
--- 390,394 ----
\begin{methoddesc}{remainder_near}{other\optional{, context}}
! Computes the modulo as either a positive or negative value depending
on which is closest to zero. For instance,
\samp{Decimal(10).remainder_near(6)} returns \code{Decimal("-2")}
***************
*** 423,433 ****
\end{methoddesc}
!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Context objects \label{decimal-decimal}}
! Contexts are environments for arithmetic operations. They govern the precision,
! rules for rounding, determine which signals are treated as exceptions, and set limits
! on the range for exponents.
Each thread has its own current context which is accessed or changed using
--- 423,434 ----
\end{methoddesc}
!
!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Context objects \label{decimal-decimal}}
! Contexts are environments for arithmetic operations. They govern precision,
! set rules for rounding, determine which signals are treated as exceptions, and
! limit the range for exponents.
Each thread has its own current context which is accessed or changed using
***************
*** 465,473 ****
that prefer to have result value of \constant{NaN} or \constant{Infinity}
instead of raising exceptions. This allows an application to complete a
! run in the presense of conditions that would otherwise halt the program.
\end{classdesc*}
\begin{classdesc*}{DefaultContext}
! This class is used by the \class{Context} constructor as a prototype for
new contexts. Changing a field (such a precision) has the effect of
changing the default for new contexts creating by the \class{Context}
--- 466,474 ----
that prefer to have result value of \constant{NaN} or \constant{Infinity}
instead of raising exceptions. This allows an application to complete a
! run in the presence of conditions that would otherwise halt the program.
\end{classdesc*}
\begin{classdesc*}{DefaultContext}
! This context is used by the \class{Context} constructor as a prototype for
new contexts. Changing a field (such a precision) has the effect of
changing the default for new contexts creating by the \class{Context}
***************
*** 480,487 ****
In single threaded environments, it is preferable to not use this context
! at all. Instead, simply create contexts explicitly. This is especially
! important because the default values context may change between releases
! (with initial release having precision=28, rounding=ROUND_HALF_EVEN,
! cleared flags, and no traps enabled).
\end{classdesc*}
--- 481,488 ----
In single threaded environments, it is preferable to not use this context
! at all. Instead, simply create contexts explicitly as described below.
!
! The default values are precision=28, rounding=ROUND_HALF_EVEN, and enabled
! traps for Overflow, InvalidOperation, and DivisionByZero.
\end{classdesc*}
***************
*** 509,514 ****
\constant{ROUND_UP} (away from zero).
! The \var{traps} and \var{flags} fields are mappings from signals
! to either \constant{0} or \constant{1}.
The \var{Emin} and \var{Emax} fields are integers specifying the outer
--- 510,515 ----
\constant{ROUND_UP} (away from zero).
! The \var{traps} and \var{flags} fields list any signals to be set.
! Generally, new contexts should only set traps and leave the flags clear.
The \var{Emin} and \var{Emax} fields are integers specifying the outer
***************
*** 517,525 ****
The \var{capitals} field is either \constant{0} or \constant{1} (the
default). If set to \constant{1}, exponents are printed with a capital
! \constant{E}; otherwise, lowercase is used: \constant{Decimal('6.02e+23')}.
\end{classdesc}
! The \class{Context} class defines several general methods as well as a
! large number of methods for doing arithmetic directly from the context.
\begin{methoddesc}{clear_flags}{}
--- 518,527 ----
The \var{capitals} field is either \constant{0} or \constant{1} (the
default). If set to \constant{1}, exponents are printed with a capital
! \constant{E}; otherwise, a lowercase \constant{e} is used:
! \constant{Decimal('6.02e+23')}.
\end{classdesc}
! The \class{Context} class defines several general purpose methods as well as a
! large number of methods for doing arithmetic directly in a given context.
\begin{methoddesc}{clear_flags}{}
***************
*** 532,541 ****
\begin{methoddesc}{create_decimal}{num}
! Creates a new Decimal instance but using \var{self} as context.
! Unlike the \class{Decimal} constructor, context precision,
rounding method, flags, and traps are applied to the conversion.
! This is useful because constants are often given to a greater
! precision than is needed by the application.
\end{methoddesc}
--- 534,543 ----
\begin{methoddesc}{create_decimal}{num}
! Creates a new Decimal instance from \var{num} but using \var{self} as
! context. Unlike the \class{Decimal} constructor, the context precision,
rounding method, flags, and traps are applied to the conversion.
! This is useful because constants are often given to a greater precision than
! is needed by the application.
\end{methoddesc}
***************
*** 543,547 ****
Returns a value equal to \samp{Emin - prec + 1} which is the minimum
exponent value for subnormal results. When underflow occurs, the
! exponont is set to \constant{Etiny}.
\end{methoddesc}
--- 545,549 ----
Returns a value equal to \samp{Emin - prec + 1} which is the minimum
exponent value for subnormal results. When underflow occurs, the
! exponent is set to \constant{Etiny}.
\end{methoddesc}
***************
*** 554,558 ****
instances and then apply arithmetic operations which take place within the
current context for the active thread. An alternate approach is to use
! context methods for calculating within s specific context. The methods are
similar to those for the \class{Decimal} class and are only briefly recounted
here.
--- 556,560 ----
instances and then apply arithmetic operations which take place within the
current context for the active thread. An alternate approach is to use
! context methods for calculating within a specific context. The methods are
similar to those for the \class{Decimal} class and are only briefly recounted
here.
***************
*** 587,591 ****
\begin{methoddesc}{max}{x, y}
! Compare two values numerically and returns the maximum.
If they are numerically equal then the left-hand operand is chosen as the
--- 589,593 ----
\begin{methoddesc}{max}{x, y}
! Compare two values numerically and return the maximum.
If they are numerically equal then the left-hand operand is chosen as the
***************
*** 594,598 ****
\begin{methoddesc}{min}{x, y}
! Compare two values numerically and returns the minimum.
If they are numerically equal then the left-hand operand is chosen as the
--- 596,600 ----
\begin{methoddesc}{min}{x, y}
! Compare two values numerically and return the minimum.
If they are numerically equal then the left-hand operand is chosen as the
***************
*** 637,648 ****
\begin{methoddesc}{quantize}{x, y}
! Returns a value equal to \var{x} after rounding and having the
! exponent of v\var{y}.
Unlike other operations, if the length of the coefficient after the quantize
! operation would be greater than precision then an
\constant{InvalidOperation} is signaled. This guarantees that, unless there
! is an error condition, the exponent of the result of a quantize is always
! equal to that of the right-hand operand.
Also unlike other operations, quantize never signals Underflow, even
--- 639,650 ----
\begin{methoddesc}{quantize}{x, y}
! Returns a value equal to \var{x} after rounding and having the exponent of
! \var{y}.
Unlike other operations, if the length of the coefficient after the quantize
! operation would be greater than precision, then an
\constant{InvalidOperation} is signaled. This guarantees that, unless there
! is an error condition, the quantized exponent is always equal to that of the
! right-hand operand.
Also unlike other operations, quantize never signals Underflow, even
***************
*** 713,717 ****
If the context's trap enabler is set for the signal, then the condition
causes a Python exception to be raised. For example, if the
! \class{DivisionByZero} trap is set, the a \exception{DivisionByZero}
exception is raised upon encountering the condition.
--- 715,719 ----
If the context's trap enabler is set for the signal, then the condition
causes a Python exception to be raised. For example, if the
! \class{DivisionByZero} trap is set, then a \exception{DivisionByZero}
exception is raised upon encountering the condition.
***************
*** 726,730 ****
\begin{classdesc*}{DecimalException}
! Base class for other signals.
\end{classdesc*}
--- 728,733 ----
\begin{classdesc*}{DecimalException}
! Base class for other signals and is a subclass of
! \exception{ArithmeticError}.
\end{classdesc*}
***************
*** 732,738 ****
Signals the division of a non-infinite number by zero.
! Can occur with division, modulo division, or when raising a number to
! a negative power. If this signal is not trapped, return
! \constant{Infinity} or \constant{-Infinity} with sign determined by
the inputs to the calculation.
\end{classdesc*}
--- 735,741 ----
Signals the division of a non-infinite number by zero.
! Can occur with division, modulo division, or when raising a number to a
! negative power. If this signal is not trapped, returns
! \constant{Infinity} or \constant{-Infinity} with the sign determined by
the inputs to the calculation.
\end{classdesc*}
***************
*** 741,747 ****
Indicates that rounding occurred and the result is not exact.
! Signals whenever non-zero digits were discarded during rounding.
! The rounded result is returned. The signal flag or trap is used
! to detect when results are inexact.
\end{classdesc*}
--- 744,750 ----
Indicates that rounding occurred and the result is not exact.
! Signals when non-zero digits were discarded during rounding. The rounded
! result is returned. The signal flag or trap is used to detect when
! results are inexact.
\end{classdesc*}
***************
*** 821,825 ****
object for each thread. Having separate thread contexts means that threads
may make changes (such as \code{getcontext.prec=10}) without interfering with
! other threads and without needing mutexes.
Likewise, the \function{setcontext()} function automatically assigns its target
--- 824,828 ----
object for each thread. Having separate thread contexts means that threads
may make changes (such as \code{getcontext.prec=10}) without interfering with
! other threads.
Likewise, the \function{setcontext()} function automatically assigns its target
***************
*** 830,847 ****
in the current thread.
! The new context is copied from a prototype context called \var{DefaultContext}.
! To control the defaults so that each thread will use the same values
! throughout the application, directly modify the \var{DefaultContext} object.
! This should be done \emph{before} any threads are started so that there won't
! be a race condition with threads calling \function{getcontext()}. For example:
\begin{verbatim}
# Set applicationwide defaults for all threads about to be launched
! DefaultContext.prec=12
! DefaultContext.rounding=ROUND_DOWN
! DefaultContext.traps=dict.fromkeys(Signals, 0)
setcontext(DefaultContext)
! # Now start all of the threads
t1.start()
t2.start()
--- 833,849 ----
in the current thread.
! The new context is copied from a prototype context called
! \var{DefaultContext}. To control the defaults so that each thread will use the
! same values throughout the application, directly modify the
! \var{DefaultContext} object. This should be done \emph{before} any threads are
! started so that there won't be a race condition between threads calling
! \function{getcontext()}. For example:
\begin{verbatim}
# Set applicationwide defaults for all threads about to be launched
! DefaultContext = Context(prec=12, rounding=ROUND_DOWN, traps=[InvalidOperation])
setcontext(DefaultContext)
! # Afterward, the threads can be started
t1.start()
t2.start()
***************
*** 855,866 ****
\subsection{Recipes \label{decimal-recipes}}
! Here are some functions demonstrating ways to work with the
! \class{Decimal} class:
\begin{verbatim}
! from decimal import Decimal, getcontext
! getcontext().prec = 28
!
! def moneyfmt(value, places=2, curr='$', sep=',', dp='.', pos='', neg='-'):
"""Convert Decimal to a money formatted string.
--- 857,866 ----
\subsection{Recipes \label{decimal-recipes}}
! Here are a few recipes that serve as utility functions and that demonstrate
! ways to work with the \class{Decimal} class:
\begin{verbatim}
! def moneyfmt(value, places=2, curr='', sep=',', dp='.',
! pos='', neg='-', trailneg=''):
"""Convert Decimal to a money formatted string.
***************
*** 869,884 ****
sep: optional grouping separator (comma, period, or blank)
dp: decimal point indicator (comma or period)
! only set to blank if places is zero
! pos: optional sign for positive numbers ("+" or blank)
! neg: optional sign for negative numbers ("-" or blank)
! leave blank to separately add brackets or a trailing minus
>>> d = Decimal('-1234567.8901')
! >>> moneyfmt(d)
'-$1,234,567.89'
! >>> moneyfmt(d, places=0, curr='', sep='.', dp='')
! '-1.234.568'
! >>> '($%s)' % moneyfmt(d, curr='', neg='')
'($1,234,567.89)'
"""
q = Decimal((0, (1,), -places)) # 2 places --> '0.01'
--- 869,885 ----
sep: optional grouping separator (comma, period, or blank)
dp: decimal point indicator (comma or period)
! only specify as blank when places is zero
! pos: optional sign for positive numbers: "+", space or blank
! neg: optional sign for negative numbers: "-", "(", space or blank
! trailneg:optional trailing minus indicator: "-", ")", space or blank
>>> d = Decimal('-1234567.8901')
! >>> moneyfmt(d, curr='$')
'-$1,234,567.89'
! >>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-')
! '1.234.568-'
! >>> moneyfmt(d, curr='$', neg='(', trailneg=')')
'($1,234,567.89)'
+
"""
q = Decimal((0, (1,), -places)) # 2 places --> '0.01'
***************
*** 886,901 ****
result = []
digits = map(str, digits)
! build, next = result.append, digits.pop
for i in range(places):
build(next())
build(dp)
! try:
! while 1:
! for i in range(3):
! build(next())
! if digits:
! build(sep)
! except IndexError:
! pass
build(curr)
if sign:
--- 887,903 ----
result = []
digits = map(str, digits)
! build, next = result.append, digits.pop
! if sign:
! build(trailneg)
for i in range(places):
build(next())
build(dp)
! i = 0
! while digits:
! build(next())
! i += 1
! if i == 3:
! i = 0
! build(sep)
build(curr)
if sign:
***************
*** 911,926 ****
>>> print pi()
3.141592653589793238462643383
"""
getcontext().prec += 2 # extra digits for intermediate steps
three = Decimal(3) # substitute "three=3.0" for regular floats
! lastc, t, c, n, na, d, da = 0, three, 3, 1, 0, 0, 24
! while c != lastc:
! lastc = c
n, na = n+na, na+8
d, da = d+da, da+32
t = (t * n) / d
! c += t
getcontext().prec -= 2
! return c + 0 # Adding zero causes rounding to the new precision
def exp(x):
--- 913,929 ----
>>> print pi()
3.141592653589793238462643383
+
"""
getcontext().prec += 2 # extra digits for intermediate steps
three = Decimal(3) # substitute "three=3.0" for regular floats
! lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
! while s != lasts:
! lasts = s
n, na = n+na, na+8
d, da = d+da, da+32
t = (t * n) / d
! s += t
getcontext().prec -= 2
! return +s # unary plus applies the new precision
def exp(x):
***************
*** 935,949 ****
>>> print exp(2+0j)
(7.38905609893+0j)
"""
! getcontext().prec += 2 # extra digits for intermediate steps
! i, laste, e, fact, num = 0, 0, 1, 1, 1
! while e != laste:
! laste = e
i += 1
fact *= i
num *= x
! e += num / fact
getcontext().prec -= 2
! return e + 0
def cos(x):
--- 938,953 ----
>>> print exp(2+0j)
(7.38905609893+0j)
+
"""
! getcontext().prec += 2
! i, lasts, s, fact, num = 0, 0, 1, 1, 1
! while s != lasts:
! lasts = s
i += 1
fact *= i
num *= x
! s += num / fact
getcontext().prec -= 2
! return +s
def cos(x):
***************
*** 956,971 ****
>>> print cos(0.5+0j)
(0.87758256189+0j)
"""
! getcontext().prec += 2 # extra digits for intermediate steps
! i, laste, e, fact, num, sign = 0, 0, 1, 1, 1, 1
! while e != laste:
! laste = e
i += 2
fact *= i * (i-1)
num *= x * x
sign *= -1
! e += num / fact * sign
getcontext().prec -= 2
! return e + 0
def sin(x):
--- 960,976 ----
>>> print cos(0.5+0j)
(0.87758256189+0j)
+
"""
! getcontext().prec += 2
! i, lasts, s, fact, num, sign = 0, 0, 1, 1, 1, 1
! while s != lasts:
! lasts = s
i += 2
fact *= i * (i-1)
num *= x * x
sign *= -1
! s += num / fact * sign
getcontext().prec -= 2
! return +s
def sin(x):
***************
*** 978,993 ****
>>> print sin(0.5+0j)
(0.479425538604+0j)
"""
! getcontext().prec += 2 # extra digits for intermediate steps
! i, laste, e, fact, num, sign = 1, 0, x, 1, x, 1
! while e != laste:
! laste = e
i += 2
fact *= i * (i-1)
num *= x * x
sign *= -1
! e += num / fact * sign
getcontext().prec -= 2
! return e + 0
\end{verbatim}
--- 983,999 ----
>>> print sin(0.5+0j)
(0.479425538604+0j)
+
"""
! getcontext().prec += 2
! 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
getcontext().prec -= 2
! return +s
\end{verbatim}
More information about the Python-checkins
mailing list