How to handle floating point exceptions?

R

Rui Maciel

Is there a C++ way to handle floating point exceptions?


Thanks in advance,
Rui Maciel
 
A

Alf P. Steinbach

* Rui Maciel, on 30.05.2010 12:51:
Is there a C++ way to handle floating point exceptions?

If you mean "does the C++ standard support floating point exceptions" then the
answer is no, as it is for GUI, database, whatever. Arguably floating point
exceptions are in a more fundamental class than GUI functionality or database
functionality. But there you are: the only slight concession to the existence of
floating point exceptions is the somewhat unreliable information available via
std::numeric_limits (it's unreliable since one main compiler, g++, can be
configured to lie and AFAIK offers no way to detect whether it lies).

If you mean "are some ways of handling C++ floating point exceptions more in the
spirit of C++ than others" then the answer is yes.

Good C++ code assumes non-restartable synchronous exceptions and that exceptions
can be safely passed up through the call chain (which means e.g. using
destructors for cleanup, called RAII). So restartable exceptions would not be in
the spirit of C++, and you'd risk Undefined Behavior. Likewise, asynchronous
exceptions, exceptions occuring as if out of nowhere in the middle of some
operation designed not to throw, risks Undefined Behavior.

A simple way to deal with floating point errors is to assert that floating point
operations are IEEE 754, turn off floating point exceptions if they're not
already off, and detect NaN values. However, considering the g++ optimization
option I alluded to above there is AFAIK /no way/ to portably assert that the
operations conform to IEEE 754. You can assert that the bitlevel representation
is IEEE 754, but that doesn't help much.

A more practical way is to /assume/ that operations are IEEE 754, and document
this. Anyone using your code with non-IEEE 754 is screwed by own choice. :)
Then you can just detect NaNs at selected points and, if needed, throw ordinary
C++ exceptions, which are non-restartable and synchronous and safe.


Cheers & hth.,

- Alf
 
R

Rui Maciel

Alf P. Steinbach wrote:

A simple way to deal with floating point errors is to assert that floating
point operations are IEEE 754, turn off floating point exceptions if
they're not already off, and detect NaN values. However, considering the
g++ optimization option I alluded to above there is AFAIK /no way/ to
portably assert that the operations conform to IEEE 754. You can assert
that the bitlevel representation is IEEE 754, but that doesn't help much.

First off, thanks for the insightful post, Alf. Great stuff.

Regarding the subject, what about fenv.h[1]? Does it provide a decent way to check for floating
point exceptions?


Rui Maciel

[1] http://www.opengroup.org/onlinepubs/000095399/basedefs/fenv.h.html
 
J

James Kanze

Is there a C++ way to handle floating point exceptions?

No.

More precisely, there aren't any floating point "exceptions", in
the sense of C++ exceptions. Floating point errors may (or may
not) generate traps (which are often called exceptions outside
of C++), but these are closer to signals that to C++ exceptions,
in the language of C++.

If your platform uses IEEE (most modern non-mainframes do), then
it should be possible to configure floating point so that it
doesn't trap, but simply propagates errors (and infinities)
through. For many applications, this is the simplest solution;
just verify at the end of the calculation that the results are
not a NaN or an infinity. (And many is not all---if the total
calculation is going to run some hours, you may want to detect
an error at the beginning immediately and abort.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top