C++ STL modulus(取模): %

取模运算在c++11中用%表示。

根据ISO14882:2011(e) 

The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined. For integral operands the / operator yields the algebraic quotient with any fractional part discarded; if the quotient a/b is representable in the type of the result, (a/b)*b + a%b is equal to a.

(-7/3)→ -2

-2*3→-6

so (-7%3)→-1

(7/-3)→-2

-2*-3→6

so (7%-3)→1 

与我想象的取模还是有差异的 - . -

发布了56 篇原创文章 · 获赞 10 · 访问量 6827

猜你喜欢

转载自blog.csdn.net/qq_22148493/article/details/88253688