New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Euclidean operator to work with negative steps #73

Merged
merged 1 commit into from Mar 14, 2016

Conversation

Projects
None yet
2 participants
@samdoshi
Collaborator

samdoshi commented Mar 14, 2016

The ER operator wasn't working correctly with a negative step value. Negative steps can come about in normal use when you're trying to do an offset, e.g.

ER fill length SUB step rotation

Anyway, turns out % in C is a remainder, rather than modulo, e.g:

#include <stdio.h>

int main() {
    for (int step=-10; step<=10; step++) {
        int length = 4;
        int remainder = step % length;
        int modulo = remainder < 0 ? remainder + length : remainder;
        printf("step: %3i   remainder: %2i   modulo: %i\n", step, remainder, modulo);
    }
}

gives:

step: -10   remainder: -2   modulo: 2
step:  -9   remainder: -1   modulo: 3
step:  -8   remainder:  0   modulo: 0
step:  -7   remainder: -3   modulo: 1
step:  -6   remainder: -2   modulo: 2
step:  -5   remainder: -1   modulo: 3
step:  -4   remainder:  0   modulo: 0
step:  -3   remainder: -3   modulo: 1
step:  -2   remainder: -2   modulo: 2
step:  -1   remainder: -1   modulo: 3
step:   0   remainder:  0   modulo: 0
step:   1   remainder:  1   modulo: 1
step:   2   remainder:  2   modulo: 2
step:   3   remainder:  3   modulo: 3
step:   4   remainder:  0   modulo: 0
step:   5   remainder:  1   modulo: 1
step:   6   remainder:  2   modulo: 2
step:   7   remainder:  3   modulo: 3
step:   8   remainder:  0   modulo: 0
step:   9   remainder:  1   modulo: 1
step:  10   remainder:  2   modulo: 2
fix euclidean operator to work with negative steps
the remainder operator, a % b, is bounded -b < a < b, we need 0 <= a < b

tehn added a commit that referenced this pull request Mar 14, 2016

Merge pull request #73 from samdoshi/er-fix
Fix the Euclidean operator to work with negative steps

@tehn tehn merged commit 3e4a063 into tehn:master Mar 14, 2016

@samdoshi samdoshi deleted the samdoshi:er-fix branch Mar 14, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment