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?

Was it helpful?

Solution

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.

OTHER TIPS

int main()
{
        short int x ;

        for(x=0;;x++)
        {
                printf("%d \n",x);

                if(x < 0)
                  break;
        }
}

o/p

---
---
--
32761
32762
32763
32764
32765
32766
32767
-32768

Assuming you are asking the underflow concept in signed no. Here the concept is the signed no is works like a circle once you reached the half of the circle it will go in -ve half of that circle and it will continue and never ends. it start for 0-32767 (+ve) -32768 to -1 (-ve) half.

This is the responsibility of programmer to handle this situation.Compiler won't raise any error if you underflow.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top