Developpez.com

Python

Choisissez la catégorie, puis la rubrique :

26.13 fpectl -- Floating point exception control

26.13 fpectl -- Floating point exception control

Availability: Unix.

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.

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



See About this document... for information on suggesting changes.
Python 3.8.0 : un aperçu des fonctionnalités attendues pour la version stable du langage de programmation
Microsoft publie l'édition de juin 2019 de l'extension Python pour Visual Studio Code, elle inclut 70 correctifs et son lot de nouveautés
Introduction à la bibliothèque Pandas - Analyse de données en Python, un tutoriel de Gabor Laszlo Hajba traduit par l'équipe de rédaction
Découvrir et apprendre les possibilités de Python pour le calcul scientifique, un tutoriel de Lejocelyn
Contacter le responsable de la rubrique Python

Partenaire : Hébergement Web