I was working on a practice coding question on leetcode in c++, and I found that using the modulo operator on negative numbers returned 0 when it should not be returning 0. For context, I was testing -4 % 3, which returns 0. Does anyone know why this happens?
Here is my code:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> vec {1,2,3};
int k{4};
std::cout << (-k) % vec.size();
return 0;
}
This prints:
0