1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>9.4. decimal — Decimal fixed point and floating point arithmetic — Python 2.7.12 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.12 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.12 documentation" href="../contents.html" />
33
<link rel="up" title="9. Numeric and Mathematical Modules" href="numeric.html" />
34
<link rel="next" title="9.5. fractions — Rational numbers" href="fractions.html" />
35
<link rel="prev" title="9.3. cmath — Mathematical functions for complex numbers" href="cmath.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="fractions.html" title="9.5. fractions — Rational numbers"
55
accesskey="N">next</a> |</li>
57
<a href="cmath.html" title="9.3. cmath — Mathematical functions for complex numbers"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.12</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="numeric.html" accesskey="U">9. Numeric and Mathematical Modules</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-decimal">
78
<span id="decimal-decimal-fixed-point-and-floating-point-arithmetic"></span><h1>9.4. <a class="reference internal" href="#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal"><span class="pre">decimal</span></code></a> — Decimal fixed point and floating point arithmetic<a class="headerlink" href="#module-decimal" title="Permalink to this headline">¶</a></h1>
79
<div class="versionadded">
80
<p><span class="versionmodified">New in version 2.4.</span></p>
82
<p>The <a class="reference internal" href="#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal"><span class="pre">decimal</span></code></a> module provides support for decimal floating point
83
arithmetic. It offers several advantages over the <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a> datatype:</p>
85
<li><p class="first">Decimal “is based on a floating-point model which was designed with people
86
in mind, and necessarily has a paramount guiding principle – computers must
87
provide an arithmetic that works in the same way as the arithmetic that
88
people learn at school.” – excerpt from the decimal arithmetic specification.</p>
90
<li><p class="first">Decimal numbers can be represented exactly. In contrast, numbers like
91
<code class="xref py py-const docutils literal"><span class="pre">1.1</span></code> and <code class="xref py py-const docutils literal"><span class="pre">2.2</span></code> do not have exact representations in binary
92
floating point. End users typically would not expect <code class="docutils literal"><span class="pre">1.1</span> <span class="pre">+</span> <span class="pre">2.2</span></code> to display
93
as <code class="xref py py-const docutils literal"><span class="pre">3.3000000000000003</span></code> as it does with binary floating point.</p>
95
<li><p class="first">The exactness carries over into arithmetic. In decimal floating point, <code class="docutils literal"><span class="pre">0.1</span>
96
<span class="pre">+</span> <span class="pre">0.1</span> <span class="pre">+</span> <span class="pre">0.1</span> <span class="pre">-</span> <span class="pre">0.3</span></code> is exactly equal to zero. In binary floating point, the result
97
is <code class="xref py py-const docutils literal"><span class="pre">5.5511151231257827e-017</span></code>. While near to zero, the differences
98
prevent reliable equality testing and differences can accumulate. For this
99
reason, decimal is preferred in accounting applications which have strict
100
equality invariants.</p>
102
<li><p class="first">The decimal module incorporates a notion of significant places so that <code class="docutils literal"><span class="pre">1.30</span>
103
<span class="pre">+</span> <span class="pre">1.20</span></code> is <code class="xref py py-const docutils literal"><span class="pre">2.50</span></code>. The trailing zero is kept to indicate significance.
104
This is the customary presentation for monetary applications. For
105
multiplication, the “schoolbook” approach uses all the figures in the
106
multiplicands. For instance, <code class="docutils literal"><span class="pre">1.3</span> <span class="pre">*</span> <span class="pre">1.2</span></code> gives <code class="xref py py-const docutils literal"><span class="pre">1.56</span></code> while <code class="docutils literal"><span class="pre">1.30</span> <span class="pre">*</span>
107
<span class="pre">1.20</span></code> gives <code class="xref py py-const docutils literal"><span class="pre">1.5600</span></code>.</p>
109
<li><p class="first">Unlike hardware based binary floating point, the decimal module has a user
110
alterable precision (defaulting to 28 places) which can be as large as needed for
112
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">decimal</span> <span class="kn">import</span> <span class="o">*</span>
113
<span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">6</span>
114
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
115
<span class="go">Decimal('0.142857')</span>
116
<span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">28</span>
117
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
118
<span class="go">Decimal('0.1428571428571428571428571429')</span>
122
<li><p class="first">Both binary and decimal floating point are implemented in terms of published
123
standards. While the built-in float type exposes only a modest portion of its
124
capabilities, the decimal module exposes all required parts of the standard.
125
When needed, the programmer has full control over rounding and signal handling.
126
This includes an option to enforce exact arithmetic by using exceptions
127
to block any inexact operations.</p>
129
<li><p class="first">The decimal module was designed to support “without prejudice, both exact
130
unrounded decimal arithmetic (sometimes called fixed-point arithmetic)
131
and rounded floating-point arithmetic.” – excerpt from the decimal
132
arithmetic specification.</p>
135
<p>The module design is centered around three concepts: the decimal number, the
136
context for arithmetic, and signals.</p>
137
<p>A decimal number is immutable. It has a sign, coefficient digits, and an
138
exponent. To preserve significance, the coefficient digits do not truncate
139
trailing zeros. Decimals also include special values such as
140
<code class="xref py py-const docutils literal"><span class="pre">Infinity</span></code>, <code class="xref py py-const docutils literal"><span class="pre">-Infinity</span></code>, and <code class="xref py py-const docutils literal"><span class="pre">NaN</span></code>. The standard also
141
differentiates <code class="xref py py-const docutils literal"><span class="pre">-0</span></code> from <code class="xref py py-const docutils literal"><span class="pre">+0</span></code>.</p>
142
<p>The context for arithmetic is an environment specifying precision, rounding
143
rules, limits on exponents, flags indicating the results of operations, and trap
144
enablers which determine whether signals are treated as exceptions. Rounding
145
options include <code class="xref py py-const docutils literal"><span class="pre">ROUND_CEILING</span></code>, <code class="xref py py-const docutils literal"><span class="pre">ROUND_DOWN</span></code>,
146
<code class="xref py py-const docutils literal"><span class="pre">ROUND_FLOOR</span></code>, <code class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_DOWN</span></code>, <code class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></code>,
147
<code class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_UP</span></code>, <code class="xref py py-const docutils literal"><span class="pre">ROUND_UP</span></code>, and <code class="xref py py-const docutils literal"><span class="pre">ROUND_05UP</span></code>.</p>
148
<p>Signals are groups of exceptional conditions arising during the course of
149
computation. Depending on the needs of the application, signals may be ignored,
150
considered as informational, or treated as exceptions. The signals in the
151
decimal module are: <a class="reference internal" href="#decimal.Clamped" title="decimal.Clamped"><code class="xref py py-const docutils literal"><span class="pre">Clamped</span></code></a>, <a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><code class="xref py py-const docutils literal"><span class="pre">InvalidOperation</span></code></a>,
152
<a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><code class="xref py py-const docutils literal"><span class="pre">DivisionByZero</span></code></a>, <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><code class="xref py py-const docutils literal"><span class="pre">Inexact</span></code></a>, <a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><code class="xref py py-const docutils literal"><span class="pre">Rounded</span></code></a>, <a class="reference internal" href="#decimal.Subnormal" title="decimal.Subnormal"><code class="xref py py-const docutils literal"><span class="pre">Subnormal</span></code></a>,
153
<a class="reference internal" href="#decimal.Overflow" title="decimal.Overflow"><code class="xref py py-const docutils literal"><span class="pre">Overflow</span></code></a>, and <a class="reference internal" href="#decimal.Underflow" title="decimal.Underflow"><code class="xref py py-const docutils literal"><span class="pre">Underflow</span></code></a>.</p>
154
<p>For each signal there is a flag and a trap enabler. When a signal is
155
encountered, its flag is set to one, then, if the trap enabler is
156
set to one, an exception is raised. Flags are sticky, so the user needs to
157
reset them before monitoring a calculation.</p>
158
<div class="admonition seealso">
159
<p class="first admonition-title">See also</p>
160
<ul class="last simple">
161
<li>IBM’s General Decimal Arithmetic Specification, <a class="reference external" href="http://speleotrove.com/decimal/">The General Decimal Arithmetic
162
Specification</a>.</li>
165
<div class="section" id="quick-start-tutorial">
166
<span id="decimal-tutorial"></span><h2>9.4.1. Quick-start Tutorial<a class="headerlink" href="#quick-start-tutorial" title="Permalink to this headline">¶</a></h2>
167
<p>The usual start to using decimals is importing the module, viewing the current
168
context with <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><code class="xref py py-func docutils literal"><span class="pre">getcontext()</span></code></a> and, if necessary, setting new values for
169
precision, rounding, or enabled traps:</p>
170
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">decimal</span> <span class="kn">import</span> <span class="o">*</span>
171
<span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span>
172
<span class="go">Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,</span>
173
<span class="go"> capitals=1, flags=[], traps=[Overflow, DivisionByZero,</span>
174
<span class="go"> InvalidOperation])</span>
176
<span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">7</span> <span class="c1"># Set a new precision</span>
179
<p>Decimal instances can be constructed from integers, strings, floats, or tuples.
180
Construction from an integer or a float performs an exact conversion of the
181
value of that integer or float. Decimal numbers include special values such as
182
<code class="xref py py-const docutils literal"><span class="pre">NaN</span></code> which stands for “Not a number”, positive and negative
183
<code class="xref py py-const docutils literal"><span class="pre">Infinity</span></code>, and <code class="xref py py-const docutils literal"><span class="pre">-0</span></code>.</p>
184
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">28</span>
185
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
186
<span class="go">Decimal('10')</span>
187
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'3.14'</span><span class="p">)</span>
188
<span class="go">Decimal('3.14')</span>
189
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mf">3.14</span><span class="p">)</span>
190
<span class="go">Decimal('3.140000000000000124344978758017532527446746826171875')</span>
191
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="o">-</span><span class="mi">2</span><span class="p">))</span>
192
<span class="go">Decimal('3.14')</span>
193
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="mf">2.0</span> <span class="o">**</span> <span class="mf">0.5</span><span class="p">))</span>
194
<span class="go">Decimal('1.41421356237')</span>
195
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">**</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">'0.5'</span><span class="p">)</span>
196
<span class="go">Decimal('1.414213562373095048801688724')</span>
197
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'NaN'</span><span class="p">)</span>
198
<span class="go">Decimal('NaN')</span>
199
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'-Infinity'</span><span class="p">)</span>
200
<span class="go">Decimal('-Infinity')</span>
203
<p>The significance of a new Decimal is determined solely by the number of digits
204
input. Context precision and rounding only come into play during arithmetic
206
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">6</span>
207
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'3.0'</span><span class="p">)</span>
208
<span class="go">Decimal('3.0')</span>
209
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'3.1415926535'</span><span class="p">)</span>
210
<span class="go">Decimal('3.1415926535')</span>
211
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'3.1415926535'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">'2.7182818285'</span><span class="p">)</span>
212
<span class="go">Decimal('5.85987')</span>
213
<span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">rounding</span> <span class="o">=</span> <span class="n">ROUND_UP</span>
214
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'3.1415926535'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">'2.7182818285'</span><span class="p">)</span>
215
<span class="go">Decimal('5.85988')</span>
218
<p>Decimals interact well with much of the rest of Python. Here is a small decimal
219
floating point flying circus:</p>
220
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">data</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">Decimal</span><span class="p">,</span> <span class="s1">'1.34 1.87 3.45 2.35 1.00 0.03 9.25'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
221
<span class="gp">>>> </span><span class="nb">max</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
222
<span class="go">Decimal('9.25')</span>
223
<span class="gp">>>> </span><span class="nb">min</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
224
<span class="go">Decimal('0.03')</span>
225
<span class="gp">>>> </span><span class="nb">sorted</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
226
<span class="go">[Decimal('0.03'), Decimal('1.00'), Decimal('1.34'), Decimal('1.87'),</span>
227
<span class="go"> Decimal('2.35'), Decimal('3.45'), Decimal('9.25')]</span>
228
<span class="gp">>>> </span><span class="nb">sum</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
229
<span class="go">Decimal('19.29')</span>
230
<span class="gp">>>> </span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">c</span> <span class="o">=</span> <span class="n">data</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span>
231
<span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
232
<span class="go">'1.34'</span>
233
<span class="gp">>>> </span><span class="nb">float</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
234
<span class="go">1.34</span>
235
<span class="gp">>>> </span><span class="nb">round</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># round() first converts to binary floating point</span>
236
<span class="go">1.3</span>
237
<span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
238
<span class="go">1</span>
239
<span class="gp">>>> </span><span class="n">a</span> <span class="o">*</span> <span class="mi">5</span>
240
<span class="go">Decimal('6.70')</span>
241
<span class="gp">>>> </span><span class="n">a</span> <span class="o">*</span> <span class="n">b</span>
242
<span class="go">Decimal('2.5058')</span>
243
<span class="gp">>>> </span><span class="n">c</span> <span class="o">%</span> <span class="n">a</span>
244
<span class="go">Decimal('0.77')</span>
247
<p>And some mathematical functions are also available to Decimal:</p>
248
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">28</span>
249
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">sqrt</span><span class="p">()</span>
250
<span class="go">Decimal('1.414213562373095048801688724')</span>
251
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">exp</span><span class="p">()</span>
252
<span class="go">Decimal('2.718281828459045235360287471')</span>
253
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'10'</span><span class="p">)</span><span class="o">.</span><span class="n">ln</span><span class="p">()</span>
254
<span class="go">Decimal('2.302585092994045684017991455')</span>
255
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'10'</span><span class="p">)</span><span class="o">.</span><span class="n">log10</span><span class="p">()</span>
256
<span class="go">Decimal('1')</span>
259
<p>The <code class="xref py py-meth docutils literal"><span class="pre">quantize()</span></code> method rounds a number to a fixed exponent. This method is
260
useful for monetary applications that often round results to a fixed number of
262
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'7.325'</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'.01'</span><span class="p">),</span> <span class="n">rounding</span><span class="o">=</span><span class="n">ROUND_DOWN</span><span class="p">)</span>
263
<span class="go">Decimal('7.32')</span>
264
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'7.325'</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'1.'</span><span class="p">),</span> <span class="n">rounding</span><span class="o">=</span><span class="n">ROUND_UP</span><span class="p">)</span>
265
<span class="go">Decimal('8')</span>
268
<p>As shown above, the <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><code class="xref py py-func docutils literal"><span class="pre">getcontext()</span></code></a> function accesses the current context and
269
allows the settings to be changed. This approach meets the needs of most
271
<p>For more advanced work, it may be useful to create alternate contexts using the
272
Context() constructor. To make an alternate active, use the <a class="reference internal" href="#decimal.setcontext" title="decimal.setcontext"><code class="xref py py-func docutils literal"><span class="pre">setcontext()</span></code></a>
274
<p>In accordance with the standard, the <a class="reference internal" href="#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal"><span class="pre">decimal</span></code></a> module provides two ready to
275
use standard contexts, <a class="reference internal" href="#decimal.BasicContext" title="decimal.BasicContext"><code class="xref py py-const docutils literal"><span class="pre">BasicContext</span></code></a> and <a class="reference internal" href="#decimal.ExtendedContext" title="decimal.ExtendedContext"><code class="xref py py-const docutils literal"><span class="pre">ExtendedContext</span></code></a>. The
276
former is especially useful for debugging because many of the traps are
278
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">myothercontext</span> <span class="o">=</span> <span class="n">Context</span><span class="p">(</span><span class="n">prec</span><span class="o">=</span><span class="mi">60</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="n">ROUND_HALF_DOWN</span><span class="p">)</span>
279
<span class="gp">>>> </span><span class="n">setcontext</span><span class="p">(</span><span class="n">myothercontext</span><span class="p">)</span>
280
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
281
<span class="go">Decimal('0.142857142857142857142857142857142857142857142857142857142857')</span>
283
<span class="gp">>>> </span><span class="n">ExtendedContext</span>
284
<span class="go">Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,</span>
285
<span class="go"> capitals=1, flags=[], traps=[])</span>
286
<span class="gp">>>> </span><span class="n">setcontext</span><span class="p">(</span><span class="n">ExtendedContext</span><span class="p">)</span>
287
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
288
<span class="go">Decimal('0.142857143')</span>
289
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
290
<span class="go">Decimal('Infinity')</span>
292
<span class="gp">>>> </span><span class="n">setcontext</span><span class="p">(</span><span class="n">BasicContext</span><span class="p">)</span>
293
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
294
<span class="gt">Traceback (most recent call last):</span>
295
File <span class="nb">"<pyshell#143>"</span>, line <span class="m">1</span>, in <span class="n">-toplevel-</span>
296
<span class="n">Decimal</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
297
<span class="gr">DivisionByZero</span>: <span class="n">x / 0</span>
300
<p>Contexts also have signal flags for monitoring exceptional conditions
301
encountered during computations. The flags remain set until explicitly cleared,
302
so it is best to clear the flags before each set of monitored computations by
303
using the <code class="xref py py-meth docutils literal"><span class="pre">clear_flags()</span></code> method.</p>
304
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">setcontext</span><span class="p">(</span><span class="n">ExtendedContext</span><span class="p">)</span>
305
<span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">clear_flags</span><span class="p">()</span>
306
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">355</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">113</span><span class="p">)</span>
307
<span class="go">Decimal('3.14159292')</span>
308
<span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span>
309
<span class="go">Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,</span>
310
<span class="go"> capitals=1, flags=[Rounded, Inexact], traps=[])</span>
313
<p>The <em>flags</em> entry shows that the rational approximation to <code class="xref py py-const docutils literal"><span class="pre">Pi</span></code> was
314
rounded (digits beyond the context precision were thrown away) and that the
315
result is inexact (some of the discarded digits were non-zero).</p>
316
<p>Individual traps are set using the dictionary in the <code class="xref py py-attr docutils literal"><span class="pre">traps</span></code> field of a
318
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">setcontext</span><span class="p">(</span><span class="n">ExtendedContext</span><span class="p">)</span>
319
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
320
<span class="go">Decimal('Infinity')</span>
321
<span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">traps</span><span class="p">[</span><span class="n">DivisionByZero</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
322
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
323
<span class="gt">Traceback (most recent call last):</span>
324
File <span class="nb">"<pyshell#112>"</span>, line <span class="m">1</span>, in <span class="n">-toplevel-</span>
325
<span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
326
<span class="gr">DivisionByZero</span>: <span class="n">x / 0</span>
329
<p>Most programs adjust the current context only once, at the beginning of the
330
program. And, in many applications, data is converted to <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> with
331
a single cast inside a loop. With context set and decimals created, the bulk of
332
the program manipulates the data no differently than with other Python numeric
335
<div class="section" id="decimal-objects">
336
<span id="decimal-decimal"></span><h2>9.4.2. Decimal objects<a class="headerlink" href="#decimal-objects" title="Permalink to this headline">¶</a></h2>
338
<dt id="decimal.Decimal">
339
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">Decimal</code><span class="sig-paren">(</span><span class="optional">[</span><em>value</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal" title="Permalink to this definition">¶</a></dt>
340
<dd><p>Construct a new <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> object based from <em>value</em>.</p>
341
<p><em>value</em> can be an integer, string, tuple, <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a>, or another <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a>
342
object. If no <em>value</em> is given, returns <code class="docutils literal"><span class="pre">Decimal('0')</span></code>. If <em>value</em> is a
343
string, it should conform to the decimal numeric string syntax after leading
344
and trailing whitespace characters are removed:</p>
345
<div class="highlight-python"><div class="highlight"><pre><span></span>sign ::= '+' | '-'
346
digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
347
indicator ::= 'e' | 'E'
348
digits ::= digit [digit]...
349
decimal-part ::= digits '.' [digits] | ['.'] digits
350
exponent-part ::= indicator [sign] digits
351
infinity ::= 'Infinity' | 'Inf'
352
nan ::= 'NaN' [digits] | 'sNaN' [digits]
353
numeric-value ::= decimal-part [exponent-part] | infinity
354
numeric-string ::= [sign] numeric-value | [sign] nan
357
<p>If <em>value</em> is a unicode string then other Unicode decimal digits
358
are also permitted where <code class="docutils literal"><span class="pre">digit</span></code> appears above. These include
359
decimal digits from various other alphabets (for example,
360
Arabic-Indic and Devanāgarī digits) along with the fullwidth digits
361
<code class="docutils literal"><span class="pre">u'\uff10'</span></code> through <code class="docutils literal"><span class="pre">u'\uff19'</span></code>.</p>
362
<p>If <em>value</em> is a <a class="reference internal" href="functions.html#tuple" title="tuple"><code class="xref py py-class docutils literal"><span class="pre">tuple</span></code></a>, it should have three components, a sign
363
(<code class="xref py py-const docutils literal"><span class="pre">0</span></code> for positive or <code class="xref py py-const docutils literal"><span class="pre">1</span></code> for negative), a <a class="reference internal" href="functions.html#tuple" title="tuple"><code class="xref py py-class docutils literal"><span class="pre">tuple</span></code></a> of
364
digits, and an integer exponent. For example, <code class="docutils literal"><span class="pre">Decimal((0,</span> <span class="pre">(1,</span> <span class="pre">4,</span> <span class="pre">1,</span> <span class="pre">4),</span> <span class="pre">-3))</span></code>
365
returns <code class="docutils literal"><span class="pre">Decimal('1.414')</span></code>.</p>
366
<p>If <em>value</em> is a <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a>, the binary floating point value is losslessly
367
converted to its exact decimal equivalent. This conversion can often require
368
53 or more digits of precision. For example, <code class="docutils literal"><span class="pre">Decimal(float('1.1'))</span></code>
370
<code class="docutils literal"><span class="pre">Decimal('1.100000000000000088817841970012523233890533447265625')</span></code>.</p>
371
<p>The <em>context</em> precision does not affect how many digits are stored. That is
372
determined exclusively by the number of digits in <em>value</em>. For example,
373
<code class="docutils literal"><span class="pre">Decimal('3.00000')</span></code> records all five zeros even if the context precision is
375
<p>The purpose of the <em>context</em> argument is determining what to do if <em>value</em> is a
376
malformed string. If the context traps <a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><code class="xref py py-const docutils literal"><span class="pre">InvalidOperation</span></code></a>, an exception
377
is raised; otherwise, the constructor returns a new Decimal with the value of
378
<code class="xref py py-const docutils literal"><span class="pre">NaN</span></code>.</p>
379
<p>Once constructed, <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> objects are immutable.</p>
380
<div class="versionchanged">
381
<p><span class="versionmodified">Changed in version 2.6: </span>leading and trailing whitespace characters are permitted when
382
creating a Decimal instance from a string.</p>
384
<div class="versionchanged">
385
<p><span class="versionmodified">Changed in version 2.7: </span>The argument to the constructor is now permitted to be a <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a> instance.</p>
387
<p>Decimal floating point objects share many properties with the other built-in
388
numeric types such as <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a> and <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal"><span class="pre">int</span></code></a>. All of the usual math
389
operations and special methods apply. Likewise, decimal objects can be
390
copied, pickled, printed, used as dictionary keys, used as set elements,
391
compared, sorted, and coerced to another type (such as <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a> or
392
<a class="reference internal" href="functions.html#long" title="long"><code class="xref py py-class docutils literal"><span class="pre">long</span></code></a>).</p>
393
<p>There are some small differences between arithmetic on Decimal objects and
394
arithmetic on integers and floats. When the remainder operator <code class="docutils literal"><span class="pre">%</span></code> is
395
applied to Decimal objects, the sign of the result is the sign of the
396
<em>dividend</em> rather than the sign of the divisor:</p>
397
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">(</span><span class="o">-</span><span class="mi">7</span><span class="p">)</span> <span class="o">%</span> <span class="mi">4</span>
398
<span class="go">1</span>
399
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="o">-</span><span class="mi">7</span><span class="p">)</span> <span class="o">%</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
400
<span class="go">Decimal('-3')</span>
403
<p>The integer division operator <code class="docutils literal"><span class="pre">//</span></code> behaves analogously, returning the
404
integer part of the true quotient (truncating towards zero) rather than its
405
floor, so as to preserve the usual identity <code class="docutils literal"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">(x</span> <span class="pre">//</span> <span class="pre">y)</span> <span class="pre">*</span> <span class="pre">y</span> <span class="pre">+</span> <span class="pre">x</span> <span class="pre">%</span> <span class="pre">y</span></code>:</p>
406
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="o">-</span><span class="mi">7</span> <span class="o">//</span> <span class="mi">4</span>
407
<span class="go">-2</span>
408
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="o">-</span><span class="mi">7</span><span class="p">)</span> <span class="o">//</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
409
<span class="go">Decimal('-1')</span>
412
<p>The <code class="docutils literal"><span class="pre">%</span></code> and <code class="docutils literal"><span class="pre">//</span></code> operators implement the <code class="docutils literal"><span class="pre">remainder</span></code> and
413
<code class="docutils literal"><span class="pre">divide-integer</span></code> operations (respectively) as described in the
415
<p>Decimal objects cannot generally be combined with floats in
416
arithmetic operations: an attempt to add a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> to a
417
<a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a>, for example, will raise a <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a>.
418
There’s one exception to this rule: it’s possible to use Python’s
419
comparison operators to compare a <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a> instance <code class="docutils literal"><span class="pre">x</span></code>
420
with a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instance <code class="docutils literal"><span class="pre">y</span></code>. Without this exception,
421
comparisons between <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> and <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a> instances
422
would follow the general rules for comparing objects of different
423
types described in the <a class="reference internal" href="../reference/expressions.html#expressions"><span>Expressions</span></a> section of the reference
424
manual, leading to confusing results.</p>
425
<div class="versionchanged">
426
<p><span class="versionmodified">Changed in version 2.7: </span>A comparison between a <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a> instance <code class="docutils literal"><span class="pre">x</span></code> and a
427
<a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instance <code class="docutils literal"><span class="pre">y</span></code> now returns a result based on
428
the values of <code class="docutils literal"><span class="pre">x</span></code> and <code class="docutils literal"><span class="pre">y</span></code>. In earlier versions <code class="docutils literal"><span class="pre">x</span> <span class="pre"><</span> <span class="pre">y</span></code>
429
returned the same (arbitrary) result for any <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a>
430
instance <code class="docutils literal"><span class="pre">x</span></code> and any <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a> instance <code class="docutils literal"><span class="pre">y</span></code>.</p>
432
<p>In addition to the standard numeric properties, decimal floating point
433
objects also have a number of specialized methods:</p>
435
<dt id="decimal.Decimal.adjusted">
436
<code class="descname">adjusted</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.adjusted" title="Permalink to this definition">¶</a></dt>
437
<dd><p>Return the adjusted exponent after shifting out the coefficient’s
438
rightmost digits until only the lead digit remains:
439
<code class="docutils literal"><span class="pre">Decimal('321e+5').adjusted()</span></code> returns seven. Used for determining the
440
position of the most significant digit with respect to the decimal point.</p>
444
<dt id="decimal.Decimal.as_tuple">
445
<code class="descname">as_tuple</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.as_tuple" title="Permalink to this definition">¶</a></dt>
446
<dd><p>Return a <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> representation of the number:
447
<code class="docutils literal"><span class="pre">DecimalTuple(sign,</span> <span class="pre">digits,</span> <span class="pre">exponent)</span></code>.</p>
448
<div class="versionchanged">
449
<p><span class="versionmodified">Changed in version 2.6: </span>Use a named tuple.</p>
454
<dt id="decimal.Decimal.canonical">
455
<code class="descname">canonical</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.canonical" title="Permalink to this definition">¶</a></dt>
456
<dd><p>Return the canonical encoding of the argument. Currently, the encoding of
457
a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instance is always canonical, so this operation returns
458
its argument unchanged.</p>
459
<div class="versionadded">
460
<p><span class="versionmodified">New in version 2.6.</span></p>
465
<dt id="decimal.Decimal.compare">
466
<code class="descname">compare</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.compare" title="Permalink to this definition">¶</a></dt>
467
<dd><p>Compare the values of two Decimal instances. This operation behaves in
468
the same way as the usual comparison method <a class="reference internal" href="../reference/datamodel.html#object.__cmp__" title="object.__cmp__"><code class="xref py py-meth docutils literal"><span class="pre">__cmp__()</span></code></a>, except that
469
<a class="reference internal" href="#decimal.Decimal.compare" title="decimal.Decimal.compare"><code class="xref py py-meth docutils literal"><span class="pre">compare()</span></code></a> returns a Decimal instance rather than an integer, and if
470
either operand is a NaN then the result is a NaN:</p>
471
<div class="highlight-python"><div class="highlight"><pre><span></span>a or b is a NaN ==> Decimal('NaN')
472
a < b ==> Decimal('-1')
473
a == b ==> Decimal('0')
474
a > b ==> Decimal('1')
480
<dt id="decimal.Decimal.compare_signal">
481
<code class="descname">compare_signal</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.compare_signal" title="Permalink to this definition">¶</a></dt>
482
<dd><p>This operation is identical to the <a class="reference internal" href="#decimal.Decimal.compare" title="decimal.Decimal.compare"><code class="xref py py-meth docutils literal"><span class="pre">compare()</span></code></a> method, except that all
483
NaNs signal. That is, if neither operand is a signaling NaN then any
484
quiet NaN operand is treated as though it were a signaling NaN.</p>
485
<div class="versionadded">
486
<p><span class="versionmodified">New in version 2.6.</span></p>
491
<dt id="decimal.Decimal.compare_total">
492
<code class="descname">compare_total</code><span class="sig-paren">(</span><em>other</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.compare_total" title="Permalink to this definition">¶</a></dt>
493
<dd><p>Compare two operands using their abstract representation rather than their
494
numerical value. Similar to the <a class="reference internal" href="#decimal.Decimal.compare" title="decimal.Decimal.compare"><code class="xref py py-meth docutils literal"><span class="pre">compare()</span></code></a> method, but the result
495
gives a total ordering on <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instances. Two
496
<a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instances with the same numeric value but different
497
representations compare unequal in this ordering:</p>
498
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'12.0'</span><span class="p">)</span><span class="o">.</span><span class="n">compare_total</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'12'</span><span class="p">))</span>
499
<span class="go">Decimal('-1')</span>
502
<p>Quiet and signaling NaNs are also included in the total ordering. The
503
result of this function is <code class="docutils literal"><span class="pre">Decimal('0')</span></code> if both operands have the same
504
representation, <code class="docutils literal"><span class="pre">Decimal('-1')</span></code> if the first operand is lower in the
505
total order than the second, and <code class="docutils literal"><span class="pre">Decimal('1')</span></code> if the first operand is
506
higher in the total order than the second operand. See the specification
507
for details of the total order.</p>
508
<div class="versionadded">
509
<p><span class="versionmodified">New in version 2.6.</span></p>
514
<dt id="decimal.Decimal.compare_total_mag">
515
<code class="descname">compare_total_mag</code><span class="sig-paren">(</span><em>other</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.compare_total_mag" title="Permalink to this definition">¶</a></dt>
516
<dd><p>Compare two operands using their abstract representation rather than their
517
value as in <a class="reference internal" href="#decimal.Decimal.compare_total" title="decimal.Decimal.compare_total"><code class="xref py py-meth docutils literal"><span class="pre">compare_total()</span></code></a>, but ignoring the sign of each operand.
518
<code class="docutils literal"><span class="pre">x.compare_total_mag(y)</span></code> is equivalent to
519
<code class="docutils literal"><span class="pre">x.copy_abs().compare_total(y.copy_abs())</span></code>.</p>
520
<div class="versionadded">
521
<p><span class="versionmodified">New in version 2.6.</span></p>
526
<dt id="decimal.Decimal.conjugate">
527
<code class="descname">conjugate</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.conjugate" title="Permalink to this definition">¶</a></dt>
528
<dd><p>Just returns self, this method is only to comply with the Decimal
530
<div class="versionadded">
531
<p><span class="versionmodified">New in version 2.6.</span></p>
536
<dt id="decimal.Decimal.copy_abs">
537
<code class="descname">copy_abs</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.copy_abs" title="Permalink to this definition">¶</a></dt>
538
<dd><p>Return the absolute value of the argument. This operation is unaffected
539
by the context and is quiet: no flags are changed and no rounding is
541
<div class="versionadded">
542
<p><span class="versionmodified">New in version 2.6.</span></p>
547
<dt id="decimal.Decimal.copy_negate">
548
<code class="descname">copy_negate</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.copy_negate" title="Permalink to this definition">¶</a></dt>
549
<dd><p>Return the negation of the argument. This operation is unaffected by the
550
context and is quiet: no flags are changed and no rounding is performed.</p>
551
<div class="versionadded">
552
<p><span class="versionmodified">New in version 2.6.</span></p>
557
<dt id="decimal.Decimal.copy_sign">
558
<code class="descname">copy_sign</code><span class="sig-paren">(</span><em>other</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.copy_sign" title="Permalink to this definition">¶</a></dt>
559
<dd><p>Return a copy of the first operand with the sign set to be the same as the
560
sign of the second operand. For example:</p>
561
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'2.3'</span><span class="p">)</span><span class="o">.</span><span class="n">copy_sign</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'-1.5'</span><span class="p">))</span>
562
<span class="go">Decimal('-2.3')</span>
565
<p>This operation is unaffected by the context and is quiet: no flags are
566
changed and no rounding is performed.</p>
567
<div class="versionadded">
568
<p><span class="versionmodified">New in version 2.6.</span></p>
573
<dt id="decimal.Decimal.exp">
574
<code class="descname">exp</code><span class="sig-paren">(</span><span class="optional">[</span><em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.exp" title="Permalink to this definition">¶</a></dt>
575
<dd><p>Return the value of the (natural) exponential function <code class="docutils literal"><span class="pre">e**x</span></code> at the
576
given number. The result is correctly rounded using the
577
<code class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></code> rounding mode.</p>
578
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">exp</span><span class="p">()</span>
579
<span class="go">Decimal('2.718281828459045235360287471')</span>
580
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">321</span><span class="p">)</span><span class="o">.</span><span class="n">exp</span><span class="p">()</span>
581
<span class="go">Decimal('2.561702493119680037517373933E+139')</span>
584
<div class="versionadded">
585
<p><span class="versionmodified">New in version 2.6.</span></p>
590
<dt id="decimal.Decimal.from_float">
591
<code class="descname">from_float</code><span class="sig-paren">(</span><em>f</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.from_float" title="Permalink to this definition">¶</a></dt>
592
<dd><p>Classmethod that converts a float to a decimal number, exactly.</p>
593
<p>Note <cite>Decimal.from_float(0.1)</cite> is not the same as <cite>Decimal(‘0.1’)</cite>.
594
Since 0.1 is not exactly representable in binary floating point, the
595
value is stored as the nearest representable value which is
596
<cite>0x1.999999999999ap-4</cite>. That equivalent value in decimal is
597
<cite>0.1000000000000000055511151231257827021181583404541015625</cite>.</p>
598
<div class="admonition note">
599
<p class="first admonition-title">Note</p>
600
<p class="last">From Python 2.7 onwards, a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instance
601
can also be constructed directly from a <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a>.</p>
603
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Decimal</span><span class="o">.</span><span class="n">from_float</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
604
<span class="go">Decimal('0.1000000000000000055511151231257827021181583404541015625')</span>
605
<span class="gp">>>> </span><span class="n">Decimal</span><span class="o">.</span><span class="n">from_float</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="s1">'nan'</span><span class="p">))</span>
606
<span class="go">Decimal('NaN')</span>
607
<span class="gp">>>> </span><span class="n">Decimal</span><span class="o">.</span><span class="n">from_float</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="s1">'inf'</span><span class="p">))</span>
608
<span class="go">Decimal('Infinity')</span>
609
<span class="gp">>>> </span><span class="n">Decimal</span><span class="o">.</span><span class="n">from_float</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="s1">'-inf'</span><span class="p">))</span>
610
<span class="go">Decimal('-Infinity')</span>
613
<div class="versionadded">
614
<p><span class="versionmodified">New in version 2.7.</span></p>
619
<dt id="decimal.Decimal.fma">
620
<code class="descname">fma</code><span class="sig-paren">(</span><em>other</em>, <em>third</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.fma" title="Permalink to this definition">¶</a></dt>
621
<dd><p>Fused multiply-add. Return self*other+third with no rounding of the
622
intermediate product self*other.</p>
623
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">fma</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
624
<span class="go">Decimal('11')</span>
627
<div class="versionadded">
628
<p><span class="versionmodified">New in version 2.6.</span></p>
633
<dt id="decimal.Decimal.is_canonical">
634
<code class="descname">is_canonical</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.is_canonical" title="Permalink to this definition">¶</a></dt>
635
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> if the argument is canonical and <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>
636
otherwise. Currently, a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instance is always canonical, so
637
this operation always returns <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>.</p>
638
<div class="versionadded">
639
<p><span class="versionmodified">New in version 2.6.</span></p>
644
<dt id="decimal.Decimal.is_finite">
645
<code class="descname">is_finite</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.is_finite" title="Permalink to this definition">¶</a></dt>
646
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> if the argument is a finite number, and
647
<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> if the argument is an infinity or a NaN.</p>
648
<div class="versionadded">
649
<p><span class="versionmodified">New in version 2.6.</span></p>
654
<dt id="decimal.Decimal.is_infinite">
655
<code class="descname">is_infinite</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.is_infinite" title="Permalink to this definition">¶</a></dt>
656
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> if the argument is either positive or negative
657
infinity and <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> otherwise.</p>
658
<div class="versionadded">
659
<p><span class="versionmodified">New in version 2.6.</span></p>
664
<dt id="decimal.Decimal.is_nan">
665
<code class="descname">is_nan</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.is_nan" title="Permalink to this definition">¶</a></dt>
666
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> if the argument is a (quiet or signaling) NaN and
667
<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> otherwise.</p>
668
<div class="versionadded">
669
<p><span class="versionmodified">New in version 2.6.</span></p>
674
<dt id="decimal.Decimal.is_normal">
675
<code class="descname">is_normal</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.is_normal" title="Permalink to this definition">¶</a></dt>
676
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> if the argument is a <em>normal</em> finite non-zero
677
number with an adjusted exponent greater than or equal to <em>Emin</em>.
678
Return <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> if the argument is zero, subnormal, infinite or a
679
NaN. Note, the term <em>normal</em> is used here in a different sense with
680
the <a class="reference internal" href="#decimal.Decimal.normalize" title="decimal.Decimal.normalize"><code class="xref py py-meth docutils literal"><span class="pre">normalize()</span></code></a> method which is used to create canonical values.</p>
681
<div class="versionadded">
682
<p><span class="versionmodified">New in version 2.6.</span></p>
687
<dt id="decimal.Decimal.is_qnan">
688
<code class="descname">is_qnan</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.is_qnan" title="Permalink to this definition">¶</a></dt>
689
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> if the argument is a quiet NaN, and
690
<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> otherwise.</p>
691
<div class="versionadded">
692
<p><span class="versionmodified">New in version 2.6.</span></p>
697
<dt id="decimal.Decimal.is_signed">
698
<code class="descname">is_signed</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.is_signed" title="Permalink to this definition">¶</a></dt>
699
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> if the argument has a negative sign and
700
<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> otherwise. Note that zeros and NaNs can both carry signs.</p>
701
<div class="versionadded">
702
<p><span class="versionmodified">New in version 2.6.</span></p>
707
<dt id="decimal.Decimal.is_snan">
708
<code class="descname">is_snan</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.is_snan" title="Permalink to this definition">¶</a></dt>
709
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> if the argument is a signaling NaN and <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>
711
<div class="versionadded">
712
<p><span class="versionmodified">New in version 2.6.</span></p>
717
<dt id="decimal.Decimal.is_subnormal">
718
<code class="descname">is_subnormal</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.is_subnormal" title="Permalink to this definition">¶</a></dt>
719
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> if the argument is subnormal, and <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>
720
otherwise. A number is subnormal is if it is nonzero, finite, and has an
721
adjusted exponent less than <em>Emin</em>.</p>
722
<div class="versionadded">
723
<p><span class="versionmodified">New in version 2.6.</span></p>
728
<dt id="decimal.Decimal.is_zero">
729
<code class="descname">is_zero</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.is_zero" title="Permalink to this definition">¶</a></dt>
730
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> if the argument is a (positive or negative) zero and
731
<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> otherwise.</p>
732
<div class="versionadded">
733
<p><span class="versionmodified">New in version 2.6.</span></p>
738
<dt id="decimal.Decimal.ln">
739
<code class="descname">ln</code><span class="sig-paren">(</span><span class="optional">[</span><em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.ln" title="Permalink to this definition">¶</a></dt>
740
<dd><p>Return the natural (base e) logarithm of the operand. The result is
741
correctly rounded using the <code class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></code> rounding mode.</p>
742
<div class="versionadded">
743
<p><span class="versionmodified">New in version 2.6.</span></p>
748
<dt id="decimal.Decimal.log10">
749
<code class="descname">log10</code><span class="sig-paren">(</span><span class="optional">[</span><em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.log10" title="Permalink to this definition">¶</a></dt>
750
<dd><p>Return the base ten logarithm of the operand. The result is correctly
751
rounded using the <code class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></code> rounding mode.</p>
752
<div class="versionadded">
753
<p><span class="versionmodified">New in version 2.6.</span></p>
758
<dt id="decimal.Decimal.logb">
759
<code class="descname">logb</code><span class="sig-paren">(</span><span class="optional">[</span><em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.logb" title="Permalink to this definition">¶</a></dt>
760
<dd><p>For a nonzero number, return the adjusted exponent of its operand as a
761
<a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instance. If the operand is a zero then
762
<code class="docutils literal"><span class="pre">Decimal('-Infinity')</span></code> is returned and the <a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><code class="xref py py-const docutils literal"><span class="pre">DivisionByZero</span></code></a> flag
763
is raised. If the operand is an infinity then <code class="docutils literal"><span class="pre">Decimal('Infinity')</span></code> is
765
<div class="versionadded">
766
<p><span class="versionmodified">New in version 2.6.</span></p>
771
<dt id="decimal.Decimal.logical_and">
772
<code class="descname">logical_and</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.logical_and" title="Permalink to this definition">¶</a></dt>
773
<dd><p><a class="reference internal" href="#decimal.Decimal.logical_and" title="decimal.Decimal.logical_and"><code class="xref py py-meth docutils literal"><span class="pre">logical_and()</span></code></a> is a logical operation which takes two <em>logical
774
operands</em> (see <a class="reference internal" href="#logical-operands-label"><span>Logical operands</span></a>). The result is the
775
digit-wise <code class="docutils literal"><span class="pre">and</span></code> of the two operands.</p>
776
<div class="versionadded">
777
<p><span class="versionmodified">New in version 2.6.</span></p>
782
<dt id="decimal.Decimal.logical_invert">
783
<code class="descname">logical_invert</code><span class="sig-paren">(</span><span class="optional">[</span><em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.logical_invert" title="Permalink to this definition">¶</a></dt>
784
<dd><p><a class="reference internal" href="#decimal.Decimal.logical_invert" title="decimal.Decimal.logical_invert"><code class="xref py py-meth docutils literal"><span class="pre">logical_invert()</span></code></a> is a logical operation. The
785
result is the digit-wise inversion of the operand.</p>
786
<div class="versionadded">
787
<p><span class="versionmodified">New in version 2.6.</span></p>
792
<dt id="decimal.Decimal.logical_or">
793
<code class="descname">logical_or</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.logical_or" title="Permalink to this definition">¶</a></dt>
794
<dd><p><a class="reference internal" href="#decimal.Decimal.logical_or" title="decimal.Decimal.logical_or"><code class="xref py py-meth docutils literal"><span class="pre">logical_or()</span></code></a> is a logical operation which takes two <em>logical
795
operands</em> (see <a class="reference internal" href="#logical-operands-label"><span>Logical operands</span></a>). The result is the
796
digit-wise <code class="docutils literal"><span class="pre">or</span></code> of the two operands.</p>
797
<div class="versionadded">
798
<p><span class="versionmodified">New in version 2.6.</span></p>
803
<dt id="decimal.Decimal.logical_xor">
804
<code class="descname">logical_xor</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.logical_xor" title="Permalink to this definition">¶</a></dt>
805
<dd><p><a class="reference internal" href="#decimal.Decimal.logical_xor" title="decimal.Decimal.logical_xor"><code class="xref py py-meth docutils literal"><span class="pre">logical_xor()</span></code></a> is a logical operation which takes two <em>logical
806
operands</em> (see <a class="reference internal" href="#logical-operands-label"><span>Logical operands</span></a>). The result is the
807
digit-wise exclusive or of the two operands.</p>
808
<div class="versionadded">
809
<p><span class="versionmodified">New in version 2.6.</span></p>
814
<dt id="decimal.Decimal.max">
815
<code class="descname">max</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.max" title="Permalink to this definition">¶</a></dt>
816
<dd><p>Like <code class="docutils literal"><span class="pre">max(self,</span> <span class="pre">other)</span></code> except that the context rounding rule is applied
817
before returning and that <code class="xref py py-const docutils literal"><span class="pre">NaN</span></code> values are either signaled or
818
ignored (depending on the context and whether they are signaling or
823
<dt id="decimal.Decimal.max_mag">
824
<code class="descname">max_mag</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.max_mag" title="Permalink to this definition">¶</a></dt>
825
<dd><p>Similar to the <a class="reference internal" href="#decimal.Decimal.max" title="decimal.Decimal.max"><code class="xref py py-meth docutils literal"><span class="pre">max()</span></code></a> method, but the comparison is done using the
826
absolute values of the operands.</p>
827
<div class="versionadded">
828
<p><span class="versionmodified">New in version 2.6.</span></p>
833
<dt id="decimal.Decimal.min">
834
<code class="descname">min</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.min" title="Permalink to this definition">¶</a></dt>
835
<dd><p>Like <code class="docutils literal"><span class="pre">min(self,</span> <span class="pre">other)</span></code> except that the context rounding rule is applied
836
before returning and that <code class="xref py py-const docutils literal"><span class="pre">NaN</span></code> values are either signaled or
837
ignored (depending on the context and whether they are signaling or
842
<dt id="decimal.Decimal.min_mag">
843
<code class="descname">min_mag</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.min_mag" title="Permalink to this definition">¶</a></dt>
844
<dd><p>Similar to the <a class="reference internal" href="#decimal.Decimal.min" title="decimal.Decimal.min"><code class="xref py py-meth docutils literal"><span class="pre">min()</span></code></a> method, but the comparison is done using the
845
absolute values of the operands.</p>
846
<div class="versionadded">
847
<p><span class="versionmodified">New in version 2.6.</span></p>
852
<dt id="decimal.Decimal.next_minus">
853
<code class="descname">next_minus</code><span class="sig-paren">(</span><span class="optional">[</span><em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.next_minus" title="Permalink to this definition">¶</a></dt>
854
<dd><p>Return the largest number representable in the given context (or in the
855
current thread’s context if no context is given) that is smaller than the
857
<div class="versionadded">
858
<p><span class="versionmodified">New in version 2.6.</span></p>
863
<dt id="decimal.Decimal.next_plus">
864
<code class="descname">next_plus</code><span class="sig-paren">(</span><span class="optional">[</span><em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.next_plus" title="Permalink to this definition">¶</a></dt>
865
<dd><p>Return the smallest number representable in the given context (or in the
866
current thread’s context if no context is given) that is larger than the
868
<div class="versionadded">
869
<p><span class="versionmodified">New in version 2.6.</span></p>
874
<dt id="decimal.Decimal.next_toward">
875
<code class="descname">next_toward</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.next_toward" title="Permalink to this definition">¶</a></dt>
876
<dd><p>If the two operands are unequal, return the number closest to the first
877
operand in the direction of the second operand. If both operands are
878
numerically equal, return a copy of the first operand with the sign set to
879
be the same as the sign of the second operand.</p>
880
<div class="versionadded">
881
<p><span class="versionmodified">New in version 2.6.</span></p>
886
<dt id="decimal.Decimal.normalize">
887
<code class="descname">normalize</code><span class="sig-paren">(</span><span class="optional">[</span><em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.normalize" title="Permalink to this definition">¶</a></dt>
888
<dd><p>Normalize the number by stripping the rightmost trailing zeros and
889
converting any result equal to <code class="xref py py-const docutils literal"><span class="pre">Decimal('0')</span></code> to
890
<code class="xref py py-const docutils literal"><span class="pre">Decimal('0e0')</span></code>. Used for producing canonical values for attributes
891
of an equivalence class. For example, <code class="docutils literal"><span class="pre">Decimal('32.100')</span></code> and
892
<code class="docutils literal"><span class="pre">Decimal('0.321000e+2')</span></code> both normalize to the equivalent value
893
<code class="docutils literal"><span class="pre">Decimal('32.1')</span></code>.</p>
897
<dt id="decimal.Decimal.number_class">
898
<code class="descname">number_class</code><span class="sig-paren">(</span><span class="optional">[</span><em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.number_class" title="Permalink to this definition">¶</a></dt>
899
<dd><p>Return a string describing the <em>class</em> of the operand. The returned value
900
is one of the following ten strings.</p>
902
<li><code class="docutils literal"><span class="pre">"-Infinity"</span></code>, indicating that the operand is negative infinity.</li>
903
<li><code class="docutils literal"><span class="pre">"-Normal"</span></code>, indicating that the operand is a negative normal number.</li>
904
<li><code class="docutils literal"><span class="pre">"-Subnormal"</span></code>, indicating that the operand is negative and subnormal.</li>
905
<li><code class="docutils literal"><span class="pre">"-Zero"</span></code>, indicating that the operand is a negative zero.</li>
906
<li><code class="docutils literal"><span class="pre">"+Zero"</span></code>, indicating that the operand is a positive zero.</li>
907
<li><code class="docutils literal"><span class="pre">"+Subnormal"</span></code>, indicating that the operand is positive and subnormal.</li>
908
<li><code class="docutils literal"><span class="pre">"+Normal"</span></code>, indicating that the operand is a positive normal number.</li>
909
<li><code class="docutils literal"><span class="pre">"+Infinity"</span></code>, indicating that the operand is positive infinity.</li>
910
<li><code class="docutils literal"><span class="pre">"NaN"</span></code>, indicating that the operand is a quiet NaN (Not a Number).</li>
911
<li><code class="docutils literal"><span class="pre">"sNaN"</span></code>, indicating that the operand is a signaling NaN.</li>
913
<div class="versionadded">
914
<p><span class="versionmodified">New in version 2.6.</span></p>
919
<dt id="decimal.Decimal.quantize">
920
<code class="descname">quantize</code><span class="sig-paren">(</span><em>exp</em><span class="optional">[</span>, <em>rounding</em><span class="optional">[</span>, <em>context</em><span class="optional">[</span>, <em>watchexp</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.quantize" title="Permalink to this definition">¶</a></dt>
921
<dd><p>Return a value equal to the first operand after rounding and having the
922
exponent of the second operand.</p>
923
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'1.41421356'</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'1.000'</span><span class="p">))</span>
924
<span class="go">Decimal('1.414')</span>
927
<p>Unlike other operations, if the length of the coefficient after the
928
quantize operation would be greater than precision, then an
929
<a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><code class="xref py py-const docutils literal"><span class="pre">InvalidOperation</span></code></a> is signaled. This guarantees that, unless there
930
is an error condition, the quantized exponent is always equal to that of
931
the right-hand operand.</p>
932
<p>Also unlike other operations, quantize never signals Underflow, even if
933
the result is subnormal and inexact.</p>
934
<p>If the exponent of the second operand is larger than that of the first
935
then rounding may be necessary. In this case, the rounding mode is
936
determined by the <code class="docutils literal"><span class="pre">rounding</span></code> argument if given, else by the given
937
<code class="docutils literal"><span class="pre">context</span></code> argument; if neither argument is given the rounding mode of
938
the current thread’s context is used.</p>
939
<p>If <em>watchexp</em> is set (default), then an error is returned whenever the
940
resulting exponent is greater than <code class="xref py py-attr docutils literal"><span class="pre">Emax</span></code> or less than
941
<code class="xref py py-attr docutils literal"><span class="pre">Etiny</span></code>.</p>
945
<dt id="decimal.Decimal.radix">
946
<code class="descname">radix</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.radix" title="Permalink to this definition">¶</a></dt>
947
<dd><p>Return <code class="docutils literal"><span class="pre">Decimal(10)</span></code>, the radix (base) in which the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a>
948
class does all its arithmetic. Included for compatibility with the
950
<div class="versionadded">
951
<p><span class="versionmodified">New in version 2.6.</span></p>
956
<dt id="decimal.Decimal.remainder_near">
957
<code class="descname">remainder_near</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.remainder_near" title="Permalink to this definition">¶</a></dt>
958
<dd><p>Return the remainder from dividing <em>self</em> by <em>other</em>. This differs from
959
<code class="docutils literal"><span class="pre">self</span> <span class="pre">%</span> <span class="pre">other</span></code> in that the sign of the remainder is chosen so as to
960
minimize its absolute value. More precisely, the return value is
961
<code class="docutils literal"><span class="pre">self</span> <span class="pre">-</span> <span class="pre">n</span> <span class="pre">*</span> <span class="pre">other</span></code> where <code class="docutils literal"><span class="pre">n</span></code> is the integer nearest to the exact
962
value of <code class="docutils literal"><span class="pre">self</span> <span class="pre">/</span> <span class="pre">other</span></code>, and if two integers are equally near then the
963
even one is chosen.</p>
964
<p>If the result is zero then its sign will be the sign of <em>self</em>.</p>
965
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">18</span><span class="p">)</span><span class="o">.</span><span class="n">remainder_near</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
966
<span class="go">Decimal('-2')</span>
967
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span><span class="o">.</span><span class="n">remainder_near</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
968
<span class="go">Decimal('5')</span>
969
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">35</span><span class="p">)</span><span class="o">.</span><span class="n">remainder_near</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
970
<span class="go">Decimal('-5')</span>
976
<dt id="decimal.Decimal.rotate">
977
<code class="descname">rotate</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.rotate" title="Permalink to this definition">¶</a></dt>
978
<dd><p>Return the result of rotating the digits of the first operand by an amount
979
specified by the second operand. The second operand must be an integer in
980
the range -precision through precision. The absolute value of the second
981
operand gives the number of places to rotate. If the second operand is
982
positive then rotation is to the left; otherwise rotation is to the right.
983
The coefficient of the first operand is padded on the left with zeros to
984
length precision if necessary. The sign and exponent of the first operand
986
<div class="versionadded">
987
<p><span class="versionmodified">New in version 2.6.</span></p>
992
<dt id="decimal.Decimal.same_quantum">
993
<code class="descname">same_quantum</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.same_quantum" title="Permalink to this definition">¶</a></dt>
994
<dd><p>Test whether self and other have the same exponent or whether both are
995
<code class="xref py py-const docutils literal"><span class="pre">NaN</span></code>.</p>
999
<dt id="decimal.Decimal.scaleb">
1000
<code class="descname">scaleb</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.scaleb" title="Permalink to this definition">¶</a></dt>
1001
<dd><p>Return the first operand with exponent adjusted by the second.
1002
Equivalently, return the first operand multiplied by <code class="docutils literal"><span class="pre">10**other</span></code>. The
1003
second operand must be an integer.</p>
1004
<div class="versionadded">
1005
<p><span class="versionmodified">New in version 2.6.</span></p>
1010
<dt id="decimal.Decimal.shift">
1011
<code class="descname">shift</code><span class="sig-paren">(</span><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.shift" title="Permalink to this definition">¶</a></dt>
1012
<dd><p>Return the result of shifting the digits of the first operand by an amount
1013
specified by the second operand. The second operand must be an integer in
1014
the range -precision through precision. The absolute value of the second
1015
operand gives the number of places to shift. If the second operand is
1016
positive then the shift is to the left; otherwise the shift is to the
1017
right. Digits shifted into the coefficient are zeros. The sign and
1018
exponent of the first operand are unchanged.</p>
1019
<div class="versionadded">
1020
<p><span class="versionmodified">New in version 2.6.</span></p>
1025
<dt id="decimal.Decimal.sqrt">
1026
<code class="descname">sqrt</code><span class="sig-paren">(</span><span class="optional">[</span><em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.sqrt" title="Permalink to this definition">¶</a></dt>
1027
<dd><p>Return the square root of the argument to full precision.</p>
1031
<dt id="decimal.Decimal.to_eng_string">
1032
<code class="descname">to_eng_string</code><span class="sig-paren">(</span><span class="optional">[</span><em>context</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.to_eng_string" title="Permalink to this definition">¶</a></dt>
1033
<dd><p>Convert to a string, using engineering notation if an exponent is needed.</p>
1034
<p>Engineering notation has an exponent which is a multiple of 3. This
1035
can leave up to 3 digits to the left of the decimal place and may
1036
require the addition of either one or two trailing zeros.</p>
1037
<p>For example, this converts <code class="docutils literal"><span class="pre">Decimal('123E+1')</span></code> to <code class="docutils literal"><span class="pre">Decimal('1.23E+3')</span></code>.</p>
1041
<dt id="decimal.Decimal.to_integral">
1042
<code class="descname">to_integral</code><span class="sig-paren">(</span><span class="optional">[</span><em>rounding</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.to_integral" title="Permalink to this definition">¶</a></dt>
1043
<dd><p>Identical to the <a class="reference internal" href="#decimal.Decimal.to_integral_value" title="decimal.Decimal.to_integral_value"><code class="xref py py-meth docutils literal"><span class="pre">to_integral_value()</span></code></a> method. The <code class="docutils literal"><span class="pre">to_integral</span></code>
1044
name has been kept for compatibility with older versions.</p>
1048
<dt id="decimal.Decimal.to_integral_exact">
1049
<code class="descname">to_integral_exact</code><span class="sig-paren">(</span><span class="optional">[</span><em>rounding</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.to_integral_exact" title="Permalink to this definition">¶</a></dt>
1050
<dd><p>Round to the nearest integer, signaling <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><code class="xref py py-const docutils literal"><span class="pre">Inexact</span></code></a> or
1051
<a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><code class="xref py py-const docutils literal"><span class="pre">Rounded</span></code></a> as appropriate if rounding occurs. The rounding mode is
1052
determined by the <code class="docutils literal"><span class="pre">rounding</span></code> parameter if given, else by the given
1053
<code class="docutils literal"><span class="pre">context</span></code>. If neither parameter is given then the rounding mode of the
1054
current context is used.</p>
1055
<div class="versionadded">
1056
<p><span class="versionmodified">New in version 2.6.</span></p>
1061
<dt id="decimal.Decimal.to_integral_value">
1062
<code class="descname">to_integral_value</code><span class="sig-paren">(</span><span class="optional">[</span><em>rounding</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Decimal.to_integral_value" title="Permalink to this definition">¶</a></dt>
1063
<dd><p>Round to the nearest integer without signaling <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><code class="xref py py-const docutils literal"><span class="pre">Inexact</span></code></a> or
1064
<a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><code class="xref py py-const docutils literal"><span class="pre">Rounded</span></code></a>. If given, applies <em>rounding</em>; otherwise, uses the
1065
rounding method in either the supplied <em>context</em> or the current context.</p>
1066
<div class="versionchanged">
1067
<p><span class="versionmodified">Changed in version 2.6: </span>renamed from <code class="docutils literal"><span class="pre">to_integral</span></code> to <code class="docutils literal"><span class="pre">to_integral_value</span></code>. The old name
1068
remains valid for compatibility.</p>
1074
<div class="section" id="logical-operands">
1075
<span id="logical-operands-label"></span><h3>9.4.2.1. Logical operands<a class="headerlink" href="#logical-operands" title="Permalink to this headline">¶</a></h3>
1076
<p>The <code class="xref py py-meth docutils literal"><span class="pre">logical_and()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">logical_invert()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">logical_or()</span></code>,
1077
and <code class="xref py py-meth docutils literal"><span class="pre">logical_xor()</span></code> methods expect their arguments to be <em>logical
1078
operands</em>. A <em>logical operand</em> is a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instance whose
1079
exponent and sign are both zero, and whose digits are all either
1080
<code class="xref py py-const docutils literal"><span class="pre">0</span></code> or <code class="xref py py-const docutils literal"><span class="pre">1</span></code>.</p>
1083
<div class="section" id="context-objects">
1084
<span id="decimal-context"></span><h2>9.4.3. Context objects<a class="headerlink" href="#context-objects" title="Permalink to this headline">¶</a></h2>
1085
<p>Contexts are environments for arithmetic operations. They govern precision, set
1086
rules for rounding, determine which signals are treated as exceptions, and limit
1087
the range for exponents.</p>
1088
<p>Each thread has its own current context which is accessed or changed using the
1089
<a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><code class="xref py py-func docutils literal"><span class="pre">getcontext()</span></code></a> and <a class="reference internal" href="#decimal.setcontext" title="decimal.setcontext"><code class="xref py py-func docutils literal"><span class="pre">setcontext()</span></code></a> functions:</p>
1090
<dl class="function">
1091
<dt id="decimal.getcontext">
1092
<code class="descclassname">decimal.</code><code class="descname">getcontext</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.getcontext" title="Permalink to this definition">¶</a></dt>
1093
<dd><p>Return the current context for the active thread.</p>
1096
<dl class="function">
1097
<dt id="decimal.setcontext">
1098
<code class="descclassname">decimal.</code><code class="descname">setcontext</code><span class="sig-paren">(</span><em>c</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.setcontext" title="Permalink to this definition">¶</a></dt>
1099
<dd><p>Set the current context for the active thread to <em>c</em>.</p>
1102
<p>Beginning with Python 2.5, you can also use the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a> statement and
1103
the <a class="reference internal" href="#decimal.localcontext" title="decimal.localcontext"><code class="xref py py-func docutils literal"><span class="pre">localcontext()</span></code></a> function to temporarily change the active context.</p>
1104
<dl class="function">
1105
<dt id="decimal.localcontext">
1106
<code class="descclassname">decimal.</code><code class="descname">localcontext</code><span class="sig-paren">(</span><span class="optional">[</span><em>c</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.localcontext" title="Permalink to this definition">¶</a></dt>
1107
<dd><p>Return a context manager that will set the current context for the active thread
1108
to a copy of <em>c</em> on entry to the with-statement and restore the previous context
1109
when exiting the with-statement. If no context is specified, a copy of the
1110
current context is used.</p>
1111
<div class="versionadded">
1112
<p><span class="versionmodified">New in version 2.5.</span></p>
1114
<p>For example, the following code sets the current decimal precision to 42 places,
1115
performs a calculation, and then automatically restores the previous context:</p>
1116
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">decimal</span> <span class="kn">import</span> <span class="n">localcontext</span>
1118
<span class="k">with</span> <span class="n">localcontext</span><span class="p">()</span> <span class="k">as</span> <span class="n">ctx</span><span class="p">:</span>
1119
<span class="n">ctx</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">42</span> <span class="c1"># Perform a high precision calculation</span>
1120
<span class="n">s</span> <span class="o">=</span> <span class="n">calculate_something</span><span class="p">()</span>
1121
<span class="n">s</span> <span class="o">=</span> <span class="o">+</span><span class="n">s</span> <span class="c1"># Round the final result back to the default precision</span>
1123
<span class="k">with</span> <span class="n">localcontext</span><span class="p">(</span><span class="n">BasicContext</span><span class="p">):</span> <span class="c1"># temporarily use the BasicContext</span>
1124
<span class="k">print</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
1125
<span class="k">print</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">355</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">113</span><span class="p">)</span>
1130
<p>New contexts can also be created using the <a class="reference internal" href="#decimal.Context" title="decimal.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a> constructor
1131
described below. In addition, the module provides three pre-made contexts:</p>
1133
<dt id="decimal.BasicContext">
1134
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">BasicContext</code><a class="headerlink" href="#decimal.BasicContext" title="Permalink to this definition">¶</a></dt>
1135
<dd><p>This is a standard context defined by the General Decimal Arithmetic
1136
Specification. Precision is set to nine. Rounding is set to
1137
<code class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_UP</span></code>. All flags are cleared. All traps are enabled (treated
1138
as exceptions) except <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><code class="xref py py-const docutils literal"><span class="pre">Inexact</span></code></a>, <a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><code class="xref py py-const docutils literal"><span class="pre">Rounded</span></code></a>, and
1139
<a class="reference internal" href="#decimal.Subnormal" title="decimal.Subnormal"><code class="xref py py-const docutils literal"><span class="pre">Subnormal</span></code></a>.</p>
1140
<p>Because many of the traps are enabled, this context is useful for debugging.</p>
1144
<dt id="decimal.ExtendedContext">
1145
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">ExtendedContext</code><a class="headerlink" href="#decimal.ExtendedContext" title="Permalink to this definition">¶</a></dt>
1146
<dd><p>This is a standard context defined by the General Decimal Arithmetic
1147
Specification. Precision is set to nine. Rounding is set to
1148
<code class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></code>. All flags are cleared. No traps are enabled (so that
1149
exceptions are not raised during computations).</p>
1150
<p>Because the traps are disabled, this context is useful for applications that
1151
prefer to have result value of <code class="xref py py-const docutils literal"><span class="pre">NaN</span></code> or <code class="xref py py-const docutils literal"><span class="pre">Infinity</span></code> instead of
1152
raising exceptions. This allows an application to complete a run in the
1153
presence of conditions that would otherwise halt the program.</p>
1157
<dt id="decimal.DefaultContext">
1158
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">DefaultContext</code><a class="headerlink" href="#decimal.DefaultContext" title="Permalink to this definition">¶</a></dt>
1159
<dd><p>This context is used by the <a class="reference internal" href="#decimal.Context" title="decimal.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a> constructor as a prototype for new
1160
contexts. Changing a field (such a precision) has the effect of changing the
1161
default for new contexts created by the <a class="reference internal" href="#decimal.Context" title="decimal.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a> constructor.</p>
1162
<p>This context is most useful in multi-threaded environments. Changing one of the
1163
fields before threads are started has the effect of setting system-wide
1164
defaults. Changing the fields after threads have started is not recommended as
1165
it would require thread synchronization to prevent race conditions.</p>
1166
<p>In single threaded environments, it is preferable to not use this context at
1167
all. Instead, simply create contexts explicitly as described below.</p>
1168
<p>The default values are precision=28, rounding=ROUND_HALF_EVEN, and enabled traps
1169
for Overflow, InvalidOperation, and DivisionByZero.</p>
1172
<p>In addition to the three supplied contexts, new contexts can be created with the
1173
<a class="reference internal" href="#decimal.Context" title="decimal.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a> constructor.</p>
1175
<dt id="decimal.Context">
1176
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">Context</code><span class="sig-paren">(</span><em>prec=None</em>, <em>rounding=None</em>, <em>traps=None</em>, <em>flags=None</em>, <em>Emin=None</em>, <em>Emax=None</em>, <em>capitals=1</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context" title="Permalink to this definition">¶</a></dt>
1177
<dd><p>Creates a new context. If a field is not specified or is <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a>, the
1178
default values are copied from the <a class="reference internal" href="#decimal.DefaultContext" title="decimal.DefaultContext"><code class="xref py py-const docutils literal"><span class="pre">DefaultContext</span></code></a>. If the <em>flags</em>
1179
field is not specified or is <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a>, all flags are cleared.</p>
1180
<p>The <em>prec</em> field is a positive integer that sets the precision for arithmetic
1181
operations in the context.</p>
1182
<p>The <em>rounding</em> option is one of:</p>
1184
<li><code class="xref py py-const docutils literal"><span class="pre">ROUND_CEILING</span></code> (towards <code class="xref py py-const docutils literal"><span class="pre">Infinity</span></code>),</li>
1185
<li><code class="xref py py-const docutils literal"><span class="pre">ROUND_DOWN</span></code> (towards zero),</li>
1186
<li><code class="xref py py-const docutils literal"><span class="pre">ROUND_FLOOR</span></code> (towards <code class="xref py py-const docutils literal"><span class="pre">-Infinity</span></code>),</li>
1187
<li><code class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_DOWN</span></code> (to nearest with ties going towards zero),</li>
1188
<li><code class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></code> (to nearest with ties going to nearest even integer),</li>
1189
<li><code class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_UP</span></code> (to nearest with ties going away from zero), or</li>
1190
<li><code class="xref py py-const docutils literal"><span class="pre">ROUND_UP</span></code> (away from zero).</li>
1191
<li><code class="xref py py-const docutils literal"><span class="pre">ROUND_05UP</span></code> (away from zero if last digit after rounding towards zero
1192
would have been 0 or 5; otherwise towards zero)</li>
1194
<p>The <em>traps</em> and <em>flags</em> fields list any signals to be set. Generally, new
1195
contexts should only set traps and leave the flags clear.</p>
1196
<p>The <em>Emin</em> and <em>Emax</em> fields are integers specifying the outer limits allowable
1198
<p>The <em>capitals</em> field is either <code class="xref py py-const docutils literal"><span class="pre">0</span></code> or <code class="xref py py-const docutils literal"><span class="pre">1</span></code> (the default). If set to
1199
<code class="xref py py-const docutils literal"><span class="pre">1</span></code>, exponents are printed with a capital <code class="xref py py-const docutils literal"><span class="pre">E</span></code>; otherwise, a
1200
lowercase <code class="xref py py-const docutils literal"><span class="pre">e</span></code> is used: <code class="xref py py-const docutils literal"><span class="pre">Decimal('6.02e+23')</span></code>.</p>
1201
<div class="versionchanged">
1202
<p><span class="versionmodified">Changed in version 2.6: </span>The <code class="xref py py-const docutils literal"><span class="pre">ROUND_05UP</span></code> rounding mode was added.</p>
1204
<p>The <a class="reference internal" href="#decimal.Context" title="decimal.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a> class defines several general purpose methods as well as
1205
a large number of methods for doing arithmetic directly in a given context.
1206
In addition, for each of the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> methods described above (with
1207
the exception of the <code class="xref py py-meth docutils literal"><span class="pre">adjusted()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">as_tuple()</span></code> methods) there is
1208
a corresponding <a class="reference internal" href="#decimal.Context" title="decimal.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a> method. For example, for a <a class="reference internal" href="#decimal.Context" title="decimal.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a>
1209
instance <code class="docutils literal"><span class="pre">C</span></code> and <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instance <code class="docutils literal"><span class="pre">x</span></code>, <code class="docutils literal"><span class="pre">C.exp(x)</span></code> is
1210
equivalent to <code class="docutils literal"><span class="pre">x.exp(context=C)</span></code>. Each <a class="reference internal" href="#decimal.Context" title="decimal.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a> method accepts a
1211
Python integer (an instance of <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal"><span class="pre">int</span></code></a> or <a class="reference internal" href="functions.html#long" title="long"><code class="xref py py-class docutils literal"><span class="pre">long</span></code></a>) anywhere that a
1212
Decimal instance is accepted.</p>
1214
<dt id="decimal.Context.clear_flags">
1215
<code class="descname">clear_flags</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.clear_flags" title="Permalink to this definition">¶</a></dt>
1216
<dd><p>Resets all of the flags to <code class="xref py py-const docutils literal"><span class="pre">0</span></code>.</p>
1220
<dt id="decimal.Context.copy">
1221
<code class="descname">copy</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.copy" title="Permalink to this definition">¶</a></dt>
1222
<dd><p>Return a duplicate of the context.</p>
1226
<dt id="decimal.Context.copy_decimal">
1227
<code class="descname">copy_decimal</code><span class="sig-paren">(</span><em>num</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.copy_decimal" title="Permalink to this definition">¶</a></dt>
1228
<dd><p>Return a copy of the Decimal instance num.</p>
1232
<dt id="decimal.Context.create_decimal">
1233
<code class="descname">create_decimal</code><span class="sig-paren">(</span><em>num</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.create_decimal" title="Permalink to this definition">¶</a></dt>
1234
<dd><p>Creates a new Decimal instance from <em>num</em> but using <em>self</em> as
1235
context. Unlike the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> constructor, the context precision,
1236
rounding method, flags, and traps are applied to the conversion.</p>
1237
<p>This is useful because constants are often given to a greater precision
1238
than is needed by the application. Another benefit is that rounding
1239
immediately eliminates unintended effects from digits beyond the current
1240
precision. In the following example, using unrounded inputs means that
1241
adding zero to a sum can change the result:</p>
1242
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">3</span>
1243
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'3.4445'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">'1.0023'</span><span class="p">)</span>
1244
<span class="go">Decimal('4.45')</span>
1245
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'3.4445'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">'1.0023'</span><span class="p">)</span>
1246
<span class="go">Decimal('4.44')</span>
1249
<p>This method implements the to-number operation of the IBM specification.
1250
If the argument is a string, no leading or trailing whitespace is
1255
<dt id="decimal.Context.create_decimal_from_float">
1256
<code class="descname">create_decimal_from_float</code><span class="sig-paren">(</span><em>f</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.create_decimal_from_float" title="Permalink to this definition">¶</a></dt>
1257
<dd><p>Creates a new Decimal instance from a float <em>f</em> but rounding using <em>self</em>
1258
as the context. Unlike the <a class="reference internal" href="#decimal.Decimal.from_float" title="decimal.Decimal.from_float"><code class="xref py py-meth docutils literal"><span class="pre">Decimal.from_float()</span></code></a> class method,
1259
the context precision, rounding method, flags, and traps are applied to
1261
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">context</span> <span class="o">=</span> <span class="n">Context</span><span class="p">(</span><span class="n">prec</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="n">ROUND_DOWN</span><span class="p">)</span>
1262
<span class="gp">>>> </span><span class="n">context</span><span class="o">.</span><span class="n">create_decimal_from_float</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span>
1263
<span class="go">Decimal('3.1415')</span>
1264
<span class="gp">>>> </span><span class="n">context</span> <span class="o">=</span> <span class="n">Context</span><span class="p">(</span><span class="n">prec</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">traps</span><span class="o">=</span><span class="p">[</span><span class="n">Inexact</span><span class="p">])</span>
1265
<span class="gp">>>> </span><span class="n">context</span><span class="o">.</span><span class="n">create_decimal_from_float</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span>
1266
<span class="gt">Traceback (most recent call last):</span>
1267
<span class="o">...</span>
1268
<span class="gr">Inexact</span>: <span class="n">None</span>
1271
<div class="versionadded">
1272
<p><span class="versionmodified">New in version 2.7.</span></p>
1277
<dt id="decimal.Context.Etiny">
1278
<code class="descname">Etiny</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.Etiny" title="Permalink to this definition">¶</a></dt>
1279
<dd><p>Returns a value equal to <code class="docutils literal"><span class="pre">Emin</span> <span class="pre">-</span> <span class="pre">prec</span> <span class="pre">+</span> <span class="pre">1</span></code> which is the minimum exponent
1280
value for subnormal results. When underflow occurs, the exponent is set
1281
to <a class="reference internal" href="#decimal.Context.Etiny" title="decimal.Context.Etiny"><code class="xref py py-const docutils literal"><span class="pre">Etiny</span></code></a>.</p>
1285
<dt id="decimal.Context.Etop">
1286
<code class="descname">Etop</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.Etop" title="Permalink to this definition">¶</a></dt>
1287
<dd><p>Returns a value equal to <code class="docutils literal"><span class="pre">Emax</span> <span class="pre">-</span> <span class="pre">prec</span> <span class="pre">+</span> <span class="pre">1</span></code>.</p>
1290
<p>The usual approach to working with decimals is to create <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a>
1291
instances and then apply arithmetic operations which take place within the
1292
current context for the active thread. An alternative approach is to use
1293
context methods for calculating within a specific context. The methods are
1294
similar to those for the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> class and are only briefly
1297
<dt id="decimal.Context.abs">
1298
<code class="descname">abs</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.abs" title="Permalink to this definition">¶</a></dt>
1299
<dd><p>Returns the absolute value of <em>x</em>.</p>
1303
<dt id="decimal.Context.add">
1304
<code class="descname">add</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.add" title="Permalink to this definition">¶</a></dt>
1305
<dd><p>Return the sum of <em>x</em> and <em>y</em>.</p>
1309
<dt id="decimal.Context.canonical">
1310
<code class="descname">canonical</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.canonical" title="Permalink to this definition">¶</a></dt>
1311
<dd><p>Returns the same Decimal object <em>x</em>.</p>
1315
<dt id="decimal.Context.compare">
1316
<code class="descname">compare</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.compare" title="Permalink to this definition">¶</a></dt>
1317
<dd><p>Compares <em>x</em> and <em>y</em> numerically.</p>
1321
<dt id="decimal.Context.compare_signal">
1322
<code class="descname">compare_signal</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.compare_signal" title="Permalink to this definition">¶</a></dt>
1323
<dd><p>Compares the values of the two operands numerically.</p>
1327
<dt id="decimal.Context.compare_total">
1328
<code class="descname">compare_total</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.compare_total" title="Permalink to this definition">¶</a></dt>
1329
<dd><p>Compares two operands using their abstract representation.</p>
1333
<dt id="decimal.Context.compare_total_mag">
1334
<code class="descname">compare_total_mag</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.compare_total_mag" title="Permalink to this definition">¶</a></dt>
1335
<dd><p>Compares two operands using their abstract representation, ignoring sign.</p>
1339
<dt id="decimal.Context.copy_abs">
1340
<code class="descname">copy_abs</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.copy_abs" title="Permalink to this definition">¶</a></dt>
1341
<dd><p>Returns a copy of <em>x</em> with the sign set to 0.</p>
1345
<dt id="decimal.Context.copy_negate">
1346
<code class="descname">copy_negate</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.copy_negate" title="Permalink to this definition">¶</a></dt>
1347
<dd><p>Returns a copy of <em>x</em> with the sign inverted.</p>
1351
<dt id="decimal.Context.copy_sign">
1352
<code class="descname">copy_sign</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.copy_sign" title="Permalink to this definition">¶</a></dt>
1353
<dd><p>Copies the sign from <em>y</em> to <em>x</em>.</p>
1357
<dt id="decimal.Context.divide">
1358
<code class="descname">divide</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.divide" title="Permalink to this definition">¶</a></dt>
1359
<dd><p>Return <em>x</em> divided by <em>y</em>.</p>
1363
<dt id="decimal.Context.divide_int">
1364
<code class="descname">divide_int</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.divide_int" title="Permalink to this definition">¶</a></dt>
1365
<dd><p>Return <em>x</em> divided by <em>y</em>, truncated to an integer.</p>
1369
<dt id="decimal.Context.divmod">
1370
<code class="descname">divmod</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.divmod" title="Permalink to this definition">¶</a></dt>
1371
<dd><p>Divides two numbers and returns the integer part of the result.</p>
1375
<dt id="decimal.Context.exp">
1376
<code class="descname">exp</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.exp" title="Permalink to this definition">¶</a></dt>
1377
<dd><p>Returns <cite>e ** x</cite>.</p>
1381
<dt id="decimal.Context.fma">
1382
<code class="descname">fma</code><span class="sig-paren">(</span><em>x</em>, <em>y</em>, <em>z</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.fma" title="Permalink to this definition">¶</a></dt>
1383
<dd><p>Returns <em>x</em> multiplied by <em>y</em>, plus <em>z</em>.</p>
1387
<dt id="decimal.Context.is_canonical">
1388
<code class="descname">is_canonical</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.is_canonical" title="Permalink to this definition">¶</a></dt>
1389
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if <em>x</em> is canonical; otherwise returns <code class="docutils literal"><span class="pre">False</span></code>.</p>
1393
<dt id="decimal.Context.is_finite">
1394
<code class="descname">is_finite</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.is_finite" title="Permalink to this definition">¶</a></dt>
1395
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if <em>x</em> is finite; otherwise returns <code class="docutils literal"><span class="pre">False</span></code>.</p>
1399
<dt id="decimal.Context.is_infinite">
1400
<code class="descname">is_infinite</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.is_infinite" title="Permalink to this definition">¶</a></dt>
1401
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if <em>x</em> is infinite; otherwise returns <code class="docutils literal"><span class="pre">False</span></code>.</p>
1405
<dt id="decimal.Context.is_nan">
1406
<code class="descname">is_nan</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.is_nan" title="Permalink to this definition">¶</a></dt>
1407
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if <em>x</em> is a qNaN or sNaN; otherwise returns <code class="docutils literal"><span class="pre">False</span></code>.</p>
1411
<dt id="decimal.Context.is_normal">
1412
<code class="descname">is_normal</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.is_normal" title="Permalink to this definition">¶</a></dt>
1413
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if <em>x</em> is a normal number; otherwise returns <code class="docutils literal"><span class="pre">False</span></code>.</p>
1417
<dt id="decimal.Context.is_qnan">
1418
<code class="descname">is_qnan</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.is_qnan" title="Permalink to this definition">¶</a></dt>
1419
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if <em>x</em> is a quiet NaN; otherwise returns <code class="docutils literal"><span class="pre">False</span></code>.</p>
1423
<dt id="decimal.Context.is_signed">
1424
<code class="descname">is_signed</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.is_signed" title="Permalink to this definition">¶</a></dt>
1425
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if <em>x</em> is negative; otherwise returns <code class="docutils literal"><span class="pre">False</span></code>.</p>
1429
<dt id="decimal.Context.is_snan">
1430
<code class="descname">is_snan</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.is_snan" title="Permalink to this definition">¶</a></dt>
1431
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if <em>x</em> is a signaling NaN; otherwise returns <code class="docutils literal"><span class="pre">False</span></code>.</p>
1435
<dt id="decimal.Context.is_subnormal">
1436
<code class="descname">is_subnormal</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.is_subnormal" title="Permalink to this definition">¶</a></dt>
1437
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if <em>x</em> is subnormal; otherwise returns <code class="docutils literal"><span class="pre">False</span></code>.</p>
1441
<dt id="decimal.Context.is_zero">
1442
<code class="descname">is_zero</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.is_zero" title="Permalink to this definition">¶</a></dt>
1443
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if <em>x</em> is a zero; otherwise returns <code class="docutils literal"><span class="pre">False</span></code>.</p>
1447
<dt id="decimal.Context.ln">
1448
<code class="descname">ln</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.ln" title="Permalink to this definition">¶</a></dt>
1449
<dd><p>Returns the natural (base e) logarithm of <em>x</em>.</p>
1453
<dt id="decimal.Context.log10">
1454
<code class="descname">log10</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.log10" title="Permalink to this definition">¶</a></dt>
1455
<dd><p>Returns the base 10 logarithm of <em>x</em>.</p>
1459
<dt id="decimal.Context.logb">
1460
<code class="descname">logb</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.logb" title="Permalink to this definition">¶</a></dt>
1461
<dd><p>Returns the exponent of the magnitude of the operand’s MSD.</p>
1465
<dt id="decimal.Context.logical_and">
1466
<code class="descname">logical_and</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.logical_and" title="Permalink to this definition">¶</a></dt>
1467
<dd><p>Applies the logical operation <em>and</em> between each operand’s digits.</p>
1471
<dt id="decimal.Context.logical_invert">
1472
<code class="descname">logical_invert</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.logical_invert" title="Permalink to this definition">¶</a></dt>
1473
<dd><p>Invert all the digits in <em>x</em>.</p>
1477
<dt id="decimal.Context.logical_or">
1478
<code class="descname">logical_or</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.logical_or" title="Permalink to this definition">¶</a></dt>
1479
<dd><p>Applies the logical operation <em>or</em> between each operand’s digits.</p>
1483
<dt id="decimal.Context.logical_xor">
1484
<code class="descname">logical_xor</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.logical_xor" title="Permalink to this definition">¶</a></dt>
1485
<dd><p>Applies the logical operation <em>xor</em> between each operand’s digits.</p>
1489
<dt id="decimal.Context.max">
1490
<code class="descname">max</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.max" title="Permalink to this definition">¶</a></dt>
1491
<dd><p>Compares two values numerically and returns the maximum.</p>
1495
<dt id="decimal.Context.max_mag">
1496
<code class="descname">max_mag</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.max_mag" title="Permalink to this definition">¶</a></dt>
1497
<dd><p>Compares the values numerically with their sign ignored.</p>
1501
<dt id="decimal.Context.min">
1502
<code class="descname">min</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.min" title="Permalink to this definition">¶</a></dt>
1503
<dd><p>Compares two values numerically and returns the minimum.</p>
1507
<dt id="decimal.Context.min_mag">
1508
<code class="descname">min_mag</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.min_mag" title="Permalink to this definition">¶</a></dt>
1509
<dd><p>Compares the values numerically with their sign ignored.</p>
1513
<dt id="decimal.Context.minus">
1514
<code class="descname">minus</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.minus" title="Permalink to this definition">¶</a></dt>
1515
<dd><p>Minus corresponds to the unary prefix minus operator in Python.</p>
1519
<dt id="decimal.Context.multiply">
1520
<code class="descname">multiply</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.multiply" title="Permalink to this definition">¶</a></dt>
1521
<dd><p>Return the product of <em>x</em> and <em>y</em>.</p>
1525
<dt id="decimal.Context.next_minus">
1526
<code class="descname">next_minus</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.next_minus" title="Permalink to this definition">¶</a></dt>
1527
<dd><p>Returns the largest representable number smaller than <em>x</em>.</p>
1531
<dt id="decimal.Context.next_plus">
1532
<code class="descname">next_plus</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.next_plus" title="Permalink to this definition">¶</a></dt>
1533
<dd><p>Returns the smallest representable number larger than <em>x</em>.</p>
1537
<dt id="decimal.Context.next_toward">
1538
<code class="descname">next_toward</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.next_toward" title="Permalink to this definition">¶</a></dt>
1539
<dd><p>Returns the number closest to <em>x</em>, in direction towards <em>y</em>.</p>
1543
<dt id="decimal.Context.normalize">
1544
<code class="descname">normalize</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.normalize" title="Permalink to this definition">¶</a></dt>
1545
<dd><p>Reduces <em>x</em> to its simplest form.</p>
1549
<dt id="decimal.Context.number_class">
1550
<code class="descname">number_class</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.number_class" title="Permalink to this definition">¶</a></dt>
1551
<dd><p>Returns an indication of the class of <em>x</em>.</p>
1555
<dt id="decimal.Context.plus">
1556
<code class="descname">plus</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.plus" title="Permalink to this definition">¶</a></dt>
1557
<dd><p>Plus corresponds to the unary prefix plus operator in Python. This
1558
operation applies the context precision and rounding, so it is <em>not</em> an
1559
identity operation.</p>
1563
<dt id="decimal.Context.power">
1564
<code class="descname">power</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="optional">[</span>, <em>modulo</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.power" title="Permalink to this definition">¶</a></dt>
1565
<dd><p>Return <code class="docutils literal"><span class="pre">x</span></code> to the power of <code class="docutils literal"><span class="pre">y</span></code>, reduced modulo <code class="docutils literal"><span class="pre">modulo</span></code> if given.</p>
1566
<p>With two arguments, compute <code class="docutils literal"><span class="pre">x**y</span></code>. If <code class="docutils literal"><span class="pre">x</span></code> is negative then <code class="docutils literal"><span class="pre">y</span></code>
1567
must be integral. The result will be inexact unless <code class="docutils literal"><span class="pre">y</span></code> is integral and
1568
the result is finite and can be expressed exactly in ‘precision’ digits.
1569
The result should always be correctly rounded, using the rounding mode of
1570
the current thread’s context.</p>
1571
<p>With three arguments, compute <code class="docutils literal"><span class="pre">(x**y)</span> <span class="pre">%</span> <span class="pre">modulo</span></code>. For the three argument
1572
form, the following restrictions on the arguments hold:</p>
1574
<div><ul class="simple">
1575
<li>all three arguments must be integral</li>
1576
<li><code class="docutils literal"><span class="pre">y</span></code> must be nonnegative</li>
1577
<li>at least one of <code class="docutils literal"><span class="pre">x</span></code> or <code class="docutils literal"><span class="pre">y</span></code> must be nonzero</li>
1578
<li><code class="docutils literal"><span class="pre">modulo</span></code> must be nonzero and have at most ‘precision’ digits</li>
1581
<p>The value resulting from <code class="docutils literal"><span class="pre">Context.power(x,</span> <span class="pre">y,</span> <span class="pre">modulo)</span></code> is
1582
equal to the value that would be obtained by computing <code class="docutils literal"><span class="pre">(x**y)</span>
1583
<span class="pre">%</span> <span class="pre">modulo</span></code> with unbounded precision, but is computed more
1584
efficiently. The exponent of the result is zero, regardless of
1585
the exponents of <code class="docutils literal"><span class="pre">x</span></code>, <code class="docutils literal"><span class="pre">y</span></code> and <code class="docutils literal"><span class="pre">modulo</span></code>. The result is
1587
<div class="versionchanged">
1588
<p><span class="versionmodified">Changed in version 2.6: </span><code class="docutils literal"><span class="pre">y</span></code> may now be nonintegral in <code class="docutils literal"><span class="pre">x**y</span></code>.
1589
Stricter requirements for the three-argument version.</p>
1594
<dt id="decimal.Context.quantize">
1595
<code class="descname">quantize</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.quantize" title="Permalink to this definition">¶</a></dt>
1596
<dd><p>Returns a value equal to <em>x</em> (rounded), having the exponent of <em>y</em>.</p>
1600
<dt id="decimal.Context.radix">
1601
<code class="descname">radix</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.radix" title="Permalink to this definition">¶</a></dt>
1602
<dd><p>Just returns 10, as this is Decimal, :)</p>
1606
<dt id="decimal.Context.remainder">
1607
<code class="descname">remainder</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.remainder" title="Permalink to this definition">¶</a></dt>
1608
<dd><p>Returns the remainder from integer division.</p>
1609
<p>The sign of the result, if non-zero, is the same as that of the original
1614
<dt id="decimal.Context.remainder_near">
1615
<code class="descname">remainder_near</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.remainder_near" title="Permalink to this definition">¶</a></dt>
1616
<dd><p>Returns <code class="docutils literal"><span class="pre">x</span> <span class="pre">-</span> <span class="pre">y</span> <span class="pre">*</span> <span class="pre">n</span></code>, where <em>n</em> is the integer nearest the exact value
1617
of <code class="docutils literal"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></code> (if the result is 0 then its sign will be the sign of <em>x</em>).</p>
1621
<dt id="decimal.Context.rotate">
1622
<code class="descname">rotate</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.rotate" title="Permalink to this definition">¶</a></dt>
1623
<dd><p>Returns a rotated copy of <em>x</em>, <em>y</em> times.</p>
1627
<dt id="decimal.Context.same_quantum">
1628
<code class="descname">same_quantum</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.same_quantum" title="Permalink to this definition">¶</a></dt>
1629
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if the two operands have the same exponent.</p>
1633
<dt id="decimal.Context.scaleb">
1634
<code class="descname">scaleb</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.scaleb" title="Permalink to this definition">¶</a></dt>
1635
<dd><p>Returns the first operand after adding the second value its exp.</p>
1639
<dt id="decimal.Context.shift">
1640
<code class="descname">shift</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.shift" title="Permalink to this definition">¶</a></dt>
1641
<dd><p>Returns a shifted copy of <em>x</em>, <em>y</em> times.</p>
1645
<dt id="decimal.Context.sqrt">
1646
<code class="descname">sqrt</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.sqrt" title="Permalink to this definition">¶</a></dt>
1647
<dd><p>Square root of a non-negative number to context precision.</p>
1651
<dt id="decimal.Context.subtract">
1652
<code class="descname">subtract</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.subtract" title="Permalink to this definition">¶</a></dt>
1653
<dd><p>Return the difference between <em>x</em> and <em>y</em>.</p>
1657
<dt id="decimal.Context.to_eng_string">
1658
<code class="descname">to_eng_string</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.to_eng_string" title="Permalink to this definition">¶</a></dt>
1659
<dd><p>Convert to a string, using engineering notation if an exponent is needed.</p>
1660
<p>Engineering notation has an exponent which is a multiple of 3. This
1661
can leave up to 3 digits to the left of the decimal place and may
1662
require the addition of either one or two trailing zeros.</p>
1666
<dt id="decimal.Context.to_integral_exact">
1667
<code class="descname">to_integral_exact</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.to_integral_exact" title="Permalink to this definition">¶</a></dt>
1668
<dd><p>Rounds to an integer.</p>
1672
<dt id="decimal.Context.to_sci_string">
1673
<code class="descname">to_sci_string</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#decimal.Context.to_sci_string" title="Permalink to this definition">¶</a></dt>
1674
<dd><p>Converts a number to a string using scientific notation.</p>
1680
<div class="section" id="signals">
1681
<span id="decimal-signals"></span><h2>9.4.4. Signals<a class="headerlink" href="#signals" title="Permalink to this headline">¶</a></h2>
1682
<p>Signals represent conditions that arise during computation. Each corresponds to
1683
one context flag and one context trap enabler.</p>
1684
<p>The context flag is set whenever the condition is encountered. After the
1685
computation, flags may be checked for informational purposes (for instance, to
1686
determine whether a computation was exact). After checking the flags, be sure to
1687
clear all flags before starting the next computation.</p>
1688
<p>If the context’s trap enabler is set for the signal, then the condition causes a
1689
Python exception to be raised. For example, if the <a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><code class="xref py py-class docutils literal"><span class="pre">DivisionByZero</span></code></a> trap
1690
is set, then a <a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><code class="xref py py-exc docutils literal"><span class="pre">DivisionByZero</span></code></a> exception is raised upon encountering the
1693
<dt id="decimal.Clamped">
1694
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">Clamped</code><a class="headerlink" href="#decimal.Clamped" title="Permalink to this definition">¶</a></dt>
1695
<dd><p>Altered an exponent to fit representation constraints.</p>
1696
<p>Typically, clamping occurs when an exponent falls outside the context’s
1697
<code class="xref py py-attr docutils literal"><span class="pre">Emin</span></code> and <code class="xref py py-attr docutils literal"><span class="pre">Emax</span></code> limits. If possible, the exponent is reduced to
1698
fit by adding zeros to the coefficient.</p>
1702
<dt id="decimal.DecimalException">
1703
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">DecimalException</code><a class="headerlink" href="#decimal.DecimalException" title="Permalink to this definition">¶</a></dt>
1704
<dd><p>Base class for other signals and a subclass of <a class="reference internal" href="exceptions.html#exceptions.ArithmeticError" title="exceptions.ArithmeticError"><code class="xref py py-exc docutils literal"><span class="pre">ArithmeticError</span></code></a>.</p>
1708
<dt id="decimal.DivisionByZero">
1709
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">DivisionByZero</code><a class="headerlink" href="#decimal.DivisionByZero" title="Permalink to this definition">¶</a></dt>
1710
<dd><p>Signals the division of a non-infinite number by zero.</p>
1711
<p>Can occur with division, modulo division, or when raising a number to a negative
1712
power. If this signal is not trapped, returns <code class="xref py py-const docutils literal"><span class="pre">Infinity</span></code> or
1713
<code class="xref py py-const docutils literal"><span class="pre">-Infinity</span></code> with the sign determined by the inputs to the calculation.</p>
1717
<dt id="decimal.Inexact">
1718
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">Inexact</code><a class="headerlink" href="#decimal.Inexact" title="Permalink to this definition">¶</a></dt>
1719
<dd><p>Indicates that rounding occurred and the result is not exact.</p>
1720
<p>Signals when non-zero digits were discarded during rounding. The rounded result
1721
is returned. The signal flag or trap is used to detect when results are
1726
<dt id="decimal.InvalidOperation">
1727
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">InvalidOperation</code><a class="headerlink" href="#decimal.InvalidOperation" title="Permalink to this definition">¶</a></dt>
1728
<dd><p>An invalid operation was performed.</p>
1729
<p>Indicates that an operation was requested that does not make sense. If not
1730
trapped, returns <code class="xref py py-const docutils literal"><span class="pre">NaN</span></code>. Possible causes include:</p>
1731
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">Infinity</span> <span class="o">-</span> <span class="n">Infinity</span>
1732
<span class="mi">0</span> <span class="o">*</span> <span class="n">Infinity</span>
1733
<span class="n">Infinity</span> <span class="o">/</span> <span class="n">Infinity</span>
1734
<span class="n">x</span> <span class="o">%</span> <span class="mi">0</span>
1735
<span class="n">Infinity</span> <span class="o">%</span> <span class="n">x</span>
1736
<span class="n">x</span><span class="o">.</span><span class="n">_rescale</span><span class="p">(</span> <span class="n">non</span><span class="o">-</span><span class="n">integer</span> <span class="p">)</span>
1737
<span class="n">sqrt</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">)</span> <span class="ow">and</span> <span class="n">x</span> <span class="o">></span> <span class="mi">0</span>
1738
<span class="mi">0</span> <span class="o">**</span> <span class="mi">0</span>
1739
<span class="n">x</span> <span class="o">**</span> <span class="p">(</span><span class="n">non</span><span class="o">-</span><span class="n">integer</span><span class="p">)</span>
1740
<span class="n">x</span> <span class="o">**</span> <span class="n">Infinity</span>
1746
<dt id="decimal.Overflow">
1747
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">Overflow</code><a class="headerlink" href="#decimal.Overflow" title="Permalink to this definition">¶</a></dt>
1748
<dd><p>Numerical overflow.</p>
1749
<p>Indicates the exponent is larger than <code class="xref py py-attr docutils literal"><span class="pre">Emax</span></code> after rounding has
1750
occurred. If not trapped, the result depends on the rounding mode, either
1751
pulling inward to the largest representable finite number or rounding outward
1752
to <code class="xref py py-const docutils literal"><span class="pre">Infinity</span></code>. In either case, <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><code class="xref py py-class docutils literal"><span class="pre">Inexact</span></code></a> and <a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><code class="xref py py-class docutils literal"><span class="pre">Rounded</span></code></a>
1753
are also signaled.</p>
1757
<dt id="decimal.Rounded">
1758
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">Rounded</code><a class="headerlink" href="#decimal.Rounded" title="Permalink to this definition">¶</a></dt>
1759
<dd><p>Rounding occurred though possibly no information was lost.</p>
1760
<p>Signaled whenever rounding discards digits; even if those digits are zero
1761
(such as rounding <code class="xref py py-const docutils literal"><span class="pre">5.00</span></code> to <code class="xref py py-const docutils literal"><span class="pre">5.0</span></code>). If not trapped, returns
1762
the result unchanged. This signal is used to detect loss of significant
1767
<dt id="decimal.Subnormal">
1768
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">Subnormal</code><a class="headerlink" href="#decimal.Subnormal" title="Permalink to this definition">¶</a></dt>
1769
<dd><p>Exponent was lower than <code class="xref py py-attr docutils literal"><span class="pre">Emin</span></code> prior to rounding.</p>
1770
<p>Occurs when an operation result is subnormal (the exponent is too small). If
1771
not trapped, returns the result unchanged.</p>
1775
<dt id="decimal.Underflow">
1776
<em class="property">class </em><code class="descclassname">decimal.</code><code class="descname">Underflow</code><a class="headerlink" href="#decimal.Underflow" title="Permalink to this definition">¶</a></dt>
1777
<dd><p>Numerical underflow with result rounded to zero.</p>
1778
<p>Occurs when a subnormal result is pushed to zero by rounding. <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><code class="xref py py-class docutils literal"><span class="pre">Inexact</span></code></a>
1779
and <a class="reference internal" href="#decimal.Subnormal" title="decimal.Subnormal"><code class="xref py py-class docutils literal"><span class="pre">Subnormal</span></code></a> are also signaled.</p>
1782
<p>The following table summarizes the hierarchy of signals:</p>
1783
<div class="highlight-python"><div class="highlight"><pre><span></span>exceptions.ArithmeticError(exceptions.StandardError)
1786
DivisionByZero(DecimalException, exceptions.ZeroDivisionError)
1788
Overflow(Inexact, Rounded)
1789
Underflow(Inexact, Rounded, Subnormal)
1796
<div class="section" id="floating-point-notes">
1797
<span id="decimal-notes"></span><h2>9.4.5. Floating Point Notes<a class="headerlink" href="#floating-point-notes" title="Permalink to this headline">¶</a></h2>
1798
<div class="section" id="mitigating-round-off-error-with-increased-precision">
1799
<h3>9.4.5.1. Mitigating round-off error with increased precision<a class="headerlink" href="#mitigating-round-off-error-with-increased-precision" title="Permalink to this headline">¶</a></h3>
1800
<p>The use of decimal floating point eliminates decimal representation error
1801
(making it possible to represent <code class="xref py py-const docutils literal"><span class="pre">0.1</span></code> exactly); however, some operations
1802
can still incur round-off error when non-zero digits exceed the fixed precision.</p>
1803
<p>The effects of round-off error can be amplified by the addition or subtraction
1804
of nearly offsetting quantities resulting in loss of significance. Knuth
1805
provides two instructive examples where rounded floating point arithmetic with
1806
insufficient precision causes the breakdown of the associative and distributive
1807
properties of addition:</p>
1808
<div class="highlight-python"><div class="highlight"><pre><span></span># Examples from Seminumerical Algorithms, Section 4.2.2.
1809
>>> from decimal import Decimal, getcontext
1810
>>> getcontext().prec = 8
1812
>>> u, v, w = Decimal(11111113), Decimal(-11111111), Decimal('7.51111111')
1813
>>> (u + v) + w
1814
Decimal('9.5111111')
1815
>>> u + (v + w)
1816
Decimal('10')
1818
>>> u, v, w = Decimal(20000), Decimal(-6), Decimal('6.0000003')
1819
>>> (u*v) + (u*w)
1820
Decimal('0.01')
1821
>>> u * (v+w)
1822
Decimal('0.0060000')
1825
<p>The <a class="reference internal" href="#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal"><span class="pre">decimal</span></code></a> module makes it possible to restore the identities by
1826
expanding the precision sufficiently to avoid loss of significance:</p>
1827
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">20</span>
1828
<span class="gp">>>> </span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">11111113</span><span class="p">),</span> <span class="n">Decimal</span><span class="p">(</span><span class="o">-</span><span class="mi">11111111</span><span class="p">),</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">'7.51111111'</span><span class="p">)</span>
1829
<span class="gp">>>> </span><span class="p">(</span><span class="n">u</span> <span class="o">+</span> <span class="n">v</span><span class="p">)</span> <span class="o">+</span> <span class="n">w</span>
1830
<span class="go">Decimal('9.51111111')</span>
1831
<span class="gp">>>> </span><span class="n">u</span> <span class="o">+</span> <span class="p">(</span><span class="n">v</span> <span class="o">+</span> <span class="n">w</span><span class="p">)</span>
1832
<span class="go">Decimal('9.51111111')</span>
1833
<span class="go">>>></span>
1834
<span class="gp">>>> </span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">20000</span><span class="p">),</span> <span class="n">Decimal</span><span class="p">(</span><span class="o">-</span><span class="mi">6</span><span class="p">),</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">'6.0000003'</span><span class="p">)</span>
1835
<span class="gp">>>> </span><span class="p">(</span><span class="n">u</span><span class="o">*</span><span class="n">v</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">u</span><span class="o">*</span><span class="n">w</span><span class="p">)</span>
1836
<span class="go">Decimal('0.0060000')</span>
1837
<span class="gp">>>> </span><span class="n">u</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span><span class="o">+</span><span class="n">w</span><span class="p">)</span>
1838
<span class="go">Decimal('0.0060000')</span>
1842
<div class="section" id="special-values">
1843
<h3>9.4.5.2. Special values<a class="headerlink" href="#special-values" title="Permalink to this headline">¶</a></h3>
1844
<p>The number system for the <a class="reference internal" href="#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal"><span class="pre">decimal</span></code></a> module provides special values
1845
including <code class="xref py py-const docutils literal"><span class="pre">NaN</span></code>, <code class="xref py py-const docutils literal"><span class="pre">sNaN</span></code>, <code class="xref py py-const docutils literal"><span class="pre">-Infinity</span></code>, <code class="xref py py-const docutils literal"><span class="pre">Infinity</span></code>,
1846
and two zeros, <code class="xref py py-const docutils literal"><span class="pre">+0</span></code> and <code class="xref py py-const docutils literal"><span class="pre">-0</span></code>.</p>
1847
<p>Infinities can be constructed directly with: <code class="docutils literal"><span class="pre">Decimal('Infinity')</span></code>. Also,
1848
they can arise from dividing by zero when the <a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><code class="xref py py-exc docutils literal"><span class="pre">DivisionByZero</span></code></a> signal is
1849
not trapped. Likewise, when the <a class="reference internal" href="#decimal.Overflow" title="decimal.Overflow"><code class="xref py py-exc docutils literal"><span class="pre">Overflow</span></code></a> signal is not trapped, infinity
1850
can result from rounding beyond the limits of the largest representable number.</p>
1851
<p>The infinities are signed (affine) and can be used in arithmetic operations
1852
where they get treated as very large, indeterminate numbers. For instance,
1853
adding a constant to infinity gives another infinite result.</p>
1854
<p>Some operations are indeterminate and return <code class="xref py py-const docutils literal"><span class="pre">NaN</span></code>, or if the
1855
<a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><code class="xref py py-exc docutils literal"><span class="pre">InvalidOperation</span></code></a> signal is trapped, raise an exception. For example,
1856
<code class="docutils literal"><span class="pre">0/0</span></code> returns <code class="xref py py-const docutils literal"><span class="pre">NaN</span></code> which means “not a number”. This variety of
1857
<code class="xref py py-const docutils literal"><span class="pre">NaN</span></code> is quiet and, once created, will flow through other computations
1858
always resulting in another <code class="xref py py-const docutils literal"><span class="pre">NaN</span></code>. This behavior can be useful for a
1859
series of computations that occasionally have missing inputs — it allows the
1860
calculation to proceed while flagging specific results as invalid.</p>
1861
<p>A variant is <code class="xref py py-const docutils literal"><span class="pre">sNaN</span></code> which signals rather than remaining quiet after every
1862
operation. This is a useful return value when an invalid result needs to
1863
interrupt a calculation for special handling.</p>
1864
<p>The behavior of Python’s comparison operators can be a little surprising where a
1865
<code class="xref py py-const docutils literal"><span class="pre">NaN</span></code> is involved. A test for equality where one of the operands is a
1866
quiet or signaling <code class="xref py py-const docutils literal"><span class="pre">NaN</span></code> always returns <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> (even when doing
1867
<code class="docutils literal"><span class="pre">Decimal('NaN')==Decimal('NaN')</span></code>), while a test for inequality always returns
1868
<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>. An attempt to compare two Decimals using any of the <code class="docutils literal"><span class="pre"><</span></code>,
1869
<code class="docutils literal"><span class="pre"><=</span></code>, <code class="docutils literal"><span class="pre">></span></code> or <code class="docutils literal"><span class="pre">>=</span></code> operators will raise the <a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><code class="xref py py-exc docutils literal"><span class="pre">InvalidOperation</span></code></a> signal
1870
if either operand is a <code class="xref py py-const docutils literal"><span class="pre">NaN</span></code>, and return <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> if this signal is
1871
not trapped. Note that the General Decimal Arithmetic specification does not
1872
specify the behavior of direct comparisons; these rules for comparisons
1873
involving a <code class="xref py py-const docutils literal"><span class="pre">NaN</span></code> were taken from the IEEE 854 standard (see Table 3 in
1874
section 5.7). To ensure strict standards-compliance, use the <code class="xref py py-meth docutils literal"><span class="pre">compare()</span></code>
1875
and <code class="xref py py-meth docutils literal"><span class="pre">compare-signal()</span></code> methods instead.</p>
1876
<p>The signed zeros can result from calculations that underflow. They keep the sign
1877
that would have resulted if the calculation had been carried out to greater
1878
precision. Since their magnitude is zero, both positive and negative zeros are
1879
treated as equal and their sign is informational.</p>
1880
<p>In addition to the two signed zeros which are distinct yet equal, there are
1881
various representations of zero with differing precisions yet equivalent in
1882
value. This takes a bit of getting used to. For an eye accustomed to
1883
normalized floating point representations, it is not immediately obvious that
1884
the following calculation returns a value equal to zero:</p>
1885
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mi">1</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">'Infinity'</span><span class="p">)</span>
1886
<span class="go">Decimal('0E-1000000026')</span>
1891
<div class="section" id="working-with-threads">
1892
<span id="decimal-threads"></span><h2>9.4.6. Working with threads<a class="headerlink" href="#working-with-threads" title="Permalink to this headline">¶</a></h2>
1893
<p>The <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><code class="xref py py-func docutils literal"><span class="pre">getcontext()</span></code></a> function accesses a different <a class="reference internal" href="#decimal.Context" title="decimal.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a> object for
1894
each thread. Having separate thread contexts means that threads may make
1895
changes (such as <code class="docutils literal"><span class="pre">getcontext.prec=10</span></code>) without interfering with other threads.</p>
1896
<p>Likewise, the <a class="reference internal" href="#decimal.setcontext" title="decimal.setcontext"><code class="xref py py-func docutils literal"><span class="pre">setcontext()</span></code></a> function automatically assigns its target to
1897
the current thread.</p>
1898
<p>If <a class="reference internal" href="#decimal.setcontext" title="decimal.setcontext"><code class="xref py py-func docutils literal"><span class="pre">setcontext()</span></code></a> has not been called before <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><code class="xref py py-func docutils literal"><span class="pre">getcontext()</span></code></a>, then
1899
<a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><code class="xref py py-func docutils literal"><span class="pre">getcontext()</span></code></a> will automatically create a new context for use in the
1901
<p>The new context is copied from a prototype context called <em>DefaultContext</em>. To
1902
control the defaults so that each thread will use the same values throughout the
1903
application, directly modify the <em>DefaultContext</em> object. This should be done
1904
<em>before</em> any threads are started so that there won’t be a race condition between
1905
threads calling <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><code class="xref py py-func docutils literal"><span class="pre">getcontext()</span></code></a>. For example:</p>
1906
<div class="highlight-python"><div class="highlight"><pre><span></span># Set applicationwide defaults for all threads about to be launched
1907
DefaultContext.prec = 12
1908
DefaultContext.rounding = ROUND_DOWN
1909
DefaultContext.traps = ExtendedContext.traps.copy()
1910
DefaultContext.traps[InvalidOperation] = 1
1911
setcontext(DefaultContext)
1913
# Afterwards, the threads can be started
1921
<div class="section" id="recipes">
1922
<span id="decimal-recipes"></span><h2>9.4.7. Recipes<a class="headerlink" href="#recipes" title="Permalink to this headline">¶</a></h2>
1923
<p>Here are a few recipes that serve as utility functions and that demonstrate ways
1924
to work with the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> class:</p>
1925
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">moneyfmt</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">places</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">curr</span><span class="o">=</span><span class="s1">''</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s1">','</span><span class="p">,</span> <span class="n">dp</span><span class="o">=</span><span class="s1">'.'</span><span class="p">,</span>
1926
<span class="n">pos</span><span class="o">=</span><span class="s1">''</span><span class="p">,</span> <span class="n">neg</span><span class="o">=</span><span class="s1">'-'</span><span class="p">,</span> <span class="n">trailneg</span><span class="o">=</span><span class="s1">''</span><span class="p">):</span>
1927
<span class="sd">"""Convert Decimal to a money formatted string.</span>
1929
<span class="sd"> places: required number of places after the decimal point</span>
1930
<span class="sd"> curr: optional currency symbol before the sign (may be blank)</span>
1931
<span class="sd"> sep: optional grouping separator (comma, period, space, or blank)</span>
1932
<span class="sd"> dp: decimal point indicator (comma or period)</span>
1933
<span class="sd"> only specify as blank when places is zero</span>
1934
<span class="sd"> pos: optional sign for positive numbers: '+', space or blank</span>
1935
<span class="sd"> neg: optional sign for negative numbers: '-', '(', space or blank</span>
1936
<span class="sd"> trailneg:optional trailing minus indicator: '-', ')', space or blank</span>
1938
<span class="sd"> >>> d = Decimal('-1234567.8901')</span>
1939
<span class="sd"> >>> moneyfmt(d, curr='$')</span>
1940
<span class="sd"> '-$1,234,567.89'</span>
1941
<span class="sd"> >>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-')</span>
1942
<span class="sd"> '1.234.568-'</span>
1943
<span class="sd"> >>> moneyfmt(d, curr='$', neg='(', trailneg=')')</span>
1944
<span class="sd"> '($1,234,567.89)'</span>
1945
<span class="sd"> >>> moneyfmt(Decimal(123456789), sep=' ')</span>
1946
<span class="sd"> '123 456 789.00'</span>
1947
<span class="sd"> >>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>')</span>
1948
<span class="sd"> '<0.02>'</span>
1950
<span class="sd"> """</span>
1951
<span class="n">q</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="o">**</span> <span class="o">-</span><span class="n">places</span> <span class="c1"># 2 places --> '0.01'</span>
1952
<span class="n">sign</span><span class="p">,</span> <span class="n">digits</span><span class="p">,</span> <span class="n">exp</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">q</span><span class="p">)</span><span class="o">.</span><span class="n">as_tuple</span><span class="p">()</span>
1953
<span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
1954
<span class="n">digits</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">digits</span><span class="p">)</span>
1955
<span class="n">build</span><span class="p">,</span> <span class="nb">next</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">,</span> <span class="n">digits</span><span class="o">.</span><span class="n">pop</span>
1956
<span class="k">if</span> <span class="n">sign</span><span class="p">:</span>
1957
<span class="n">build</span><span class="p">(</span><span class="n">trailneg</span><span class="p">)</span>
1958
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">places</span><span class="p">):</span>
1959
<span class="n">build</span><span class="p">(</span><span class="nb">next</span><span class="p">()</span> <span class="k">if</span> <span class="n">digits</span> <span class="k">else</span> <span class="s1">'0'</span><span class="p">)</span>
1960
<span class="n">build</span><span class="p">(</span><span class="n">dp</span><span class="p">)</span>
1961
<span class="k">if</span> <span class="ow">not</span> <span class="n">digits</span><span class="p">:</span>
1962
<span class="n">build</span><span class="p">(</span><span class="s1">'0'</span><span class="p">)</span>
1963
<span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
1964
<span class="k">while</span> <span class="n">digits</span><span class="p">:</span>
1965
<span class="n">build</span><span class="p">(</span><span class="nb">next</span><span class="p">())</span>
1966
<span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
1967
<span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">3</span> <span class="ow">and</span> <span class="n">digits</span><span class="p">:</span>
1968
<span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
1969
<span class="n">build</span><span class="p">(</span><span class="n">sep</span><span class="p">)</span>
1970
<span class="n">build</span><span class="p">(</span><span class="n">curr</span><span class="p">)</span>
1971
<span class="n">build</span><span class="p">(</span><span class="n">neg</span> <span class="k">if</span> <span class="n">sign</span> <span class="k">else</span> <span class="n">pos</span><span class="p">)</span>
1972
<span class="k">return</span> <span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">result</span><span class="p">))</span>
1974
<span class="k">def</span> <span class="nf">pi</span><span class="p">():</span>
1975
<span class="sd">"""Compute Pi to the current precision.</span>
1977
<span class="sd"> >>> print pi()</span>
1978
<span class="sd"> 3.141592653589793238462643383</span>
1980
<span class="sd"> """</span>
1981
<span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">+=</span> <span class="mi">2</span> <span class="c1"># extra digits for intermediate steps</span>
1982
<span class="n">three</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># substitute "three=3.0" for regular floats</span>
1983
<span class="n">lasts</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">na</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">da</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">three</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">24</span>
1984
<span class="k">while</span> <span class="n">s</span> <span class="o">!=</span> <span class="n">lasts</span><span class="p">:</span>
1985
<span class="n">lasts</span> <span class="o">=</span> <span class="n">s</span>
1986
<span class="n">n</span><span class="p">,</span> <span class="n">na</span> <span class="o">=</span> <span class="n">n</span><span class="o">+</span><span class="n">na</span><span class="p">,</span> <span class="n">na</span><span class="o">+</span><span class="mi">8</span>
1987
<span class="n">d</span><span class="p">,</span> <span class="n">da</span> <span class="o">=</span> <span class="n">d</span><span class="o">+</span><span class="n">da</span><span class="p">,</span> <span class="n">da</span><span class="o">+</span><span class="mi">32</span>
1988
<span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="n">t</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> <span class="o">/</span> <span class="n">d</span>
1989
<span class="n">s</span> <span class="o">+=</span> <span class="n">t</span>
1990
<span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">-=</span> <span class="mi">2</span>
1991
<span class="k">return</span> <span class="o">+</span><span class="n">s</span> <span class="c1"># unary plus applies the new precision</span>
1993
<span class="k">def</span> <span class="nf">exp</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
1994
<span class="sd">"""Return e raised to the power of x. Result type matches input type.</span>
1996
<span class="sd"> >>> print exp(Decimal(1))</span>
1997
<span class="sd"> 2.718281828459045235360287471</span>
1998
<span class="sd"> >>> print exp(Decimal(2))</span>
1999
<span class="sd"> 7.389056098930650227230427461</span>
2000
<span class="sd"> >>> print exp(2.0)</span>
2001
<span class="sd"> 7.38905609893</span>
2002
<span class="sd"> >>> print exp(2+0j)</span>
2003
<span class="sd"> (7.38905609893+0j)</span>
2005
<span class="sd"> """</span>
2006
<span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">+=</span> <span class="mi">2</span>
2007
<span class="n">i</span><span class="p">,</span> <span class="n">lasts</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">fact</span><span class="p">,</span> <span class="n">num</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span>
2008
<span class="k">while</span> <span class="n">s</span> <span class="o">!=</span> <span class="n">lasts</span><span class="p">:</span>
2009
<span class="n">lasts</span> <span class="o">=</span> <span class="n">s</span>
2010
<span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
2011
<span class="n">fact</span> <span class="o">*=</span> <span class="n">i</span>
2012
<span class="n">num</span> <span class="o">*=</span> <span class="n">x</span>
2013
<span class="n">s</span> <span class="o">+=</span> <span class="n">num</span> <span class="o">/</span> <span class="n">fact</span>
2014
<span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">-=</span> <span class="mi">2</span>
2015
<span class="k">return</span> <span class="o">+</span><span class="n">s</span>
2017
<span class="k">def</span> <span class="nf">cos</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
2018
<span class="sd">"""Return the cosine of x as measured in radians.</span>
2020
<span class="sd"> >>> print cos(Decimal('0.5'))</span>
2021
<span class="sd"> 0.8775825618903727161162815826</span>
2022
<span class="sd"> >>> print cos(0.5)</span>
2023
<span class="sd"> 0.87758256189</span>
2024
<span class="sd"> >>> print cos(0.5+0j)</span>
2025
<span class="sd"> (0.87758256189+0j)</span>
2027
<span class="sd"> """</span>
2028
<span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">+=</span> <span class="mi">2</span>
2029
<span class="n">i</span><span class="p">,</span> <span class="n">lasts</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">fact</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">sign</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span>
2030
<span class="k">while</span> <span class="n">s</span> <span class="o">!=</span> <span class="n">lasts</span><span class="p">:</span>
2031
<span class="n">lasts</span> <span class="o">=</span> <span class="n">s</span>
2032
<span class="n">i</span> <span class="o">+=</span> <span class="mi">2</span>
2033
<span class="n">fact</span> <span class="o">*=</span> <span class="n">i</span> <span class="o">*</span> <span class="p">(</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
2034
<span class="n">num</span> <span class="o">*=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span>
2035
<span class="n">sign</span> <span class="o">*=</span> <span class="o">-</span><span class="mi">1</span>
2036
<span class="n">s</span> <span class="o">+=</span> <span class="n">num</span> <span class="o">/</span> <span class="n">fact</span> <span class="o">*</span> <span class="n">sign</span>
2037
<span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">-=</span> <span class="mi">2</span>
2038
<span class="k">return</span> <span class="o">+</span><span class="n">s</span>
2040
<span class="k">def</span> <span class="nf">sin</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
2041
<span class="sd">"""Return the sine of x as measured in radians.</span>
2043
<span class="sd"> >>> print sin(Decimal('0.5'))</span>
2044
<span class="sd"> 0.4794255386042030002732879352</span>
2045
<span class="sd"> >>> print sin(0.5)</span>
2046
<span class="sd"> 0.479425538604</span>
2047
<span class="sd"> >>> print sin(0.5+0j)</span>
2048
<span class="sd"> (0.479425538604+0j)</span>
2050
<span class="sd"> """</span>
2051
<span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">+=</span> <span class="mi">2</span>
2052
<span class="n">i</span><span class="p">,</span> <span class="n">lasts</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">fact</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">sign</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">1</span>
2053
<span class="k">while</span> <span class="n">s</span> <span class="o">!=</span> <span class="n">lasts</span><span class="p">:</span>
2054
<span class="n">lasts</span> <span class="o">=</span> <span class="n">s</span>
2055
<span class="n">i</span> <span class="o">+=</span> <span class="mi">2</span>
2056
<span class="n">fact</span> <span class="o">*=</span> <span class="n">i</span> <span class="o">*</span> <span class="p">(</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
2057
<span class="n">num</span> <span class="o">*=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span>
2058
<span class="n">sign</span> <span class="o">*=</span> <span class="o">-</span><span class="mi">1</span>
2059
<span class="n">s</span> <span class="o">+=</span> <span class="n">num</span> <span class="o">/</span> <span class="n">fact</span> <span class="o">*</span> <span class="n">sign</span>
2060
<span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">-=</span> <span class="mi">2</span>
2061
<span class="k">return</span> <span class="o">+</span><span class="n">s</span>
2065
<div class="section" id="decimal-faq">
2066
<span id="id1"></span><h2>9.4.8. Decimal FAQ<a class="headerlink" href="#decimal-faq" title="Permalink to this headline">¶</a></h2>
2067
<p>Q. It is cumbersome to type <code class="docutils literal"><span class="pre">decimal.Decimal('1234.5')</span></code>. Is there a way to
2068
minimize typing when using the interactive interpreter?</p>
2069
<p>A. Some users abbreviate the constructor to just a single letter:</p>
2070
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">D</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span>
2071
<span class="gp">>>> </span><span class="n">D</span><span class="p">(</span><span class="s1">'1.23'</span><span class="p">)</span> <span class="o">+</span> <span class="n">D</span><span class="p">(</span><span class="s1">'3.45'</span><span class="p">)</span>
2072
<span class="go">Decimal('4.68')</span>
2075
<p>Q. In a fixed-point application with two decimal places, some inputs have many
2076
places and need to be rounded. Others are not supposed to have excess digits
2077
and need to be validated. What methods should be used?</p>
2078
<p>A. The <code class="xref py py-meth docutils literal"><span class="pre">quantize()</span></code> method rounds to a fixed number of decimal places. If
2079
the <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><code class="xref py py-const docutils literal"><span class="pre">Inexact</span></code></a> trap is set, it is also useful for validation:</p>
2080
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">TWOPLACES</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="o">**</span> <span class="o">-</span><span class="mi">2</span> <span class="c1"># same as Decimal('0.01')</span>
2083
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="c1"># Round to two places</span>
2084
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'3.214'</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">TWOPLACES</span><span class="p">)</span>
2085
<span class="go">Decimal('3.21')</span>
2088
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="c1"># Validate that a number does not exceed two places</span>
2089
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'3.21'</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">TWOPLACES</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="n">Context</span><span class="p">(</span><span class="n">traps</span><span class="o">=</span><span class="p">[</span><span class="n">Inexact</span><span class="p">]))</span>
2090
<span class="go">Decimal('3.21')</span>
2093
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'3.214'</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">TWOPLACES</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="n">Context</span><span class="p">(</span><span class="n">traps</span><span class="o">=</span><span class="p">[</span><span class="n">Inexact</span><span class="p">]))</span>
2094
<span class="gt">Traceback (most recent call last):</span>
2095
<span class="c">...</span>
2096
<span class="gr">Inexact</span>: <span class="n">None</span>
2099
<p>Q. Once I have valid two place inputs, how do I maintain that invariant
2100
throughout an application?</p>
2101
<p>A. Some operations like addition, subtraction, and multiplication by an integer
2102
will automatically preserve fixed point. Others operations, like division and
2103
non-integer multiplication, will change the number of decimal places and need to
2104
be followed-up with a <code class="xref py py-meth docutils literal"><span class="pre">quantize()</span></code> step:</p>
2105
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">'102.72'</span><span class="p">)</span> <span class="c1"># Initial fixed-point values</span>
2106
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">'3.17'</span><span class="p">)</span>
2107
<span class="gp">>>> </span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="c1"># Addition preserves fixed-point</span>
2108
<span class="go">Decimal('105.89')</span>
2109
<span class="gp">>>> </span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span>
2110
<span class="go">Decimal('99.55')</span>
2111
<span class="gp">>>> </span><span class="n">a</span> <span class="o">*</span> <span class="mi">42</span> <span class="c1"># So does integer multiplication</span>
2112
<span class="go">Decimal('4314.24')</span>
2113
<span class="gp">>>> </span><span class="p">(</span><span class="n">a</span> <span class="o">*</span> <span class="n">b</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">TWOPLACES</span><span class="p">)</span> <span class="c1"># Must quantize non-integer multiplication</span>
2114
<span class="go">Decimal('325.62')</span>
2115
<span class="gp">>>> </span><span class="p">(</span><span class="n">b</span> <span class="o">/</span> <span class="n">a</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">TWOPLACES</span><span class="p">)</span> <span class="c1"># And quantize division</span>
2116
<span class="go">Decimal('0.03')</span>
2119
<p>In developing fixed-point applications, it is convenient to define functions
2120
to handle the <code class="xref py py-meth docutils literal"><span class="pre">quantize()</span></code> step:</p>
2121
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">mul</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">fp</span><span class="o">=</span><span class="n">TWOPLACES</span><span class="p">):</span>
2122
<span class="gp">... </span> <span class="k">return</span> <span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">y</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span>
2123
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">div</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">fp</span><span class="o">=</span><span class="n">TWOPLACES</span><span class="p">):</span>
2124
<span class="gp">... </span> <span class="k">return</span> <span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">y</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span>
2127
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">mul</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="c1"># Automatically preserve fixed-point</span>
2128
<span class="go">Decimal('325.62')</span>
2129
<span class="gp">>>> </span><span class="n">div</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
2130
<span class="go">Decimal('0.03')</span>
2133
<p>Q. There are many ways to express the same value. The numbers <code class="xref py py-const docutils literal"><span class="pre">200</span></code>,
2134
<code class="xref py py-const docutils literal"><span class="pre">200.000</span></code>, <code class="xref py py-const docutils literal"><span class="pre">2E2</span></code>, and <code class="xref py py-const docutils literal"><span class="pre">02E+4</span></code> all have the same value at
2135
various precisions. Is there a way to transform them to a single recognizable
2136
canonical value?</p>
2137
<p>A. The <code class="xref py py-meth docutils literal"><span class="pre">normalize()</span></code> method maps all equivalent values to a single
2139
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">values</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">Decimal</span><span class="p">,</span> <span class="s1">'200 200.000 2E2 .02E+4'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
2140
<span class="gp">>>> </span><span class="p">[</span><span class="n">v</span><span class="o">.</span><span class="n">normalize</span><span class="p">()</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">values</span><span class="p">]</span>
2141
<span class="go">[Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2')]</span>
2144
<p>Q. Some decimal values always print with exponential notation. Is there a way
2145
to get a non-exponential representation?</p>
2146
<p>A. For some values, exponential notation is the only way to express the number
2147
of significant places in the coefficient. For example, expressing
2148
<code class="xref py py-const docutils literal"><span class="pre">5.0E+3</span></code> as <code class="xref py py-const docutils literal"><span class="pre">5000</span></code> keeps the value constant but cannot show the
2149
original’s two-place significance.</p>
2150
<p>If an application does not care about tracking significance, it is easy to
2151
remove the exponent and trailing zeros, losing significance, but keeping the
2152
value unchanged:</p>
2153
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">remove_exponent</span><span class="p">(</span><span class="n">d</span><span class="p">):</span>
2154
<span class="sd">'''Remove exponent and trailing zeros.</span>
2156
<span class="sd"> >>> remove_exponent(Decimal('5E+3'))</span>
2157
<span class="sd"> Decimal('5000')</span>
2159
<span class="sd"> '''</span>
2160
<span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span> <span class="k">if</span> <span class="n">d</span> <span class="o">==</span> <span class="n">d</span><span class="o">.</span><span class="n">to_integral</span><span class="p">()</span> <span class="k">else</span> <span class="n">d</span><span class="o">.</span><span class="n">normalize</span><span class="p">()</span>
2163
<p>Q. Is there a way to convert a regular float to a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a>?</p>
2164
<p>A. Yes, any binary floating point number can be exactly expressed as a
2165
Decimal though an exact conversion may take more precision than intuition would
2167
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span>
2168
<span class="go">Decimal('3.141592653589793115997963468544185161590576171875')</span>
2171
<p>Q. Within a complex calculation, how can I make sure that I haven’t gotten a
2172
spurious result because of insufficient precision or rounding anomalies.</p>
2173
<p>A. The decimal module makes it easy to test results. A best practice is to
2174
re-run calculations using greater precision and with various rounding modes.
2175
Widely differing results indicate insufficient precision, rounding mode issues,
2176
ill-conditioned inputs, or a numerically unstable algorithm.</p>
2177
<p>Q. I noticed that context precision is applied to the results of operations but
2178
not to the inputs. Is there anything to watch out for when mixing values of
2179
different precisions?</p>
2180
<p>A. Yes. The principle is that all values are considered to be exact and so is
2181
the arithmetic on those values. Only the results are rounded. The advantage
2182
for inputs is that “what you type is what you get”. A disadvantage is that the
2183
results can look odd if you forget that the inputs haven’t been rounded:</p>
2184
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">3</span>
2185
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'3.104'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">'2.104'</span><span class="p">)</span>
2186
<span class="go">Decimal('5.21')</span>
2187
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'3.104'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">'0.000'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">'2.104'</span><span class="p">)</span>
2188
<span class="go">Decimal('5.20')</span>
2191
<p>The solution is either to increase precision or to force rounding of inputs
2192
using the unary plus operation:</p>
2193
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">3</span>
2194
<span class="gp">>>> </span><span class="o">+</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'1.23456789'</span><span class="p">)</span> <span class="c1"># unary plus triggers rounding</span>
2195
<span class="go">Decimal('1.23')</span>
2198
<p>Alternatively, inputs can be rounded upon creation using the
2199
<a class="reference internal" href="#decimal.Context.create_decimal" title="decimal.Context.create_decimal"><code class="xref py py-meth docutils literal"><span class="pre">Context.create_decimal()</span></code></a> method:</p>
2200
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Context</span><span class="p">(</span><span class="n">prec</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="n">ROUND_DOWN</span><span class="p">)</span><span class="o">.</span><span class="n">create_decimal</span><span class="p">(</span><span class="s1">'1.2345678'</span><span class="p">)</span>
2201
<span class="go">Decimal('1.2345')</span>
2211
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
2212
<div class="sphinxsidebarwrapper">
2213
<h3><a href="../contents.html">Table Of Contents</a></h3>
2215
<li><a class="reference internal" href="#">9.4. <code class="docutils literal"><span class="pre">decimal</span></code> — Decimal fixed point and floating point arithmetic</a><ul>
2216
<li><a class="reference internal" href="#quick-start-tutorial">9.4.1. Quick-start Tutorial</a></li>
2217
<li><a class="reference internal" href="#decimal-objects">9.4.2. Decimal objects</a><ul>
2218
<li><a class="reference internal" href="#logical-operands">9.4.2.1. Logical operands</a></li>
2221
<li><a class="reference internal" href="#context-objects">9.4.3. Context objects</a></li>
2222
<li><a class="reference internal" href="#signals">9.4.4. Signals</a></li>
2223
<li><a class="reference internal" href="#floating-point-notes">9.4.5. Floating Point Notes</a><ul>
2224
<li><a class="reference internal" href="#mitigating-round-off-error-with-increased-precision">9.4.5.1. Mitigating round-off error with increased precision</a></li>
2225
<li><a class="reference internal" href="#special-values">9.4.5.2. Special values</a></li>
2228
<li><a class="reference internal" href="#working-with-threads">9.4.6. Working with threads</a></li>
2229
<li><a class="reference internal" href="#recipes">9.4.7. Recipes</a></li>
2230
<li><a class="reference internal" href="#decimal-faq">9.4.8. Decimal FAQ</a></li>
2235
<h4>Previous topic</h4>
2236
<p class="topless"><a href="cmath.html"
2237
title="previous chapter">9.3. <code class="docutils literal"><span class="pre">cmath</span></code> — Mathematical functions for complex numbers</a></p>
2239
<p class="topless"><a href="fractions.html"
2240
title="next chapter">9.5. <code class="docutils literal"><span class="pre">fractions</span></code> — Rational numbers</a></p>
2242
<ul class="this-page-menu">
2243
<li><a href="../bugs.html">Report a Bug</a></li>
2244
<li><a href="../_sources/library/decimal.txt"
2245
rel="nofollow">Show Source</a></li>
2248
<div id="searchbox" style="display: none" role="search">
2249
<h3>Quick search</h3>
2250
<form class="search" action="../search.html" method="get">
2251
<input type="text" name="q" />
2252
<input type="submit" value="Go" />
2253
<input type="hidden" name="check_keywords" value="yes" />
2254
<input type="hidden" name="area" value="default" />
2256
<p class="searchtip" style="font-size: 90%">
2257
Enter search terms or a module, class or function name.
2260
<script type="text/javascript">$('#searchbox').show(0);</script>
2263
<div class="clearer"></div>
2265
<div class="related" role="navigation" aria-label="related navigation">
2268
<li class="right" style="margin-right: 10px">
2269
<a href="../genindex.html" title="General Index"
2272
<a href="../py-modindex.html" title="Python Module Index"
2275
<a href="fractions.html" title="9.5. fractions — Rational numbers"
2278
<a href="cmath.html" title="9.3. cmath — Mathematical functions for complex numbers"
2279
>previous</a> |</li>
2280
<li><img src="../_static/py.png" alt=""
2281
style="vertical-align: middle; margin-top: -1px"/></li>
2282
<li><a href="https://www.python.org/">Python</a> »</li>
2284
<span class="version_switcher_placeholder">2.7.12</span>
2285
<a href="../index.html">Documentation</a> »
2288
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
2289
<li class="nav-item nav-item-2"><a href="numeric.html" >9. Numeric and Mathematical Modules</a> »</li>
2292
<div class="footer">
2293
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
2295
The Python Software Foundation is a non-profit corporation.
2296
<a href="https://www.python.org/psf/donations/">Please donate.</a>
2298
Last updated on Sep 20, 2016.
2299
<a href="../bugs.html">Found a bug</a>?
2301
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'