Message259944
Just for the record, there's a less-branchy analog of the floor division code I suggested for modulo. In Python-land, it looks like this:
def propermod(a, b):
# mimic the C setup
assert a != 0 and b != 0
left = abs(a)
size_a = -1 if a < 0 else 1
right = abs(b)
size_b = -1 if b < 0 else 1
# Compute mod: only one branch needed.
mod = left % right if size_a == size_b else right - 1 - (left - 1) % right
return mod * size_b
# Verify that we get the same results as the regular mod.
for n in range(-100, 100):
if n == 0:
continue
for d in range(-100, 100):
if d == 0:
continue
assert propermod(n, d) == n % d
It may well not have any significant effect here, though. |
|
Date |
User |
Action |
Args |
2016-02-09 16:35:40 | mark.dickinson | set | recipients:
+ mark.dickinson, pitrou, vstinner, serhiy.storchaka, yselivanov |
2016-02-09 16:35:40 | mark.dickinson | set | messageid: <1455035740.37.0.122502784336.issue26289@psf.upfronthosting.co.za> |
2016-02-09 16:35:40 | mark.dickinson | link | issue26289 messages |
2016-02-09 16:35:40 | mark.dickinson | create | |
|