Python无穷大
具有讽刺意味的是,无穷大被定义为一个未定义的数字,它可以是正值也可以是负值。对无限值执行的所有算术运算总是导致无限数,例如求和、减法、乘法或任何其他运算。
在计算机科学领域,无穷大通常用于衡量性能并优化在大规模应用程序上执行计算的算法。
在Python中将无穷大表示为整数
将无穷大表示为整数的概念违反了无穷大本身的定义。截至 2020 年,到目前为止,在任何编程语言中都没有将无穷大表示为整数的方法。但是在Python中,由于它是一种动态语言,浮点值可以用来表示一个无限整数。可以使用float('inf')作为整数来表示它为无穷大。下面是在Python中表示无穷大的方法列表。
1. 使用 float('inf') 和 float('-inf'):
由于无穷大可以是正数和负数,它们可以分别表示为 float('inf') 和 float('-inf')。下面的代码显示了上述内容的实现:
Python3
# Defining a positive infinite integer
positive_infinity = float('inf')
print('Positive Infinity: ', positive_infinity)
# Defining a negative infinite integer
negative_infinity = float('-inf')
print('Negative Infinity: ', negative_infinity)
Python3
import math
# Defining a positive infinite integer
positive_infinity = math.inf
print('Positive Infinity: ', positive_infinity)
# Defining a negative infinite integer
negative_infinity = -math.inf
print('Negative Infinity: ', negative_infinity)
Python3
from decimal import Decimal
# Defining a positive infinite integer
positive_infinity = Decimal('Infinity')
print('Positive Infinity: ', positive_infinity)
# Defining a negative infinite integer
negative_infinity = Decimal('-Infinity')
print('Negative Infinity: ', negative_infinity)
Python3
import numpy as np
# Defining a positive infinite integer
positive_infinity = np.inf
print('Positive Infinity: ', positive_infinity)
# Defining a negative infinite integer
negative_infinity = -np.inf
print('Negative Infinity: ', negative_infinity)
Python3
import numpy as np
import math
# Defining a positive infinite integer
a = np.inf
# Defining a negative infinite integer
b = -np.inf
# Define a finite integer
c = 300
# check if a in infinite
print(math.isinf(a))
# check if b in infinite
print(math.isinf(b))
# check if c in infinite
print(math.isinf(c))
Python3
import numpy as np
# Defining a positive infinite integer
a = np.inf
# Defining a negative infinite integer
b = -np.inf
# Define a finite + ve integer
c = 300
# Define a finite -ve integer
d = -300
# helper function to make comparisons
def compare(x, y):
if x>y:
print("True")
else:
print("False")
compare(a, b)
compare(a, c)
compare(a, d)
compare(b, c)
compare(b, d)
输出:
Positive Infinity: inf
Negative Infinity: -inf
2.使用Python的数学模块:
Python 的数学模块也可用于表示无限整数。下面的代码显示了它是如何完成的:
Python3
import math
# Defining a positive infinite integer
positive_infinity = math.inf
print('Positive Infinity: ', positive_infinity)
# Defining a negative infinite integer
negative_infinity = -math.inf
print('Negative Infinity: ', negative_infinity)
输出:
Positive Infinity: inf
Negative Infinity: -inf
3.使用Python的十进制模块:
Python 的十进制模块也可用于表示无限浮点值。
它用作正数的 Decimal( ' Infinity') 和负无穷大值的 Decimal('-Infinity') 。
下面的代码展示了它的实现:
Python3
from decimal import Decimal
# Defining a positive infinite integer
positive_infinity = Decimal('Infinity')
print('Positive Infinity: ', positive_infinity)
# Defining a negative infinite integer
negative_infinity = Decimal('-Infinity')
print('Negative Infinity: ', negative_infinity)
输出:
Positive Infinity: Infinity
Negative Infinity: -Infinity
4. 使用 Python 的 Numpy 库:
Python 的 Numpy 模块也可用于表示无限值。它用作np.inf正数和-np.inf负无限值。使用 Numpy 库来表示无限值如下代码所示:
Python3
import numpy as np
# Defining a positive infinite integer
positive_infinity = np.inf
print('Positive Infinity: ', positive_infinity)
# Defining a negative infinite integer
negative_infinity = -np.inf
print('Negative Infinity: ', negative_infinity)
输出:
Positive Infinity: inf
Negative Infinity: -inf
在Python中检查一个数是否为无穷大
要检查给定数字是否无限,可以使用数学库的isinf()方法,该方法返回一个布尔值。下面的代码展示了 isinf() 方法的使用:
Python3
import numpy as np
import math
# Defining a positive infinite integer
a = np.inf
# Defining a negative infinite integer
b = -np.inf
# Define a finite integer
c = 300
# check if a in infinite
print(math.isinf(a))
# check if b in infinite
print(math.isinf(b))
# check if c in infinite
print(math.isinf(c))
输出:
True
True
False
将无限值与Python中的有限值进行比较
将无限值与有限值进行比较的概念非常简单。因为正无穷总是大于每个自然数,负无穷总是小于负数。为了更好地理解,请查看以下代码:
Python3
import numpy as np
# Defining a positive infinite integer
a = np.inf
# Defining a negative infinite integer
b = -np.inf
# Define a finite + ve integer
c = 300
# Define a finite -ve integer
d = -300
# helper function to make comparisons
def compare(x, y):
if x>y:
print("True")
else:
print("False")
compare(a, b)
compare(a, c)
compare(a, d)
compare(b, c)
compare(b, d)
输出:
True
True
True
False
False