通过将resultIndex递增2来执行通过较大向量的迭代.在每次迭代期间,对索引处的较小向量(resultIndex-INDEX_OFFSET)/ 2进行赋值.
本质上,代码依赖于以下假设:无论INDEX_OFFSET是奇数还是偶数,上述除以2将始终向下舍入,无论体系结构如何.例如,如果resultIndex为0或1,则预期为0,如果是2或3,则预期为1,依此类推.在上述参数范围内,这是一个安全的假设吗?
注:我承认‘Dividing integer types – Are results predictable?’的存在,但它似乎并不完全匹配.
解决方法
[C++11: 5.6/4]:
The binary/
operator yields the quotient,and the binary%
operator yields the remainder from the division of the first expression by the second. If the second operand of/
or%
is zero the behavior is undefined. For integral operands the/
operator yields the algebraic quotient with any fractional part discarded; if the quotienta/b
is representable in the type of the result,(a/b)*b + a%b
is equal toa
.
在3/2中,3和2都是整数操作数;这个操作的代数商是1.5,当你丢弃小数部分.5时,你得到1.这适用于你的其他例子,以及所有其他例子.
原文地址:https://www.jb51.cc/c/117204.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。