mercredi 25 mars 2015

Modulo Operator (%) gives divergent results

Given this Example:



std::vector<int> numbers = {5,6,7}; //size is 3
int i = -1;
std::cout << i % 3 <<"\n"; // output: -1
std::cout << i % numbers.size() << "\n"; // output: 0


basically in both statements im processing -1 % 3 but the compiler outputs different numbers. I don't understand this outcome, maybe someone can explain it to me.


Aucun commentaire:

Enregistrer un commentaire