3 ms·
I don't think you got that index calculation right. -1 in array of 5 elements is located at 5-1 == 4, not at 1%5 == 1.
by viraptor 8y ago
I don't think you got that index calculation right. -1 in array of 5 elements is located at 5-1 == 4, not at 1%5 == 1.
- kvakil 8y agoIt depends on how you define the modulo operator [0]--there are definitions where (-1)%5 == 4 and definitions where (-1)%5 == -1. Presumably the GP is referring to the former case. [0]: https://en.wikipedia.org/wiki/Modulo_operation#Remainder_calculation_for_the_modulo_operation https://en.wikipedia.org/wiki/Modulo_operation#Remainder_cal...
- pulisse 8y agoI don't follow your point. We're concerned with the value of -1 % 5, not 1 % 5. There's no definition of the modulus operator on which -1 % 5 = 1.
- viraptor 8y agoPhrasing issue, I think. I understood "Negative `i` indexes the element" as `i` itself being positive. Like you'd say "negative 1 indexes" (i is 1), not "negative -1 indexes" (i is -1)