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