Reputation: 169
Is there a function that computes modulo but with offset like in second output?
Something like mod(23,13,-6)==-3
where -6
is offset. It is equivalent to (23+6)%13-6==-3
.
I know that I can define my own function but I am not sure it would be as fast as built-in function.
d=13
off=int((d-1)/2)
print([x%d for x in range(d)])
print([(x+off)%d-off for x in range(d)])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[0, 1, 2, 3, 4, 5, 6, -6, -5, -4, -3, -2, -1]
Update:
The variant with offset equal (d-1)/2
can be defined also this way (see Wikipedia link in my comment below):
d=13
print([x-d*round(x/d) for x in range(d)])
[0, 1, 2, 3, 4, 5, 6, -6, -5, -4, -3, -2, -1]
Upvotes: 1
Views: 280
Reputation: 169
I just found this (going to check whether I again any speed improvements over the definitions in OP or not):
Also the down side is that math.remainder(x,d)
needs additional int
to convert to integers.
import math
d=13
print([int(math.remainder(x,d)) for x in range(d)])
[0, 1, 2, 3, 4, 5, 6, -6, -5, -4, -3, -2, -1]
Upvotes: 0