mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Minor improvements, fixups and wording changes everywhere.
This commit is contained in:
parent
ad5206fc02
commit
65df07bf23
1 changed files with 169 additions and 163 deletions
|
@ -21,7 +21,7 @@ arithmetic. It offers several advantages over the \class{float()} datatype:
|
||||||
\begin{itemize}
|
\begin{itemize}
|
||||||
|
|
||||||
\item Decimal numbers can be represented exactly. In contrast, numbers like
|
\item Decimal numbers can be represented exactly. In contrast, numbers like
|
||||||
\constant{1.1} do not have an exact representations in binary floating point.
|
\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
|
End users typically wound not expect \constant{1.1} to display as
|
||||||
\constant{1.1000000000000001} as it does with binary floating point.
|
\constant{1.1000000000000001} as it does with binary floating point.
|
||||||
|
|
||||||
|
@ -70,14 +70,14 @@ trailing zeroes. Decimals also include special values such as
|
||||||
also differentiates \constant{-0} from \constant{+0}.
|
also differentiates \constant{-0} from \constant{+0}.
|
||||||
|
|
||||||
The context for arithmetic is an environment specifying precision, rounding
|
The context for arithmetic is an environment specifying precision, rounding
|
||||||
rules, limits on exponents, flags that indicate the results of operations,
|
rules, limits on exponents, flags indicating the results of operations,
|
||||||
and trap enablers which determine whether signals are to be treated as
|
and trap enablers which determine whether signals are treated as
|
||||||
exceptions. Rounding options include \constant{ROUND_CEILING},
|
exceptions. Rounding options include \constant{ROUND_CEILING},
|
||||||
\constant{ROUND_DOWN}, \constant{ROUND_FLOOR}, \constant{ROUND_HALF_DOWN},
|
\constant{ROUND_DOWN}, \constant{ROUND_FLOOR}, \constant{ROUND_HALF_DOWN},
|
||||||
\constant{ROUND_HALF_EVEN}, \constant{ROUND_HALF_UP}, and \constant{ROUND_UP}.
|
\constant{ROUND_HALF_EVEN}, \constant{ROUND_HALF_UP}, and \constant{ROUND_UP}.
|
||||||
|
|
||||||
Signals are types of information that arise during the course of a
|
Signals are groups of exceptional conditions arising during the course of
|
||||||
computation. Depending on the needs of the application, some signals may be
|
computation. Depending on the needs of the application, signals may be
|
||||||
ignored, considered as informational, or treated as exceptions. The signals in
|
ignored, considered as informational, or treated as exceptions. The signals in
|
||||||
the decimal module are: \constant{Clamped}, \constant{InvalidOperation},
|
the decimal module are: \constant{Clamped}, \constant{InvalidOperation},
|
||||||
\constant{DivisionByZero}, \constant{Inexact}, \constant{Rounded},
|
\constant{DivisionByZero}, \constant{Inexact}, \constant{Rounded},
|
||||||
|
@ -104,26 +104,27 @@ needs to reset them before monitoring a calculation.
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
\subsection{Quick-start Tutorial \label{decimal-tutorial}}
|
\subsection{Quick-start Tutorial \label{decimal-tutorial}}
|
||||||
|
|
||||||
The normal start to using decimals is to import the module, and then use
|
The usual start to using decimals is importing the module, viewing the current
|
||||||
\function{getcontext()} to view the context and, if necessary, set the context
|
context with \function{getcontext()} and, if necessary, setting new values
|
||||||
precision, rounding, or trap enablers:
|
for precision, rounding, or enabled traps:
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> from decimal import *
|
>>> from decimal import *
|
||||||
>>> getcontext()
|
>>> getcontext()
|
||||||
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,
|
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,
|
||||||
capitals=1, flags=[], traps=[])
|
capitals=1, flags=[], traps=[Overflow, InvalidOperation,
|
||||||
|
DivisionByZero])
|
||||||
|
|
||||||
>>> getcontext().prec = 7
|
>>> getcontext().prec = 7 # Set a new precision
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
|
|
||||||
Decimal instances can be constructed from integers, strings or tuples. To
|
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
|
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
|
serves as an explicit reminder of the details of the conversion (including
|
||||||
representation error). Malformed strings signal \constant{InvalidOperation}
|
representation error). Decimal numbers include special values such as
|
||||||
and return a special kind of Decimal called a \constant{NaN} which stands for
|
\constant{NaN} which stands for ``Not a number'', positive and negative
|
||||||
``Not a number''. Positive and negative \constant{Infinity} is yet another
|
\constant{Infinity}, and \constant{-0}.
|
||||||
special kind of Decimal.
|
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> Decimal(10)
|
>>> Decimal(10)
|
||||||
|
@ -140,14 +141,13 @@ Decimal("NaN")
|
||||||
Decimal("-Infinity")
|
Decimal("-Infinity")
|
||||||
\end{verbatim}
|
\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 significance of a new Decimal is determined solely by the number
|
||||||
the arithmetic operations that are governed by context.
|
of digits input. Context precision and rounding only come into play during
|
||||||
|
arithmetic operations.
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> getcontext().prec = 6
|
>>> getcontext().prec = 6
|
||||||
>>> Decimal('3.0000')
|
|
||||||
Decimal("3.0000")
|
|
||||||
>>> Decimal('3.0')
|
>>> Decimal('3.0')
|
||||||
Decimal("3.0")
|
Decimal("3.0")
|
||||||
>>> Decimal('3.1415926535')
|
>>> Decimal('3.1415926535')
|
||||||
|
@ -159,6 +159,7 @@ Decimal("5.85987")
|
||||||
Decimal("5.85988")
|
Decimal("5.85988")
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
|
|
||||||
Decimals interact well with much of the rest of python. Here is a small
|
Decimals interact well with much of the rest of python. Here is a small
|
||||||
decimal floating point flying circus:
|
decimal floating point flying circus:
|
||||||
|
|
||||||
|
@ -190,10 +191,24 @@ Decimal("2.5058")
|
||||||
Decimal("0.77")
|
Decimal("0.77")
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
The \function{getcontext()} function accesses the current context. This one
|
The \method{quantize()} method rounds a number to a fixed exponent. This
|
||||||
context is sufficient for many applications; however, for more advanced work,
|
method is useful for monetary applications that often round results to a fixed
|
||||||
multiple contexts can be created using the Context() constructor. To make a
|
number of places:
|
||||||
new context active, use the \function{setcontext()} function.
|
|
||||||
|
\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
|
In accordance with the standard, the \module{Decimal} module provides two
|
||||||
ready to use standard contexts, \constant{BasicContext} and
|
ready to use standard contexts, \constant{BasicContext} and
|
||||||
|
@ -205,17 +220,19 @@ because many of the traps are enabled:
|
||||||
>>> myothercontext
|
>>> myothercontext
|
||||||
Context(prec=60, rounding=ROUND_HALF_DOWN, Emin=-999999999, Emax=999999999,
|
Context(prec=60, rounding=ROUND_HALF_DOWN, Emin=-999999999, Emax=999999999,
|
||||||
capitals=1, flags=[], traps=[])
|
capitals=1, flags=[], traps=[])
|
||||||
>>> ExtendedContext
|
|
||||||
Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,
|
|
||||||
capitals=1, flags=[], traps=[])
|
|
||||||
>>> setcontext(myothercontext)
|
>>> setcontext(myothercontext)
|
||||||
>>> Decimal(1) / Decimal(7)
|
>>> Decimal(1) / Decimal(7)
|
||||||
Decimal("0.142857142857142857142857142857142857142857142857142857142857")
|
Decimal("0.142857142857142857142857142857142857142857142857142857142857")
|
||||||
|
|
||||||
|
>>> ExtendedContext
|
||||||
|
Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,
|
||||||
|
capitals=1, flags=[], traps=[])
|
||||||
>>> setcontext(ExtendedContext)
|
>>> setcontext(ExtendedContext)
|
||||||
>>> Decimal(1) / Decimal(7)
|
>>> Decimal(1) / Decimal(7)
|
||||||
Decimal("0.142857143")
|
Decimal("0.142857143")
|
||||||
>>> Decimal(42) / Decimal(0)
|
>>> Decimal(42) / Decimal(0)
|
||||||
Decimal("Infinity")
|
Decimal("Infinity")
|
||||||
|
|
||||||
>>> setcontext(BasicContext)
|
>>> setcontext(BasicContext)
|
||||||
>>> Decimal(42) / Decimal(0)
|
>>> Decimal(42) / Decimal(0)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
|
@ -224,14 +241,15 @@ Traceback (most recent call last):
|
||||||
DivisionByZero: x / 0
|
DivisionByZero: x / 0
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
Besides using contexts to control precision, rounding, and trapping signals,
|
|
||||||
they can be used to monitor flags which give information collected during
|
Contexts also have signal flags for monitoring exceptional conditions
|
||||||
computation. The flags remain set until explicitly cleared, so it is best to
|
encountered during computations. The flags remain set until explicitly
|
||||||
clear the flags before each set of monitored computations by using the
|
cleared, so it is best to clear the flags before each set of monitored
|
||||||
\method{clear_flags()} method.
|
computations by using the \method{clear_flags()} method.
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> setcontext(ExtendedContext)
|
>>> setcontext(ExtendedContext)
|
||||||
|
>>> getcontext().clear_flags()
|
||||||
>>> Decimal(355) / Decimal(113)
|
>>> Decimal(355) / Decimal(113)
|
||||||
Decimal("3.14159292")
|
Decimal("3.14159292")
|
||||||
>>> getcontext()
|
>>> getcontext()
|
||||||
|
@ -239,10 +257,9 @@ Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,
|
||||||
capitals=1, flags=[Inexact, Rounded], traps=[])
|
capitals=1, flags=[Inexact, Rounded], traps=[])
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
The \var{flags} entry shows that the rational approximation to
|
The \var{flags} entry shows that the rational approximation to \constant{Pi}
|
||||||
\constant{Pi} was rounded (digits beyond the context precision were thrown
|
was rounded (digits beyond the context precision were thrown away) and that
|
||||||
away) and that the result is inexact (some of the discarded digits were
|
the result is inexact (some of the discarded digits were non-zero).
|
||||||
non-zero).
|
|
||||||
|
|
||||||
Individual traps are set using the dictionary in the \member{traps}
|
Individual traps are set using the dictionary in the \member{traps}
|
||||||
field of a context:
|
field of a context:
|
||||||
|
@ -259,26 +276,11 @@ Traceback (most recent call last):
|
||||||
DivisionByZero: x / 0
|
DivisionByZero: x / 0
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
To turn all the traps on or off all at once, use a loop. Also, the
|
Most programs adjust the current context only once, at the beginning of the
|
||||||
\method{dict.update()} method is useful for changing a handfull of values.
|
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
|
||||||
\begin{verbatim}
|
of the program manipulates the data no differently than with other Python
|
||||||
>>> getcontext.clear_flags()
|
numeric types.
|
||||||
>>> 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.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -308,20 +310,18 @@ as other Python numeric types.
|
||||||
|
|
||||||
If \var{value} is a \class{tuple}, it should have three components,
|
If \var{value} is a \class{tuple}, it should have three components,
|
||||||
a sign (\constant{0} for positive or \constant{1} for negative),
|
a sign (\constant{0} for positive or \constant{1} for negative),
|
||||||
a \class{tuple} of digits, and an exponent represented as an integer.
|
a \class{tuple} of digits, and an integer exponent. For example,
|
||||||
For example, \samp{Decimal((0, (1, 4, 1, 4), -3))} returns
|
\samp{Decimal((0, (1, 4, 1, 4), -3))} returns \code{Decimal("1.414")}.
|
||||||
\code{Decimal("1.414")}.
|
|
||||||
|
|
||||||
The supplied \var{context} or, if not specified, the current context
|
The \var{context} precision does not affect how many digits are stored.
|
||||||
governs only the handling of malformed strings not conforming to the
|
That is determined exclusively by the number of digits in \var{value}. For
|
||||||
numeric string syntax. If the context traps \constant{InvalidOperation},
|
example, \samp{Decimal("3.00000")} records all five zeroes even if the
|
||||||
an exception is raised; otherwise, the constructor returns a new Decimal
|
context precision is only three.
|
||||||
with the value of \constant{NaN}.
|
|
||||||
|
|
||||||
The context serves no other purpose. The number of significant digits
|
The purpose of the \var{context} argument is determining what to do if
|
||||||
recorded is determined solely by the \var{value} and the \var{context}
|
\var{value} is a malformed string. If the context traps
|
||||||
precision is not a factor. For example, \samp{Decimal("3.0000")} records
|
\constant{InvalidOperation}, an exception is raised; otherwise, the
|
||||||
all four zeroes even if the context precision is only three.
|
constructor returns a new Decimal with the value of \constant{NaN}.
|
||||||
|
|
||||||
Once constructed, \class{Decimal} objects are immutable.
|
Once constructed, \class{Decimal} objects are immutable.
|
||||||
\end{classdesc}
|
\end{classdesc}
|
||||||
|
@ -334,13 +334,13 @@ compared, sorted, and coerced to another type (such as \class{float}
|
||||||
or \class{long}).
|
or \class{long}).
|
||||||
|
|
||||||
In addition to the standard numeric properties, decimal floating point objects
|
In addition to the standard numeric properties, decimal floating point objects
|
||||||
have a number of more specialized methods:
|
also have a number of specialized methods:
|
||||||
|
|
||||||
\begin{methoddesc}{adjusted}{}
|
\begin{methoddesc}{adjusted}{}
|
||||||
Return the adjusted exponent after shifting out the coefficient's rightmost
|
Return the adjusted exponent after shifting out the coefficient's rightmost
|
||||||
digits until only the lead digit remains: \code{Decimal("321e+5").adjusted()}
|
digits until only the lead digit remains: \code{Decimal("321e+5").adjusted()}
|
||||||
returns seven. Used for determining the place value of the most significant
|
returns seven. Used for determining the position of the most significant
|
||||||
digit.
|
digit with respect to the decimal point.
|
||||||
\end{methoddesc}
|
\end{methoddesc}
|
||||||
|
|
||||||
\begin{methoddesc}{as_tuple}{}
|
\begin{methoddesc}{as_tuple}{}
|
||||||
|
@ -389,7 +389,7 @@ have a number of more specialized methods:
|
||||||
\end{methoddesc}
|
\end{methoddesc}
|
||||||
|
|
||||||
\begin{methoddesc}{remainder_near}{other\optional{, context}}
|
\begin{methoddesc}{remainder_near}{other\optional{, context}}
|
||||||
Computed the modulo as either a positive or negative value depending
|
Computes the modulo as either a positive or negative value depending
|
||||||
on which is closest to zero. For instance,
|
on which is closest to zero. For instance,
|
||||||
\samp{Decimal(10).remainder_near(6)} returns \code{Decimal("-2")}
|
\samp{Decimal(10).remainder_near(6)} returns \code{Decimal("-2")}
|
||||||
which is closer to zero than \code{Decimal("4")}.
|
which is closer to zero than \code{Decimal("4")}.
|
||||||
|
@ -422,13 +422,14 @@ have a number of more specialized methods:
|
||||||
current context.
|
current context.
|
||||||
\end{methoddesc}
|
\end{methoddesc}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
\subsection{Context objects \label{decimal-decimal}}
|
\subsection{Context objects \label{decimal-decimal}}
|
||||||
|
|
||||||
Contexts are environments for arithmetic operations. They govern the precision,
|
Contexts are environments for arithmetic operations. They govern precision,
|
||||||
rules for rounding, determine which signals are treated as exceptions, and set limits
|
set rules for rounding, determine which signals are treated as exceptions, and
|
||||||
on the range for exponents.
|
limit the range for exponents.
|
||||||
|
|
||||||
Each thread has its own current context which is accessed or changed using
|
Each thread has its own current context which is accessed or changed using
|
||||||
the \function{getcontext()} and \function{setcontext()} functions:
|
the \function{getcontext()} and \function{setcontext()} functions:
|
||||||
|
@ -464,11 +465,11 @@ In addition, the module provides three pre-made contexts:
|
||||||
Because the trapped are disabled, this context is useful for applications
|
Because the trapped are disabled, this context is useful for applications
|
||||||
that prefer to have result value of \constant{NaN} or \constant{Infinity}
|
that prefer to have result value of \constant{NaN} or \constant{Infinity}
|
||||||
instead of raising exceptions. This allows an application to complete a
|
instead of raising exceptions. This allows an application to complete a
|
||||||
run in the presense of conditions that would otherwise halt the program.
|
run in the presence of conditions that would otherwise halt the program.
|
||||||
\end{classdesc*}
|
\end{classdesc*}
|
||||||
|
|
||||||
\begin{classdesc*}{DefaultContext}
|
\begin{classdesc*}{DefaultContext}
|
||||||
This class is used by the \class{Context} constructor as a prototype for
|
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
|
new contexts. Changing a field (such a precision) has the effect of
|
||||||
changing the default for new contexts creating by the \class{Context}
|
changing the default for new contexts creating by the \class{Context}
|
||||||
constructor.
|
constructor.
|
||||||
|
@ -479,10 +480,10 @@ In addition, the module provides three pre-made contexts:
|
||||||
as it would require thread synchronization to prevent race conditions.
|
as it would require thread synchronization to prevent race conditions.
|
||||||
|
|
||||||
In single threaded environments, it is preferable to not use this context
|
In single threaded environments, it is preferable to not use this context
|
||||||
at all. Instead, simply create contexts explicitly. This is especially
|
at all. Instead, simply create contexts explicitly as described below.
|
||||||
important because the default values context may change between releases
|
|
||||||
(with initial release having precision=28, rounding=ROUND_HALF_EVEN,
|
The default values are precision=28, rounding=ROUND_HALF_EVEN, and enabled
|
||||||
cleared flags, and no traps enabled).
|
traps for Overflow, InvalidOperation, and DivisionByZero.
|
||||||
\end{classdesc*}
|
\end{classdesc*}
|
||||||
|
|
||||||
|
|
||||||
|
@ -508,19 +509,20 @@ with the \class{Context} constructor.
|
||||||
\constant{ROUND_HALF_UP} (away from zero), or
|
\constant{ROUND_HALF_UP} (away from zero), or
|
||||||
\constant{ROUND_UP} (away from zero).
|
\constant{ROUND_UP} (away from zero).
|
||||||
|
|
||||||
The \var{traps} and \var{flags} fields are mappings from signals
|
The \var{traps} and \var{flags} fields list any signals to be set.
|
||||||
to either \constant{0} or \constant{1}.
|
Generally, new contexts should only set traps and leave the flags clear.
|
||||||
|
|
||||||
The \var{Emin} and \var{Emax} fields are integers specifying the outer
|
The \var{Emin} and \var{Emax} fields are integers specifying the outer
|
||||||
limits allowable for exponents.
|
limits allowable for exponents.
|
||||||
|
|
||||||
The \var{capitals} field is either \constant{0} or \constant{1} (the
|
The \var{capitals} field is either \constant{0} or \constant{1} (the
|
||||||
default). If set to \constant{1}, exponents are printed with a capital
|
default). If set to \constant{1}, exponents are printed with a capital
|
||||||
\constant{E}; otherwise, lowercase is used: \constant{Decimal('6.02e+23')}.
|
\constant{E}; otherwise, a lowercase \constant{e} is used:
|
||||||
|
\constant{Decimal('6.02e+23')}.
|
||||||
\end{classdesc}
|
\end{classdesc}
|
||||||
|
|
||||||
The \class{Context} class defines several general methods as well as a
|
The \class{Context} class defines several general purpose methods as well as a
|
||||||
large number of methods for doing arithmetic directly from the context.
|
large number of methods for doing arithmetic directly in a given context.
|
||||||
|
|
||||||
\begin{methoddesc}{clear_flags}{}
|
\begin{methoddesc}{clear_flags}{}
|
||||||
Sets all of the flags to \constant{0}.
|
Sets all of the flags to \constant{0}.
|
||||||
|
@ -531,18 +533,18 @@ large number of methods for doing arithmetic directly from the context.
|
||||||
\end{methoddesc}
|
\end{methoddesc}
|
||||||
|
|
||||||
\begin{methoddesc}{create_decimal}{num}
|
\begin{methoddesc}{create_decimal}{num}
|
||||||
Creates a new Decimal instance but using \var{self} as context.
|
Creates a new Decimal instance from \var{num} but using \var{self} as
|
||||||
Unlike the \class{Decimal} constructor, context precision,
|
context. Unlike the \class{Decimal} constructor, the context precision,
|
||||||
rounding method, flags, and traps are applied to the conversion.
|
rounding method, flags, and traps are applied to the conversion.
|
||||||
|
|
||||||
This is useful because constants are often given to a greater
|
This is useful because constants are often given to a greater precision than
|
||||||
precision than is needed by the application.
|
is needed by the application.
|
||||||
\end{methoddesc}
|
\end{methoddesc}
|
||||||
|
|
||||||
\begin{methoddesc}{Etiny}{}
|
\begin{methoddesc}{Etiny}{}
|
||||||
Returns a value equal to \samp{Emin - prec + 1} which is the minimum
|
Returns a value equal to \samp{Emin - prec + 1} which is the minimum
|
||||||
exponent value for subnormal results. When underflow occurs, the
|
exponent value for subnormal results. When underflow occurs, the
|
||||||
exponont is set to \constant{Etiny}.
|
exponent is set to \constant{Etiny}.
|
||||||
\end{methoddesc}
|
\end{methoddesc}
|
||||||
|
|
||||||
\begin{methoddesc}{Etop}{}
|
\begin{methoddesc}{Etop}{}
|
||||||
|
@ -553,7 +555,7 @@ large number of methods for doing arithmetic directly from the context.
|
||||||
The usual approach to working with decimals is to create \class{Decimal}
|
The usual approach to working with decimals is to create \class{Decimal}
|
||||||
instances and then apply arithmetic operations which take place within the
|
instances and then apply arithmetic operations which take place within the
|
||||||
current context for the active thread. An alternate approach is to use
|
current context for the active thread. An alternate approach is to use
|
||||||
context methods for calculating within s specific context. The methods are
|
context methods for calculating within a specific context. The methods are
|
||||||
similar to those for the \class{Decimal} class and are only briefly recounted
|
similar to those for the \class{Decimal} class and are only briefly recounted
|
||||||
here.
|
here.
|
||||||
|
|
||||||
|
@ -586,14 +588,14 @@ here.
|
||||||
\end{methoddesc}
|
\end{methoddesc}
|
||||||
|
|
||||||
\begin{methoddesc}{max}{x, y}
|
\begin{methoddesc}{max}{x, y}
|
||||||
Compare two values numerically and returns the maximum.
|
Compare two values numerically and return the maximum.
|
||||||
|
|
||||||
If they are numerically equal then the left-hand operand is chosen as the
|
If they are numerically equal then the left-hand operand is chosen as the
|
||||||
result.
|
result.
|
||||||
\end{methoddesc}
|
\end{methoddesc}
|
||||||
|
|
||||||
\begin{methoddesc}{min}{x, y}
|
\begin{methoddesc}{min}{x, y}
|
||||||
Compare two values numerically and returns the minimum.
|
Compare two values numerically and return the minimum.
|
||||||
|
|
||||||
If they are numerically equal then the left-hand operand is chosen as the
|
If they are numerically equal then the left-hand operand is chosen as the
|
||||||
result.
|
result.
|
||||||
|
@ -636,14 +638,14 @@ here.
|
||||||
\end{methoddesc}
|
\end{methoddesc}
|
||||||
|
|
||||||
\begin{methoddesc}{quantize}{x, y}
|
\begin{methoddesc}{quantize}{x, y}
|
||||||
Returns a value equal to \var{x} after rounding and having the
|
Returns a value equal to \var{x} after rounding and having the exponent of
|
||||||
exponent of v\var{y}.
|
\var{y}.
|
||||||
|
|
||||||
Unlike other operations, if the length of the coefficient after the quantize
|
Unlike other operations, if the length of the coefficient after the quantize
|
||||||
operation would be greater than precision then an
|
operation would be greater than precision, then an
|
||||||
\constant{InvalidOperation} is signaled. This guarantees that, unless there
|
\constant{InvalidOperation} is signaled. This guarantees that, unless there
|
||||||
is an error condition, the exponent of the result of a quantize is always
|
is an error condition, the quantized exponent is always equal to that of the
|
||||||
equal to that of the right-hand operand.
|
right-hand operand.
|
||||||
|
|
||||||
Also unlike other operations, quantize never signals Underflow, even
|
Also unlike other operations, quantize never signals Underflow, even
|
||||||
if the result is subnormal and inexact.
|
if the result is subnormal and inexact.
|
||||||
|
@ -712,7 +714,7 @@ the next computation.
|
||||||
|
|
||||||
If the context's trap enabler is set for the signal, then the condition
|
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
|
causes a Python exception to be raised. For example, if the
|
||||||
\class{DivisionByZero} trap is set, the a \exception{DivisionByZero}
|
\class{DivisionByZero} trap is set, then a \exception{DivisionByZero}
|
||||||
exception is raised upon encountering the condition.
|
exception is raised upon encountering the condition.
|
||||||
|
|
||||||
|
|
||||||
|
@ -725,24 +727,25 @@ exception is raised upon encountering the condition.
|
||||||
\end{classdesc*}
|
\end{classdesc*}
|
||||||
|
|
||||||
\begin{classdesc*}{DecimalException}
|
\begin{classdesc*}{DecimalException}
|
||||||
Base class for other signals.
|
Base class for other signals and is a subclass of
|
||||||
|
\exception{ArithmeticError}.
|
||||||
\end{classdesc*}
|
\end{classdesc*}
|
||||||
|
|
||||||
\begin{classdesc*}{DivisionByZero}
|
\begin{classdesc*}{DivisionByZero}
|
||||||
Signals the division of a non-infinite number by zero.
|
Signals the division of a non-infinite number by zero.
|
||||||
|
|
||||||
Can occur with division, modulo division, or when raising a number to
|
Can occur with division, modulo division, or when raising a number to a
|
||||||
a negative power. If this signal is not trapped, return
|
negative power. If this signal is not trapped, returns
|
||||||
\constant{Infinity} or \constant{-Infinity} with sign determined by
|
\constant{Infinity} or \constant{-Infinity} with the sign determined by
|
||||||
the inputs to the calculation.
|
the inputs to the calculation.
|
||||||
\end{classdesc*}
|
\end{classdesc*}
|
||||||
|
|
||||||
\begin{classdesc*}{Inexact}
|
\begin{classdesc*}{Inexact}
|
||||||
Indicates that rounding occurred and the result is not exact.
|
Indicates that rounding occurred and the result is not exact.
|
||||||
|
|
||||||
Signals whenever non-zero digits were discarded during rounding.
|
Signals when non-zero digits were discarded during rounding. The rounded
|
||||||
The rounded result is returned. The signal flag or trap is used
|
result is returned. The signal flag or trap is used to detect when
|
||||||
to detect when results are inexact.
|
results are inexact.
|
||||||
\end{classdesc*}
|
\end{classdesc*}
|
||||||
|
|
||||||
\begin{classdesc*}{InvalidOperation}
|
\begin{classdesc*}{InvalidOperation}
|
||||||
|
@ -820,7 +823,7 @@ The following table summarizes the hierarchy of signals:
|
||||||
The \function{getcontext()} function accesses a different \class{Context}
|
The \function{getcontext()} function accesses a different \class{Context}
|
||||||
object for each thread. Having separate thread contexts means that threads
|
object for each thread. Having separate thread contexts means that threads
|
||||||
may make changes (such as \code{getcontext.prec=10}) without interfering with
|
may make changes (such as \code{getcontext.prec=10}) without interfering with
|
||||||
other threads and without needing mutexes.
|
other threads.
|
||||||
|
|
||||||
Likewise, the \function{setcontext()} function automatically assigns its target
|
Likewise, the \function{setcontext()} function automatically assigns its target
|
||||||
to the current thread.
|
to the current thread.
|
||||||
|
@ -829,20 +832,19 @@ If \function{setcontext()} has not been called before \function{getcontext()},
|
||||||
then \function{getcontext()} will automatically create a new context for use
|
then \function{getcontext()} will automatically create a new context for use
|
||||||
in the current thread.
|
in the current thread.
|
||||||
|
|
||||||
The new context is copied from a prototype context called \var{DefaultContext}.
|
The new context is copied from a prototype context called
|
||||||
To control the defaults so that each thread will use the same values
|
\var{DefaultContext}. To control the defaults so that each thread will use the
|
||||||
throughout the application, directly modify the \var{DefaultContext} object.
|
same values throughout the application, directly modify the
|
||||||
This should be done \emph{before} any threads are started so that there won't
|
\var{DefaultContext} object. This should be done \emph{before} any threads are
|
||||||
be a race condition with threads calling \function{getcontext()}. For example:
|
started so that there won't be a race condition between threads calling
|
||||||
|
\function{getcontext()}. For example:
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
# Set applicationwide defaults for all threads about to be launched
|
# Set applicationwide defaults for all threads about to be launched
|
||||||
DefaultContext.prec=12
|
DefaultContext = Context(prec=12, rounding=ROUND_DOWN, traps=[InvalidOperation])
|
||||||
DefaultContext.rounding=ROUND_DOWN
|
|
||||||
DefaultContext.traps=dict.fromkeys(Signals, 0)
|
|
||||||
setcontext(DefaultContext)
|
setcontext(DefaultContext)
|
||||||
|
|
||||||
# Now start all of the threads
|
# Afterward, the threads can be started
|
||||||
t1.start()
|
t1.start()
|
||||||
t2.start()
|
t2.start()
|
||||||
t3.start()
|
t3.start()
|
||||||
|
@ -854,49 +856,49 @@ t3.start()
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
\subsection{Recipes \label{decimal-recipes}}
|
\subsection{Recipes \label{decimal-recipes}}
|
||||||
|
|
||||||
Here are some functions demonstrating ways to work with the
|
Here are a few recipes that serve as utility functions and that demonstrate
|
||||||
\class{Decimal} class:
|
ways to work with the \class{Decimal} class:
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
from decimal import Decimal, getcontext
|
def moneyfmt(value, places=2, curr='', sep=',', dp='.',
|
||||||
getcontext().prec = 28
|
pos='', neg='-', trailneg=''):
|
||||||
|
|
||||||
def moneyfmt(value, places=2, curr='$', sep=',', dp='.', pos='', neg='-'):
|
|
||||||
"""Convert Decimal to a money formatted string.
|
"""Convert Decimal to a money formatted string.
|
||||||
|
|
||||||
places: required number of places after the decimal point
|
places: required number of places after the decimal point
|
||||||
curr: optional currency symbol before the sign (may be blank)
|
curr: optional currency symbol before the sign (may be blank)
|
||||||
sep: optional grouping separator (comma, period, or blank)
|
sep: optional grouping separator (comma, period, or blank)
|
||||||
dp: decimal point indicator (comma or period)
|
dp: decimal point indicator (comma or period)
|
||||||
only set to blank if places is zero
|
only specify as blank when places is zero
|
||||||
pos: optional sign for positive numbers ("+" or blank)
|
pos: optional sign for positive numbers: "+", space or blank
|
||||||
neg: optional sign for negative numbers ("-" or blank)
|
neg: optional sign for negative numbers: "-", "(", space or blank
|
||||||
leave blank to separately add brackets or a trailing minus
|
trailneg:optional trailing minus indicator: "-", ")", space or blank
|
||||||
|
|
||||||
>>> d = Decimal('-1234567.8901')
|
>>> d = Decimal('-1234567.8901')
|
||||||
>>> moneyfmt(d)
|
>>> moneyfmt(d, curr='$')
|
||||||
'-$1,234,567.89'
|
'-$1,234,567.89'
|
||||||
>>> moneyfmt(d, places=0, curr='', sep='.', dp='')
|
>>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-')
|
||||||
'-1.234.568'
|
'1.234.568-'
|
||||||
>>> '($%s)' % moneyfmt(d, curr='', neg='')
|
>>> moneyfmt(d, curr='$', neg='(', trailneg=')')
|
||||||
'($1,234,567.89)'
|
'($1,234,567.89)'
|
||||||
|
|
||||||
"""
|
"""
|
||||||
q = Decimal((0, (1,), -places)) # 2 places --> '0.01'
|
q = Decimal((0, (1,), -places)) # 2 places --> '0.01'
|
||||||
sign, digits, exp = value.quantize(q).as_tuple()
|
sign, digits, exp = value.quantize(q).as_tuple()
|
||||||
result = []
|
result = []
|
||||||
digits = map(str, digits)
|
digits = map(str, digits)
|
||||||
build, next = result.append, digits.pop
|
build, next = result.append, digits.pop
|
||||||
|
if sign:
|
||||||
|
build(trailneg)
|
||||||
for i in range(places):
|
for i in range(places):
|
||||||
build(next())
|
build(next())
|
||||||
build(dp)
|
build(dp)
|
||||||
try:
|
i = 0
|
||||||
while 1:
|
while digits:
|
||||||
for i in range(3):
|
build(next())
|
||||||
build(next())
|
i += 1
|
||||||
if digits:
|
if i == 3:
|
||||||
build(sep)
|
i = 0
|
||||||
except IndexError:
|
build(sep)
|
||||||
pass
|
|
||||||
build(curr)
|
build(curr)
|
||||||
if sign:
|
if sign:
|
||||||
build(neg)
|
build(neg)
|
||||||
|
@ -910,18 +912,19 @@ def pi():
|
||||||
|
|
||||||
>>> print pi()
|
>>> print pi()
|
||||||
3.141592653589793238462643383
|
3.141592653589793238462643383
|
||||||
|
|
||||||
"""
|
"""
|
||||||
getcontext().prec += 2 # extra digits for intermediate steps
|
getcontext().prec += 2 # extra digits for intermediate steps
|
||||||
three = Decimal(3) # substitute "three=3.0" for regular floats
|
three = Decimal(3) # substitute "three=3.0" for regular floats
|
||||||
lastc, t, c, n, na, d, da = 0, three, 3, 1, 0, 0, 24
|
lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
|
||||||
while c != lastc:
|
while s != lasts:
|
||||||
lastc = c
|
lasts = s
|
||||||
n, na = n+na, na+8
|
n, na = n+na, na+8
|
||||||
d, da = d+da, da+32
|
d, da = d+da, da+32
|
||||||
t = (t * n) / d
|
t = (t * n) / d
|
||||||
c += t
|
s += t
|
||||||
getcontext().prec -= 2
|
getcontext().prec -= 2
|
||||||
return c + 0 # Adding zero causes rounding to the new precision
|
return +s # unary plus applies the new precision
|
||||||
|
|
||||||
def exp(x):
|
def exp(x):
|
||||||
"""Return e raised to the power of x. Result type matches input type.
|
"""Return e raised to the power of x. Result type matches input type.
|
||||||
|
@ -934,17 +937,18 @@ def exp(x):
|
||||||
7.38905609893
|
7.38905609893
|
||||||
>>> print exp(2+0j)
|
>>> print exp(2+0j)
|
||||||
(7.38905609893+0j)
|
(7.38905609893+0j)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
getcontext().prec += 2 # extra digits for intermediate steps
|
getcontext().prec += 2
|
||||||
i, laste, e, fact, num = 0, 0, 1, 1, 1
|
i, lasts, s, fact, num = 0, 0, 1, 1, 1
|
||||||
while e != laste:
|
while s != lasts:
|
||||||
laste = e
|
lasts = s
|
||||||
i += 1
|
i += 1
|
||||||
fact *= i
|
fact *= i
|
||||||
num *= x
|
num *= x
|
||||||
e += num / fact
|
s += num / fact
|
||||||
getcontext().prec -= 2
|
getcontext().prec -= 2
|
||||||
return e + 0
|
return +s
|
||||||
|
|
||||||
def cos(x):
|
def cos(x):
|
||||||
"""Return the cosine of x as measured in radians.
|
"""Return the cosine of x as measured in radians.
|
||||||
|
@ -955,18 +959,19 @@ def cos(x):
|
||||||
0.87758256189
|
0.87758256189
|
||||||
>>> print cos(0.5+0j)
|
>>> print cos(0.5+0j)
|
||||||
(0.87758256189+0j)
|
(0.87758256189+0j)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
getcontext().prec += 2 # extra digits for intermediate steps
|
getcontext().prec += 2
|
||||||
i, laste, e, fact, num, sign = 0, 0, 1, 1, 1, 1
|
i, lasts, s, fact, num, sign = 0, 0, 1, 1, 1, 1
|
||||||
while e != laste:
|
while s != lasts:
|
||||||
laste = e
|
lasts = s
|
||||||
i += 2
|
i += 2
|
||||||
fact *= i * (i-1)
|
fact *= i * (i-1)
|
||||||
num *= x * x
|
num *= x * x
|
||||||
sign *= -1
|
sign *= -1
|
||||||
e += num / fact * sign
|
s += num / fact * sign
|
||||||
getcontext().prec -= 2
|
getcontext().prec -= 2
|
||||||
return e + 0
|
return +s
|
||||||
|
|
||||||
def sin(x):
|
def sin(x):
|
||||||
"""Return the cosine of x as measured in radians.
|
"""Return the cosine of x as measured in radians.
|
||||||
|
@ -977,17 +982,18 @@ def sin(x):
|
||||||
0.479425538604
|
0.479425538604
|
||||||
>>> print sin(0.5+0j)
|
>>> print sin(0.5+0j)
|
||||||
(0.479425538604+0j)
|
(0.479425538604+0j)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
getcontext().prec += 2 # extra digits for intermediate steps
|
getcontext().prec += 2
|
||||||
i, laste, e, fact, num, sign = 1, 0, x, 1, x, 1
|
i, lasts, s, fact, num, sign = 1, 0, x, 1, x, 1
|
||||||
while e != laste:
|
while s != lasts:
|
||||||
laste = e
|
lasts = s
|
||||||
i += 2
|
i += 2
|
||||||
fact *= i * (i-1)
|
fact *= i * (i-1)
|
||||||
num *= x * x
|
num *= x * x
|
||||||
sign *= -1
|
sign *= -1
|
||||||
e += num / fact * sign
|
s += num / fact * sign
|
||||||
getcontext().prec -= 2
|
getcontext().prec -= 2
|
||||||
return e + 0
|
return +s
|
||||||
|
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue