新手问题 请教一个关于 Ruby 取模的问题?

abulo · May 19, 2013 · Last by Thomas replied at April 12, 2019 · 5886 hits

请教一个关于 ruby 取模的问题 比如: -8%5 =>2 8%-5 =>-2 -8%-5 =>-3

这个值为什么会这样??

$ ri Integer.%
...
See Numeric#divmod.
$ ri Numeric.divmod
...
The quotient is rounded toward -infinity.

商总是向负无穷方向取整,那余数都是按这个规则算出来的

@luikore 刚刚看了下,好像明白了,谢谢

不明白,-8 整除 5 怎么会得到 2?

You need to Sign in before reply, if you don't have an account, please Sign up first.