gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:46:22 25 4
gpt4 key购买 nike

算术下溢和溢出在 C 编程中意味着什么?

最佳答案

溢出

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

例如:

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

请注意,正如 @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.

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

下溢

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

例如:

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

关于c - C语言中的算术下溢和溢出是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6360049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com