function modHalf(v, d) {
    return Math.abs(v) > (d / 2) ?
        v - Math.round(v / d) * d :
        v;
}
sanitized modulus function that always returns in the range [-d/2, d/2]
rather than (-d, 0] if v is negative