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?
An example of floating-point underflow is:
double d = DBL_MIN / 3.0;
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.