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

发布于 2024-11-15 17:08:32 字数 25 浏览 3 评论 0原文

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

What do arithmetic underflow and overflow mean in C programming?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

辞慾 2024-11-22 17:08:32

溢出

来自http://en.wikipedia.org/wiki/Arithmetic_overflow

发生的情况
计算产生的结果是
其幅度大于
给定的寄存器或存储位置可以
存储或代表。

因此,例如:

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

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

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

当然,这是“溢出”的一个相当特殊的定义。大多数人将模数减少(即环绕)称为“溢出”。

下溢

来自http://en.wikipedia.org/wiki/Arithmetic_underflow

计算机程序中的条件
当真实结果发生时,可能会发生
浮点运算较小
幅度(即接近于零)
比可表示的最小值
作为普通浮点数
目标数据类型。

因此,例如:

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

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!
迷你仙 2024-11-22 17:08:32

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

超出允许范围的整数需要的位数超过可存储的位数的情况称为溢出。

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

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.

初见终念 2024-11-22 17:08:32

int 是 C 语言中最常见的数据类型,是 32 位数据类型。这意味着每个 int 在内存中都有 32 位。如果我有一个

int a = 2;

实际在内存中表示为 32 位二进制数的变量:
00000000000000000000000000000010。

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

10000000000000000000000000000000

10000000000000000000000000000000,

它们的总和将是 100000000000000000000000000000000,长度为 33 位。然而,计算机只取 32 个最低有效位,这些位全部为 0。在这种情况下,计算机识别出总和大于 32 位可以存储的值,并给出溢出错误。

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

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.

比忠 2024-11-22 17:08:32

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文