加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:49:08 所属栏目:百科 来源:网络整理
导读:我在发布问题之前做过Google搜索. This是我在搜索结果中得到的.我想知道关于C编程的算术下溢和溢出的概念是什么. 解决方法 溢出 从http://en.wikipedia.org/wiki/Arithmetic_overflow: the condition that occurs when a calculation produces a result tha
我在发布问题之前做过Google搜索. This是我在搜索结果中得到的.我想知道关于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!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读