• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Decimal fixed-point and floating-point arithmetic.
2
3This is an implementation of decimal floating-point arithmetic based on
4the General Decimal Arithmetic Specification:
5
6    http://speleotrove.com/decimal/decarith.html
7
8and IEEE standard 854-1987:
9
10    http://en.wikipedia.org/wiki/IEEE_854-1987
11
12Decimal floating point has finite precision with arbitrarily large bounds.
13
14The purpose of this module is to support arithmetic using familiar
15"schoolhouse" rules and to avoid some of the tricky representation
16issues associated with binary floating point.  The package is especially
17useful for financial applications or for contexts where users have
18expectations that are at odds with binary floating point (for instance,
19in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead
20of 0.0; Decimal('1.00') % Decimal('0.1') returns the expected
21Decimal('0.00')).
22
23Here are some examples of using the decimal module:
24
25>>> from decimal import *
26>>> setcontext(ExtendedContext)
27>>> Decimal(0)
28Decimal('0')
29>>> Decimal('1')
30Decimal('1')
31>>> Decimal('-.0123')
32Decimal('-0.0123')
33>>> Decimal(123456)
34Decimal('123456')
35>>> Decimal('123.45e12345678')
36Decimal('1.2345E+12345680')
37>>> Decimal('1.33') + Decimal('1.27')
38Decimal('2.60')
39>>> Decimal('12.34') + Decimal('3.87') - Decimal('18.41')
40Decimal('-2.20')
41>>> dig = Decimal(1)
42>>> print(dig / Decimal(3))
430.333333333
44>>> getcontext().prec = 18
45>>> print(dig / Decimal(3))
460.333333333333333333
47>>> print(dig.sqrt())
481
49>>> print(Decimal(3).sqrt())
501.73205080756887729
51>>> print(Decimal(3) ** 123)
524.85192780976896427E+58
53>>> inf = Decimal(1) / Decimal(0)
54>>> print(inf)
55Infinity
56>>> neginf = Decimal(-1) / Decimal(0)
57>>> print(neginf)
58-Infinity
59>>> print(neginf + inf)
60NaN
61>>> print(neginf * inf)
62-Infinity
63>>> print(dig / 0)
64Infinity
65>>> getcontext().traps[DivisionByZero] = 1
66>>> print(dig / 0)
67Traceback (most recent call last):
68  ...
69  ...
70  ...
71decimal.DivisionByZero: x / 0
72>>> c = Context()
73>>> c.traps[InvalidOperation] = 0
74>>> print(c.flags[InvalidOperation])
750
76>>> c.divide(Decimal(0), Decimal(0))
77Decimal('NaN')
78>>> c.traps[InvalidOperation] = 1
79>>> print(c.flags[InvalidOperation])
801
81>>> c.flags[InvalidOperation] = 0
82>>> print(c.flags[InvalidOperation])
830
84>>> print(c.divide(Decimal(0), Decimal(0)))
85Traceback (most recent call last):
86  ...
87  ...
88  ...
89decimal.InvalidOperation: 0 / 0
90>>> print(c.flags[InvalidOperation])
911
92>>> c.flags[InvalidOperation] = 0
93>>> c.traps[InvalidOperation] = 0
94>>> print(c.divide(Decimal(0), Decimal(0)))
95NaN
96>>> print(c.flags[InvalidOperation])
971
98>>>
99"""
100
101try:
102    from _decimal import *
103    from _decimal import __version__
104    from _decimal import __libmpdec_version__
105except ImportError:
106    import _pydecimal
107    import sys
108    _pydecimal.__doc__ = __doc__
109    sys.modules[__name__] = _pydecimal
110