整数除法在C ++ 11 [英] Integer Division in C++11

查看:107
本文介绍了整数除法在C ++ 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到C ++ 11的第5.6节的一些措辞更改。 (我正在查看草案C ++标准N3242,日期2011-02-28。)新的(草案)标准包括句子:

I was noticing some wording changes to section 5.6 for C++11. (I'm looking at the draft C++ standard N3242, dated 2011-02-28.) The new (draft) standard includes the sentence:

对于整数操作数/操作符产生代数商,任何小数部分都被丢弃;

"For integral operands the / operator yields the algebraic quotient with any fractional part discarded;"

这个语句不是03标准的5.6(ISO-IEC-14882-2003)不认为这是一个变化,是吗?这是C和C ++如何工作多年,除非我失去了我的想法(这可能已经发生了)。

This statement is not in 5.6 of the 03 standard (ISO-IEC-14882-2003), but I don't think this is a change, is it? This is how C and C++ has worked for years unless I've lost my mind (which may have happened anyway).

推荐答案

5.6 / 4 的脚注说:


[C ++ 03 footnote 74]:根据修订版本
C,用于整数除法的优选算法遵循在ISO Fortran标准ISO / IEC 1539:1991中定义的
规则,其中,商品总是舍入为零。

[C++03 footnote 74]: According to work underway toward the revision of ISO C, the preferred algorithm for integer division follows the rules defined in the ISO Fortran standard, ISO/IEC 1539:1991, in which the quotient is always rounded toward zero.

在C ++ 11中,这种行为是明确需要的,而不是preferred。更改列在兼容性部分中:

In C++11 this behaviour is explicitly required rather than being "preferred"; the change is listed in the compatibility section:


[C ++ 11:C.2.2] code>

更改:为整数 / %的结果指定舍入 对原始功能的影响使用整数除法的有效C ++ 2003代码
将结果四舍五入到0或负无穷大,而这个
国际标准总是将结果四舍五入到0.

[C++11: C.2.2]:
Change: Specify rounding for results of integer / and %
Rationale: Increase portability, C99 compatibility.
Effect on original feature: Valid C++ 2003 code that uses integer division rounds the result toward 0 or toward negative infinity, whereas this International Standard always rounds the result toward 0.

这篇关于整数除法在C ++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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