How to make the mod of a negative number to be positive?

后端 未结 6 1201
独厮守ぢ
独厮守ぢ 2021-02-18 13:03

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!

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-18 13:24

    I see a lot of suggestions for ((x % 5) + 5) % 5 But I'm getting the same result with just (X + 5) % 5

    (X + divisor) % divisor.

提交回复
热议问题