@@ -828,6 +828,102 @@ \subsection{Signals \label{decimal-signals}}
828828\end {verbatim }
829829
830830
831+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
832+ \subsection {Floating Point Notes \label {decimal-notes } }
833+
834+ The use of decimal floating point eliminates decimal representation error
835+ (making it possible to represent \constant {0.1} exactly); however, some
836+ operations can still incur round-off error when non-zero digits exceed the
837+ fixed precision.
838+
839+ The effects of round-off error can be amplified by the addition or subtraction
840+ of nearly offsetting quantities resulting in loss of significance. Knuth
841+ provides two instructive examples where rounded floating point arithmetic with
842+ insufficient precision causes the break down of the associative and
843+ distributive properties of addition:
844+
845+ \begin {verbatim }
846+ # Examples from Seminumerical Algorithms, Section 4.2.2.
847+ >>> from decimal import *
848+ >>> getcontext().prec = 8
849+
850+ >>> u, v, w = Decimal(11111113), Decimal(-11111111), Decimal('7.51111111')
851+ >>> (u + v) + w
852+ Decimal("9.5111111")
853+ >>> u + (v + w)
854+ Decimal("10")
855+
856+ >>> u, v, w = Decimal(20000), Decimal(-6), Decimal('6.0000003')
857+ >>> (u*v) + (u*w)
858+ Decimal("0.01")
859+ >>> u * (v+w)
860+ Decimal("0.0060000")
861+ \end {verbatim }
862+
863+ The \module {decimal} module makes it possible to restore the identities
864+ by expanding the precision sufficiently to avoid loss of significance:
865+
866+ \begin {verbatim }
867+ >>> getcontext().prec = 20
868+ >>> u, v, w = Decimal(11111113), Decimal(-11111111), Decimal('7.51111111')
869+ >>> (u + v) + w
870+ Decimal("9.51111111")
871+ >>> u + (v + w)
872+ Decimal("9.51111111")
873+ >>>
874+ >>> u, v, w = Decimal(20000), Decimal(-6), Decimal('6.0000003')
875+ >>> (u*v) + (u*w)
876+ Decimal("0.0060000")
877+ >>> u * (v+w)
878+ Decimal("0.0060000")
879+ \end {verbatim }
880+
881+
882+ The number system for the \module {decimal} module provides special
883+ values including \constant {NaN}, \constant {sNaN}, \constant {-Infinity},
884+ \constant {Infinity}, and two zeroes, \constant {+0} and \constant {-0}.
885+
886+ Infinities can constructed directly with: \code {Decimal('Infinity')}. Also,
887+ they can arise from dividing by zero when the \exception {DivisionByZero}
888+ signal is not trapped. Likewise, when the \exception {Overflow} signal is not
889+ trapped, infinity can result from rounding beyond the limits of the largest
890+ representable number.
891+
892+ The infinities are signed (affine) and can be used in arithmetic operations
893+ where they get treated as very large, indeterminate numbers. For instance,
894+ adding a constant to infinity gives another infinite result.
895+
896+ Some operations are indeterminate and return \constant {NaN} or when the
897+ \exception {InvalidOperation} signal is trapped, raise an exception. For
898+ example, \code {0/0} returns \constant {NaN} which means `` not a number'' . This
899+ variety of \constant {NaN} is quiet and, once created, will flow through other
900+ computations always resulting in another \constant {NaN}. This behavior can be
901+ useful for a series of computations that occasionally have missing inputs ---
902+ it allows the calculation to proceed while flagging specific results as
903+ invalid.
904+
905+ A variant is \constant {sNaN} which signals rather than remaining quiet
906+ after every operation. This is a useful return value when an invalid
907+ result needs to interrupt a calculation for special handling.
908+
909+ The signed zeros can result from calculations that underflow.
910+ They keep the sign that would have resulted if the calculation had
911+ been carried out to greater precision. Since their magnitude is
912+ zero, the positive and negative zero are treated as equal and their
913+ sign is informational.
914+
915+ In addition to the two signed zeros which are distinct, yet equal,
916+ there are various representations of zero with differing precisions,
917+ yet equivalent in value. This takes a bit of getting used to. For
918+ an eye accustomed to normalized floating point representations, it
919+ is not immediately obvious that the following calculation returns
920+ a value equal to zero:
921+
922+ \begin {verbatim }
923+ >>> 1 / Decimal('Infinity')
924+ Decimal("0E-1000000026")
925+ \end {verbatim }
926+
831927% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
832928\subsection {Working with threads \label {decimal-threads } }
833929
@@ -864,7 +960,7 @@ \subsection{Working with threads \label{decimal-threads}}
864960t3.start()
865961 . . .
866962\end {verbatim }
867-
963+
868964
869965
870966% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0 commit comments