C语言 C中的算术下溢和溢出是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6360049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:55:36  来源:igfitidea点击:

What are arithmetic underflow and overflow in C?

cmathinteger-overflow

提问by Registered User

What do arithmetic underflow and overflow mean in C programming?

C 编程中的算术下溢和上溢是什么意思?

回答by Oliver Charlesworth

Overflow

溢出

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

来自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:

请注意,正如@R 在下面的评论中提到的,C 标准建议:

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.

涉及无符号操作数的计算永远不会溢出,因为无法由结果无符号整数类型表示的结果会以比结果类型可以表示的最大值大 1 的数为模减少。

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:

来自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!

回答by AlexandraC

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.

计算机仅使用 0 和 1 来表示数据,因此可以表示的值的范围是有限的。很多计算机使用32位来存储整数,所以这种情况下可以存储的最大无符号整数是2^32 -1 = 4294967295。但是第一位是用来表示符号的,所以,实际上最大值是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.

同样,对于实数,太小而无法存储的指数会导致下溢。

回答by Ben Sutton

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 是 C 中最常见的数据类型,是 32 位数据类型。这意味着每个 int 在内存中都有 32 位。如果我有变量

int a = 2;

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

这实际上在内存中表示为 32 位二进制数:00000000000000000000000000000010。

If you have two binary numbers such as

如果您有两个二进制数,例如

10000000000000000000000000000000
and
10000000000000000000000000000000,

10000000000000000000000000000000

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.

它们的总和为 100000000000000000000000000000000,即 33 位长。但是,计算机只取最低的 32 位,它们都是 0。在这种情况下,计算机识别出总和大于 32 位可以存储的总和,并给出溢出错误。

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.

下溢基本上是在相反方向发生的相同的事情。用于 C 的浮点标准允许小数点后 23 位;如果数字的精度超过这一点,它将无法存储这些位。这会导致下溢错误和/或精度损失。

回答by anita

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

下溢完全取决于给定的算法和给定的输入数据,因此程序员没有直接控制。另一方面,下溢取决于程序员为每个堆栈保留的内存空间量的任意选择,以及此选择确实会影响可能发生溢出的次数