python如何获得截短的模型与地板模型

发布于 2025-02-12 18:55:37 字数 1189 浏览 3 评论 0原文

我可以像 Trunco​​red 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的截短与地板部门,还是我需要使用自己的?

这是关于截断的与地板模型,而不是关于划分

参考 https://www.omnicalculator.com/math/modulo-of-negative-numbers#:~: text=remainder%20are%20ARE%20Negative.-,当%20both%20%20Divisor%20和%20Dividend%20 are%20元,division%20 return%20%20个元%20个emagemainder

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

reference https://www.omnicalculator.com/math/modulo-of-negative-numbers#:~:text=remainder%20are%20negative.-,When%20both%20the%20divisor%20and%20dividend%20are%20negative,division%20return%20the%20negative%20remainder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞旧 2025-02-19 18:55:37

math.remain.remain.remain.remainder

>>> import math
>>> math.remainder(-9, 4)
-1.0
>>> int(math.remainder(-9, 4))
-1
>>> -9 % 4
3

There is math.remainder:

>>> import math
>>> math.remainder(-9, 4)
-1.0
>>> int(math.remainder(-9, 4))
-1
>>> -9 % 4
3
骄兵必败 2025-02-19 18:55:37

python

8 % -3 = -1

因此,经过一些研究,默认模拟方法是语言取决于语言的,例如,javaScript的

8 % -3 = 2

是因为它使用js,它使用trunc方法

r = a - (n * trunc(a/n))
r is the remainder.
a is the dividend.
n is the divisor.

用于python,

r = a - (n * floor(a/n))

但是没有简单的方法可以像我们一样在python中切换python和使用 / vs //
但是,有一个数学库功能,我们可以使用

>>> 8.0 % -3
-1.0

>>> import math
>>> math.fmod(8.0, -3.0)
2.0

值得注意的是,并不是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

8 % -3 = -1

for javascript for example

8 % -3 = 2

it is because for js it uses the trunc method

r = a - (n * trunc(a/n))
r is the remainder.
a is the dividend.
n is the divisor.

for python it is the floor method

r = a - (n * floor(a/n))

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

>>> 8.0 % -3
-1.0

>>> import math
>>> math.fmod(8.0, -3.0)
2.0

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文