通过将resultIndex递增2来执行通过较大向量的迭代.在每次迭代期间,对索引处的较小向量(resultIndex-INDEX_OFFSET)/ 2进行赋值.@H_403_3@
本质上,代码依赖于以下假设:无论INDEX_OFFSET是奇数还是偶数,上述除以2将始终向下舍入,无论体系结构如何.例如,如果resultIndex为0或1,则预期为0,如果是2或3,则预期为1,依此类推.在上述参数范围内,这是一个安全的假设吗?@H_403_3@
注:我承认‘Dividing integer types – Are results predictable?’的存在,但它似乎并不完全匹配.@H_403_3@
解决方法
[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
.@H_403_3@
在3/2中,3和2都是整数操作数;这个操作的代数商是1.5,当你丢弃小数部分.5时,你得到1.这适用于你的其他例子,以及所有其他例子.@H_403_3@