简体   繁体   English

整数的模运算

[英]Modulo operation for integers

I am new in Python. 我是Python新手。 I just wonder, is there a modulo operation (%) exclusive for integers? 我只是想知道,是否存在整数专用的取模运算(%)? My interest focuses into large (number of digits ~ 10^9) integers. 我的兴趣集中在大整数(位数〜10 ^ 9)上。

Appreciate any help. 感谢任何帮助。 Erwin 欧文

The default modulo is ok for any kind of bigint. 对于任何类型的bigint,默认的模数都可以。 And the python3 int and python2 long are bigint by default. 并且python3 int和python2 long默认为bigint。

If you are worrying about performance, it is also ok because python check the types of operands and call the correct version of modulo. 如果您担心性能,也可以,因为python检查操作数的类型并调用正确的取模版本。 You can call int to int mod by this way: 您可以通过以下方式将int调用为int mod:

(10).__mod__(3)

which is just same to below except for type checking: 除类型检查外,其余与下面相同:

10 % 3

In this case, the problem is mostly the digits. 在这种情况下,问题主要是数字。 10^9 digits are not fit to design of python bigint. 10 ^ 9位数字不适合python bigint设计。 So it will take really long time. 因此,这将花费很长时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM