GNU Info

Info Node: (python2.1-lib.info)fpectl

(python2.1-lib.info)fpectl


Next: atexit Prev: weakref Up: Python Runtime Services
Enter node , (file) or (file)node

Floating point exception control
================================

This module was written by Lee Busby <busby1@llnl.gov>.
This manual section was written by Lee Busby <busby1@llnl.gov>.
Provide control for floating point exception handling.

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

     >>> import math
     >>> math.exp(1000)
     inf
     >>> math.exp(1000) / math.exp(1000)
     nan

(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.

In some circumstances, it would be better to raise an exception and
stop processing at the point where the faulty operation was attempted.
The `fpectl' 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 `SIGFPE' 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, `SIGFPE' is trapped and
converted into the Python `FloatingPointError' exception.

The `fpectl' module defines the following functions and may raise the
given exception:

`turnon_sigfpe()'
     Turn on the generation of `SIGFPE', and set up an appropriate
     signal handler.

`turnoff_sigfpe()'
     Reset default handling of floating point exceptions.

`FloatingPointError'
     After `turnon_sigfpe()' 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.

Example 2
Limitations and other considerations

automatically generated by info2www version 1.2.2.9