blob: 96607165ba4e3e43100943fb081d68c2cc8f1bc4 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`fpectl` --- Floating point exception control
2==================================================
3
4.. module:: fpectl
5 :platform: Unix
6 :synopsis: Provide control for floating point exception handling.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007
Georg Brandl116aa622007-08-15 14:28:22 +00008.. moduleauthor:: Lee Busby <busby1@llnl.gov>
9.. sectionauthor:: Lee Busby <busby1@llnl.gov>
10
Georg Brandl116aa622007-08-15 14:28:22 +000011.. note::
12
13 The :mod:`fpectl` module is not built by default, and its usage is discouraged
14 and may be dangerous except in the hands of experts. See also the section
15 :ref:`fpectl-limitations` on limitations for more details.
16
17.. index:: single: IEEE-754
18
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040019--------------
20
Georg Brandl116aa622007-08-15 14:28:22 +000021Most computers carry out floating point operations in conformance with the
22so-called IEEE-754 standard. On any real computer, some floating point
23operations produce results that cannot be expressed as a normal floating point
24value. For example, try ::
25
26 >>> import math
27 >>> math.exp(1000)
28 inf
29 >>> math.exp(1000) / math.exp(1000)
30 nan
31
32(The example above will work on many platforms. DEC Alpha may be one exception.)
33"Inf" is a special, non-numeric value in IEEE-754 that stands for "infinity",
34and "nan" means "not a number." Note that, other than the non-numeric results,
35nothing special happened when you asked Python to carry out those calculations.
36That is in fact the default behaviour prescribed in the IEEE-754 standard, and
37if it works for you, stop reading now.
38
39In some circumstances, it would be better to raise an exception and stop
40processing at the point where the faulty operation was attempted. The
41:mod:`fpectl` module is for use in that situation. It provides control over
42floating point units from several hardware manufacturers, allowing the user to
43turn on the generation of :const:`SIGFPE` whenever any of the IEEE-754
44exceptions Division by Zero, Overflow, or Invalid Operation occurs. In tandem
45with a pair of wrapper macros that are inserted into the C code comprising your
46python system, :const:`SIGFPE` is trapped and converted into the Python
47:exc:`FloatingPointError` exception.
48
49The :mod:`fpectl` module defines the following functions and may raise the given
50exception:
51
52
53.. function:: turnon_sigfpe()
54
55 Turn on the generation of :const:`SIGFPE`, and set up an appropriate signal
56 handler.
57
58
59.. function:: turnoff_sigfpe()
60
61 Reset default handling of floating point exceptions.
62
63
64.. exception:: FloatingPointError
65
66 After :func:`turnon_sigfpe` has been executed, a floating point operation that
67 raises one of the IEEE-754 exceptions Division by Zero, Overflow, or Invalid
68 operation will in turn raise this standard Python exception.
69
70
71.. _fpectl-example:
72
73Example
74-------
75
76The following example demonstrates how to start up and test operation of the
77:mod:`fpectl` module. ::
78
79 >>> import fpectl
80 >>> import fpetest
81 >>> fpectl.turnon_sigfpe()
82 >>> fpetest.test()
83 overflow PASS
84 FloatingPointError: Overflow
85
86 div by 0 PASS
87 FloatingPointError: Division by zero
88 [ more output from test elided ]
89 >>> import math
90 >>> math.exp(1000)
91 Traceback (most recent call last):
UltimateCoder88569402017-05-03 22:16:45 +053092 File "<stdin>", line 1, in <module>
Georg Brandl116aa622007-08-15 14:28:22 +000093 FloatingPointError: in math_1
94
95
96.. _fpectl-limitations:
97
98Limitations and other considerations
99------------------------------------
100
101Setting up a given processor to trap IEEE-754 floating point errors currently
102requires custom code on a per-architecture basis. You may have to modify
103:mod:`fpectl` to control your particular hardware.
104
105Conversion of an IEEE-754 exception to a Python exception requires that the
106wrapper macros ``PyFPE_START_PROTECT`` and ``PyFPE_END_PROTECT`` be inserted
107into your code in an appropriate fashion. Python itself has been modified to
108support the :mod:`fpectl` module, but many other codes of interest to numerical
109analysts have not.
110
111The :mod:`fpectl` module is not thread-safe.
112
113
114.. seealso::
115
116 Some files in the source distribution may be interesting in learning more about
117 how this module operates. The include file :file:`Include/pyfpe.h` discusses the
118 implementation of this module at some length. :file:`Modules/fpetestmodule.c`
119 gives several examples of use. Many additional examples can be found in
120 :file:`Objects/floatobject.c`.
121