Sophie

Sophie

distrib > Mageia > 6 > armv5tl > by-pkgid > a600cd26dfe6bfd8c11f12bce5cb0eee > files > 768

python3-docs-3.5.3-1.1.mga6.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>29.14. fpectl — Floating point exception control &mdash; Python 3.5.3 documentation</title>
    
    <link rel="stylesheet" href="../_static/pydoctheme.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.5.3',
        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 3.5.3 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 3.5.3 documentation" href="../contents.html" />
    <link rel="up" title="29. Python Runtime Services" href="python.html" />
    <link rel="next" title="30. Custom Python Interpreters" href="custominterp.html" />
    <link rel="prev" title="29.13. site — Site-specific configuration hook" href="site.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/version_switch.js"></script>
    
    
 

  </head>
  <body role="document">  
    <div class="related" role="navigation" aria-label="related navigation">
      <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="custominterp.html" title="30. Custom Python Interpreters"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="site.html" title="29.13. site — Site-specific configuration hook"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.5.3</span>
          <a href="../index.html">Documentation </a> &raquo;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li class="nav-item nav-item-2"><a href="python.html" accesskey="U">29. Python Runtime Services</a> &raquo;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-fpectl">
<span id="fpectl-floating-point-exception-control"></span><h1>29.14. <a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">fpectl</span></code></a> &#8212; Floating point exception control<a class="headerlink" href="#module-fpectl" title="Permalink to this headline">¶</a></h1>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">fpectl</span></code></a> module is not built by default, and its usage is discouraged
and may be dangerous except in the hands of experts.  See also the section
<a class="reference internal" href="#fpectl-limitations"><span>Limitations and other considerations</span></a> on limitations for more details.</p>
</div>
<hr class="docutils" id="index-0" />
<p>Most computers carry out floating point operations in conformance with the
so-called IEEE-754 standard. On any real computer, some floating point
operations produce results that cannot be expressed as a normal floating point
value. For example, try</p>
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span>
<span class="go">inf</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span> <span class="o">/</span> <span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span>
<span class="go">nan</span>
</pre></div>
</div>
<p>(The example above will work on many platforms. DEC Alpha may be one exception.)
&#8220;Inf&#8221; is a special, non-numeric value in IEEE-754 that stands for &#8220;infinity&#8221;,
and &#8220;nan&#8221; means &#8220;not a number.&#8221; Note that, other than the non-numeric results,
nothing special happened when you asked Python to carry out those calculations.
That is in fact the default behaviour prescribed in the IEEE-754 standard, and
if it works for you, stop reading now.</p>
<p>In some circumstances, it would be better to raise an exception and stop
processing at the point where the faulty operation was attempted. The
<a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">fpectl</span></code></a> module is for use in that situation. It provides control over
floating point units from several hardware manufacturers, allowing the user to
turn on the generation of <code class="xref py py-const docutils literal"><span class="pre">SIGFPE</span></code> whenever any of the IEEE-754
exceptions Division by Zero, Overflow, or Invalid Operation occurs. In tandem
with a pair of wrapper macros that are inserted into the C code comprising your
python system, <code class="xref py py-const docutils literal"><span class="pre">SIGFPE</span></code> is trapped and converted into the Python
<a class="reference internal" href="exceptions.html#FloatingPointError" title="FloatingPointError"><code class="xref py py-exc docutils literal"><span class="pre">FloatingPointError</span></code></a> exception.</p>
<p>The <a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">fpectl</span></code></a> module defines the following functions and may raise the given
exception:</p>
<dl class="function">
<dt id="fpectl.turnon_sigfpe">
<code class="descclassname">fpectl.</code><code class="descname">turnon_sigfpe</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#fpectl.turnon_sigfpe" title="Permalink to this definition">¶</a></dt>
<dd><p>Turn on the generation of <code class="xref py py-const docutils literal"><span class="pre">SIGFPE</span></code>, and set up an appropriate signal
handler.</p>
</dd></dl>

<dl class="function">
<dt id="fpectl.turnoff_sigfpe">
<code class="descclassname">fpectl.</code><code class="descname">turnoff_sigfpe</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#fpectl.turnoff_sigfpe" title="Permalink to this definition">¶</a></dt>
<dd><p>Reset default handling of floating point exceptions.</p>
</dd></dl>

<dl class="exception">
<dt id="fpectl.FloatingPointError">
<em class="property">exception </em><code class="descclassname">fpectl.</code><code class="descname">FloatingPointError</code><a class="headerlink" href="#fpectl.FloatingPointError" title="Permalink to this definition">¶</a></dt>
<dd><p>After <a class="reference internal" href="#fpectl.turnon_sigfpe" title="fpectl.turnon_sigfpe"><code class="xref py py-func docutils literal"><span class="pre">turnon_sigfpe()</span></code></a> has been executed, a floating point operation that
raises one of the IEEE-754 exceptions Division by Zero, Overflow, or Invalid
operation will in turn raise this standard Python exception.</p>
</dd></dl>

<div class="section" id="example">
<span id="fpectl-example"></span><h2>29.14.1. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
<p>The following example demonstrates how to start up and test operation of the
<a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">fpectl</span></code></a> module.</p>
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">fpectl</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">fpetest</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fpectl</span><span class="o">.</span><span class="n">turnon_sigfpe</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fpetest</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
<span class="go">overflow        PASS</span>
<span class="go">FloatingPointError: Overflow</span>

<span class="go">div by 0        PASS</span>
<span class="go">FloatingPointError: Division by zero</span>
<span class="go">  [ more output from test elided ]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">?</span>
<span class="gr">FloatingPointError</span>: <span class="n">in math_1</span>
</pre></div>
</div>
</div>
<div class="section" id="limitations-and-other-considerations">
<span id="fpectl-limitations"></span><h2>29.14.2. Limitations and other considerations<a class="headerlink" href="#limitations-and-other-considerations" title="Permalink to this headline">¶</a></h2>
<p>Setting up a given processor to trap IEEE-754 floating point errors currently
requires custom code on a per-architecture basis. You may have to modify
<a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">fpectl</span></code></a> to control your particular hardware.</p>
<p>Conversion of an IEEE-754 exception to a Python exception requires that the
wrapper macros <code class="docutils literal"><span class="pre">PyFPE_START_PROTECT</span></code> and <code class="docutils literal"><span class="pre">PyFPE_END_PROTECT</span></code> be inserted
into your code in an appropriate fashion.  Python itself has been modified to
support the <a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">fpectl</span></code></a> module, but many other codes of interest to numerical
analysts have not.</p>
<p>The <a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">fpectl</span></code></a> module is not thread-safe.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">Some files in the source distribution may be interesting in learning more about
how this module operates. The include file <code class="file docutils literal"><span class="pre">Include/pyfpe.h</span></code> discusses the
implementation of this module at some length. <code class="file docutils literal"><span class="pre">Modules/fpetestmodule.c</span></code>
gives several examples of use. Many additional examples can be found in
<code class="file docutils literal"><span class="pre">Objects/floatobject.c</span></code>.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">29.14. <code class="docutils literal"><span class="pre">fpectl</span></code> &#8212; Floating point exception control</a><ul>
<li><a class="reference internal" href="#example">29.14.1. Example</a></li>
<li><a class="reference internal" href="#limitations-and-other-considerations">29.14.2. Limitations and other considerations</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="site.html"
                        title="previous chapter">29.13. <code class="docutils literal"><span class="pre">site</span></code> &#8212; Site-specific configuration hook</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="custominterp.html"
                        title="next chapter">30. Custom Python Interpreters</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">Report a Bug</a></li>
      <li><a href="../_sources/library/fpectl.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <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="custominterp.html" title="30. Custom Python Interpreters"
             >next</a> |</li>
        <li class="right" >
          <a href="site.html" title="29.13. site — Site-specific configuration hook"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.5.3</span>
          <a href="../index.html">Documentation </a> &raquo;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li class="nav-item nav-item-2"><a href="python.html" >29. Python Runtime Services</a> &raquo;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 2001-2017, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Jan 20, 2017.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
    </div>

  </body>
</html>