Basically, I need (-3) % 5
to be "2" instead of "-3". Python produces "2", but C++ produces "-3". Not sure how to produce "2" in C++. Thanks!
The modulus of a negative number is found by ignoring the minus sign. The modulus of a number is denoted by writing vertical lines around the number. Note also that the modulus of a negative number can be found by multiplying it by −1 since, for example, −(−8) = 8.
Most easily: ((x % 5) + 5) % 5
Add the base if the input number X
is negative:
X % Y + (X % Y < 0 ? Y : 0);
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