整数除法的行为是什么?

发布于 2024-09-16 10:04:41 字数 152 浏览 6 评论 0 原文

例如,

int result;

result = 125/100;

或者

result = 43/100;

结果将始终是除法的下限吗?定义的行为是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

别靠近我心 2024-09-23 10:04:41

结果总是该部门的下限吗?定义的行为是什么?

不完全是。它向 0 截断,而不是向下截断。

6.5.5 乘法运算符

6 整数相除时,/运算符的结果是与任意整数的代数商
丢弃小数部分。88) 如果商 a/b 可表示,则表达式
(a/b)*b + a%b 应等于 a。

以及相应的脚注:

  • 这通常称为“向零截断”。
  • 当然,有两点需要注意:

    3 对操作数执行通常的算术转换。

    和:

    5 / 运算符的结果是
    除以的商
    第一个操作数由第二个操作数;这
    % 运算符的结果是
    余。 在这两个操作中,如果
    第二个操作数的值为零,
    行为未定义。

    [注意:强调我的]

    Will result always be the floor of the division? What is the defined behavior?

    Not quite. It truncates toward 0, rather than flooring.

    6.5.5 Multiplicative operators

    6 When 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.

    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.

    and:

    5 The 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.

    [Note: Emphasis mine]

    脱离于你 2024-09-23 10:04:41

    Dirkgently 给出了精彩的描述 C99 中的整数除法,但您还应该知道,在 C89 中,带有负操作数的整数除法具有实现定义的方向。

    来自 ANSI C 草案 (3.3.5):

    如果任一操作数为负,则 / 运算符的结果是小于代数商的最大整数还是大于代数商的最小整数是实现定义的,% 运算符结果的符号也是如此。如果商a/b是可表示的,则表达式(a/b)*b + a%b应等于a。

    因此,当您陷入 C89 编译器困境时,请注意负数。

    有趣的是,C99 选择了向零截断,因为 FORTRAN 就是这么做的。请参阅 comp.std.c 上的此消息

    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.

    From the ANSI C draft (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.

    So watch out with negative numbers when you are stuck with a C89 compiler.

    It's a fun fact that C99 chose truncation towards zero because that was how FORTRAN did it. See this message on comp.std.c.

    櫻之舞 2024-09-23 10:04:41

    是的,结果总是被截断为零。它将向最小绝对值舍入。

    -5 / 2 = -2
     5 / 2 =  2
    

    对于无符号和非负有符号值,这与下限相同(向 -Infinity 舍入)。

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

    脸赞 2024-09-23 10:04:41

    当结果为负数时,C 会向 0 截断而不是向下取整 - 我在这篇文章中了解到为什么 Python 整数除法总是向下取整:为什么 Python 的整数除法层

    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

    吻泪 2024-09-23 10:04:41

    结果总是该部门的下限吗?

    不会。结果会有所不同,但只有负值才会发生变化。

    定义的行为是什么?

    为了清楚起见,向下舍入到负无穷大,而整数除法舍入到零(截断)

    对于正值,它们是相同的

    int integerDivisionResultPositive= 125/100;//= 1
    double flooringResultPositive= floor(125.0/100.0);//=1.0
    

    对于负值,这是不同的

    int integerDivisionResultNegative= -125/100;//=-1
    double flooringResultNegative= floor(-125.0/100.0);//=-2.0
    

    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
    
    吾家有女初长成 2024-09-23 10:04:41

    我知道人们已经回答了你的问题,但用外行的话来说:

    5 / 2 = 2 //since 5 和 2 都是整数,整数除法总是截断小数

    5.0 / 2 或 5 / 2.0 或5.0 /2.0 = 2.5 //这里5或2或两者都有小数,因此你得到的商将是小数。

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

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文