[英]Trying to find smallest number
In my program I'm trying to find the smallest number that python can give me.在我的程序中,我试图找到 python 可以给我的最小数字。 When I kept dividing a number by 2, I got 5 x 10^-324 (5e-324).
当我不断将数字除以 2 时,我得到了 5 x 10^-324 (5e-324)。 I thought I could divide this by the biggest number I can use in python.
我想我可以除以我可以在 python 中使用的最大数字。 I tried to get the biggest number in python by doing this:
我试图通过这样做来获得 python 中的最大数字:
z = 1
while True:
try:
z = z+1
except OverflowError:
z = z-1
break
Here is my full code:这是我的完整代码:
from os import system
x = 76556758478567587
while True:
x = x/2
if x/2 == 0.0:
break
print("Smallest number I can compute:", x)
print()
z = 1
while True:
try:
z = z+1
except OverflowError:
z = z-1
break
print(str(x) + " divided by " + str(z) + " is...")
z = x/z
print(z)
system("pause >nul")
Every time I run this it does nothing.每次我运行它时它什么都不做。 I suddenly recognize it's still trying to solve the problem so I open task manager and Python was eating up my CPU like a pack of wolves eating a dead cow.
我突然意识到它仍在尝试解决问题,所以我打开任务管理器,Python 像狼吃死牛一样吃掉我的 CPU。
I know the smallest number in python would be negative but I want to get the the smallest number above zero .我知道 python 中的最小数字是负数,但我想获得大于零的最小数字。
You may use sys.float_info
to get the maximum/minimum representable finite float as:您可以使用
sys.float_info
来获得最大/最小可表示的有限浮点数:
>>> import sys
>>> sys.float_info.min
2.2250738585072014e-308
>>> sys.float_info.max
1.7976931348623157e+308
Python uses double-precision floats, which can hold values from about 10 to the -308 to 10 to the 308 power. Python 使用双精度浮点数,它可以保存大约 10 到 -308 到 10 到 308 次方的值。 Below is the experiment from the python prompt:
下面是python提示符下的实验:
# for maximum
>>> 1e308
1e+308
>>> 1e+309
inf <-- Infinite
You may even get numbers smaller than 1e-308
via denormals
, but there is a significant performance hit to this and such numbers are represented with a loss of precision .您甚至可以通过
denormals
数获得小于1e-308
数字,但这会严重影响性能,并且这些数字会以精度损失表示。 I found that Python is able to handle 1e-323 but underflows on 1e-324 and returns 0.0 as the value.我发现 Python 能够处理 1e-323 但在 1e-324 上下溢并返回 0.0 作为值。
# for minimum
>>> 1e-323
1e-323
>>> 1e-324
0.0
You can get denormalized minimum as sys.float_info.min * sys.float_info.epsilon
, which comes as 5e-324
.您可以获得非规范化最小值为
sys.float_info.min * sys.float_info.epsilon
,即5e-324
。
As per the document:根据文件:
sys.float_info.max is maximum representable finite float
sys.float_info.max是最大可表示的有限浮点数
sys.float_info.min is minimum positive normalized float
sys.float_info.min是最小正标准化浮点数
To get more information check:sys.float_info
:要获得更多信息检查:
sys.float_info
:
sys.float_info is a struct sequence holding information about the float type.
sys.float_info是一个结构序列,包含有关浮点类型的信息。 It contains low level information about the precision and internal representation.
它包含有关精度和内部表示的低级信息。 The values correspond to the various floating-point constants defined in the standard header file float.h for the 'C' programming language;
这些值对应于“C”编程语言的标准头文件 float.h 中定义的各种浮点常量; see section 5.2.4.2.2 of the 1999 ISO/IEC C standard [C99], 'Characteristics of floating types', for details.
有关详细信息,请参阅 1999 ISO/IEC C 标准 [C99] 的第 5.2.4.2.2 节“浮动类型的特征”。
The smallest floating point number representable in Python is 5e-324
, or 2.0**-1075
. Python 中可表示的最小浮点数是
5e-324
或2.0**-1075
。
The other two answers have each given you half of what you need to find this number.其他两个答案分别为您提供了找到此数字所需的一半。 It can be found by multiplying
sys.float_info.min
( 2.2250738585072014e-308
, the smallest normalized float) by sys.float_info.epsilon
( 2.220446049250313e-16
, the smallest proportion you can modify a number by while still getting a distinct value).可以通过将
sys.float_info.min
( 2.2250738585072014e-308
,最小标准化浮点数)乘以sys.float_info.epsilon
( 2.220446049250313e-16
,您可以修改一个数字的最小比例)找到它,同时仍然得到一个不同的值.
The key word that you're apparently missing is " epsilon ."您显然缺少的关键词是“ epsilon” 。 If you search on Python epsilon value then it returns a links to StackOverflow: Value for epsilon in Python near the top of the results in Google.
如果您搜索Python epsilon 值,那么它会返回一个指向StackOverflow: Value for epsilon in Python的链接,位于 Google 结果顶部附近。
Obviously it helps if you understand how the term is associated with this concept.显然,如果您了解该术语与该概念的关系,将会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.