Server : Apache System : Linux server1.cgrithy.com 3.10.0-1160.95.1.el7.x86_64 #1 SMP Mon Jul 24 13:59:37 UTC 2023 x86_64 User : nobody ( 99) PHP Version : 8.1.23 Disable Function : NONE Directory : /usr/share/doc/python-docs-2.7.5/html/library/ |
<!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>27.16. fpectl — Floating point exception control — Python 2.7.5 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: '2.7.5', 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 2.7.5 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 2.7.5 documentation" href="../index.html" /> <link rel="up" title="27. Python Runtime Services" href="python.html" /> <link rel="next" title="27.17. distutils — Building and installing Python modules" href="distutils.html" /> <link rel="prev" title="27.15. user — User-specific configuration hook" href="user.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </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="distutils.html" title="27.17. distutils — Building and installing Python modules" accesskey="N">next</a> |</li> <li class="right" > <a href="user.html" title="27.15. user — User-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="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="python.html" accesskey="U">27. Python Runtime Services</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-fpectl"> <span id="fpectl-floating-point-exception-control"></span><h1>27.16. <a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">fpectl</span></tt></a> — 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)"><tt class="xref py py-mod docutils literal"><span class="pre">fpectl</span></tt></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"><em>Limitations and other considerations</em></a> on limitations for more details.</p> </div> <p id="index-0">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-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">math</span> <span class="gp">>>> </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">>>> </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.) “Inf” is a special, non-numeric value in IEEE-754 that stands for “infinity”, and “nan” means “not a number.” 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)"><tt class="xref py py-mod docutils literal"><span class="pre">fpectl</span></tt></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 <tt class="xref py py-const docutils literal"><span class="pre">SIGFPE</span></tt> 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, <tt class="xref py py-const docutils literal"><span class="pre">SIGFPE</span></tt> is trapped and converted into the Python <a class="reference internal" href="#fpectl.FloatingPointError" title="fpectl.FloatingPointError"><tt class="xref py py-exc docutils literal"><span class="pre">FloatingPointError</span></tt></a> exception.</p> <p>The <a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">fpectl</span></tt></a> module defines the following functions and may raise the given exception:</p> <dl class="function"> <dt id="fpectl.turnon_sigfpe"> <tt class="descclassname">fpectl.</tt><tt class="descname">turnon_sigfpe</tt><big>(</big><big>)</big><a class="headerlink" href="#fpectl.turnon_sigfpe" title="Permalink to this definition">¶</a></dt> <dd><p>Turn on the generation of <tt class="xref py py-const docutils literal"><span class="pre">SIGFPE</span></tt>, and set up an appropriate signal handler.</p> </dd></dl> <dl class="function"> <dt id="fpectl.turnoff_sigfpe"> <tt class="descclassname">fpectl.</tt><tt class="descname">turnoff_sigfpe</tt><big>(</big><big>)</big><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><tt class="descclassname">fpectl.</tt><tt class="descname">FloatingPointError</tt><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"><tt class="xref py py-func docutils literal"><span class="pre">turnon_sigfpe()</span></tt></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>27.16.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)"><tt class="xref py py-mod docutils literal"><span class="pre">fpectl</span></tt></a> module.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">fpectl</span> <span class="gp">>>> </span><span class="kn">import</span> <span class="nn">fpetest</span> <span class="gp">>>> </span><span class="n">fpectl</span><span class="o">.</span><span class="n">turnon_sigfpe</span><span class="p">()</span> <span class="gp">>>> </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">>>> </span><span class="kn">import</span> <span class="nn">math</span> <span class="gp">>>> </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">"<stdin>"</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>27.16.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)"><tt class="xref py py-mod docutils literal"><span class="pre">fpectl</span></tt></a> to control your particular hardware.</p> <p>Conversion of an IEEE-754 exception to a Python exception requires that the wrapper macros <tt class="docutils literal"><span class="pre">PyFPE_START_PROTECT</span></tt> and <tt class="docutils literal"><span class="pre">PyFPE_END_PROTECT</span></tt> 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)"><tt class="xref py py-mod docutils literal"><span class="pre">fpectl</span></tt></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)"><tt class="xref py py-mod docutils literal"><span class="pre">fpectl</span></tt></a> module is not thread-safe.</p> <div class="admonition-see-also 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 <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Include/pyfpe.h">Include/pyfpe.h</a> discusses the implementation of this module at some length. <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Modules/fpetestmodule.c">Modules/fpetestmodule.c</a> gives several examples of use. Many additional examples can be found in <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Objects/floatobject.c">Objects/floatobject.c</a>.</p> </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="#">27.16. <tt class="docutils literal"><span class="pre">fpectl</span></tt> — Floating point exception control</a><ul> <li><a class="reference internal" href="#example">27.16.1. Example</a></li> <li><a class="reference internal" href="#limitations-and-other-considerations">27.16.2. Limitations and other considerations</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="user.html" title="previous chapter">27.15. <tt class="docutils literal"><span class="pre">user</span></tt> — User-specific configuration hook</a></p> <h4>Next topic</h4> <p class="topless"><a href="distutils.html" title="next chapter">27.17. <tt class="docutils literal"><span class="pre">distutils</span></tt> — Building and installing Python modules</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/fpectl.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" /> <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="distutils.html" title="27.17. distutils — Building and installing Python modules" >next</a> |</li> <li class="right" > <a href="user.html" title="27.15. user — User-specific configuration hook" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="python.html" >27. Python Runtime Services</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2020, 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 Oct 13, 2020. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>