Python-如何确定我的系统可以处理的最大/最小整数/长/浮点数/复数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17000632/
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
Python-How to determine largest/smallest int/long/float/complex numbers my system can handle
提问by hakuna121
Specs:Python 3.3.1
规格:Python 3.3.1
What I was trying to do:"Using Python, determine the largest and smallest ints, longs, floats, and complex numbers that your system can handle."
我想做什么:“使用 Python,确定您的系统可以处理的最大和最小整数、长整数、浮点数和复数。”
What I did:I went through Python's math modules and all built-in functions relating to math and numbers, but couldn't find a way to do this. I also tried something like max(range(0,))but it returned ValueError: max() arg is an empty sequenceerror.
我做了什么:我浏览了 Python 的数学模块和所有与数学和数字相关的内置函数,但找不到办法做到这一点。我也尝试过类似的东西,max(range(0,))但它返回了ValueError: max() arg is an empty sequence错误。
Question:How to determine largest/smallest int/long/float/complex numbers my system can handle using Python? As a total beginner, I know I must have missed something, but I tried and wasn't able to figure it out. I appreciate your help!
问题:如何确定我的系统可以使用 Python 处理的最大/最小 int/long/float/complex 数?作为一个初学者,我知道我一定错过了一些东西,但我尝试过但无法弄清楚。我感谢您的帮助!
采纳答案by Martijn Pieters
The python numeric limitations, such as there are any, are available on the sysmodule:
sys模块上提供了 python 数字限制,例如有任何限制:
sys.float_infois a named tuple with floating point limitations for your platform. Floating point numbers consist of a exponent and a precision; you'd have to be more precise about what you mean by the largest number here; the number with the largest exponent and the full precision in use issys.float_info.max.sys.int_info; not so much limitations as the implementation detail; you should be able to estimate the largest integer possible from this. Python integers are only limited by your available memory.sys.maxsize; the platform word size and limit to lists and tuples and the likes.
sys.float_info是一个命名元组,对您的平台具有浮点限制。浮点数由指数和精度组成;您必须更准确地了解此处最大数字的含义;具有最大指数和使用的全精度的数字是sys.float_info.max。sys.int_info; 与其说是实现细节,不如说是限制;您应该能够从中估计出最大的整数。Python 整数仅受可用内存的限制。sys.maxsize; 平台字大小和限制到列表和元组等。
So for integers, there basically is a softlimit to the maximum and minimum values. It depends on how much memory your process can use, and how much memory your process is already using for other things.
所以对于整数,基本上有最大值和最小值的软限制。这取决于您的进程可以使用多少内存,以及您的进程已经为其他事情使用了多少内存。
In Python 3, there no longer is a separate longtype, but in Python 2, sys.maxsize + 1would have to be a long, as would -sys.maxsize - 2. Between those two extremes lies the range of possible 'short' integers.
在 Python 3 中,不再有单独的long类型,但在 Python 2 中,sys.maxsize + 1必须是 a long,就像-sys.maxsize - 2. 在这两个极端之间是可能的“短”整数范围。
For complex numbers, ordering is a little more.... complex anyway. Complex numbers have a real and imaginary component, both are floats. Guess what? These are python floats and you already have their limit info above:
对于复数,排序要稍微复杂一点……无论如何。复数有实部和虚部,两者都是浮点数。你猜怎么着?这些是 python 浮点数,上面已经有了它们的限制信息:
>>> type(1j)
<type 'complex'>
>>> type(1j.real)
<type 'float'>
>>> type(1j.imag)
<type 'float'>
回答by chepner
sys.float_infoprovides the desired information for floating-point values.
sys.float_info提供浮点值所需的信息。
>>> sys.float_info.max
1.7976931348623157e+308
Python 3 does not have upper or lower limits on integers, and there exists no mathematical definition for ordering arbitrary complex numbers (although the real or imaginary parts of two complex numbers can be ordered separately).
Python 3 对整数没有上限或下限,也不存在对任意复数进行排序的数学定义(尽管两个复数的实部或虚部可以分别排序)。

