简体   繁体   English

integer 分区的行为是什么?

[英]What is the behavior of integer division?

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?定义的行为是什么?

Will result always be the floor of the division?结果会永远是分区的底线吗? What is the defined behavior?定义的行为是什么?

Not quite.不完全的。 It rounds toward 0, rather than flooring.它向 0 舍入,而不是地板。

6.5.5 Multiplicative operators 6.5.5 乘法运算符

6 When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. 6当整数相除时,/ 运算符的结果是代数商,其中任何小数部分都被丢弃。 88) If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a. 88)如果商 a/b 是可表示的,则表达式 (a/b)*b + a%b 应等于 a。

and the corresponding footnote:和相应的脚注:

  1. This is often called ''truncation toward zero''.这通常被称为“向零截断”。

Of course two points to note are:当然有两点需要注意:

3 The usual arithmetic conversions are performed on the operands. 3对操作数执行通常的算术转换。

and:和:

5 The result of the / operator is the quotient from the division of the first operand by the second; 5 / 运算符的结果是第一个操作数除以第二个的商; the result of the % operator is the remainder. % 运算符的结果是余数。 In both operations, if the value of the second operand is zero, the behavior is undefined.在这两种操作中,如果第二个操作数的值为零,则行为未定义。

[Note: Emphasis mine] [注:强调我的]

Dirkgently gives an excellent description of 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.这是一个有趣的事实,C99 选择截断为零,因为 FORTRAN 就是这样做的。 See this message on comp.std.c.在 comp.std.c 上查看 此消息

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 舍入)。

结果为负数时,C 向 0 截断而不是取整——我读到这篇文章,了解为什么 Python 整数除法总是在这里取整: Why Python's Integer Division Floors<\/a>

"

Will result always be the floor of the division?结果会永远是分区的底线吗?

<\/blockquote>

No. The result varies, but variation happens only for negative values.不会。结果会有所不同,但只有负值才会发生变化。

What is the defined behavior?定义的行为是什么?

<\/blockquote>

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<\/code><\/pre>

For negative value this is different对于负值,这是不同的

int integerDivisionResultNegative= -125\/100;\/\/=-1 double flooringResultNegative= floor(-125.0\/100.0);\/\/=-2.0<\/code><\/pre>"

I know people have answered your question but in layman terms:我知道人们已经回答了你的问题,但是用外行的话:

5 \/ 2 = 2<\/code> \/\/since both 5 and 2 are integers and integers division always truncates decimals 5 \/ 2 = 2<\/code> \/\/因为 5 和 2 都是整数,整数除法总是截断小数

5.0 \/ 2 or 5 \/ 2.0 or 5.0 \/2.0 = 2.5<\/code> \/\/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<\/code> \/\/这里 5 或 2 或两者都有小数,因此您得到的商将是小数。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM