什么是算术下溢和溢出用C? [英] What are arithmetic underflow and overflow in C?

查看:250
本文介绍了什么是算术下溢和溢出用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有张贴问题之前谷歌搜索。 <一href=\"http://www.google.co.in/search?q=underflow%20and%20overflow%20errors%20in%20C&hl=en&prmd=ivns&ei=7sv4TeiOFIHWrQes-8C5CA&start=20&sa=N&biw=1366&bih=611\"相对=nofollow>这是我在我的搜索结果了。我想知道什么是算术下溢和溢出的概念相对于C语言编程。

I did a Google search before posting the question. This is what I got in my search results. I want to know what is this concept of arithmetic underflow and overflow with respect to C programming.

推荐答案

从<一个href=\"http://en.wikipedia.org/wiki/Arithmetic_overflow\">http://en.wikipedia.org/wiki/Arithmetic_overflow:

这是发生在一个条件
  计算产生的结果是
  在幅度上大于该哪一个
  给定的寄存器或存储位置
  存储或重新present。

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标准建议:

Note that as @R mentions in a comment below, the C standard suggests:

在无符号的一种计算
  操作数可以从不溢出,因为一个
  结果,不能再由psented $ P $
  所产生的无符号整数类型
  降低模是第一位的
  大于最大值,该值
  可通过由此而来psented重新$ P $
  类型。

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".

下溢

从<一个href=\"http://en.wikipedia.org/wiki/Arithmetic_underflow\">http://en.wikipedia.org/wiki/Arithmetic_underflow:

中的计算机程序的条件,即
  时可发生的真实结果
  浮点运算是在较小的
  大小(即,接近零)
  比最小的价值重估presentable
  在一个正常的浮点数
  目标数据类型。

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆