Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect double precision floating point overflow and underflow?

Tags:

I have following variables:

double dblVar1; double dblVar2; 

They may have big values but less than double max.

I have various arithmetic on above variables like addition, multiplication and power:

double dblVar3 = dblVar1 * dblVar2;  double dblVar4 = dblVar1 + dblVar2; double dblVar5 = pow(dblVar1, 2); 

In all above I have to check overflow and underflow. How can I achieve this in C++?

like image 612
venkysmarty Avatar asked Mar 27 '13 09:03

venkysmarty


People also ask

What is overflow and underflow in floating point representation?

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.

What are the overflow underflow conditions for floating point addition and subtraction?

Underflow can in part be regarded as negative overflow of the exponent of the floating point value. For example, if the exponent part can represent values from −128 to 127, then a result with a value less than −128 may cause underflow.

Is 1.5 float or double?

And the reason the comparison succeeds with 1.5 is that 1.5 can be represented exactly as a float and as a double ; it has a bunch of zeros in its low bits, so when the promotion adds zeros the result is the same as the double representation.

What causes overflow in floating point?

When a program attempts to do that a floating point overflow occurs. In general, a floating point overflow occurs whenever the value being assigned to a variable is larger than the maximum possible value for that variable.


1 Answers

A lot depends on context. To be perfectly portable, you have to check before the operation, e.g. (for addition):

if ( (a < 0.0) == (b < 0.0)     && std::abs( b ) > std::numeric_limits<double>::max() - std::abs( a ) ) {     //  Addition would overflow... } 

Similar logic can be used for the four basic operators.

If all of the machines you target support IEEE (which is probably the case if you don't have to consider mainframes), you can just do the operations, then use isfinite or isinf on the results.

For underflow, the first question is whether a gradual underflow counts as underflow or not. If not, then simply checking if the results are zero and a != -b would do the trick. If you want to detect gradual underflow (which is probably only present if you have IEEE), then you can use isnormal—this will return false if the results correspond to gradual underflow. (Unlike overflow, you test for underflow after the operation.)

like image 125
James Kanze Avatar answered Nov 11 '22 13:11

James Kanze