这样不是容易搞错吗?
C/Java/C# 的 -13/3 = 13/-3 = -4
都是简单的正数除法加个负号
而 Ruby, Python 和 Perl 都是向负无穷规约,商为 -5
, 这个设计的优点是被除数为正的时候能保证余数总是非负的
(-13).divmod 3 # 商为 -5, 余 2
在数论上一般都是用非负余数
http://en.wikipedia.org/wiki/Modulo_operation
Usually, in number theory, the positive remainder is always chosen
Raymond T. Boute[6] describes the Euclidean definition, which is the one in which the remainder is always positive or 0, and is therefore consistent with the division algorithm