Mod liczby ujemnej topi mój mózg
Próbuję zmodyfikować liczbę całkowitą, aby uzyskać pozycję tablicową, tak aby była pętla. Robići % arrayLength
działa dobrze dla liczb dodatnich, ale dla liczb ujemnych wszystko idzie źle.
4 % 3 == 1
3 % 3 == 0
2 % 3 == 2
1 % 3 == 1
0 % 3 == 0
-1 % 3 == -1
-2 % 3 == -2
-3 % 3 == 0
-4 % 3 == -1
więc potrzebuję wdrożenia
int GetArrayIndex(int i, int arrayLength)
takie
GetArrayIndex( 4, 3) == 1
GetArrayIndex( 3, 3) == 0
GetArrayIndex( 2, 3) == 2
GetArrayIndex( 1, 3) == 1
GetArrayIndex( 0, 3) == 0
GetArrayIndex(-1, 3) == 2
GetArrayIndex(-2, 3) == 1
GetArrayIndex(-3, 3) == 0
GetArrayIndex(-4, 3) == 2
Zrobiłem to już wcześniej, ale z jakiegoś powodu topi mój mózg dzisiaj :(