WildThing WildThing - 1 year ago 104
C++ Question

How to handle floating-point underflow?

I'm trying to understand C++ numerical properties. Thus, I am interested by the underflow phenomenon. Can anyone give me an example of an underflow and how to handle it?

Answer Source

An example of floating-point underflow is:

double d = DBL_MIN / 3.0;

A conforming IEEE 754 implementation should set d to a “subnormal”, that is, a number that is so close to zero that precision is reduced. You will find plenty of information on Wikipedia.

Some implementations may “Flush to Zero”. The consequence in the example above is to set d to zero.

An underflow is the result of larger negative exponents not being available to represent the number. It is sometimes possible to avoid them by “normalizing” the computation, which amounts to somehow computing on x1*2N, x2*2N, … instead of x1, x2, … for an N of your choice.

Floating-point underflow is not undefined behavior. You can, if you wish, use “FPU exceptions” to detect it either by polling or by receiving SIGFPE. Note that “FPU exceptions” have nothing in common with C++ exceptions except the name.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download