如何在 Python 中管理大数的除法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27946595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:30:12  来源:igfitidea点击:

How to manage division of huge numbers in Python?

pythonfloating-pointintegerdivision

提问by Ambidextrous

I have a 100 digit number and I am trying to put all the digits of the number into a list, so that I can perform operations on them. To do this, I am using the following code:

我有一个 100 位数字,我试图将数字的所有数字放入一个列表中,以便我可以对它们执行操作。为此,我使用以下代码:

for x in range (0, 1000):
   list[x] = number % 10
   number = number / 10

But the problem I am facing is that I am getting an overflow error something like too large number float/integer. I even tried using following alternative

但是我面临的问题是我收到了一个溢出错误,比如浮点数/整数太大。我什至尝试使用以下替代方法

number = int (number / 10)

How can I divide this huge number with the result back in integer type, that is no floats?

如何将这个巨大的数字与整数类型的结果相除,即没有浮点数?

采纳答案by Alex Riley

In Python 3, number / 10will try to return a float. However, floating point values can't be of arbitrarily large size in Python and if numberis large an OverflowErrorwill be raised.

在 Python 3 中,number / 10将尝试返回一个float. 但是,浮点值在 Python 中不能有任意大的大小,如果number很大,OverflowError将会引发。

You can find the maximum that Python floating point values can take on your system using the sysmodule:

您可以使用以下sys模块找到 Python 浮点值可以在您的系统上使用的最大值:

>>> import sys
>>> sys.float_info.max
1.7976931348623157e+308

To get around this limitation, instead use //to get an integer back from the division of the two integers:

要解决此限制,请改用//从两个整数的除法中获取整数:

number // 10

This will return the intfloor value of number / 10(it does not produce a float). Unlike floats, intvalues can be as large as you need them to be in Python 3 (within memory limits).

这将返回的下int限值number / 10(它不产生浮点数)。与浮点数不同,int值可以根据您在 Python 3 中所需的大小(在内存限制内)。

You can now divide the large numbers. For instance, in Python 3:

您现在可以对大数进行除法。例如,在 Python 3 中:

>>> 2**3000 / 10
OverflowError: integer division result too large for a float

>>> 2**3000 // 10
123023192216111717693155881327...

回答by erip

Try int(number) % 10. You can only mod integers.

试试int(number) % 10。您只能对整数进行模数。

回答by ZekeDroid

Python will automatically handle large ints of arbitrary length. What it won't do is handle floats of arbitrary length so you need to make sure you're not getting floats along the way.

Python 将自动处理int任意长度的大s。它不会做的是处理float任意长度的 s,因此您需要确保一路上没有浮动。

回答by fredtantini

If you have an integer and you want each digit in a list, you can use:

如果您有一个整数并且您希望列表中的每个数字,您可以使用:

>>> map(int,list(str(number)))
[1, 5, 0, 3, 0, 0, 7, 6, 4, 2, 2, 6, 8, 3, 9, 7, 5, 0, 3, 6, 6, 4, 0, 5, 1, 2, 4, 3, 7, 8, 2, 5, 2, 4, 4, 5, 4, 8, 4, 0, 6, 6, 4, 5, 0, 9, 2, 4, 8, 9, 2, 9, 7, 8, 7, 3, 9, 9, 9, 7, 0, 1, 7, 4, 8, 2, 4, 4, 2, 9, 6, 9, 5, 1, 7, 1, 3, 4, 8, 5, 1, 3, 3, 1, 7, 9, 0, 1, 0, 1, 9, 3, 8, 4, 2, 0, 1, 9, 2, 9]

it transform the int into a string, then listwill take each character of the string and put it in a list. Finally, mapwill convert each item of the list into an int again

它将 int 转换为字符串,然后list将字符串的每个字符放入一个列表中。最后,map将列表中的每一项再次转换为一个int