WildThing - 3 months ago 39

C++ Question

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

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 x_{1}*2^{N}, x_{2}*2^{N}, … instead of x_{1}, x_{2}, … 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.