Why does -26 % 10 output -6 in C++ ?
How to get the standard modulo, that would output 4 ?
Note: this question is probably a duplicate of this one, or many others, but I haven't found one formulated in a short way, understandable in an eye blink. And as I personnaly like finding short questions / short answers (for simple issues), I thought it may interest other people. I'll delete this notice later.
One needs to do
(-26 % 10 + 10) % 10
to get 4.
For future reference :
int mod(int a, int b) { return (a % b + b) % b; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With