如何在Python中表示无限数?

发布于 2024-12-10 07:02:09 字数 57 浏览 0 评论 0原文

如何在Python中表示无限数?无论您在程序中输入哪个数字,任何数字都不应大于这个无穷大的表示形式。

How can I represent an infinite number in python? No matter which number you enter in the program, no number should be greater than this representation of infinity.

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

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

发布评论

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

评论(13

东北女汉子 2024-12-17 07:02:09

在 Python 中,您可以执行以下操作:

test = float("inf")

在 Python 3.5 中,您可以执行以下操作:

import math
test = math.inf

然后:

test > 1
test > 10000
test > x

永远为真。当然,除非如所指出的那样,x 也是无穷大或“nan”(“不是数字”)。

此外(仅限 Python 2.x),与 Ellipsis 相比,float(inf) 较小,例如:

float('inf') < Ellipsis

将返回 true。

In Python, you can do:

test = float("inf")

In Python 3.5, you can do:

import math
test = math.inf

And then:

test > 1
test > 10000
test > x

Will always be true. Unless of course, as pointed out, x is also infinity or "nan" ("not a number").

Additionally (Python 2.x ONLY), in a comparison to Ellipsis, float(inf) is lesser, e.g:

float('inf') < Ellipsis

would return true.

反差帅 2024-12-17 07:02:09

从 Python 3.5 开始,您可以使用 math.inf:

>>> import math
>>> math.inf
inf

Since Python 3.5 you can use math.inf:

>>> import math
>>> math.inf
inf
娇纵 2024-12-17 07:02:09

似乎没有人明确提到负无穷大,所以我想我应该添加它。

对于负无穷大:

-math.inf

对于正无穷大(只是为了完整性):

math.inf

No one seems to have mentioned about the negative infinity explicitly, so I think I should add it.

For negative infinity:

-math.inf

For positive infinity (just for the sake of completeness):

math.inf
踏雪无痕 2024-12-17 07:02:09

我不知道你到底在做什么,但是 float("inf") 给你一个 float Infinity,它比任何其他数字都大。

I don't know exactly what you are doing, but float("inf") gives you a float Infinity, which is greater than any other number.

装迷糊 2024-12-17 07:02:09

NumPy 库中有一个无穷大:from numpy import inf。要获得负无穷大,只需编写 -inf 即可。

There is an infinity in the NumPy library: from numpy import inf. To get negative infinity one can simply write -inf.

看海 2024-12-17 07:02:09

另一种不太方便的方法是使用 Decimal< /a> 类:

from decimal import Decimal
pos_inf = Decimal('Infinity')
neg_inf = Decimal('-Infinity')

Another, less convenient, way to do it is to use Decimal class:

from decimal import Decimal
pos_inf = Decimal('Infinity')
neg_inf = Decimal('-Infinity')
染墨丶若流云 2024-12-17 07:02:09

在 python2.x 中,有一个肮脏的 hack 可以达到此目的(除非绝对必要,否则不要使用它):

None < any integer < any string

因此检查 i i i i i i i '' 对于任何整数 i 都保持 True

它在 python3 中已被合理地弃用。现在这样的比较最终结果是

TypeError: unorderable types: str() < int()

In python2.x there was a dirty hack that served this purpose (NEVER use it unless absolutely necessary):

None < any integer < any string

Thus the check i < '' holds True for any integer i.

It has been reasonably deprecated in python3. Now such comparisons end up with

TypeError: unorderable types: str() < int()
奶茶白久 2024-12-17 07:02:09

无穷

大 1.使用 float('inf')float('-inf)

positive_infinity = float('inf') 
negative_infinity = float('-inf')

2.使用Python的数学模块

import math
 
positive_infinity = math.inf 
negative_infinity = -math.inf 

3.整数 maxsize

import sys

maxSize = sys.maxsize 
minSize = -sys.maxsize 

4。使用Python的decimal模块

from decimal import Decimal
 
positive_infinity = Decimal('Infinity') 
negative_infinity = Decimal('-Infinity') 

5.使用 Numpy 库

from numpy import inf

positive_infinity = inf 
negative_infinity = -inf 

Infinity

1. Using float('inf') and float('-inf)

positive_infinity = float('inf') 
negative_infinity = float('-inf')

2. Using Python’s math module

import math
 
positive_infinity = math.inf 
negative_infinity = -math.inf 

3. Integer maxsize

import sys

maxSize = sys.maxsize 
minSize = -sys.maxsize 

4. Using Python’s decimal module

from decimal import Decimal
 
positive_infinity = Decimal('Infinity') 
negative_infinity = Decimal('-Infinity') 

5. Using Numpy Library

from numpy import inf

positive_infinity = inf 
negative_infinity = -inf 
匿名。 2024-12-17 07:02:09

另外,如果您使用 SymPy,您可以使用 sympy.oo

>>> from sympy import oo
>>> oo + 1
oo
>>> oo - oo
nan

等。

Also if you use SymPy you can use sympy.oo

>>> from sympy import oo
>>> oo + 1
oo
>>> oo - oo
nan

etc.

情愿 2024-12-17 07:02:09

对于正无穷

pos_inf_val = float("infinity")

对于负无穷

neg_inf_val = float("-infinity")

For Positive Infinity

pos_inf_val = float("infinity")

For Negative Infinity

neg_inf_val = float("-infinity")
合约呢 2024-12-17 07:02:09

python 中表示

float("inf")float("INF")float ("Inf")float("inF")float("infinity")float("Infinity") 创建一个float 对象持有

您也可以在 python 中表示 -∞

float("-inf")float("-INF")float("-Inf")float("-infinity") 创建一个漂浮的物体-∞

您可以执行算术运算

infinity = float("inf")
ninfinity = float("-inf")
nan = float("nan")

print(infinity*infinity)#inf
print(ninfinity+infinity)#not a number
print(1/-infinity)#is -0.0
print(nan*nan)# is not a number
print(1/infinity) # is 0.0 since 1/∞ is 0

输出:

$ python3 floating.py
inf
nan
-0.0
nan
0.0

Representing in python

float("inf") or float("INF") or float("Inf") or float("inF") or float("infinity") or float("Infinity") creates a float object holding

You can also represent -∞ in python

float("-inf") or float("-INF") or float("-Inf") or float("-infinity") creates a float object holding -∞

You can perform arithmetic operations:

infinity = float("inf")
ninfinity = float("-inf")
nan = float("nan")

print(infinity*infinity)#inf
print(ninfinity+infinity)#not a number
print(1/-infinity)#is -0.0
print(nan*nan)# is not a number
print(1/infinity) # is 0.0 since 1/∞ is 0

Output:

$ python3 floating.py
inf
nan
-0.0
nan
0.0
小瓶盖 2024-12-17 07:02:09

总而言之,无穷大有两种定义。

对于正无穷

posVal1 = math.inf
posVal2 = float("inf")

对于负无穷

negVal1 = -math.inf
negVal2 = float("-inf")

In Summary, there is two kinds definition for Infinity.

For Positive Infinity

posVal1 = math.inf
posVal2 = float("inf")

For Negative Infinity

negVal1 = -math.inf
negVal2 = float("-inf")
最好是你 2024-12-17 07:02:09

使用:

float('inf')

或者math模块:

import math
math.inf

但是如果打印出来,它们都会返回inf,这证明math使用了float('inf') 也是如此。

Use:

float('inf')

Or the math module:

import math
math.inf

But if you print it, they will both return inf, which proves that math uses float('inf') as well.

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