2 :mod:`fpectl` --- Floating point exception control
3 ==================================================
7 :synopsis: Provide control for floating point exception handling.
8 .. moduleauthor:: Lee Busby <busby1@llnl.gov>
9 .. sectionauthor:: Lee Busby <busby1@llnl.gov>
14 The :mod:`fpectl` module is not built by default, and its usage is discouraged
15 and may be dangerous except in the hands of experts. See also the section
16 :ref:`fpectl-limitations` on limitations for more details.
18 .. index:: single: IEEE-754
20 Most computers carry out floating point operations in conformance with the
21 so-called IEEE-754 standard. On any real computer, some floating point
22 operations produce results that cannot be expressed as a normal floating point
23 value. For example, try ::
28 >>> math.exp(1000) / math.exp(1000)
31 (The example above will work on many platforms. DEC Alpha may be one exception.)
32 "Inf" is a special, non-numeric value in IEEE-754 that stands for "infinity",
33 and "nan" means "not a number." Note that, other than the non-numeric results,
34 nothing special happened when you asked Python to carry out those calculations.
35 That is in fact the default behaviour prescribed in the IEEE-754 standard, and
36 if it works for you, stop reading now.
38 In some circumstances, it would be better to raise an exception and stop
39 processing at the point where the faulty operation was attempted. The
40 :mod:`fpectl` module is for use in that situation. It provides control over
41 floating point units from several hardware manufacturers, allowing the user to
42 turn on the generation of :const:`SIGFPE` whenever any of the IEEE-754
43 exceptions Division by Zero, Overflow, or Invalid Operation occurs. In tandem
44 with a pair of wrapper macros that are inserted into the C code comprising your
45 python system, :const:`SIGFPE` is trapped and converted into the Python
46 :exc:`FloatingPointError` exception.
48 The :mod:`fpectl` module defines the following functions and may raise the given
52 .. function:: turnon_sigfpe()
54 Turn on the generation of :const:`SIGFPE`, and set up an appropriate signal
58 .. function:: turnoff_sigfpe()
60 Reset default handling of floating point exceptions.
63 .. exception:: FloatingPointError
65 After :func:`turnon_sigfpe` has been executed, a floating point operation that
66 raises one of the IEEE-754 exceptions Division by Zero, Overflow, or Invalid
67 operation will in turn raise this standard Python exception.
75 The following example demonstrates how to start up and test operation of the
76 :mod:`fpectl` module. ::
80 >>> fpectl.turnon_sigfpe()
83 FloatingPointError: Overflow
86 FloatingPointError: Division by zero
87 [ more output from test elided ]
90 Traceback (most recent call last):
91 File "<stdin>", line 1, in ?
92 FloatingPointError: in math_1
95 .. _fpectl-limitations:
97 Limitations and other considerations
98 ------------------------------------
100 Setting up a given processor to trap IEEE-754 floating point errors currently
101 requires custom code on a per-architecture basis. You may have to modify
102 :mod:`fpectl` to control your particular hardware.
104 Conversion of an IEEE-754 exception to a Python exception requires that the
105 wrapper macros ``PyFPE_START_PROTECT`` and ``PyFPE_END_PROTECT`` be inserted
106 into your code in an appropriate fashion. Python itself has been modified to
107 support the :mod:`fpectl` module, but many other codes of interest to numerical
110 The :mod:`fpectl` module is not thread-safe.
115 Some files in the source distribution may be interesting in learning more about
116 how this module operates. The include file :file:`Include/pyfpe.h` discusses the
117 implementation of this module at some length. :file:`Modules/fpetestmodule.c`
118 gives several examples of use. Many additional examples can be found in
119 :file:`Objects/floatobject.c`.