Commit e34c3bd6 authored by Andrew M. Kuchling's avatar Andrew M. Kuchling

Change from Raymond: use pos/neg instead of +/- 1; minor edits

parent ab778228
...@@ -439,17 +439,18 @@ The limitations arise from the representation used for floating-point numbers. ...@@ -439,17 +439,18 @@ The limitations arise from the representation used for floating-point numbers.
FP numbers are made up of three components: FP numbers are made up of three components:
\begin{itemize} \begin{itemize}
\item The sign, which is -1 or +1. \item The sign, which is positive or negative.
\item The mantissa, which is a single-digit binary number \item The mantissa, which is a single-digit binary number
followed by a fractional part. For example, \code{1.01} in base-2 notation followed by a fractional part. For example, \code{1.01} in base-2 notation
is \code{1 + 0/2 + 1/4}, or 1.25 in decimal notation. is \code{1 + 0/2 + 1/4}, or 1.25 in decimal notation.
\item The exponent, which tells where the decimal point is located in the number represented. \item The exponent, which tells where the decimal point is located in the number represented.
\end{itemize} \end{itemize}
For example, the number 1.25 has sign +1, mantissa 1.01 (in binary), For example, the number 1.25 has positive sign, a mantissa value of
and exponent of 0 (the decimal point doesn't need to be shifted). The 1.01 (in binary), and an exponent of 0 (the decimal point doesn't need
number 5 has the same sign and mantissa, but the exponent is 2 to be shifted). The number 5 has the same sign and mantissa, but the
because the mantissa is multiplied by 4 (2 to the power of the exponent 2). exponent is 2 because the mantissa is multiplied by 4 (2 to the power
of the exponent 2).
Modern systems usually provide floating-point support that conforms to Modern systems usually provide floating-point support that conforms to
a relevant standard called IEEE 754. C's \ctype{double} type is a relevant standard called IEEE 754. C's \ctype{double} type is
...@@ -458,11 +459,11 @@ space for the mantissa. This means that numbers can only be specified ...@@ -458,11 +459,11 @@ space for the mantissa. This means that numbers can only be specified
to 52 bits of precision. If you're trying to represent numbers whose to 52 bits of precision. If you're trying to represent numbers whose
expansion repeats endlessly, the expansion is cut off after 52 bits. expansion repeats endlessly, the expansion is cut off after 52 bits.
Unfortunately, most software needs to produce output in base 10, and Unfortunately, most software needs to produce output in base 10, and
base 10 often gives rise to such repeating decimals. For example, 1.1 base 10 often gives rise to such repeating decimals in the binary
decimal is binary \code{1.0001100110011 ...}; .1 = 1/16 + 1/32 + 1/256 expansion. For example, 1.1 decimal is binary \code{1.0001100110011
plus an infinite number of additional terms. IEEE 754 has to chop off ...}; .1 = 1/16 + 1/32 + 1/256 plus an infinite number of additional
that infinitely repeated decimal after 52 digits, so the terms. IEEE 754 has to chop off that infinitely repeated decimal
representation is slightly inaccurate. after 52 digits, so the representation is slightly inaccurate.
Sometimes you can see this inaccuracy when the number is printed: Sometimes you can see this inaccuracy when the number is printed:
\begin{verbatim} \begin{verbatim}
...@@ -471,9 +472,10 @@ Sometimes you can see this inaccuracy when the number is printed: ...@@ -471,9 +472,10 @@ Sometimes you can see this inaccuracy when the number is printed:
\end{verbatim} \end{verbatim}
The inaccuracy isn't always visible when you print the number because The inaccuracy isn't always visible when you print the number because
the FP-to-decimal-string conversion is provided by the C library and the FP-to-decimal-string conversion is provided by the C library, and
most C libraries try to produce sensible output, but the inaccuracy is most C libraries try to produce sensible output. Even if it's not
still there and subsequent operations can magnify the error. displayed, however, the inaccuracy is still there and subsequent
operations can magnify the error.
For many applications this doesn't matter. If I'm plotting points and For many applications this doesn't matter. If I'm plotting points and
displaying them on my monitor, the difference between 1.1 and displaying them on my monitor, the difference between 1.1 and
...@@ -483,6 +485,8 @@ number to two or three or even eight decimal places, the error is ...@@ -483,6 +485,8 @@ number to two or three or even eight decimal places, the error is
never apparent. However, for applications where it does matter, never apparent. However, for applications where it does matter,
it's a lot of work to implement your own custom arithmetic routines. it's a lot of work to implement your own custom arithmetic routines.
Hence, the \class{Decimal} type was created.
\subsection{The \class{Decimal} type} \subsection{The \class{Decimal} type}
A new module, \module{decimal}, was added to Python's standard library. A new module, \module{decimal}, was added to Python's standard library.
...@@ -490,8 +494,10 @@ It contains two classes, \class{Decimal} and \class{Context}. ...@@ -490,8 +494,10 @@ It contains two classes, \class{Decimal} and \class{Context}.
\class{Decimal} instances represent numbers, and \class{Decimal} instances represent numbers, and
\class{Context} instances are used to wrap up various settings such as the precision and default rounding mode. \class{Context} instances are used to wrap up various settings such as the precision and default rounding mode.
\class{Decimal} instances, like regular Python integers and FP numbers, are immutable; once they've been created, you can't change the value it represents. \class{Decimal} instances, like regular Python integers and FP
\class{Decimal} instances can be created from integers or strings: numbers, are immutable; once they've been created, you can't change
the value it represents. \class{Decimal} instances can be created
from integers or strings:
\begin{verbatim} \begin{verbatim}
>>> import decimal >>> import decimal
...@@ -501,22 +507,24 @@ Decimal("1972") ...@@ -501,22 +507,24 @@ Decimal("1972")
Decimal("1.1") Decimal("1.1")
\end{verbatim} \end{verbatim}
You can also provide tuples containing the sign, mantissa represented You can also provide tuples containing the sign, the mantissa represented
as a tuple of decimal digits, and exponent: as a tuple of decimal digits, and the exponent:
\begin{verbatim} \begin{verbatim}
>>> decimal.Decimal((1, (1, 4, 7, 5), -2)) >>> decimal.Decimal((1, (1, 4, 7, 5), -2))
Decimal("-14.75") Decimal("-14.75")
\end{verbatim} \end{verbatim}
Cautionary note: the sign bit is a Boolean value, so 0 is positive and 1 is negative. Cautionary note: the sign bit is a Boolean value, so 0 is positive and
1 is negative.
Floating-point numbers posed a bit of a problem: should the FP number Converting from floating-point numbers poses a bit of a problem:
representing 1.1 turn into the decimal number for exactly 1.1, or for should the FP number representing 1.1 turn into the decimal number for
1.1 plus whatever inaccuracies are introduced? The decision was to exactly 1.1, or for 1.1 plus whatever inaccuracies are introduced?
leave such a conversion out of the API. Instead, you should convert The decision was to leave such a conversion out of the API. Instead,
the floating-point number into a string using the desired precision and you should convert the floating-point number into a string using the
pass the string to the \class{Decimal} constructor: desired precision and pass the string to the \class{Decimal}
constructor:
\begin{verbatim} \begin{verbatim}
>>> f = 1.1 >>> f = 1.1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment