Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 543
WildThing Avatar asked Jul 12 '13 08:07

WildThing


People also ask

What is underflow in floating point?

Underflow is said to occur when the true result of an arithmetic operation is smaller in magnitude (infinitesimal) than the smallest normalized floating point number which can be stored. Overflow can't be ignored in calculations whereas underflow can effectively be replaced by zero.

How do you avoid underflow errors?

One way around the problem is to multiply the integrand by a constant so large that there is no danger of underflow. Multiplying by constants does commute with integration. So suppose your integrand is on the order of 10^-400, far below the smallest representable number.

What is the difference between overflow and underflow when dealing with floating point?

Simply put, overflow and underflow happen when we assign a value that is out of range of the declared data type of the variable. If the (absolute) value is too big, we call it overflow, if the value is too small, we call it underflow.

What causes an underflow error?

Underflow is a condition or exception that results if a number calculation is too small to be represented by the CPU or memory. It may be caused by a limitation of the computer's hardware, its architecture, or the data type of the numbers used in the calculation.


1 Answers

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.

like image 150
Pascal Cuoq Avatar answered Oct 07 '22 00:10

Pascal Cuoq