<!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.15. fpectl — Floating point exception control — Python v2.6.6 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.6.6',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python v2.6.6 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 v2.6.6 documentation" href="../index.html" />
<link rel="up" title="27. Python Runtime Services" href="python.html" />
<link rel="next" title="27.16. distutils — Building and installing Python modules" href="distutils.html" />
<link rel="prev" title="27.14. user — User-specific configuration hook" href="user.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="../modindex.html" title="Global Module Index"
accesskey="M">modules</a> |</li>
<li class="right" >
<a href="distutils.html" title="27.16. distutils — Building and installing Python modules"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="user.html" title="27.14. 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="../index.html">Python v2.6.6 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">
<h1>27.15. <tt class="xref docutils literal"><span class="pre">fpectl</span></tt> — Floating point exception control<a class="headerlink" href="#module-fpectl" title="Permalink to this headline">¶</a></h1>
<p><em>Platforms: </em>Unix</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <tt class="xref docutils literal"><span class="pre">fpectl</span></tt> 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-340">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
<tt class="xref docutils literal"><span class="pre">fpectl</span></tt> 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 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 docutils literal"><span class="pre">SIGFPE</span></tt> is trapped and converted into the Python
<a title="fpectl.FloatingPointError" class="reference internal" href="#fpectl.FloatingPointError"><tt class="xref docutils literal"><span class="pre">FloatingPointError</span></tt></a> exception.</p>
<p>The <tt class="xref docutils literal"><span class="pre">fpectl</span></tt> 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>Turn on the generation of <tt class="xref docutils literal"><span class="pre">SIGFPE</span></tt>, and set up an appropriate signal
handler.</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>Reset default handling of floating point exceptions.</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>After <a title="fpectl.turnon_sigfpe" class="reference internal" href="#fpectl.turnon_sigfpe"><tt class="xref 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.</dd></dl>
<div class="section" id="example">
<span id="fpectl-example"></span><h2>27.15.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
<tt class="xref docutils literal"><span class="pre">fpectl</span></tt> 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-Identifier">?</span>
<span class="nc">FloatingPointError</span>: <span class="n-Identifier">in math_1</span>
</pre></div>
</div>
</div>
<div class="section" id="limitations-and-other-considerations">
<span id="fpectl-limitations"></span><h2>27.15.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
<tt class="xref docutils literal"><span class="pre">fpectl</span></tt> 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 <tt class="xref docutils literal"><span class="pre">fpectl</span></tt> module, but many other codes of interest to numerical
analysts have not.</p>
<p>The <tt class="xref docutils literal"><span class="pre">fpectl</span></tt> 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 <tt class="docutils literal"><span class="pre">Include/pyfpe.h</span></tt> discusses the
implementation of this module at some length. <tt class="docutils literal"><span class="pre">Modules/fpetestmodule.c</span></tt>
gives several examples of use. Many additional examples can be found in
<tt class="docutils literal"><span class="pre">Objects/floatobject.c</span></tt>.</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 external" href="#">27.15. <tt class="docutils literal"><span class="pre">fpectl</span></tt> — Floating point exception control</a><ul>
<li><a class="reference external" href="#example">27.15.1. Example</a></li>
<li><a class="reference external" href="#limitations-and-other-considerations">27.15.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.14. <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.16. <tt class="docutils literal docutils literal 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" 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="../modindex.html" title="Global Module Index"
>modules</a> |</li>
<li class="right" >
<a href="distutils.html" title="27.16. distutils — Building and installing Python modules"
>next</a> |</li>
<li class="right" >
<a href="user.html" title="27.14. 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="../index.html">Python v2.6.6 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-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 Jul 20, 2011.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.6.
</div>
</body>
</html>
Copyright 2K16 - 2K18 Indonesian Hacker Rulez