Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-backports > by-pkgid > 3ba3bd1608c672ba2129b098a48e9e4d > files > 656

python3-docs-3.2.2-3mdv2010.2.noarch.rpm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>8.4. decimal — Decimal fixed point and floating point arithmetic &mdash; Python v3.2.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.2.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.2.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.2.2 documentation" href="../index.html" />
    <link rel="up" title="8. Numeric and Mathematical Modules" href="numeric.html" />
    <link rel="next" title="8.5. fractions — Rational numbers" href="fractions.html" />
    <link rel="prev" title="8.3. cmath — Mathematical functions for complex numbers" href="cmath.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="fractions.html" title="8.5. fractions — Rational numbers"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="cmath.html" title="8.3. cmath — Mathematical functions for complex numbers"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="numeric.html" accesskey="U">8. Numeric and Mathematical Modules</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-decimal">
<span id="decimal-decimal-fixed-point-and-floating-point-arithmetic"></span><h1>8.4. <a class="reference internal" href="#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic  Specification."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> &#8212; Decimal fixed point and floating point arithmetic<a class="headerlink" href="#module-decimal" title="Permalink to this headline">¶</a></h1>
<p>The <a class="reference internal" href="#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic  Specification."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> module provides support for decimal floating point
arithmetic.  It offers several advantages over the <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> datatype:</p>
<ul>
<li><p class="first">Decimal &#8220;is based on a floating-point model which was designed with people
in mind, and necessarily has a paramount guiding principle &#8211; computers must
provide an arithmetic that works in the same way as the arithmetic that
people learn at school.&#8221; &#8211; excerpt from the decimal arithmetic specification.</p>
</li>
<li><p class="first">Decimal numbers can be represented exactly.  In contrast, numbers like
<tt class="xref py py-const docutils literal"><span class="pre">1.1</span></tt> and <tt class="xref py py-const docutils literal"><span class="pre">2.2</span></tt> do not have an exact representations in binary
floating point. End users typically would not expect <tt class="docutils literal"><span class="pre">1.1</span> <span class="pre">+</span> <span class="pre">2.2</span></tt> to display
as <tt class="xref py py-const docutils literal"><span class="pre">3.3000000000000003</span></tt> as it does with binary floating point.</p>
</li>
<li><p class="first">The exactness carries over into arithmetic.  In decimal floating point, <tt class="docutils literal"><span class="pre">0.1</span>
<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></tt> is exactly equal to zero.  In binary floating point, the result
is <tt class="xref py py-const docutils literal"><span class="pre">5.5511151231257827e-017</span></tt>.  While near to zero, the differences
prevent reliable equality testing and differences can accumulate. For this
reason, decimal is preferred in accounting applications which have strict
equality invariants.</p>
</li>
<li><p class="first">The decimal module incorporates a notion of significant places so that <tt class="docutils literal"><span class="pre">1.30</span>
<span class="pre">+</span> <span class="pre">1.20</span></tt> is <tt class="xref py py-const docutils literal"><span class="pre">2.50</span></tt>.  The trailing zero is kept to indicate significance.
This is the customary presentation for monetary applications. For
multiplication, the &#8220;schoolbook&#8221; approach uses all the figures in the
multiplicands.  For instance, <tt class="docutils literal"><span class="pre">1.3</span> <span class="pre">*</span> <span class="pre">1.2</span></tt> gives <tt class="xref py py-const docutils literal"><span class="pre">1.56</span></tt> while <tt class="docutils literal"><span class="pre">1.30</span> <span class="pre">*</span>
<span class="pre">1.20</span></tt> gives <tt class="xref py py-const docutils literal"><span class="pre">1.5600</span></tt>.</p>
</li>
<li><p class="first">Unlike hardware based binary floating point, the decimal module has a user
alterable precision (defaulting to 28 places) which can be as large as needed for
a given problem:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">decimal</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;0.142857&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;0.1428571428571428571428571429&#39;)</span>
</pre></div>
</div>
</li>
<li><p class="first">Both binary and decimal floating point are implemented in terms of published
standards.  While the built-in float type exposes only a modest portion of its
capabilities, the decimal module exposes all required parts of the standard.
When needed, the programmer has full control over rounding and signal handling.
This includes an option to enforce exact arithmetic by using exceptions
to block any inexact operations.</p>
</li>
<li><p class="first">The decimal module was designed to support &#8220;without prejudice, both exact
unrounded decimal arithmetic (sometimes called fixed-point arithmetic)
and rounded floating-point arithmetic.&#8221;  &#8211; excerpt from the decimal
arithmetic specification.</p>
</li>
</ul>
<p>The module design is centered around three concepts:  the decimal number, the
context for arithmetic, and signals.</p>
<p>A decimal number is immutable.  It has a sign, coefficient digits, and an
exponent.  To preserve significance, the coefficient digits do not truncate
trailing zeros.  Decimals also include special values such as
<tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">-Infinity</span></tt>, and <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>.  The standard also
differentiates <tt class="xref py py-const docutils literal"><span class="pre">-0</span></tt> from <tt class="xref py py-const docutils literal"><span class="pre">+0</span></tt>.</p>
<p>The context for arithmetic is an environment specifying precision, rounding
rules, limits on exponents, flags indicating the results of operations, and trap
enablers which determine whether signals are treated as exceptions.  Rounding
options include <tt class="xref py py-const docutils literal"><span class="pre">ROUND_CEILING</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ROUND_DOWN</span></tt>,
<tt class="xref py py-const docutils literal"><span class="pre">ROUND_FLOOR</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_DOWN</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></tt>,
<tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_UP</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ROUND_UP</span></tt>, and <tt class="xref py py-const docutils literal"><span class="pre">ROUND_05UP</span></tt>.</p>
<p>Signals are groups of exceptional conditions arising during the course of
computation.  Depending on the needs of the application, signals may be ignored,
considered as informational, or treated as exceptions. The signals in the
decimal module are: <a class="reference internal" href="#decimal.Clamped" title="decimal.Clamped"><tt class="xref py py-const docutils literal"><span class="pre">Clamped</span></tt></a>, <a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><tt class="xref py py-const docutils literal"><span class="pre">InvalidOperation</span></tt></a>,
<a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><tt class="xref py py-const docutils literal"><span class="pre">DivisionByZero</span></tt></a>, <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-const docutils literal"><span class="pre">Inexact</span></tt></a>, <a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><tt class="xref py py-const docutils literal"><span class="pre">Rounded</span></tt></a>, <a class="reference internal" href="#decimal.Subnormal" title="decimal.Subnormal"><tt class="xref py py-const docutils literal"><span class="pre">Subnormal</span></tt></a>,
<a class="reference internal" href="#decimal.Overflow" title="decimal.Overflow"><tt class="xref py py-const docutils literal"><span class="pre">Overflow</span></tt></a>, and <a class="reference internal" href="#decimal.Underflow" title="decimal.Underflow"><tt class="xref py py-const docutils literal"><span class="pre">Underflow</span></tt></a>.</p>
<p>For each signal there is a flag and a trap enabler.  When a signal is
encountered, its flag is set to one, then, if the trap enabler is
set to one, an exception is raised.  Flags are sticky, so the user needs to
reset them before monitoring a calculation.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<ul class="last simple">
<li>IBM&#8217;s General Decimal Arithmetic Specification, <a class="reference external" href="http://speleotrove.com/decimal/decarith.html">The General Decimal Arithmetic
Specification</a>.</li>
<li>IEEE standard 854-1987, <a class="reference external" href="http://754r.ucbtest.org/standards/854.pdf">Unofficial IEEE 854 Text</a>.</li>
</ul>
</div>
<div class="section" id="quick-start-tutorial">
<span id="decimal-tutorial"></span><h2>8.4.1. Quick-start Tutorial<a class="headerlink" href="#quick-start-tutorial" title="Permalink to this headline">¶</a></h2>
<p>The usual start to using decimals is importing the module, viewing the current
context with <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a> and, if necessary, setting new values for
precision, rounding, or enabled traps:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">decimal</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getcontext</span><span class="p">()</span>
<span class="go">Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,</span>
<span class="go">        capitals=1, clamp=0, flags=[], traps=[Overflow, DivisionByZero,</span>
<span class="go">        InvalidOperation])</span>

<span class="gp">&gt;&gt;&gt; </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="c"># Set a new precision</span>
</pre></div>
</div>
<p>Decimal instances can be constructed from integers, strings, floats, or tuples.
Construction from an integer or a float performs an exact conversion of the
value of that integer or float.  Decimal numbers include special values such as
<tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> which stands for &#8220;Not a number&#8221;, positive and negative
<tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt>, and <tt class="xref py py-const docutils literal"><span class="pre">-0</span></tt>.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">Decimal(&#39;10&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;3.14&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;3.14&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="mf">3.14</span><span class="p">)</span>
<span class="go">Decimal(&#39;3.140000000000000124344978758017532527446746826171875&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;3.14&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;1.4142135623730951&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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="s">&#39;0.5&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;1.414213562373095048801688724&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;NaN&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;NaN&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;-Infinity&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;-Infinity&#39;)</span>
</pre></div>
</div>
<p>The significance of a new Decimal is determined solely by the number of digits
input.  Context precision and rounding only come into play during arithmetic
operations.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;3.0&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;3.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;3.1415926535&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;3.1415926535&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;3.1415926535&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;2.7182818285&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;5.85987&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;3.1415926535&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;2.7182818285&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;5.85988&#39;)</span>
</pre></div>
</div>
<p>Decimals interact well with much of the rest of Python.  Here is a small decimal
floating point flying circus:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">Decimal</span><span class="p">,</span> <span class="s">&#39;1.34 1.87 3.45 2.35 1.00 0.03 9.25&#39;</span><span class="o">.</span><span class="n">split</span><span class="p">()))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">max</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="go">Decimal(&#39;9.25&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">min</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="go">Decimal(&#39;0.03&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="go">[Decimal(&#39;0.03&#39;), Decimal(&#39;1.00&#39;), Decimal(&#39;1.34&#39;), Decimal(&#39;1.87&#39;),</span>
<span class="go"> Decimal(&#39;2.35&#39;), Decimal(&#39;3.45&#39;), Decimal(&#39;9.25&#39;)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">sum</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="go">Decimal(&#39;19.29&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">&#39;1.34&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">1.34</span>
<span class="gp">&gt;&gt;&gt; </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="go">Decimal(&#39;1.3&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">*</span> <span class="mi">5</span>
<span class="go">Decimal(&#39;6.70&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">*</span> <span class="n">b</span>
<span class="go">Decimal(&#39;2.5058&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">%</span> <span class="n">a</span>
<span class="go">Decimal(&#39;0.77&#39;)</span>
</pre></div>
</div>
<p>And some mathematical functions are also available to Decimal:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;1.414213562373095048801688724&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;2.718281828459045235360287471&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;10&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">ln</span><span class="p">()</span>
<span class="go">Decimal(&#39;2.302585092994045684017991455&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;10&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">log10</span><span class="p">()</span>
<span class="go">Decimal(&#39;1&#39;)</span>
</pre></div>
</div>
<p>The <tt class="xref py py-meth docutils literal"><span class="pre">quantize()</span></tt> method rounds a number to a fixed exponent.  This method is
useful for monetary applications that often round results to a fixed number of
places:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;7.325&#39;</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="s">&#39;.01&#39;</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="go">Decimal(&#39;7.32&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;7.325&#39;</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="s">&#39;1.&#39;</span><span class="p">),</span> <span class="n">rounding</span><span class="o">=</span><span class="n">ROUND_UP</span><span class="p">)</span>
<span class="go">Decimal(&#39;8&#39;)</span>
</pre></div>
</div>
<p>As shown above, the <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a> function accesses the current context and
allows the settings to be changed.  This approach meets the needs of most
applications.</p>
<p>For more advanced work, it may be useful to create alternate contexts using the
Context() constructor.  To make an alternate active, use the <a class="reference internal" href="#decimal.setcontext" title="decimal.setcontext"><tt class="xref py py-func docutils literal"><span class="pre">setcontext()</span></tt></a>
function.</p>
<p>In accordance with the standard, the <tt class="xref py py-mod docutils literal"><span class="pre">Decimal</span></tt> module provides two ready to
use standard contexts, <a class="reference internal" href="#decimal.BasicContext" title="decimal.BasicContext"><tt class="xref py py-const docutils literal"><span class="pre">BasicContext</span></tt></a> and <a class="reference internal" href="#decimal.ExtendedContext" title="decimal.ExtendedContext"><tt class="xref py py-const docutils literal"><span class="pre">ExtendedContext</span></tt></a>. The
former is especially useful for debugging because many of the traps are
enabled:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">setcontext</span><span class="p">(</span><span class="n">myothercontext</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;0.142857142857142857142857142857142857142857142857142857142857&#39;)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">ExtendedContext</span>
<span class="go">Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,</span>
<span class="go">        capitals=1, clamp=0, flags=[], traps=[])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">setcontext</span><span class="p">(</span><span class="n">ExtendedContext</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;0.142857143&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;Infinity&#39;)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">setcontext</span><span class="p">(</span><span class="n">BasicContext</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;pyshell#143&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">-toplevel-</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>
<span class="nc">DivisionByZero</span>: <span class="n-Identifier">x / 0</span>
</pre></div>
</div>
<p>Contexts also have signal flags for monitoring exceptional conditions
encountered during computations.  The flags remain set until explicitly cleared,
so it is best to clear the flags before each set of monitored computations by
using the <tt class="xref py py-meth docutils literal"><span class="pre">clear_flags()</span></tt> method.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">setcontext</span><span class="p">(</span><span class="n">ExtendedContext</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">clear_flags</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;3.14159292&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getcontext</span><span class="p">()</span>
<span class="go">Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,</span>
<span class="go">        capitals=1, clamp=0, flags=[Inexact, Rounded], traps=[])</span>
</pre></div>
</div>
<p>The <em>flags</em> entry shows that the rational approximation to <tt class="xref py py-const docutils literal"><span class="pre">Pi</span></tt> was
rounded (digits beyond the context precision were thrown away) and that the
result is inexact (some of the discarded digits were non-zero).</p>
<p>Individual traps are set using the dictionary in the <tt class="xref py py-attr docutils literal"><span class="pre">traps</span></tt> field of a
context:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">setcontext</span><span class="p">(</span><span class="n">ExtendedContext</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;Infinity&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </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>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;pyshell#112&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">-toplevel-</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>
<span class="nc">DivisionByZero</span>: <span class="n-Identifier">x / 0</span>
</pre></div>
</div>
<p>Most programs adjust the current context only once, at the beginning of the
program.  And, in many applications, data is converted to <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> with
a single cast inside a loop.  With context set and decimals created, the bulk of
the program manipulates the data no differently than with other Python numeric
types.</p>
</div>
<div class="section" id="decimal-objects">
<span id="decimal-decimal"></span><h2>8.4.2. Decimal objects<a class="headerlink" href="#decimal-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="decimal.Decimal">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Decimal</tt><big>(</big><em>value=&quot;0&quot;</em>, <em>context=None</em><big>)</big><a class="headerlink" href="#decimal.Decimal" title="Permalink to this definition">¶</a></dt>
<dd><p>Construct a new <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> object based from <em>value</em>.</p>
<p><em>value</em> can be an integer, string, tuple, <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a>, or another <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a>
object. If no <em>value</em> is given, returns <tt class="docutils literal"><span class="pre">Decimal('0')</span></tt>.  If <em>value</em> is a
string, it should conform to the decimal numeric string syntax after leading
and trailing whitespace characters are removed:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">sign</span>           <span class="p">::</span><span class="o">=</span>  <span class="s">&#39;+&#39;</span> <span class="o">|</span> <span class="s">&#39;-&#39;</span>
<span class="n">digit</span>          <span class="p">::</span><span class="o">=</span>  <span class="s">&#39;0&#39;</span> <span class="o">|</span> <span class="s">&#39;1&#39;</span> <span class="o">|</span> <span class="s">&#39;2&#39;</span> <span class="o">|</span> <span class="s">&#39;3&#39;</span> <span class="o">|</span> <span class="s">&#39;4&#39;</span> <span class="o">|</span> <span class="s">&#39;5&#39;</span> <span class="o">|</span> <span class="s">&#39;6&#39;</span> <span class="o">|</span> <span class="s">&#39;7&#39;</span> <span class="o">|</span> <span class="s">&#39;8&#39;</span> <span class="o">|</span> <span class="s">&#39;9&#39;</span>
<span class="n">indicator</span>      <span class="p">::</span><span class="o">=</span>  <span class="s">&#39;e&#39;</span> <span class="o">|</span> <span class="s">&#39;E&#39;</span>
<span class="n">digits</span>         <span class="p">::</span><span class="o">=</span>  <span class="n">digit</span> <span class="p">[</span><span class="n">digit</span><span class="p">]</span><span class="o">...</span>
<span class="n">decimal</span><span class="o">-</span><span class="n">part</span>   <span class="p">::</span><span class="o">=</span>  <span class="n">digits</span> <span class="s">&#39;.&#39;</span> <span class="p">[</span><span class="n">digits</span><span class="p">]</span> <span class="o">|</span> <span class="p">[</span><span class="s">&#39;.&#39;</span><span class="p">]</span> <span class="n">digits</span>
<span class="n">exponent</span><span class="o">-</span><span class="n">part</span>  <span class="p">::</span><span class="o">=</span>  <span class="n">indicator</span> <span class="p">[</span><span class="n">sign</span><span class="p">]</span> <span class="n">digits</span>
<span class="n">infinity</span>       <span class="p">::</span><span class="o">=</span>  <span class="s">&#39;Infinity&#39;</span> <span class="o">|</span> <span class="s">&#39;Inf&#39;</span>
<span class="n">nan</span>            <span class="p">::</span><span class="o">=</span>  <span class="s">&#39;NaN&#39;</span> <span class="p">[</span><span class="n">digits</span><span class="p">]</span> <span class="o">|</span> <span class="s">&#39;sNaN&#39;</span> <span class="p">[</span><span class="n">digits</span><span class="p">]</span>
<span class="n">numeric</span><span class="o">-</span><span class="n">value</span>  <span class="p">::</span><span class="o">=</span>  <span class="n">decimal</span><span class="o">-</span><span class="n">part</span> <span class="p">[</span><span class="n">exponent</span><span class="o">-</span><span class="n">part</span><span class="p">]</span> <span class="o">|</span> <span class="n">infinity</span>
<span class="n">numeric</span><span class="o">-</span><span class="n">string</span> <span class="p">::</span><span class="o">=</span>  <span class="p">[</span><span class="n">sign</span><span class="p">]</span> <span class="n">numeric</span><span class="o">-</span><span class="n">value</span> <span class="o">|</span> <span class="p">[</span><span class="n">sign</span><span class="p">]</span> <span class="n">nan</span>
</pre></div>
</div>
<p>Other Unicode decimal digits are also permitted where <tt class="docutils literal"><span class="pre">digit</span></tt>
appears above.  These include decimal digits from various other
alphabets (for example, Arabic-Indic and Devanāgarī digits) along
with the fullwidth digits <tt class="docutils literal"><span class="pre">'\uff10'</span></tt> through <tt class="docutils literal"><span class="pre">'\uff19'</span></tt>.</p>
<p>If <em>value</em> is a <a class="reference internal" href="functions.html#tuple" title="tuple"><tt class="xref py py-class docutils literal"><span class="pre">tuple</span></tt></a>, it should have three components, a sign
(<tt class="xref py py-const docutils literal"><span class="pre">0</span></tt> for positive or <tt class="xref py py-const docutils literal"><span class="pre">1</span></tt> for negative), a <a class="reference internal" href="functions.html#tuple" title="tuple"><tt class="xref py py-class docutils literal"><span class="pre">tuple</span></tt></a> of
digits, and an integer exponent. For example, <tt 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></tt>
returns <tt class="docutils literal"><span class="pre">Decimal('1.414')</span></tt>.</p>
<p>If <em>value</em> is a <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a>, the binary floating point value is losslessly
converted to its exact decimal equivalent.  This conversion can often require
53 or more digits of precision.  For example, <tt class="docutils literal"><span class="pre">Decimal(float('1.1'))</span></tt>
converts to
<tt class="docutils literal"><span class="pre">Decimal('1.100000000000000088817841970012523233890533447265625')</span></tt>.</p>
<p>The <em>context</em> precision does not affect how many digits are stored. That is
determined exclusively by the number of digits in <em>value</em>. For example,
<tt class="docutils literal"><span class="pre">Decimal('3.00000')</span></tt> records all five zeros even if the context precision is
only three.</p>
<p>The purpose of the <em>context</em> argument is determining what to do if <em>value</em> is a
malformed string.  If the context traps <a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><tt class="xref py py-const docutils literal"><span class="pre">InvalidOperation</span></tt></a>, an exception
is raised; otherwise, the constructor returns a new Decimal with the value of
<tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>.</p>
<p>Once constructed, <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> objects are immutable.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 3.2: </span>The argument to the constructor is now permitted to be a <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a>
instance.</p>
<p>Decimal floating point objects share many properties with the other built-in
numeric types such as <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> and <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a>.  All of the usual math
operations and special methods apply.  Likewise, decimal objects can be
copied, pickled, printed, used as dictionary keys, used as set elements,
compared, sorted, and coerced to another type (such as <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> or
<a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a>).</p>
<p>Decimal objects cannot generally be combined with floats or
instances of <a class="reference internal" href="fractions.html#fractions.Fraction" title="fractions.Fraction"><tt class="xref py py-class docutils literal"><span class="pre">fractions.Fraction</span></tt></a> in arithmetic operations:
an attempt to add a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> to a <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a>, for
example, will raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>.  However, it is possible to
use Python&#8217;s comparison operators to compare a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a>
instance <tt class="docutils literal"><span class="pre">x</span></tt> with another number <tt class="docutils literal"><span class="pre">y</span></tt>.  This avoids confusing results
when doing equality comparisons between numbers of different types.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 3.2: </span>Mixed-type comparisons between <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instances and other
numeric types are now fully supported.</p>
<p>In addition to the standard numeric properties, decimal floating point
objects also have a number of specialized methods:</p>
<dl class="method">
<dt id="decimal.Decimal.adjusted">
<tt class="descname">adjusted</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.adjusted" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the adjusted exponent after shifting out the coefficient&#8217;s
rightmost digits until only the lead digit remains:
<tt class="docutils literal"><span class="pre">Decimal('321e+5').adjusted()</span></tt> returns seven.  Used for determining the
position of the most significant digit with respect to the decimal point.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.as_tuple">
<tt class="descname">as_tuple</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.as_tuple" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="../glossary.html#term-named-tuple"><em class="xref std std-term">named tuple</em></a> representation of the number:
<tt class="docutils literal"><span class="pre">DecimalTuple(sign,</span> <span class="pre">digits,</span> <span class="pre">exponent)</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.canonical">
<tt class="descname">canonical</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.canonical" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the canonical encoding of the argument.  Currently, the encoding of
a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance is always canonical, so this operation returns
its argument unchanged.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.compare">
<tt class="descname">compare</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.compare" title="Permalink to this definition">¶</a></dt>
<dd><p>Compare the values of two Decimal instances.  <a class="reference internal" href="#decimal.Decimal.compare" title="decimal.Decimal.compare"><tt class="xref py py-meth docutils literal"><span class="pre">compare()</span></tt></a> returns a
Decimal instance, and if either operand is a NaN then the result is a
NaN:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">a</span> <span class="ow">or</span> <span class="n">b</span> <span class="ow">is</span> <span class="n">a</span> <span class="n">NaN</span>  <span class="o">==&gt;</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;NaN&#39;</span><span class="p">)</span>
<span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span>            <span class="o">==&gt;</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;-1&#39;</span><span class="p">)</span>
<span class="n">a</span> <span class="o">==</span> <span class="n">b</span>           <span class="o">==&gt;</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;0&#39;</span><span class="p">)</span>
<span class="n">a</span> <span class="o">&gt;</span> <span class="n">b</span>            <span class="o">==&gt;</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;1&#39;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.compare_signal">
<tt class="descname">compare_signal</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.compare_signal" title="Permalink to this definition">¶</a></dt>
<dd><p>This operation is identical to the <a class="reference internal" href="#decimal.Decimal.compare" title="decimal.Decimal.compare"><tt class="xref py py-meth docutils literal"><span class="pre">compare()</span></tt></a> method, except that all
NaNs signal.  That is, if neither operand is a signaling NaN then any
quiet NaN operand is treated as though it were a signaling NaN.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.compare_total">
<tt class="descname">compare_total</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#decimal.Decimal.compare_total" title="Permalink to this definition">¶</a></dt>
<dd><p>Compare two operands using their abstract representation rather than their
numerical value.  Similar to the <a class="reference internal" href="#decimal.Decimal.compare" title="decimal.Decimal.compare"><tt class="xref py py-meth docutils literal"><span class="pre">compare()</span></tt></a> method, but the result
gives a total ordering on <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instances.  Two
<a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instances with the same numeric value but different
representations compare unequal in this ordering:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;12.0&#39;</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="s">&#39;12&#39;</span><span class="p">))</span>
<span class="go">Decimal(&#39;-1&#39;)</span>
</pre></div>
</div>
<p>Quiet and signaling NaNs are also included in the total ordering.  The
result of this function is <tt class="docutils literal"><span class="pre">Decimal('0')</span></tt> if both operands have the same
representation, <tt class="docutils literal"><span class="pre">Decimal('-1')</span></tt> if the first operand is lower in the
total order than the second, and <tt class="docutils literal"><span class="pre">Decimal('1')</span></tt> if the first operand is
higher in the total order than the second operand.  See the specification
for details of the total order.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.compare_total_mag">
<tt class="descname">compare_total_mag</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#decimal.Decimal.compare_total_mag" title="Permalink to this definition">¶</a></dt>
<dd><p>Compare two operands using their abstract representation rather than their
value as in <a class="reference internal" href="#decimal.Decimal.compare_total" title="decimal.Decimal.compare_total"><tt class="xref py py-meth docutils literal"><span class="pre">compare_total()</span></tt></a>, but ignoring the sign of each operand.
<tt class="docutils literal"><span class="pre">x.compare_total_mag(y)</span></tt> is equivalent to
<tt class="docutils literal"><span class="pre">x.copy_abs().compare_total(y.copy_abs())</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.conjugate">
<tt class="descname">conjugate</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.conjugate" title="Permalink to this definition">¶</a></dt>
<dd><p>Just returns self, this method is only to comply with the Decimal
Specification.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.copy_abs">
<tt class="descname">copy_abs</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.copy_abs" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the absolute value of the argument.  This operation is unaffected
by the context and is quiet: no flags are changed and no rounding is
performed.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.copy_negate">
<tt class="descname">copy_negate</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.copy_negate" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the negation of the argument.  This operation is unaffected by the
context and is quiet: no flags are changed and no rounding is performed.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.copy_sign">
<tt class="descname">copy_sign</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#decimal.Decimal.copy_sign" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a copy of the first operand with the sign set to be the same as the
sign of the second operand.  For example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;2.3&#39;</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="s">&#39;-1.5&#39;</span><span class="p">))</span>
<span class="go">Decimal(&#39;-2.3&#39;)</span>
</pre></div>
</div>
<p>This operation is unaffected by the context and is quiet: no flags are
changed and no rounding is performed.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.exp">
<tt class="descname">exp</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.exp" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the value of the (natural) exponential function <tt class="docutils literal"><span class="pre">e**x</span></tt> at the
given number.  The result is correctly rounded using the
<tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></tt> rounding mode.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;2.718281828459045235360287471&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;2.561702493119680037517373933E+139&#39;)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.from_float">
<tt class="descname">from_float</tt><big>(</big><em>f</em><big>)</big><a class="headerlink" href="#decimal.Decimal.from_float" title="Permalink to this definition">¶</a></dt>
<dd><p>Classmethod that converts a float to a decimal number, exactly.</p>
<p>Note <cite>Decimal.from_float(0.1)</cite> is not the same as <cite>Decimal(&#8216;0.1&#8217;)</cite>.
Since 0.1 is not exactly representable in binary floating point, the
value is stored as the nearest representable value which is
<cite>0x1.999999999999ap-4</cite>.  That equivalent value in decimal is
<cite>0.1000000000000000055511151231257827021181583404541015625</cite>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">From Python 3.2 onwards, a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance
can also be constructed directly from a <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a>.</p>
</div>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;0.1000000000000000055511151231257827021181583404541015625&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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="s">&#39;nan&#39;</span><span class="p">))</span>
<span class="go">Decimal(&#39;NaN&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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="s">&#39;inf&#39;</span><span class="p">))</span>
<span class="go">Decimal(&#39;Infinity&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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="s">&#39;-inf&#39;</span><span class="p">))</span>
<span class="go">Decimal(&#39;-Infinity&#39;)</span>
</pre></div>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.fma">
<tt class="descname">fma</tt><big>(</big><em>other</em>, <em>third</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.fma" title="Permalink to this definition">¶</a></dt>
<dd><p>Fused multiply-add.  Return self*other+third with no rounding of the
intermediate product self*other.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;11&#39;)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.is_canonical">
<tt class="descname">is_canonical</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_canonical" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a> if the argument is canonical and <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const xref docutils literal"><span class="pre">False</span></tt></a>
otherwise.  Currently, a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance is always canonical, so
this operation always returns <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.is_finite">
<tt class="descname">is_finite</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_finite" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a> if the argument is a finite number, and
<a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const xref docutils literal"><span class="pre">False</span></tt></a> if the argument is an infinity or a NaN.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.is_infinite">
<tt class="descname">is_infinite</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_infinite" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a> if the argument is either positive or negative
infinity and <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const xref docutils literal"><span class="pre">False</span></tt></a> otherwise.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.is_nan">
<tt class="descname">is_nan</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_nan" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a> if the argument is a (quiet or signaling) NaN and
<a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const xref docutils literal"><span class="pre">False</span></tt></a> otherwise.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.is_normal">
<tt class="descname">is_normal</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_normal" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a> if the argument is a <em>normal</em> finite number.  Return
<a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const xref docutils literal"><span class="pre">False</span></tt></a> if the argument is zero, subnormal, infinite or a NaN.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.is_qnan">
<tt class="descname">is_qnan</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_qnan" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a> if the argument is a quiet NaN, and
<a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const xref docutils literal"><span class="pre">False</span></tt></a> otherwise.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.is_signed">
<tt class="descname">is_signed</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_signed" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a> if the argument has a negative sign and
<a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const xref docutils literal"><span class="pre">False</span></tt></a> otherwise.  Note that zeros and NaNs can both carry signs.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.is_snan">
<tt class="descname">is_snan</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_snan" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a> if the argument is a signaling NaN and <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const xref docutils literal"><span class="pre">False</span></tt></a>
otherwise.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.is_subnormal">
<tt class="descname">is_subnormal</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_subnormal" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a> if the argument is subnormal, and <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const xref docutils literal"><span class="pre">False</span></tt></a>
otherwise.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.is_zero">
<tt class="descname">is_zero</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_zero" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a> if the argument is a (positive or negative) zero and
<a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const xref docutils literal"><span class="pre">False</span></tt></a> otherwise.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.ln">
<tt class="descname">ln</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.ln" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the natural (base e) logarithm of the operand.  The result is
correctly rounded using the <tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></tt> rounding mode.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.log10">
<tt class="descname">log10</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.log10" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the base ten logarithm of the operand.  The result is correctly
rounded using the <tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></tt> rounding mode.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.logb">
<tt class="descname">logb</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.logb" title="Permalink to this definition">¶</a></dt>
<dd><p>For a nonzero number, return the adjusted exponent of its operand as a
<a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance.  If the operand is a zero then
<tt class="docutils literal"><span class="pre">Decimal('-Infinity')</span></tt> is returned and the <a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><tt class="xref py py-const docutils literal"><span class="pre">DivisionByZero</span></tt></a> flag
is raised.  If the operand is an infinity then <tt class="docutils literal"><span class="pre">Decimal('Infinity')</span></tt> is
returned.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.logical_and">
<tt class="descname">logical_and</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.logical_and" title="Permalink to this definition">¶</a></dt>
<dd><p><a class="reference internal" href="#decimal.Decimal.logical_and" title="decimal.Decimal.logical_and"><tt class="xref py py-meth docutils literal"><span class="pre">logical_and()</span></tt></a> is a logical operation which takes two <em>logical
operands</em> (see <a class="reference internal" href="#logical-operands-label"><em>Logical operands</em></a>).  The result is the
digit-wise <tt class="docutils literal"><span class="pre">and</span></tt> of the two operands.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.logical_invert">
<tt class="descname">logical_invert</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.logical_invert" title="Permalink to this definition">¶</a></dt>
<dd><p><a class="reference internal" href="#decimal.Decimal.logical_invert" title="decimal.Decimal.logical_invert"><tt class="xref py py-meth docutils literal"><span class="pre">logical_invert()</span></tt></a> is a logical operation.  The
result is the digit-wise inversion of the operand.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.logical_or">
<tt class="descname">logical_or</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.logical_or" title="Permalink to this definition">¶</a></dt>
<dd><p><a class="reference internal" href="#decimal.Decimal.logical_or" title="decimal.Decimal.logical_or"><tt class="xref py py-meth docutils literal"><span class="pre">logical_or()</span></tt></a> is a logical operation which takes two <em>logical
operands</em> (see <a class="reference internal" href="#logical-operands-label"><em>Logical operands</em></a>).  The result is the
digit-wise <tt class="docutils literal"><span class="pre">or</span></tt> of the two operands.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.logical_xor">
<tt class="descname">logical_xor</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.logical_xor" title="Permalink to this definition">¶</a></dt>
<dd><p><a class="reference internal" href="#decimal.Decimal.logical_xor" title="decimal.Decimal.logical_xor"><tt class="xref py py-meth docutils literal"><span class="pre">logical_xor()</span></tt></a> is a logical operation which takes two <em>logical
operands</em> (see <a class="reference internal" href="#logical-operands-label"><em>Logical operands</em></a>).  The result is the
digit-wise exclusive or of the two operands.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.max">
<tt class="descname">max</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.max" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <tt class="docutils literal"><span class="pre">max(self,</span> <span class="pre">other)</span></tt> except that the context rounding rule is applied
before returning and that <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> values are either signaled or
ignored (depending on the context and whether they are signaling or
quiet).</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.max_mag">
<tt class="descname">max_mag</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.max_mag" title="Permalink to this definition">¶</a></dt>
<dd><p>Similar to the <a class="reference internal" href="#decimal.Decimal.max" title="decimal.Decimal.max"><tt class="xref py py-meth docutils literal"><span class="pre">max()</span></tt></a> method, but the comparison is done using the
absolute values of the operands.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.min">
<tt class="descname">min</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.min" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <tt class="docutils literal"><span class="pre">min(self,</span> <span class="pre">other)</span></tt> except that the context rounding rule is applied
before returning and that <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> values are either signaled or
ignored (depending on the context and whether they are signaling or
quiet).</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.min_mag">
<tt class="descname">min_mag</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.min_mag" title="Permalink to this definition">¶</a></dt>
<dd><p>Similar to the <a class="reference internal" href="#decimal.Decimal.min" title="decimal.Decimal.min"><tt class="xref py py-meth docutils literal"><span class="pre">min()</span></tt></a> method, but the comparison is done using the
absolute values of the operands.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.next_minus">
<tt class="descname">next_minus</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.next_minus" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the largest number representable in the given context (or in the
current thread&#8217;s context if no context is given) that is smaller than the
given operand.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.next_plus">
<tt class="descname">next_plus</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.next_plus" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the smallest number representable in the given context (or in the
current thread&#8217;s context if no context is given) that is larger than the
given operand.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.next_toward">
<tt class="descname">next_toward</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.next_toward" title="Permalink to this definition">¶</a></dt>
<dd><p>If the two operands are unequal, return the number closest to the first
operand in the direction of the second operand.  If both operands are
numerically equal, return a copy of the first operand with the sign set to
be the same as the sign of the second operand.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.normalize">
<tt class="descname">normalize</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.normalize" title="Permalink to this definition">¶</a></dt>
<dd><p>Normalize the number by stripping the rightmost trailing zeros and
converting any result equal to <tt class="xref py py-const docutils literal"><span class="pre">Decimal('0')</span></tt> to
<tt class="xref py py-const docutils literal"><span class="pre">Decimal('0e0')</span></tt>. Used for producing canonical values for attributes
of an equivalence class. For example, <tt class="docutils literal"><span class="pre">Decimal('32.100')</span></tt> and
<tt class="docutils literal"><span class="pre">Decimal('0.321000e+2')</span></tt> both normalize to the equivalent value
<tt class="docutils literal"><span class="pre">Decimal('32.1')</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.number_class">
<tt class="descname">number_class</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.number_class" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string describing the <em>class</em> of the operand.  The returned value
is one of the following ten strings.</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">&quot;-Infinity&quot;</span></tt>, indicating that the operand is negative infinity.</li>
<li><tt class="docutils literal"><span class="pre">&quot;-Normal&quot;</span></tt>, indicating that the operand is a negative normal number.</li>
<li><tt class="docutils literal"><span class="pre">&quot;-Subnormal&quot;</span></tt>, indicating that the operand is negative and subnormal.</li>
<li><tt class="docutils literal"><span class="pre">&quot;-Zero&quot;</span></tt>, indicating that the operand is a negative zero.</li>
<li><tt class="docutils literal"><span class="pre">&quot;+Zero&quot;</span></tt>, indicating that the operand is a positive zero.</li>
<li><tt class="docutils literal"><span class="pre">&quot;+Subnormal&quot;</span></tt>, indicating that the operand is positive and subnormal.</li>
<li><tt class="docutils literal"><span class="pre">&quot;+Normal&quot;</span></tt>, indicating that the operand is a positive normal number.</li>
<li><tt class="docutils literal"><span class="pre">&quot;+Infinity&quot;</span></tt>, indicating that the operand is positive infinity.</li>
<li><tt class="docutils literal"><span class="pre">&quot;NaN&quot;</span></tt>, indicating that the operand is a quiet NaN (Not a Number).</li>
<li><tt class="docutils literal"><span class="pre">&quot;sNaN&quot;</span></tt>, indicating that the operand is a signaling NaN.</li>
</ul>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.quantize">
<tt class="descname">quantize</tt><big>(</big><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><big>)</big><a class="headerlink" href="#decimal.Decimal.quantize" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a value equal to the first operand after rounding and having the
exponent of the second operand.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;1.41421356&#39;</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="s">&#39;1.000&#39;</span><span class="p">))</span>
<span class="go">Decimal(&#39;1.414&#39;)</span>
</pre></div>
</div>
<p>Unlike other operations, if the length of the coefficient after the
quantize operation would be greater than precision, then an
<a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><tt class="xref py py-const docutils literal"><span class="pre">InvalidOperation</span></tt></a> is signaled. This guarantees that, unless there
is an error condition, the quantized exponent is always equal to that of
the right-hand operand.</p>
<p>Also unlike other operations, quantize never signals Underflow, even if
the result is subnormal and inexact.</p>
<p>If the exponent of the second operand is larger than that of the first
then rounding may be necessary.  In this case, the rounding mode is
determined by the <tt class="docutils literal"><span class="pre">rounding</span></tt> argument if given, else by the given
<tt class="docutils literal"><span class="pre">context</span></tt> argument; if neither argument is given the rounding mode of
the current thread&#8217;s context is used.</p>
<p>If <em>watchexp</em> is set (default), then an error is returned whenever the
resulting exponent is greater than <tt class="xref py py-attr docutils literal"><span class="pre">Emax</span></tt> or less than
<tt class="xref py py-attr docutils literal"><span class="pre">Etiny</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.radix">
<tt class="descname">radix</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.radix" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <tt class="docutils literal"><span class="pre">Decimal(10)</span></tt>, the radix (base) in which the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a>
class does all its arithmetic.  Included for compatibility with the
specification.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.remainder_near">
<tt class="descname">remainder_near</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.remainder_near" title="Permalink to this definition">¶</a></dt>
<dd><p>Compute the modulo as either a positive or negative value depending on
which is closest to zero.  For instance, <tt class="docutils literal"><span class="pre">Decimal(10).remainder_near(6)</span></tt>
returns <tt class="docutils literal"><span class="pre">Decimal('-2')</span></tt> which is closer to zero than <tt class="docutils literal"><span class="pre">Decimal('4')</span></tt>.</p>
<p>If both are equally close, the one chosen will have the same sign as
<em>self</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.rotate">
<tt class="descname">rotate</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.rotate" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the result of rotating the digits of the first operand by an amount
specified by the second operand.  The second operand must be an integer in
the range -precision through precision.  The absolute value of the second
operand gives the number of places to rotate.  If the second operand is
positive then rotation is to the left; otherwise rotation is to the right.
The coefficient of the first operand is padded on the left with zeros to
length precision if necessary.  The sign and exponent of the first operand
are unchanged.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.same_quantum">
<tt class="descname">same_quantum</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.same_quantum" title="Permalink to this definition">¶</a></dt>
<dd><p>Test whether self and other have the same exponent or whether both are
<tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.scaleb">
<tt class="descname">scaleb</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.scaleb" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the first operand with exponent adjusted by the second.
Equivalently, return the first operand multiplied by <tt class="docutils literal"><span class="pre">10**other</span></tt>.  The
second operand must be an integer.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.shift">
<tt class="descname">shift</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.shift" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the result of shifting the digits of the first operand by an amount
specified by the second operand.  The second operand must be an integer in
the range -precision through precision.  The absolute value of the second
operand gives the number of places to shift.  If the second operand is
positive then the shift is to the left; otherwise the shift is to the
right.  Digits shifted into the coefficient are zeros.  The sign and
exponent of the first operand are unchanged.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.sqrt">
<tt class="descname">sqrt</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.sqrt" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the square root of the argument to full precision.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.to_eng_string">
<tt class="descname">to_eng_string</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.to_eng_string" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert to an engineering-type string.</p>
<p>Engineering notation has an exponent which is a multiple of 3, so there
are up to 3 digits left of the decimal place.  For example, converts
<tt class="docutils literal"><span class="pre">Decimal('123E+1')</span></tt> to <tt class="docutils literal"><span class="pre">Decimal('1.23E+3')</span></tt></p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.to_integral">
<tt class="descname">to_integral</tt><big>(</big><span class="optional">[</span><em>rounding</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.to_integral" title="Permalink to this definition">¶</a></dt>
<dd><p>Identical to the <a class="reference internal" href="#decimal.Decimal.to_integral_value" title="decimal.Decimal.to_integral_value"><tt class="xref py py-meth docutils literal"><span class="pre">to_integral_value()</span></tt></a> method.  The <tt class="docutils literal"><span class="pre">to_integral</span></tt>
name has been kept for compatibility with older versions.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.to_integral_exact">
<tt class="descname">to_integral_exact</tt><big>(</big><span class="optional">[</span><em>rounding</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.to_integral_exact" title="Permalink to this definition">¶</a></dt>
<dd><p>Round to the nearest integer, signaling <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-const docutils literal"><span class="pre">Inexact</span></tt></a> or
<a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><tt class="xref py py-const docutils literal"><span class="pre">Rounded</span></tt></a> as appropriate if rounding occurs.  The rounding mode is
determined by the <tt class="docutils literal"><span class="pre">rounding</span></tt> parameter if given, else by the given
<tt class="docutils literal"><span class="pre">context</span></tt>.  If neither parameter is given then the rounding mode of the
current context is used.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Decimal.to_integral_value">
<tt class="descname">to_integral_value</tt><big>(</big><span class="optional">[</span><em>rounding</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.to_integral_value" title="Permalink to this definition">¶</a></dt>
<dd><p>Round to the nearest integer without signaling <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-const docutils literal"><span class="pre">Inexact</span></tt></a> or
<a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><tt class="xref py py-const docutils literal"><span class="pre">Rounded</span></tt></a>.  If given, applies <em>rounding</em>; otherwise, uses the
rounding method in either the supplied <em>context</em> or the current context.</p>
</dd></dl>

</dd></dl>

<div class="section" id="logical-operands">
<span id="logical-operands-label"></span><h3>8.4.2.1. Logical operands<a class="headerlink" href="#logical-operands" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="xref py py-meth docutils literal"><span class="pre">logical_and()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">logical_invert()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">logical_or()</span></tt>,
and <tt class="xref py py-meth docutils literal"><span class="pre">logical_xor()</span></tt> methods expect their arguments to be <em>logical
operands</em>.  A <em>logical operand</em> is a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance whose
exponent and sign are both zero, and whose digits are all either
<tt class="xref py py-const docutils literal"><span class="pre">0</span></tt> or <tt class="xref py py-const docutils literal"><span class="pre">1</span></tt>.</p>
</div>
</div>
<div class="section" id="context-objects">
<span id="decimal-context"></span><h2>8.4.3. Context objects<a class="headerlink" href="#context-objects" title="Permalink to this headline">¶</a></h2>
<p>Contexts are environments for arithmetic operations.  They govern precision, set
rules for rounding, determine which signals are treated as exceptions, and limit
the range for exponents.</p>
<p>Each thread has its own current context which is accessed or changed using the
<a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a> and <a class="reference internal" href="#decimal.setcontext" title="decimal.setcontext"><tt class="xref py py-func docutils literal"><span class="pre">setcontext()</span></tt></a> functions:</p>
<dl class="function">
<dt id="decimal.getcontext">
<tt class="descclassname">decimal.</tt><tt class="descname">getcontext</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.getcontext" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the current context for the active thread.</p>
</dd></dl>

<dl class="function">
<dt id="decimal.setcontext">
<tt class="descclassname">decimal.</tt><tt class="descname">setcontext</tt><big>(</big><em>c</em><big>)</big><a class="headerlink" href="#decimal.setcontext" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the current context for the active thread to <em>c</em>.</p>
</dd></dl>

<p>You can also use the <a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a> statement and the <a class="reference internal" href="#decimal.localcontext" title="decimal.localcontext"><tt class="xref py py-func docutils literal"><span class="pre">localcontext()</span></tt></a>
function to temporarily change the active context.</p>
<dl class="function">
<dt id="decimal.localcontext">
<tt class="descclassname">decimal.</tt><tt class="descname">localcontext</tt><big>(</big><span class="optional">[</span><em>c</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.localcontext" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a context manager that will set the current context for the active thread
to a copy of <em>c</em> on entry to the with-statement and restore the previous context
when exiting the with-statement. If no context is specified, a copy of the
current context is used.</p>
<p>For example, the following code sets the current decimal precision to 42 places,
performs a calculation, and then automatically restores the previous context:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">decimal</span> <span class="k">import</span> <span class="n">localcontext</span>

<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>
    <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="c"># Perform a high precision calculation</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">calculate_something</span><span class="p">()</span>
<span class="n">s</span> <span class="o">=</span> <span class="o">+</span><span class="n">s</span>  <span class="c"># Round the final result back to the default precision</span>
</pre></div>
</div>
</dd></dl>

<p>New contexts can also be created using the <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> constructor
described below. In addition, the module provides three pre-made contexts:</p>
<dl class="class">
<dt id="decimal.BasicContext">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">BasicContext</tt><a class="headerlink" href="#decimal.BasicContext" title="Permalink to this definition">¶</a></dt>
<dd><p>This is a standard context defined by the General Decimal Arithmetic
Specification.  Precision is set to nine.  Rounding is set to
<tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_UP</span></tt>.  All flags are cleared.  All traps are enabled (treated
as exceptions) except <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-const docutils literal"><span class="pre">Inexact</span></tt></a>, <a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><tt class="xref py py-const docutils literal"><span class="pre">Rounded</span></tt></a>, and
<a class="reference internal" href="#decimal.Subnormal" title="decimal.Subnormal"><tt class="xref py py-const docutils literal"><span class="pre">Subnormal</span></tt></a>.</p>
<p>Because many of the traps are enabled, this context is useful for debugging.</p>
</dd></dl>

<dl class="class">
<dt id="decimal.ExtendedContext">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">ExtendedContext</tt><a class="headerlink" href="#decimal.ExtendedContext" title="Permalink to this definition">¶</a></dt>
<dd><p>This is a standard context defined by the General Decimal Arithmetic
Specification.  Precision is set to nine.  Rounding is set to
<tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></tt>.  All flags are cleared.  No traps are enabled (so that
exceptions are not raised during computations).</p>
<p>Because the traps are disabled, this context is useful for applications that
prefer to have result value of <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> or <tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt> instead of
raising exceptions.  This allows an application to complete a run in the
presence of conditions that would otherwise halt the program.</p>
</dd></dl>

<dl class="class">
<dt id="decimal.DefaultContext">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">DefaultContext</tt><a class="headerlink" href="#decimal.DefaultContext" title="Permalink to this definition">¶</a></dt>
<dd><p>This context is used by the <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> constructor as a prototype for new
contexts.  Changing a field (such a precision) has the effect of changing the
default for new contexts created by the <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> constructor.</p>
<p>This context is most useful in multi-threaded environments.  Changing one of the
fields before threads are started has the effect of setting system-wide
defaults.  Changing the fields after threads have started is not recommended as
it would require thread synchronization to prevent race conditions.</p>
<p>In single threaded environments, it is preferable to not use this context at
all.  Instead, simply create contexts explicitly as described below.</p>
<p>The default values are precision=28, rounding=ROUND_HALF_EVEN, and enabled traps
for Overflow, InvalidOperation, and DivisionByZero.</p>
</dd></dl>

<p>In addition to the three supplied contexts, new contexts can be created with the
<a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> constructor.</p>
<dl class="class">
<dt id="decimal.Context">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Context</tt><big>(</big><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=None</em>, <em>clamp=None</em><big>)</big><a class="headerlink" href="#decimal.Context" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a new context.  If a field is not specified or is <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const xref docutils literal"><span class="pre">None</span></tt></a>, the
default values are copied from the <a class="reference internal" href="#decimal.DefaultContext" title="decimal.DefaultContext"><tt class="xref py py-const docutils literal"><span class="pre">DefaultContext</span></tt></a>.  If the <em>flags</em>
field is not specified or is <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const xref docutils literal"><span class="pre">None</span></tt></a>, all flags are cleared.</p>
<p>The <em>prec</em> field is a positive integer that sets the precision for arithmetic
operations in the context.</p>
<p>The <em>rounding</em> option is one of:</p>
<ul class="simple">
<li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_CEILING</span></tt> (towards <tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt>),</li>
<li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_DOWN</span></tt> (towards zero),</li>
<li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_FLOOR</span></tt> (towards <tt class="xref py py-const docutils literal"><span class="pre">-Infinity</span></tt>),</li>
<li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_DOWN</span></tt> (to nearest with ties going towards zero),</li>
<li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></tt> (to nearest with ties going to nearest even integer),</li>
<li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_UP</span></tt> (to nearest with ties going away from zero), or</li>
<li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_UP</span></tt> (away from zero).</li>
<li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_05UP</span></tt> (away from zero if last digit after rounding towards zero
would have been 0 or 5; otherwise towards zero)</li>
</ul>
<p>The <em>traps</em> and <em>flags</em> fields list any signals to be set. Generally, new
contexts should only set traps and leave the flags clear.</p>
<p>The <em>Emin</em> and <em>Emax</em> fields are integers specifying the outer limits allowable
for exponents.</p>
<p>The <em>capitals</em> field is either <tt class="xref py py-const docutils literal"><span class="pre">0</span></tt> or <tt class="xref py py-const docutils literal"><span class="pre">1</span></tt> (the default). If set to
<tt class="xref py py-const docutils literal"><span class="pre">1</span></tt>, exponents are printed with a capital <tt class="xref py py-const docutils literal"><span class="pre">E</span></tt>; otherwise, a
lowercase <tt class="xref py py-const docutils literal"><span class="pre">e</span></tt> is used: <tt class="xref py py-const docutils literal"><span class="pre">Decimal('6.02e+23')</span></tt>.</p>
<p>The <em>clamp</em> field is either <tt class="xref py py-const docutils literal"><span class="pre">0</span></tt> (the default) or <tt class="xref py py-const docutils literal"><span class="pre">1</span></tt>.
If set to <tt class="xref py py-const docutils literal"><span class="pre">1</span></tt>, the exponent <tt class="docutils literal"><span class="pre">e</span></tt> of a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a>
instance representable in this context is strictly limited to the
range <tt 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> <span class="pre">&lt;=</span> <span class="pre">e</span> <span class="pre">&lt;=</span> <span class="pre">Emax</span> <span class="pre">-</span> <span class="pre">prec</span> <span class="pre">+</span> <span class="pre">1</span></tt>.  If <em>clamp</em> is
<tt class="xref py py-const docutils literal"><span class="pre">0</span></tt> then a weaker condition holds: the adjusted exponent of
the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance is at most <tt class="docutils literal"><span class="pre">Emax</span></tt>.  When <em>clamp</em> is
<tt class="xref py py-const docutils literal"><span class="pre">1</span></tt>, a large normal number will, where possible, have its
exponent reduced and a corresponding number of zeros added to its
coefficient, in order to fit the exponent constraints; this
preserves the value of the number but loses information about
significant trailing zeros.  For example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Context</span><span class="p">(</span><span class="n">prec</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">Emax</span><span class="o">=</span><span class="mi">999</span><span class="p">,</span> <span class="n">clamp</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">create_decimal</span><span class="p">(</span><span class="s">&#39;1.23e999&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;1.23000E+999&#39;)</span>
</pre></div>
</div>
<p>A <em>clamp</em> value of <tt class="xref py py-const docutils literal"><span class="pre">1</span></tt> allows compatibility with the
fixed-width decimal interchange formats specified in IEEE 754.</p>
<p>The <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> class defines several general purpose methods as well as
a large number of methods for doing arithmetic directly in a given context.
In addition, for each of the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> methods described above (with
the exception of the <tt class="xref py py-meth docutils literal"><span class="pre">adjusted()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">as_tuple()</span></tt> methods) there is
a corresponding <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> method.  For example, for a <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a>
instance <tt class="docutils literal"><span class="pre">C</span></tt> and <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance <tt class="docutils literal"><span class="pre">x</span></tt>, <tt class="docutils literal"><span class="pre">C.exp(x)</span></tt> is
equivalent to <tt class="docutils literal"><span class="pre">x.exp(context=C)</span></tt>.  Each <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> method accepts a
Python integer (an instance of <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a>) anywhere that a
Decimal instance is accepted.</p>
<dl class="method">
<dt id="decimal.Context.clear_flags">
<tt class="descname">clear_flags</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Context.clear_flags" title="Permalink to this definition">¶</a></dt>
<dd><p>Resets all of the flags to <tt class="xref py py-const docutils literal"><span class="pre">0</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.copy">
<tt class="descname">copy</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Context.copy" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a duplicate of the context.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.copy_decimal">
<tt class="descname">copy_decimal</tt><big>(</big><em>num</em><big>)</big><a class="headerlink" href="#decimal.Context.copy_decimal" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a copy of the Decimal instance num.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.create_decimal">
<tt class="descname">create_decimal</tt><big>(</big><em>num</em><big>)</big><a class="headerlink" href="#decimal.Context.create_decimal" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a new Decimal instance from <em>num</em> but using <em>self</em> as
context. Unlike the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> constructor, the context precision,
rounding method, flags, and traps are applied to the conversion.</p>
<p>This is useful because constants are often given to a greater precision
than is needed by the application.  Another benefit is that rounding
immediately eliminates unintended effects from digits beyond the current
precision. In the following example, using unrounded inputs means that
adding zero to a sum can change the result:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;3.4445&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;1.0023&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;4.45&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;3.4445&#39;</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="s">&#39;1.0023&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;4.44&#39;)</span>
</pre></div>
</div>
<p>This method implements the to-number operation of the IBM specification.
If the argument is a string, no leading or trailing whitespace is
permitted.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.create_decimal_from_float">
<tt class="descname">create_decimal_from_float</tt><big>(</big><em>f</em><big>)</big><a class="headerlink" href="#decimal.Context.create_decimal_from_float" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a new Decimal instance from a float <em>f</em> but rounding using <em>self</em>
as the context.  Unlike the <a class="reference internal" href="#decimal.Decimal.from_float" title="decimal.Decimal.from_float"><tt class="xref py py-meth docutils literal"><span class="pre">Decimal.from_float()</span></tt></a> class method,
the context precision, rounding method, flags, and traps are applied to
the conversion.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;3.1415&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </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>
<span class="gt">Traceback (most recent call last):</span>
    <span class="o">...</span>
<span class="nc">decimal.Inexact</span>: <span class="n-Identifier">None</span>
</pre></div>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.Etiny">
<tt class="descname">Etiny</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Context.Etiny" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a value equal to <tt 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></tt> which is the minimum exponent
value for subnormal results.  When underflow occurs, the exponent is set
to <a class="reference internal" href="#decimal.Context.Etiny" title="decimal.Context.Etiny"><tt class="xref py py-const docutils literal"><span class="pre">Etiny</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.Etop">
<tt class="descname">Etop</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Context.Etop" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a value equal to <tt 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></tt>.</p>
</dd></dl>

<p>The usual approach to working with decimals is to create <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a>
instances and then apply arithmetic operations which take place within the
current context for the active thread.  An alternative approach is to use
context methods for calculating within a specific context.  The methods are
similar to those for the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> class and are only briefly
recounted here.</p>
<dl class="method">
<dt id="decimal.Context.abs">
<tt class="descname">abs</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.abs" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the absolute value of <em>x</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.add">
<tt class="descname">add</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.add" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the sum of <em>x</em> and <em>y</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.canonical">
<tt class="descname">canonical</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.canonical" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the same Decimal object <em>x</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.compare">
<tt class="descname">compare</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.compare" title="Permalink to this definition">¶</a></dt>
<dd><p>Compares <em>x</em> and <em>y</em> numerically.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.compare_signal">
<tt class="descname">compare_signal</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.compare_signal" title="Permalink to this definition">¶</a></dt>
<dd><p>Compares the values of the two operands numerically.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.compare_total">
<tt class="descname">compare_total</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.compare_total" title="Permalink to this definition">¶</a></dt>
<dd><p>Compares two operands using their abstract representation.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.compare_total_mag">
<tt class="descname">compare_total_mag</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.compare_total_mag" title="Permalink to this definition">¶</a></dt>
<dd><p>Compares two operands using their abstract representation, ignoring sign.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.copy_abs">
<tt class="descname">copy_abs</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.copy_abs" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a copy of <em>x</em> with the sign set to 0.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.copy_negate">
<tt class="descname">copy_negate</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.copy_negate" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a copy of <em>x</em> with the sign inverted.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.copy_sign">
<tt class="descname">copy_sign</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.copy_sign" title="Permalink to this definition">¶</a></dt>
<dd><p>Copies the sign from <em>y</em> to <em>x</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.divide">
<tt class="descname">divide</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.divide" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <em>x</em> divided by <em>y</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.divide_int">
<tt class="descname">divide_int</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.divide_int" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <em>x</em> divided by <em>y</em>, truncated to an integer.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.divmod">
<tt class="descname">divmod</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.divmod" title="Permalink to this definition">¶</a></dt>
<dd><p>Divides two numbers and returns the integer part of the result.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.exp">
<tt class="descname">exp</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.exp" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <cite>e ** x</cite>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.fma">
<tt class="descname">fma</tt><big>(</big><em>x</em>, <em>y</em>, <em>z</em><big>)</big><a class="headerlink" href="#decimal.Context.fma" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <em>x</em> multiplied by <em>y</em>, plus <em>z</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.is_canonical">
<tt class="descname">is_canonical</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_canonical" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <em>x</em> is canonical; otherwise returns False.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.is_finite">
<tt class="descname">is_finite</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_finite" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <em>x</em> is finite; otherwise returns False.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.is_infinite">
<tt class="descname">is_infinite</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_infinite" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <em>x</em> is infinite; otherwise returns False.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.is_nan">
<tt class="descname">is_nan</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_nan" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <em>x</em> is a qNaN or sNaN; otherwise returns False.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.is_normal">
<tt class="descname">is_normal</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_normal" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <em>x</em> is a normal number; otherwise returns False.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.is_qnan">
<tt class="descname">is_qnan</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_qnan" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <em>x</em> is a quiet NaN; otherwise returns False.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.is_signed">
<tt class="descname">is_signed</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_signed" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <em>x</em> is negative; otherwise returns False.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.is_snan">
<tt class="descname">is_snan</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_snan" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <em>x</em> is a signaling NaN; otherwise returns False.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.is_subnormal">
<tt class="descname">is_subnormal</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_subnormal" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <em>x</em> is subnormal; otherwise returns False.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.is_zero">
<tt class="descname">is_zero</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_zero" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <em>x</em> is a zero; otherwise returns False.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.ln">
<tt class="descname">ln</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.ln" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the natural (base e) logarithm of <em>x</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.log10">
<tt class="descname">log10</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.log10" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the base 10 logarithm of <em>x</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.logb">
<tt class="descname">logb</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.logb" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the exponent of the magnitude of the operand&#8217;s MSD.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.logical_and">
<tt class="descname">logical_and</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.logical_and" title="Permalink to this definition">¶</a></dt>
<dd><p>Applies the logical operation <em>and</em> between each operand&#8217;s digits.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.logical_invert">
<tt class="descname">logical_invert</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.logical_invert" title="Permalink to this definition">¶</a></dt>
<dd><p>Invert all the digits in <em>x</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.logical_or">
<tt class="descname">logical_or</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.logical_or" title="Permalink to this definition">¶</a></dt>
<dd><p>Applies the logical operation <em>or</em> between each operand&#8217;s digits.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.logical_xor">
<tt class="descname">logical_xor</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.logical_xor" title="Permalink to this definition">¶</a></dt>
<dd><p>Applies the logical operation <em>xor</em> between each operand&#8217;s digits.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.max">
<tt class="descname">max</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.max" title="Permalink to this definition">¶</a></dt>
<dd><p>Compares two values numerically and returns the maximum.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.max_mag">
<tt class="descname">max_mag</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.max_mag" title="Permalink to this definition">¶</a></dt>
<dd><p>Compares the values numerically with their sign ignored.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.min">
<tt class="descname">min</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.min" title="Permalink to this definition">¶</a></dt>
<dd><p>Compares two values numerically and returns the minimum.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.min_mag">
<tt class="descname">min_mag</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.min_mag" title="Permalink to this definition">¶</a></dt>
<dd><p>Compares the values numerically with their sign ignored.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.minus">
<tt class="descname">minus</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.minus" title="Permalink to this definition">¶</a></dt>
<dd><p>Minus corresponds to the unary prefix minus operator in Python.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.multiply">
<tt class="descname">multiply</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.multiply" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the product of <em>x</em> and <em>y</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.next_minus">
<tt class="descname">next_minus</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.next_minus" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the largest representable number smaller than <em>x</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.next_plus">
<tt class="descname">next_plus</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.next_plus" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the smallest representable number larger than <em>x</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.next_toward">
<tt class="descname">next_toward</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.next_toward" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the number closest to <em>x</em>, in direction towards <em>y</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.normalize">
<tt class="descname">normalize</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.normalize" title="Permalink to this definition">¶</a></dt>
<dd><p>Reduces <em>x</em> to its simplest form.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.number_class">
<tt class="descname">number_class</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.number_class" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns an indication of the class of <em>x</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.plus">
<tt class="descname">plus</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.plus" title="Permalink to this definition">¶</a></dt>
<dd><p>Plus corresponds to the unary prefix plus operator in Python.  This
operation applies the context precision and rounding, so it is <em>not</em> an
identity operation.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.power">
<tt class="descname">power</tt><big>(</big><em>x</em>, <em>y</em><span class="optional">[</span>, <em>modulo</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Context.power" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <tt class="docutils literal"><span class="pre">x</span></tt> to the power of <tt class="docutils literal"><span class="pre">y</span></tt>, reduced modulo <tt class="docutils literal"><span class="pre">modulo</span></tt> if given.</p>
<p>With two arguments, compute <tt class="docutils literal"><span class="pre">x**y</span></tt>.  If <tt class="docutils literal"><span class="pre">x</span></tt> is negative then <tt class="docutils literal"><span class="pre">y</span></tt>
must be integral.  The result will be inexact unless <tt class="docutils literal"><span class="pre">y</span></tt> is integral and
the result is finite and can be expressed exactly in &#8216;precision&#8217; digits.
The result should always be correctly rounded, using the rounding mode of
the current thread&#8217;s context.</p>
<p>With three arguments, compute <tt class="docutils literal"><span class="pre">(x**y)</span> <span class="pre">%</span> <span class="pre">modulo</span></tt>.  For the three argument
form, the following restrictions on the arguments hold:</p>
<blockquote>
<div><ul class="simple">
<li>all three arguments must be integral</li>
<li><tt class="docutils literal"><span class="pre">y</span></tt> must be nonnegative</li>
<li>at least one of <tt class="docutils literal"><span class="pre">x</span></tt> or <tt class="docutils literal"><span class="pre">y</span></tt> must be nonzero</li>
<li><tt class="docutils literal"><span class="pre">modulo</span></tt> must be nonzero and have at most &#8216;precision&#8217; digits</li>
</ul>
</div></blockquote>
<p>The value resulting from <tt class="docutils literal"><span class="pre">Context.power(x,</span> <span class="pre">y,</span> <span class="pre">modulo)</span></tt> is
equal to the value that would be obtained by computing <tt class="docutils literal"><span class="pre">(x**y)</span>
<span class="pre">%</span> <span class="pre">modulo</span></tt> with unbounded precision, but is computed more
efficiently.  The exponent of the result is zero, regardless of
the exponents of <tt class="docutils literal"><span class="pre">x</span></tt>, <tt class="docutils literal"><span class="pre">y</span></tt> and <tt class="docutils literal"><span class="pre">modulo</span></tt>.  The result is
always exact.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.quantize">
<tt class="descname">quantize</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.quantize" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a value equal to <em>x</em> (rounded), having the exponent of <em>y</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.radix">
<tt class="descname">radix</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Context.radix" title="Permalink to this definition">¶</a></dt>
<dd><p>Just returns 10, as this is Decimal, :)</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.remainder">
<tt class="descname">remainder</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.remainder" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the remainder from integer division.</p>
<p>The sign of the result, if non-zero, is the same as that of the original
dividend.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.remainder_near">
<tt class="descname">remainder_near</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.remainder_near" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <tt 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></tt>, where <em>n</em> is the integer nearest the exact value
of <tt class="docutils literal"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></tt> (if the result is 0 then its sign will be the sign of <em>x</em>).</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.rotate">
<tt class="descname">rotate</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.rotate" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a rotated copy of <em>x</em>, <em>y</em> times.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.same_quantum">
<tt class="descname">same_quantum</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.same_quantum" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if the two operands have the same exponent.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.scaleb">
<tt class="descname">scaleb</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.scaleb" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the first operand after adding the second value its exp.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.shift">
<tt class="descname">shift</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.shift" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a shifted copy of <em>x</em>, <em>y</em> times.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.sqrt">
<tt class="descname">sqrt</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.sqrt" title="Permalink to this definition">¶</a></dt>
<dd><p>Square root of a non-negative number to context precision.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.subtract">
<tt class="descname">subtract</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.subtract" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the difference between <em>x</em> and <em>y</em>.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.to_eng_string">
<tt class="descname">to_eng_string</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.to_eng_string" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts a number to a string, using scientific notation.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.to_integral_exact">
<tt class="descname">to_integral_exact</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.to_integral_exact" title="Permalink to this definition">¶</a></dt>
<dd><p>Rounds to an integer.</p>
</dd></dl>

<dl class="method">
<dt id="decimal.Context.to_sci_string">
<tt class="descname">to_sci_string</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.to_sci_string" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts a number to a string using scientific notation.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="signals">
<span id="decimal-signals"></span><h2>8.4.4. Signals<a class="headerlink" href="#signals" title="Permalink to this headline">¶</a></h2>
<p>Signals represent conditions that arise during computation. Each corresponds to
one context flag and one context trap enabler.</p>
<p>The context flag is set whenever the condition is encountered. After the
computation, flags may be checked for informational purposes (for instance, to
determine whether a computation was exact). After checking the flags, be sure to
clear all flags before starting the next computation.</p>
<p>If the context&#8217;s trap enabler is set for the signal, then the condition causes a
Python exception to be raised.  For example, if the <a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><tt class="xref py py-class docutils literal"><span class="pre">DivisionByZero</span></tt></a> trap
is set, then a <a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><tt class="xref py py-exc docutils literal"><span class="pre">DivisionByZero</span></tt></a> exception is raised upon encountering the
condition.</p>
<dl class="class">
<dt id="decimal.Clamped">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Clamped</tt><a class="headerlink" href="#decimal.Clamped" title="Permalink to this definition">¶</a></dt>
<dd><p>Altered an exponent to fit representation constraints.</p>
<p>Typically, clamping occurs when an exponent falls outside the context&#8217;s
<tt class="xref py py-attr docutils literal"><span class="pre">Emin</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">Emax</span></tt> limits.  If possible, the exponent is reduced to
fit by adding zeros to the coefficient.</p>
</dd></dl>

<dl class="class">
<dt id="decimal.DecimalException">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">DecimalException</tt><a class="headerlink" href="#decimal.DecimalException" title="Permalink to this definition">¶</a></dt>
<dd><p>Base class for other signals and a subclass of <a class="reference internal" href="exceptions.html#ArithmeticError" title="ArithmeticError"><tt class="xref py py-exc docutils literal"><span class="pre">ArithmeticError</span></tt></a>.</p>
</dd></dl>

<dl class="class">
<dt id="decimal.DivisionByZero">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">DivisionByZero</tt><a class="headerlink" href="#decimal.DivisionByZero" title="Permalink to this definition">¶</a></dt>
<dd><p>Signals the division of a non-infinite number by zero.</p>
<p>Can occur with division, modulo division, or when raising a number to a negative
power.  If this signal is not trapped, returns <tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt> or
<tt class="xref py py-const docutils literal"><span class="pre">-Infinity</span></tt> with the sign determined by the inputs to the calculation.</p>
</dd></dl>

<dl class="class">
<dt id="decimal.Inexact">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Inexact</tt><a class="headerlink" href="#decimal.Inexact" title="Permalink to this definition">¶</a></dt>
<dd><p>Indicates that rounding occurred and the result is not exact.</p>
<p>Signals when non-zero digits were discarded during rounding. The rounded result
is returned.  The signal flag or trap is used to detect when results are
inexact.</p>
</dd></dl>

<dl class="class">
<dt id="decimal.InvalidOperation">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">InvalidOperation</tt><a class="headerlink" href="#decimal.InvalidOperation" title="Permalink to this definition">¶</a></dt>
<dd><p>An invalid operation was performed.</p>
<p>Indicates that an operation was requested that does not make sense. If not
trapped, returns <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>.  Possible causes include:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">Infinity</span> <span class="o">-</span> <span class="n">Infinity</span>
<span class="mi">0</span> <span class="o">*</span> <span class="n">Infinity</span>
<span class="n">Infinity</span> <span class="o">/</span> <span class="n">Infinity</span>
<span class="n">x</span> <span class="o">%</span> <span class="mi">0</span>
<span class="n">Infinity</span> <span class="o">%</span> <span class="n">x</span>
<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>
<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">&gt;</span> <span class="mi">0</span>
<span class="mi">0</span> <span class="o">**</span> <span class="mi">0</span>
<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>
<span class="n">x</span> <span class="o">**</span> <span class="n">Infinity</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="decimal.Overflow">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Overflow</tt><a class="headerlink" href="#decimal.Overflow" title="Permalink to this definition">¶</a></dt>
<dd><p>Numerical overflow.</p>
<p>Indicates the exponent is larger than <tt class="xref py py-attr docutils literal"><span class="pre">Emax</span></tt> after rounding has
occurred.  If not trapped, the result depends on the rounding mode, either
pulling inward to the largest representable finite number or rounding outward
to <tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt>.  In either case, <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-class docutils literal"><span class="pre">Inexact</span></tt></a> and <a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><tt class="xref py py-class docutils literal"><span class="pre">Rounded</span></tt></a>
are also signaled.</p>
</dd></dl>

<dl class="class">
<dt id="decimal.Rounded">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Rounded</tt><a class="headerlink" href="#decimal.Rounded" title="Permalink to this definition">¶</a></dt>
<dd><p>Rounding occurred though possibly no information was lost.</p>
<p>Signaled whenever rounding discards digits; even if those digits are zero
(such as rounding <tt class="xref py py-const docutils literal"><span class="pre">5.00</span></tt> to <tt class="xref py py-const docutils literal"><span class="pre">5.0</span></tt>).  If not trapped, returns
the result unchanged.  This signal is used to detect loss of significant
digits.</p>
</dd></dl>

<dl class="class">
<dt id="decimal.Subnormal">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Subnormal</tt><a class="headerlink" href="#decimal.Subnormal" title="Permalink to this definition">¶</a></dt>
<dd><p>Exponent was lower than <tt class="xref py py-attr docutils literal"><span class="pre">Emin</span></tt> prior to rounding.</p>
<p>Occurs when an operation result is subnormal (the exponent is too small). If
not trapped, returns the result unchanged.</p>
</dd></dl>

<dl class="class">
<dt id="decimal.Underflow">
<em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Underflow</tt><a class="headerlink" href="#decimal.Underflow" title="Permalink to this definition">¶</a></dt>
<dd><p>Numerical underflow with result rounded to zero.</p>
<p>Occurs when a subnormal result is pushed to zero by rounding. <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-class docutils literal"><span class="pre">Inexact</span></tt></a>
and <a class="reference internal" href="#decimal.Subnormal" title="decimal.Subnormal"><tt class="xref py py-class docutils literal"><span class="pre">Subnormal</span></tt></a> are also signaled.</p>
</dd></dl>

<p>The following table summarizes the hierarchy of signals:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">exceptions</span><span class="o">.</span><span class="n">ArithmeticError</span><span class="p">(</span><span class="n">exceptions</span><span class="o">.</span><span class="n">Exception</span><span class="p">)</span>
    <span class="n">DecimalException</span>
        <span class="n">Clamped</span>
        <span class="n">DivisionByZero</span><span class="p">(</span><span class="n">DecimalException</span><span class="p">,</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">ZeroDivisionError</span><span class="p">)</span>
        <span class="n">Inexact</span>
            <span class="n">Overflow</span><span class="p">(</span><span class="n">Inexact</span><span class="p">,</span> <span class="n">Rounded</span><span class="p">)</span>
            <span class="n">Underflow</span><span class="p">(</span><span class="n">Inexact</span><span class="p">,</span> <span class="n">Rounded</span><span class="p">,</span> <span class="n">Subnormal</span><span class="p">)</span>
        <span class="n">InvalidOperation</span>
        <span class="n">Rounded</span>
        <span class="n">Subnormal</span>
</pre></div>
</div>
</div>
<div class="section" id="floating-point-notes">
<span id="decimal-notes"></span><h2>8.4.5. Floating Point Notes<a class="headerlink" href="#floating-point-notes" title="Permalink to this headline">¶</a></h2>
<div class="section" id="mitigating-round-off-error-with-increased-precision">
<h3>8.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>
<p>The use of decimal floating point eliminates decimal representation error
(making it possible to represent <tt class="xref py py-const docutils literal"><span class="pre">0.1</span></tt> exactly); however, some operations
can still incur round-off error when non-zero digits exceed the fixed precision.</p>
<p>The effects of round-off error can be amplified by the addition or subtraction
of nearly offsetting quantities resulting in loss of significance.  Knuth
provides two instructive examples where rounded floating point arithmetic with
insufficient precision causes the breakdown of the associative and distributive
properties of addition:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="c"># Examples from Seminumerical Algorithms, Section 4.2.2.</span>
<span class="o">&gt;&gt;&gt;</span> <span class="kn">from</span> <span class="nn">decimal</span> <span class="k">import</span> <span class="n">Decimal</span><span class="p">,</span> <span class="n">getcontext</span>
<span class="o">&gt;&gt;&gt;</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">8</span>

<span class="o">&gt;&gt;&gt;</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="s">&#39;7.51111111&#39;</span><span class="p">)</span>
<span class="o">&gt;&gt;&gt;</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>
<span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;9.5111111&#39;</span><span class="p">)</span>
<span class="o">&gt;&gt;&gt;</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>
<span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;10&#39;</span><span class="p">)</span>

<span class="o">&gt;&gt;&gt;</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="s">&#39;6.0000003&#39;</span><span class="p">)</span>
<span class="o">&gt;&gt;&gt;</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>
<span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;0.01&#39;</span><span class="p">)</span>
<span class="o">&gt;&gt;&gt;</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>
<span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;0.0060000&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic  Specification."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> module makes it possible to restore the identities by
expanding the precision sufficiently to avoid loss of significance:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </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="s">&#39;7.51111111&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;9.51111111&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;9.51111111&#39;)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </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="s">&#39;6.0000003&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;0.0060000&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;0.0060000&#39;)</span>
</pre></div>
</div>
</div>
<div class="section" id="special-values">
<h3>8.4.5.2. Special values<a class="headerlink" href="#special-values" title="Permalink to this headline">¶</a></h3>
<p>The number system for the <a class="reference internal" href="#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic  Specification."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> module provides special values
including <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">sNaN</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">-Infinity</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt>,
and two zeros, <tt class="xref py py-const docutils literal"><span class="pre">+0</span></tt> and <tt class="xref py py-const docutils literal"><span class="pre">-0</span></tt>.</p>
<p>Infinities can be constructed directly with:  <tt class="docutils literal"><span class="pre">Decimal('Infinity')</span></tt>. Also,
they can arise from dividing by zero when the <a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><tt class="xref py py-exc docutils literal"><span class="pre">DivisionByZero</span></tt></a> signal is
not trapped.  Likewise, when the <a class="reference internal" href="#decimal.Overflow" title="decimal.Overflow"><tt class="xref py py-exc docutils literal"><span class="pre">Overflow</span></tt></a> signal is not trapped, infinity
can result from rounding beyond the limits of the largest representable number.</p>
<p>The infinities are signed (affine) and can be used in arithmetic operations
where they get treated as very large, indeterminate numbers.  For instance,
adding a constant to infinity gives another infinite result.</p>
<p>Some operations are indeterminate and return <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>, or if the
<a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><tt class="xref py py-exc docutils literal"><span class="pre">InvalidOperation</span></tt></a> signal is trapped, raise an exception.  For example,
<tt class="docutils literal"><span class="pre">0/0</span></tt> returns <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> which means &#8220;not a number&#8221;.  This variety of
<tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> is quiet and, once created, will flow through other computations
always resulting in another <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>.  This behavior can be useful for a
series of computations that occasionally have missing inputs &#8212; it allows the
calculation to proceed while flagging specific results as invalid.</p>
<p>A variant is <tt class="xref py py-const docutils literal"><span class="pre">sNaN</span></tt> which signals rather than remaining quiet after every
operation.  This is a useful return value when an invalid result needs to
interrupt a calculation for special handling.</p>
<p>The behavior of Python&#8217;s comparison operators can be a little surprising where a
<tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> is involved.  A test for equality where one of the operands is a
quiet or signaling <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> always returns <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const xref docutils literal"><span class="pre">False</span></tt></a> (even when doing
<tt class="docutils literal"><span class="pre">Decimal('NaN')==Decimal('NaN')</span></tt>), while a test for inequality always returns
<a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a>.  An attempt to compare two Decimals using any of the <tt class="docutils literal"><span class="pre">&lt;</span></tt>,
<tt class="docutils literal"><span class="pre">&lt;=</span></tt>, <tt class="docutils literal"><span class="pre">&gt;</span></tt> or <tt class="docutils literal"><span class="pre">&gt;=</span></tt> operators will raise the <a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><tt class="xref py py-exc docutils literal"><span class="pre">InvalidOperation</span></tt></a> signal
if either operand is a <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>, and return <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const xref docutils literal"><span class="pre">False</span></tt></a> if this signal is
not trapped.  Note that the General Decimal Arithmetic specification does not
specify the behavior of direct comparisons; these rules for comparisons
involving a <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> were taken from the IEEE 854 standard (see Table 3 in
section 5.7).  To ensure strict standards-compliance, use the <tt class="xref py py-meth docutils literal"><span class="pre">compare()</span></tt>
and <tt class="xref py py-meth docutils literal"><span class="pre">compare-signal()</span></tt> methods instead.</p>
<p>The signed zeros can result from calculations that underflow. They keep the sign
that would have resulted if the calculation had been carried out to greater
precision.  Since their magnitude is zero, both positive and negative zeros are
treated as equal and their sign is informational.</p>
<p>In addition to the two signed zeros which are distinct yet equal, there are
various representations of zero with differing precisions yet equivalent in
value.  This takes a bit of getting used to.  For an eye accustomed to
normalized floating point representations, it is not immediately obvious that
the following calculation returns a value equal to zero:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="mi">1</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;Infinity&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;0E-1000000026&#39;)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="working-with-threads">
<span id="decimal-threads"></span><h2>8.4.6. Working with threads<a class="headerlink" href="#working-with-threads" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a> function accesses a different <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> object for
each thread.  Having separate thread contexts means that threads may make
changes (such as <tt class="docutils literal"><span class="pre">getcontext.prec=10</span></tt>) without interfering with other threads.</p>
<p>Likewise, the <a class="reference internal" href="#decimal.setcontext" title="decimal.setcontext"><tt class="xref py py-func docutils literal"><span class="pre">setcontext()</span></tt></a> function automatically assigns its target to
the current thread.</p>
<p>If <a class="reference internal" href="#decimal.setcontext" title="decimal.setcontext"><tt class="xref py py-func docutils literal"><span class="pre">setcontext()</span></tt></a> has not been called before <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a>, then
<a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a> will automatically create a new context for use in the
current thread.</p>
<p>The new context is copied from a prototype context called <em>DefaultContext</em>. To
control the defaults so that each thread will use the same values throughout the
application, directly modify the <em>DefaultContext</em> object. This should be done
<em>before</em> any threads are started so that there won&#8217;t be a race condition between
threads calling <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a>. For example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="c"># Set applicationwide defaults for all threads about to be launched</span>
<span class="n">DefaultContext</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">12</span>
<span class="n">DefaultContext</span><span class="o">.</span><span class="n">rounding</span> <span class="o">=</span> <span class="n">ROUND_DOWN</span>
<span class="n">DefaultContext</span><span class="o">.</span><span class="n">traps</span> <span class="o">=</span> <span class="n">ExtendedContext</span><span class="o">.</span><span class="n">traps</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="n">DefaultContext</span><span class="o">.</span><span class="n">traps</span><span class="p">[</span><span class="n">InvalidOperation</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">setcontext</span><span class="p">(</span><span class="n">DefaultContext</span><span class="p">)</span>

<span class="c"># Afterwards, the threads can be started</span>
<span class="n">t1</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="n">t2</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="n">t3</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
 <span class="o">.</span> <span class="o">.</span> <span class="o">.</span>
</pre></div>
</div>
</div>
<div class="section" id="recipes">
<span id="decimal-recipes"></span><h2>8.4.7. Recipes<a class="headerlink" href="#recipes" title="Permalink to this headline">¶</a></h2>
<p>Here are a few recipes that serve as utility functions and that demonstrate ways
to work with the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> class:</p>
<div class="highlight-python3"><div class="highlight"><pre><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="s">&#39;&#39;</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s">&#39;,&#39;</span><span class="p">,</span> <span class="n">dp</span><span class="o">=</span><span class="s">&#39;.&#39;</span><span class="p">,</span>
             <span class="n">pos</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">neg</span><span class="o">=</span><span class="s">&#39;-&#39;</span><span class="p">,</span> <span class="n">trailneg</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Convert Decimal to a money formatted string.</span>

<span class="sd">    places:  required number of places after the decimal point</span>
<span class="sd">    curr:    optional currency symbol before the sign (may be blank)</span>
<span class="sd">    sep:     optional grouping separator (comma, period, space, or blank)</span>
<span class="sd">    dp:      decimal point indicator (comma or period)</span>
<span class="sd">             only specify as blank when places is zero</span>
<span class="sd">    pos:     optional sign for positive numbers: &#39;+&#39;, space or blank</span>
<span class="sd">    neg:     optional sign for negative numbers: &#39;-&#39;, &#39;(&#39;, space or blank</span>
<span class="sd">    trailneg:optional trailing minus indicator:  &#39;-&#39;, &#39;)&#39;, space or blank</span>

<span class="sd">    &gt;&gt;&gt; d = Decimal(&#39;-1234567.8901&#39;)</span>
<span class="sd">    &gt;&gt;&gt; moneyfmt(d, curr=&#39;$&#39;)</span>
<span class="sd">    &#39;-$1,234,567.89&#39;</span>
<span class="sd">    &gt;&gt;&gt; moneyfmt(d, places=0, sep=&#39;.&#39;, dp=&#39;&#39;, neg=&#39;&#39;, trailneg=&#39;-&#39;)</span>
<span class="sd">    &#39;1.234.568-&#39;</span>
<span class="sd">    &gt;&gt;&gt; moneyfmt(d, curr=&#39;$&#39;, neg=&#39;(&#39;, trailneg=&#39;)&#39;)</span>
<span class="sd">    &#39;($1,234,567.89)&#39;</span>
<span class="sd">    &gt;&gt;&gt; moneyfmt(Decimal(123456789), sep=&#39; &#39;)</span>
<span class="sd">    &#39;123 456 789.00&#39;</span>
<span class="sd">    &gt;&gt;&gt; moneyfmt(Decimal(&#39;-0.02&#39;), neg=&#39;&lt;&#39;, trailneg=&#39;&gt;&#39;)</span>
<span class="sd">    &#39;&lt;0.02&gt;&#39;</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <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="c"># 2 places --&gt; &#39;0.01&#39;</span>
    <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>
    <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">digits</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</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>
    <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>
    <span class="k">if</span> <span class="n">sign</span><span class="p">:</span>
        <span class="n">build</span><span class="p">(</span><span class="n">trailneg</span><span class="p">)</span>
    <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>
        <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="s">&#39;0&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">places</span><span class="p">:</span>
        <span class="n">build</span><span class="p">(</span><span class="n">dp</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">digits</span><span class="p">:</span>
        <span class="n">build</span><span class="p">(</span><span class="s">&#39;0&#39;</span><span class="p">)</span>
    <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">digits</span><span class="p">:</span>
        <span class="n">build</span><span class="p">(</span><span class="nb">next</span><span class="p">())</span>
        <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
        <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>
            <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="n">build</span><span class="p">(</span><span class="n">sep</span><span class="p">)</span>
    <span class="n">build</span><span class="p">(</span><span class="n">curr</span><span class="p">)</span>
    <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>
    <span class="k">return</span> <span class="s">&#39;&#39;</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>

<span class="k">def</span> <span class="nf">pi</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;Compute Pi to the current precision.</span>

<span class="sd">    &gt;&gt;&gt; print(pi())</span>
<span class="sd">    3.141592653589793238462643383</span>

<span class="sd">    &quot;&quot;&quot;</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">2</span>  <span class="c"># extra digits for intermediate steps</span>
    <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="c"># substitute &quot;three=3.0&quot; for regular floats</span>
    <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>
    <span class="k">while</span> <span class="n">s</span> <span class="o">!=</span> <span class="n">lasts</span><span class="p">:</span>
        <span class="n">lasts</span> <span class="o">=</span> <span class="n">s</span>
        <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>
        <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>
        <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>
        <span class="n">s</span> <span class="o">+=</span> <span class="n">t</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">2</span>
    <span class="k">return</span> <span class="o">+</span><span class="n">s</span>               <span class="c"># unary plus applies the new precision</span>

<span class="k">def</span> <span class="nf">exp</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Return e raised to the power of x.  Result type matches input type.</span>

<span class="sd">    &gt;&gt;&gt; print(exp(Decimal(1)))</span>
<span class="sd">    2.718281828459045235360287471</span>
<span class="sd">    &gt;&gt;&gt; print(exp(Decimal(2)))</span>
<span class="sd">    7.389056098930650227230427461</span>
<span class="sd">    &gt;&gt;&gt; print(exp(2.0))</span>
<span class="sd">    7.38905609893</span>
<span class="sd">    &gt;&gt;&gt; print(exp(2+0j))</span>
<span class="sd">    (7.38905609893+0j)</span>

<span class="sd">    &quot;&quot;&quot;</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">2</span>
    <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>
    <span class="k">while</span> <span class="n">s</span> <span class="o">!=</span> <span class="n">lasts</span><span class="p">:</span>
        <span class="n">lasts</span> <span class="o">=</span> <span class="n">s</span>
        <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">fact</span> <span class="o">*=</span> <span class="n">i</span>
        <span class="n">num</span> <span class="o">*=</span> <span class="n">x</span>
        <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="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="k">return</span> <span class="o">+</span><span class="n">s</span>

<span class="k">def</span> <span class="nf">cos</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Return the cosine of x as measured in radians.</span>

<span class="sd">    The Taylor series approximation works best for a small value of x.</span>
<span class="sd">    For larger values, first compute x = x % (2 * pi).</span>

<span class="sd">    &gt;&gt;&gt; print(cos(Decimal(&#39;0.5&#39;)))</span>
<span class="sd">    0.8775825618903727161162815826</span>
<span class="sd">    &gt;&gt;&gt; print(cos(0.5))</span>
<span class="sd">    0.87758256189</span>
<span class="sd">    &gt;&gt;&gt; print(cos(0.5+0j))</span>
<span class="sd">    (0.87758256189+0j)</span>

<span class="sd">    &quot;&quot;&quot;</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">2</span>
    <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>
    <span class="k">while</span> <span class="n">s</span> <span class="o">!=</span> <span class="n">lasts</span><span class="p">:</span>
        <span class="n">lasts</span> <span class="o">=</span> <span class="n">s</span>
        <span class="n">i</span> <span class="o">+=</span> <span class="mi">2</span>
        <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>
        <span class="n">num</span> <span class="o">*=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span>
        <span class="n">sign</span> <span class="o">*=</span> <span class="o">-</span><span class="mi">1</span>
        <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>
    <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="k">return</span> <span class="o">+</span><span class="n">s</span>

<span class="k">def</span> <span class="nf">sin</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Return the sine of x as measured in radians.</span>

<span class="sd">    The Taylor series approximation works best for a small value of x.</span>
<span class="sd">    For larger values, first compute x = x % (2 * pi).</span>

<span class="sd">    &gt;&gt;&gt; print(sin(Decimal(&#39;0.5&#39;)))</span>
<span class="sd">    0.4794255386042030002732879352</span>
<span class="sd">    &gt;&gt;&gt; print(sin(0.5))</span>
<span class="sd">    0.479425538604</span>
<span class="sd">    &gt;&gt;&gt; print(sin(0.5+0j))</span>
<span class="sd">    (0.479425538604+0j)</span>

<span class="sd">    &quot;&quot;&quot;</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">2</span>
    <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>
    <span class="k">while</span> <span class="n">s</span> <span class="o">!=</span> <span class="n">lasts</span><span class="p">:</span>
        <span class="n">lasts</span> <span class="o">=</span> <span class="n">s</span>
        <span class="n">i</span> <span class="o">+=</span> <span class="mi">2</span>
        <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>
        <span class="n">num</span> <span class="o">*=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span>
        <span class="n">sign</span> <span class="o">*=</span> <span class="o">-</span><span class="mi">1</span>
        <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>
    <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="k">return</span> <span class="o">+</span><span class="n">s</span>
</pre></div>
</div>
</div>
<div class="section" id="decimal-faq">
<span id="id1"></span><h2>8.4.8. Decimal FAQ<a class="headerlink" href="#decimal-faq" title="Permalink to this headline">¶</a></h2>
<p>Q. It is cumbersome to type <tt class="docutils literal"><span class="pre">decimal.Decimal('1234.5')</span></tt>.  Is there a way to
minimize typing when using the interactive interpreter?</p>
<p>A. Some users abbreviate the constructor to just a single letter:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">D</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">D</span><span class="p">(</span><span class="s">&#39;1.23&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="n">D</span><span class="p">(</span><span class="s">&#39;3.45&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;4.68&#39;)</span>
</pre></div>
</div>
<p>Q. In a fixed-point application with two decimal places, some inputs have many
places and need to be rounded.  Others are not supposed to have excess digits
and need to be validated.  What methods should be used?</p>
<p>A. The <tt class="xref py py-meth docutils literal"><span class="pre">quantize()</span></tt> method rounds to a fixed number of decimal places. If
the <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-const docutils literal"><span class="pre">Inexact</span></tt></a> trap is set, it is also useful for validation:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="c"># same as Decimal(&#39;0.01&#39;)</span>
</pre></div>
</div>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Round to two places</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;3.214&#39;</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="go">Decimal(&#39;3.21&#39;)</span>
</pre></div>
</div>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Validate that a number does not exceed two places</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;3.21&#39;</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>
<span class="go">Decimal(&#39;3.21&#39;)</span>
</pre></div>
</div>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;3.214&#39;</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>
<span class="gt">Traceback (most recent call last):</span>
   <span class="c">...</span>
<span class="nc">Inexact</span>: <span class="n-Identifier">None</span>
</pre></div>
</div>
<p>Q. Once I have valid two place inputs, how do I maintain that invariant
throughout an application?</p>
<p>A. Some operations like addition, subtraction, and multiplication by an integer
will automatically preserve fixed point.  Others operations, like division and
non-integer multiplication, will change the number of decimal places and need to
be followed-up with a <tt class="xref py py-meth docutils literal"><span class="pre">quantize()</span></tt> step:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;102.72&#39;</span><span class="p">)</span>           <span class="c"># Initial fixed-point values</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;3.17&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span>                           <span class="c"># Addition preserves fixed-point</span>
<span class="go">Decimal(&#39;105.89&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span>
<span class="go">Decimal(&#39;99.55&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">*</span> <span class="mi">42</span>                          <span class="c"># So does integer multiplication</span>
<span class="go">Decimal(&#39;4314.24&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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="c"># Must quantize non-integer multiplication</span>
<span class="go">Decimal(&#39;325.62&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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="c"># And quantize division</span>
<span class="go">Decimal(&#39;0.03&#39;)</span>
</pre></div>
</div>
<p>In developing fixed-point applications, it is convenient to define functions
to handle the <tt class="xref py py-meth docutils literal"><span class="pre">quantize()</span></tt> step:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<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>
<span class="gp">&gt;&gt;&gt; </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>
<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>
</pre></div>
</div>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="c"># Automatically preserve fixed-point</span>
<span class="go">Decimal(&#39;325.62&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;0.03&#39;)</span>
</pre></div>
</div>
<p>Q. There are many ways to express the same value.  The numbers <tt class="xref py py-const docutils literal"><span class="pre">200</span></tt>,
<tt class="xref py py-const docutils literal"><span class="pre">200.000</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">2E2</span></tt>, and <tt class="xref py py-const docutils literal"><span class="pre">02E+4</span></tt> all have the same value at
various precisions. Is there a way to transform them to a single recognizable
canonical value?</p>
<p>A. The <tt class="xref py py-meth docutils literal"><span class="pre">normalize()</span></tt> method maps all equivalent values to a single
representative:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="s">&#39;200 200.000 2E2 .02E+4&#39;</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="go">[Decimal(&#39;2E+2&#39;), Decimal(&#39;2E+2&#39;), Decimal(&#39;2E+2&#39;), Decimal(&#39;2E+2&#39;)]</span>
</pre></div>
</div>
<p>Q. Some decimal values always print with exponential notation.  Is there a way
to get a non-exponential representation?</p>
<p>A. For some values, exponential notation is the only way to express the number
of significant places in the coefficient.  For example, expressing
<tt class="xref py py-const docutils literal"><span class="pre">5.0E+3</span></tt> as <tt class="xref py py-const docutils literal"><span class="pre">5000</span></tt> keeps the value constant but cannot show the
original&#8217;s two-place significance.</p>
<p>If an application does not care about tracking significance, it is easy to
remove the exponent and trailing zeroes, losing significance, but keeping the
value unchanged:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="gp">... </span>    <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>
</pre></div>
</div>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">remove_exponent</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;5E+3&#39;</span><span class="p">))</span>
<span class="go">Decimal(&#39;5000&#39;)</span>
</pre></div>
</div>
<p>Q. Is there a way to convert a regular float to a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a>?</p>
<p>A. Yes, any binary floating point number can be exactly expressed as a
Decimal though an exact conversion may take more precision than intuition would
suggest:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="go">Decimal(&#39;3.141592653589793115997963468544185161590576171875&#39;)</span>
</pre></div>
</div>
<p>Q. Within a complex calculation, how can I make sure that I haven&#8217;t gotten a
spurious result because of insufficient precision or rounding anomalies.</p>
<p>A. The decimal module makes it easy to test results.  A best practice is to
re-run calculations using greater precision and with various rounding modes.
Widely differing results indicate insufficient precision, rounding mode issues,
ill-conditioned inputs, or a numerically unstable algorithm.</p>
<p>Q. I noticed that context precision is applied to the results of operations but
not to the inputs.  Is there anything to watch out for when mixing values of
different precisions?</p>
<p>A. Yes.  The principle is that all values are considered to be exact and so is
the arithmetic on those values.  Only the results are rounded.  The advantage
for inputs is that &#8220;what you type is what you get&#8221;.  A disadvantage is that the
results can look odd if you forget that the inputs haven&#8217;t been rounded:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;3.104&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;2.104&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;5.21&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;3.104&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;0.000&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;2.104&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;5.20&#39;)</span>
</pre></div>
</div>
<p>The solution is either to increase precision or to force rounding of inputs
using the unary plus operation:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </span><span class="o">+</span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;1.23456789&#39;</span><span class="p">)</span>      <span class="c"># unary plus triggers rounding</span>
<span class="go">Decimal(&#39;1.23&#39;)</span>
</pre></div>
</div>
<p>Alternatively, inputs can be rounded upon creation using the
<a class="reference internal" href="#decimal.Context.create_decimal" title="decimal.Context.create_decimal"><tt class="xref py py-meth docutils literal"><span class="pre">Context.create_decimal()</span></tt></a> method:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="s">&#39;1.2345678&#39;</span><span class="p">)</span>
<span class="go">Decimal(&#39;1.2345&#39;)</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">8.4. <tt class="docutils literal"><span class="pre">decimal</span></tt> &#8212; Decimal fixed point and floating point arithmetic</a><ul>
<li><a class="reference internal" href="#quick-start-tutorial">8.4.1. Quick-start Tutorial</a></li>
<li><a class="reference internal" href="#decimal-objects">8.4.2. Decimal objects</a><ul>
<li><a class="reference internal" href="#logical-operands">8.4.2.1. Logical operands</a></li>
</ul>
</li>
<li><a class="reference internal" href="#context-objects">8.4.3. Context objects</a></li>
<li><a class="reference internal" href="#signals">8.4.4. Signals</a></li>
<li><a class="reference internal" href="#floating-point-notes">8.4.5. Floating Point Notes</a><ul>
<li><a class="reference internal" href="#mitigating-round-off-error-with-increased-precision">8.4.5.1. Mitigating round-off error with increased precision</a></li>
<li><a class="reference internal" href="#special-values">8.4.5.2. Special values</a></li>
</ul>
</li>
<li><a class="reference internal" href="#working-with-threads">8.4.6. Working with threads</a></li>
<li><a class="reference internal" href="#recipes">8.4.7. Recipes</a></li>
<li><a class="reference internal" href="#decimal-faq">8.4.8. Decimal FAQ</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="cmath.html"
                        title="previous chapter">8.3. <tt class="docutils literal docutils literal"><span class="pre">cmath</span></tt> &#8212; Mathematical functions for complex numbers</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="fractions.html"
                        title="next chapter">8.5. <tt class="docutils literal"><span class="pre">fractions</span></tt> &#8212; Rational numbers</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/decimal.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="fractions.html" title="8.5. fractions — Rational numbers"
             >next</a> |</li>
        <li class="right" >
          <a href="cmath.html" title="8.3. cmath — Mathematical functions for complex numbers"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="numeric.html" >8. Numeric and Mathematical Modules</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2011, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Sep 04, 2011.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>