d = ((d % 360) + 360) % 360;
will work if modulo you get a value from -359 to 359.
I personally would put this in a separate function, since it was as ugly as sin.
def mymodulo(n,m): return ((n % m) + m) % m;
- The first modulo gives you a value from -359 to 359.
- Addition adds to something between 1 and 719.
- The second module returns this back to the desired range: from 0 to 359.
For code golf, 29 characters, including a new line in Python:
def f(n,m):return((n%m)+m)%m
Not that Python really needed this: (-5)%360 gives you 355 in this language :-)
source share