C语言中的算术下溢和溢出是什么?
C 编程中算术下溢和溢出是什么意思?
What do arithmetic underflow and overflow mean in C programming?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
C 编程中算术下溢和溢出是什么意思?
What do arithmetic underflow and overflow mean in C programming?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
溢出
来自http://en.wikipedia.org/wiki/Arithmetic_overflow:
因此,例如:
请注意,正如 @R 在下面的评论中提到的,C 标准建议:
当然,这是“溢出”的一个相当特殊的定义。大多数人将模数减少(即环绕)称为“溢出”。
下溢
来自http://en.wikipedia.org/wiki/Arithmetic_underflow:
因此,例如:
Overflow
From http://en.wikipedia.org/wiki/Arithmetic_overflow:
So, for instance:
Note that as @R mentions in a comment below, the C standard suggests:
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:
So, for instance:
计算机仅使用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.
int 是 C 语言中最常见的数据类型,是 32 位数据类型。这意味着每个 int 在内存中都有 32 位。如果我有一个
实际在内存中表示为 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
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