Duplicate of Weird Objective-C Mod Behavior
I'm trying to mod an integer to get an array position so that it will loop round. Doing i % arrayLength works fine for positive numbers but for negative numbers it all goes wrong.
so i need an implementation of
int GetArrayIndex(int i, int arrayLength)
such that
GetArrayIndex(-4, 3) == 2
GetArrayIndex(-3, 3) == 0
GetArrayIndex(-2, 3) == 1
GetArrayIndex(-1, 3) == 2
GetArrayIndex( 0, 3) == 0
GetArrayIndex( 1, 3) == 1
GetArrayIndex( 2, 3) == 2
GetArrayIndex( 3, 3) == 0
GetArrayIndex( 4, 3) == 1
I've done this before but for some reason it's melting my brain today :(