The number system for the decimal module provides special values including NaN, sNaN, -Infinity, Infinity, and two zeroes, +0 and -0.
Infinities can be constructed directly with:
they can arise from dividing by zero when the DivisionByZero
signal is not trapped. Likewise, when the Overflow signal is not
trapped, infinity can result from rounding beyond the limits of the largest
The infinities are signed (affine) and can be used in arithmetic operations where they get treated as very large, indeterminate numbers. For instance, adding a constant to infinity gives another infinite result.
Some operations are indeterminate and return NaN, or if the
InvalidOperation signal is trapped, raise an exception. For
0/0 returns NaN which means ``not a number''. This
variety of NaN is quiet and, once created, will flow through other
computations always resulting in another NaN. This behavior can be
useful for a series of computations that occasionally have missing inputs --
it allows the calculation to proceed while flagging specific results as
A variant is sNaN which signals rather than remaining quiet after every operation. This is a useful return value when an invalid result needs to interrupt a calculation for special handling.
The signed zeros can result from calculations that underflow. They keep the sign that would have resulted if the calculation had been carried out to greater precision. Since their magnitude is zero, both positive and negative zeros are treated as equal and their sign is informational.
In addition to the two signed zeros which are distinct yet equal, there are various representations of zero with differing precisions yet equivalent in value. This takes a bit of getting used to. For an eye accustomed to normalized floating point representations, it is not immediately obvious that the following calculation returns a value equal to zero:
>>> 1 / Decimal('Infinity') Decimal("0E-1000000026")
See About this document... for information on suggesting changes.