python如何获得截短的模型与地板模型
我可以像 Truncored versused Discore diverored
我们使用 / vs //
>>> from __future__ import division
>>> -1 / 2
-0.5
>>> -1 // 2
-1
,但是Modulo是否有等效的?
就像当股息为负面
时 -9 mod 4。显然,我们有
-9 = 4 *(-2) - 1
和
-9 = 4 *(-3) + 3。
地板模量暗示-9 mod 4 = 3
截断的模量暗示 - 9 mod 4 = -1
对于Python Modulo操作员,它使用%地板版本为默认版本 -61%60 = 59
是否内置/库支持类似于Modulo的截短与地板部门,还是我需要使用自己的?
这是关于截断的与地板模型,而不是关于划分
I get Truncated versus floored division in Python can be used like this Truncated versus floored division in Python
we use / vs //
>>> from __future__ import division
>>> -1 / 2
-0.5
>>> -1 // 2
-1
but is there an equivalent for modulo?
like when the dividend is negative
for
−9 mod 4. Clearly, we have
−9 = 4 * (−2) − 1
and
−9 = 4 * (−3) + 3.
the floored modulo implies that −9 mod 4 = 3
the truncated modulo implies that −9 mod 4 = −1
for python modulo operator, it is using % floored version as default
-61 % 60 = 59
is there built in/library support similar to truncated versus floored division for modulo or I need to use my own?
this is about truncated vs floored modulo not about division
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有
math.remain.remain.remain.remainder
:There is
math.remainder
:python
因此,经过一些研究,默认模拟方法是语言取决于语言的,例如,javaScript的
是因为它使用js,它使用trunc方法
用于python,
但是没有简单的方法可以像我们一样在python中切换python和使用 / vs //
但是,有一个数学库功能,我们可以使用
值得注意的是,并不是Python中的所有Modulo操作都是相同的。虽然与int和float类型一起使用的模量将带有除数的迹象,但其他类型不会。
参考 https://realpython.com/python-modulo-operator/#modulo-operator-with-a-a-negative-operand
So upon some research the default modulo method is language dependent, for python
for javascript for example
it is because for js it uses the trunc method
for python it is the floor method
however there is no simple way to switch in python like we do with floor and trunc division to use / vs //
but there is a math library function we could use
worth noting not all modulo operations in Python are the same. While the modulo used with the int and float types will take the sign of the divisor, other types will not.
reference https://realpython.com/python-modulo-operator/#modulo-operator-with-a-negative-operand