我知道Python//向负无穷大舍入,而在C++中则/是截断,向0舍入。
到目前为止,这是我所知道的:
|remainder|
-12 / 10 = -1, - 2 // C++
-12 // 10 = -2, + 8 # Python
12 / -10 = -1, 2 // C++
12 // -10 = -2, - 8 # Python
12 / 10 = 1, 2 // Both
12 // 10 = 1, 2
-12 / -10 = 1, - 2 // Both
= 2, + 8
C++:
1. m%(-n) == m%n
2. -m%n == -(m%n)
3. (m/n)*n …Run Code Online (Sandbox Code Playgroud) 正在查看其他答案,我仍然不理解python中负数的模数
例如df的回答
x == (x/y)*y + (x%y)
Run Code Online (Sandbox Code Playgroud)
所以有意义的是(-2)%5 = -2 - (-2/5)*5 = 3
这不是(-2 - (-2/5)*5)= 0还是我只是疯了? 具有负值的模数运算 - 奇怪的是什么?
最后,如果该标志取决于股息,为什么负股息与正面股票的产出不相同?
例如,输出
print([8%5,-8%5,4%5,-4%5])
Run Code Online (Sandbox Code Playgroud)
是
[3, 2, 4, 1]
Run Code Online (Sandbox Code Playgroud)