def opt_mod(num, div):
    """returns nonnegative or negative modulo residue depending on whichever one has a lower absolute value
    (if both equal, returns nonnegative)"""
    res = num % div
    return res if res <= (div/2) else res-div