I'm working on a modular sorter in python3 and have noticed weird behavior to do with the signs of input variables when taking the mod. I intend for it to sort a value by the remainder after dividing by the cycle length.
I.e., c = 4 / 14 = 0 r **4**", "c = 18 / 14 = 1 r **4**", and "c = 32 / 14 = 2 r **4**
All of these are 4th position values because they leave 4. However, if any of the input variables are negative, the result is not what I intend. Any ideas as to how this works (I might use this weird functionality in later coding)?
a = 4 #the value for sorting
b = 14 #the length of the cycle before it reaches the 0 position
def modS(val, cyc):
c = val % cyc
print(c)
modS(a, b)
E.g.,
a = 4
b = 14
prints 4
a = -4
b = 14
prints 10
a = 4
b = -14
prints -10
a = -4
b = -14
prints -4
These printed values might be useful for stuff like pH calculations, but not for my purposes.