How to handle floating point streams?

I am trying to understand the numerical properties of C ++. Thus, I am interested in the phenomenon of insufficiency. Can someone give me an example of overflow and how to handle it?

+4
source share
2 answers

An example of a floating point overflow is:

double d = DBL_MIN / 3.0; 

The corresponding implementation of IEEE 754 should set d to " subnormal ", that is, a number that is so close to zero that accuracy decreases. You will find a lot of information about Wikipedia .

Some implementations may Flush to Zero. The consequence in the above example is setting d to zero.

Inadequacy is the result of an increase in negative indicators that are not available to represent the number. Sometimes they can be avoided by β€œnormalizing” the calculation, which is equivalent to computing on x 1 * 2 N x 2 * 2 N , ... instead of x 1 , x 2 , ... for N of your choice.

The disadvantage of floating point is not undefined behavior. You can, if you want, use FPU exceptions to detect it either by polling or by receiving SIGFPE. Note that "FPU exceptions" have nothing to do with C ++ exceptions except for the name.

+4
source
 int main() { short int x ; for(x=0;;x++) { printf("%d \n",x); if(x < 0) break; } } 

o / r

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

Assuming you are asking for an underflow concept in a signed no. Here, the concept β€” signed no β€” is work like a circle, when you reach half the circle that it will go into - half that circle, and it will continue and never end. it starts from 0-32767 (+ve) -32768 to -1 (-ve) half.

The responsibility for this situation lies with the programmer. The compiler will not cause overflow errors.

Hope this helps.

-3
source

Source: https://habr.com/ru/post/1491072/


All Articles