Decimal fixed-point arithmetic
decimalfp provides a
Decimal number type which can represent
decimal numbers of arbitrary magnitude and arbitrary precision, i.e. any
number of fractional digits.
decimalfp.Decimal instances are created by giving a
value (default: 0) and
precision (i.e the number of fractional digits, default: None).
precision is given, it must be of type
int and >= 0.
value is given, it must either be a string (type
str in Python 3.x), an instance of
long in Python 2.x,
int in Python 3.x),
number.Rational (for example
float or be convertable to a
float or an
If a string is given as value, it must be a string in one of two formats:
- [+|-]<int>[.<frac>][<e|E>[+|-]<exp>] or
The value is always adjusted to the given precision or the precision is calculated from the given value, if no precision is given.
When the given
precision is lower than the precision of the given
the result is rounded, according to the rounding mode of the current context
held by the standard module
decimal (which defaults to ROUND_HALF_EVEN, in
contrast to the
round function in Python 2.x !!!).
precision is given and the given
value is a
float or a
numbers.Rational (but no
Decimal constructor tries to
value exactly. But, for performance reasons, this is done only up a
fixed limit of fractional digits. This limit defaults to 32 and is accessible
value can not be represented as a
within this limit, an exception is raised.
Decimal does not deal with infinity, division by 0 always raises a
ZeroDivisionError. Likewise, infinite instances of type
decimal.Decimal can not be converted to
Decimal instances. The same is
true for the ‘not a number’ instances of these types.
Decimal type is registered in Pythons
numerical stack as
number.Rational. It supports all operations defined for
that base class and its instances can be mixed in computations with instances
of all numeric types mentioned above.
All numerical operations give an exact result, i.e. they are not automatically
constraint to the precision of the operands or to a number of significant
digits (like the floating-point
Decimal type from the standard module
decimal). When the result can not exactly be represented by a
instance within the limit given by
decimalfp.LIMIT_PREC, an instance of
fractions.Fraction is returned.
Decimal supports rounding via the built-in function
round using the same
rounding mode as the
float type by default (i.e. ROUND_HALF_UP in Pyhton 2.x
and ROUND_HALF_EVEN in Python 3.x). In addition, via the method
Decimal with a different precision can be derived, supporting all rounding
modes defined by the standard library module
For more details see the documentation provided with the source distribution or here.