通过将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.这适用于你的其他例子,以及所有其他例子.
总结以上是内存溢出为你收集整理的对于c整数,1除以2是否可靠地等于0,而3/2 = 1,5/2 = 2等?全部内容,希望文章能够帮你解决对于c整数,1除以2是否可靠地等于0,而3/2 = 1,5/2 = 2等?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)