C语言 整数除法的行为是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3602827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the behavior of integer division?
提问by T.T.T.
For example,
例如,
int result;
result = 125/100;
or
或者
result = 43/100;
Will result always be the floor of the division? What is the defined behavior?
结果将始终是除法的底线吗?什么是定义的行为?
采纳答案by dirkgently
Will result always be the floor of the division? What is the defined behavior?
结果将始终是除法的底线吗?什么是定义的行为?
Yes, integer quotient of the two operands.
是的,两个操作数的整数商。
6.5.5 Multiplicative operators
6When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded.88)If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.
6.5.5 乘法运算符
6整数相除时, / 运算符的结果是代数商,其中任何小数部分都被丢弃。88)如果商 a/b 是可表示的,则表达式 (a/b)*b + a%b 应等于 a。
and the corresponding footnote:
和相应的脚注:
88) This is often called ‘‘truncation toward zero''.
88) 这通常被称为“向零截断”。
Of course two points to note are:
当然要注意两点:
3The usual arithmetic conversions are performed on the operands.
3对操作数执行通常的算术转换。
and:
和:
5The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.
5/ 运算符的结果是第一个操作数除以第二个操作数的商;% 运算符的结果是余数。在这两个操作中,如果第二个操作数的值为零,则行为未定义。
[Note: Emphasis mine]
[注:强调我的]
回答by schot
Dirkgently gives an excellent descriptionof integer division in C99, but you should also know that in C89 integer division with a negative operand has an implementation-defined direction.
Dirkgently很好地描述了 C99 中的整数除法,但您也应该知道,在 C89 中,负操作数的整数除法具有实现定义的方向。
From the ANSI C draft (3.3.5):
来自 ANSI C 草案 (3.3.5):
If either operand is negative, whether the result of the / operator is the largest integer less than the algebraic quotient or the smallest integer greater than the algebraic quotient is implementation-defined, as is the sign of the result of the % operator. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.
如果任一操作数为负,则 / 运算符的结果是小于代数商的最大整数还是大于代数商的最小整数是实现定义的,% 运算符的结果符号也是如此。如果商 a/b 是可表示的,则表达式 (a/b)*b + a%b 应等于 a。
So watch out with negative numbers when you are stuck with a C89 compiler.
因此,当您使用 C89 编译器时,请注意负数。
It's a fun fact that C99 chose truncation towards zero because that was how FORTRAN did it. See this messageon comp.std.c.
C99 选择向零截断是一个有趣的事实,因为 FORTRAN 就是这样做的。在 comp.std.c 上查看此消息。
回答by Gareth Williams
Where the result is negative, C truncates towards 0 rather than flooring - I learnt this reading about why Python integer division always floors here: Why Python's Integer Division Floors
在结果为负的情况下,C 向 0 截断而不是地板 - 我在阅读这篇关于为什么 Python 整数除法总是地板的阅读:Why Python's Integer Division Floors
回答by Leonid
Yes, the result is always truncated towards zero. It will round towards the smallest absolute value.
是的,结果总是向零截断。它将向最小的绝对值四舍五入。
-5 / 2 = -2
5 / 2 = 2
For unsigned and non-negative signed values, this is the same as floor (rounding towards -Infinity).
对于无符号和非负有符号值,这与 floor 相同(向 -Infinity 四舍五入)。
回答by Mohamed El-Nakib
Will result always be the floor of the division?
结果将始终是除法的底线吗?
No. The result varies, but variation happens only for negative values.
不会。结果会发生变化,但变化只会发生在负值上。
What is the defined behavior?
什么是定义的行为?
To make it clear floor rounds towards negative infinity,while integer division rounds towards zero (truncates)
为了清楚地向负无穷大舍入,而整数除法向零舍入(截断)
For positive values they are the same
对于正值,它们是相同的
int integerDivisionResultPositive= 125/100;//= 1
double flooringResultPositive= floor(125.0/100.0);//=1.0
For negative value this is different
对于负值,这是不同的
int integerDivisionResultNegative= -125/100;//=-1
double flooringResultNegative= floor(-125.0/100.0);//=-2.0
回答by adi1ya
I know people have answered your question but in layman terms:
我知道人们已经回答了您的问题,但通俗地说:
5 / 2 = 2//since both 5 and 2 are integers and integers division always truncates decimals
5 / 2 = 2//因为5和2都是整数,整数除法总是截断小数
5.0 / 2 or 5 / 2.0 or 5.0 /2.0 = 2.5//here either 5 or 2 or both has decimal hence the quotient you will get will be in decimal.
5.0 / 2 or 5 / 2.0 or 5.0 /2.0 = 2.5//这里 5 或 2 或两者都有小数,因此您将得到的商为十进制。

