3 ms·
You are correct. I thinko'd and missed the edit window. I meant to say: Math usually considers (-7 mod 5) === (3 mod 5). But in C, (-7 % 5 != 3 % 5). The is
by michael1999 2y ago
You are correct. I thinko'd and missed the edit window. I meant to say:
Math usually considers (-7 mod 5) === (3 mod 5). But in C, (-7 % 5 != 3 % 5).
The issue is that -7 and 3 are congruent, but the % operator keeps the sign. So -7 % 5 yields -2, not +3. Those are congruent, but not equal. I've never had a use for this behaviour, but I've definitely had to work around it. The lazy way is ((x % n) + n) % n which is safe (assuming n > 0).
- arcastroe 2y ago+1. I wholeheartedly agree. That "lazy way" looks all too familiar haha.