开发者

What are arithmetic underflow and overflow in C?

What do arithmetic underflow and overflow mea开发者_如何转开发n in C programming?


Overflow

From http://en.wikipedia.org/wiki/Arithmetic_overflow:

the condition that occurs when a calculation produces a result that is greater in magnitude than that which a given register or storage location can store or represent.

So, for instance:

uint32_t x = 1UL << 31;
x *= 2;  // Overflow!

Note that as @R mentions in a comment below, the C standard suggests:

A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.

Of course, this is a fairly idiosyncratic definition of "overflow". Most people would refer to modulo reduction (i.e wrap-around) as "overflow".

Underflow

From http://en.wikipedia.org/wiki/Arithmetic_underflow:

the condition in a computer program that can occur when the true result of a floating point operation is smaller in magnitude (that is, closer to zero) than the smallest value representable as a normal floating point number in the target datatype.

So, for instance:

float x = 1e-30;
x /= 1e20; // Underflow!


Computers use only 0 and 1 to represent data so that the range of values that can be represented is limited. Many computers use 32 bits to store integers, so the largest unsigned integer that can be stored in this case is 2^32 -1 = 4294967295. But the first bit is used to represent the sign, so, in fact, the largest value is 2^31 - 1 = 2147483647.

The situation where an integer outside the allowed range requires more bits than can be stored is called an overflow.

Similarly, with real numbers, an exponent that is too small to be stored causes an underflow.


int, the most common data type in C, is a 32-bit data type. This means that each int is given 32 bits in memory. If I had the variable

int a = 2;

that would actually be represented in memory as a 32-bit binary number: 00000000000000000000000000000010.

If you have two binary numbers such as

10000000000000000000000000000000
and
10000000000000000000000000000000,

their sum would be 100000000000000000000000000000000, which is 33 bits long. However, the computer only takes the 32 least significant bits, which are all 0. In this case the computer recognizes that the sum is greater than what can be stored in 32 bits, and gives an overflow error.

An underflow is basically the same thing happening in the opposite direction. The floating-point standard used for C allows for 23 bits after the decimal place; if the number has precision beyond this point it won't be able to store those bits. This results in an underflow error and/or loss of precision.


underflow depends exclusively upon the given algorithm and the given input data,and hence there is no direct control by the programmer .Overflow on the other hand, depends upon the arbitrary choice of the programmer for the amount of memory space reserved for each stack ,and this choice does influence the number of times overflow may occur

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜