我正在从教科书中实现量化算法.我正处于几乎可以工作的地方,除了我在四舍五入时得到一个一个错误.这就是教科书对此的评价:
2^p可以通过添加偏移和右移位p位位置来执行舍入除法
现在,我对正确的转变有所了解,但他们谈论的是什么偏移?
这是我的示例代码:
def scale(x, power2=16):
if x < 0:
return -((-x) >> power2)
else:
return x >> power2
def main():
inp = [ 12595827, -330706, 196605, -387168, -274244, 377496, -241980,
-545272, -196605, 24198, 196605, 193584, 104858, 424683,
-40330, 41944 ]
expect = [ 192, -5, 3, -6, -4, 5, -3, -8, -3, 0, 3, 3, 1, 6, 0, 0 ]
actual = map(scale, inp)
for i in range(len(expect)):
if actual[i] == expect[i]:
continue
print 'inp: % 8d …Run Code Online (Sandbox Code Playgroud)