如何在 Python 中表示無限數?


在本文中,我們將向您展示如何在 python 中表示無限數。

無限是一個未定義的數字,可以是正數或負數。一個數字用於表示無限;兩個數值的和可能是一個數值,但模式不同;它可能具有負值或正值。

對無限值的任何算術運算,無論是求和、減法、乘法還是任何其他運算,最終都將得到一個無限數。

在計算機科學領域,無限通常用於評估和最佳化對大規模資料進行計算的演算法。

Python 中的無限

無限是浮點資料型別而不是int資料型別的原因在於 Python 中整數的表示方式。整數以二進位制表示,例如,值7表示為0111

在 Python 中,無法將無限表示為整數。這是其他幾種流行程式語言的基本特性。但是,由於 Python 是一種動態型別語言,因此您可以使用float(inf)作為整數來表達無限。

因此,我們無法在 Python 中表示無限,或者我們可以說無法將無限表示為整數。但是,float (inf)可以用作整數。

Positive Infinity: inf
Negative Infinity: -inf

使用 float('inf') 表示無限數

由於無限可以是正數也可以是負數,因此它可以分別表示為float('inf')float('-inf')

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 使用float('inf')獲取正無限整數的值並建立一個變數來儲存它。

  • 列印正無限值。

  • 使用float('-inf')獲取負無限整數的值並建立一個變數來儲存它。

  • 列印負無限值。

示例

以下程式使用float('inf')返回正無限值:

# getting a positive infinite integer value using the float(inf) function positiveInfinity = float('inf') # printing a positive infinite integer value print('Positive Infinity value = ', positiveInfinity) # getting a negative infinite integer value negativeInfinity = float('-inf') # printing a negative infinite integer value print('Negative Infinity value = ', negativeInfinity)

輸出

執行上述程式後,將生成以下輸出:

Positive Infinity value = inf
Negative Infinity value = -inf

使用 math 模組表示無限數

我們可以使用 math 模組來表示無限值,但是,它僅適用於 Python 3.5 或更高版本。由於無限可以是正數也可以是負數,因此它分別用math.inf-math.inf表示。

示例

# importing math module import math # printing a positive infinite integer value using math.inf print('Positive Infinity value = ', math.inf) # printing a negative infinite integer value using -math.inf print('Negative Infinity value = ', -math.inf)

輸出

執行上述程式後,將生成以下輸出:

Positive Infinity value = inf
Negative Infinity value = -inf

使用 Decimal() 函式表示無限數

我們使用Decimal('Infinity')表示正無限,使用Decimal('-Infinity')表示負無限,以使用 decimal 模組表示無限。

示例

# importing Decimal from the decimal module from decimal import Decimal # printing a positive infinite integer value using the Decimal() function print('Positive Infinity value = ', Decimal('Infinity')) # printing a negative infinite integer value using the Decimal() function print('Negative Infinity value = ', Decimal('-Infinity'))

輸出

執行上述程式後,將生成以下輸出:

Positive Infinity value = Infinity
Negative Infinity value = -Infinity

使用 NumPy 模組表示無限數

在 Python 中表示無限的另一種方法是透過NumPy模組,其中np.inf和-np.inf分別表示正無限和負無限。

NumPy 是一個 Python 庫,旨在有效地處理 Python 中的陣列。它速度快、易於學習且儲存效率高。

示例

# importing NumPy module import numpy as np # printing a positive infinite integer value using numpy.inf print('Positive Infinity value = ', np.inf) # printing a negative infinite integer value using -numpy.inf print('Negative Infinity value = ', -np.inf)

輸出

執行上述程式後,將生成以下輸出:

Positive Infinity value = inf
Negative Infinity value = -inf

檢查 Python 中的數字是否為無限

要確定給定數字是否為無限,請使用 math 庫的isinf()方法,該方法返回布林值。

示例

# importing numpy and math modules import numpy as np import math # creating a positive infinite integer using numpy.inf x = np.inf # creating a negative infinite integer using -numpy.inf y = -np.inf # finite integer z = 40 # checking whether x is infinite number using isinf() function print(math.isinf(x)) # checking whether y is infinite number using isinf() function print(math.isinf(y)) # checking whether z is infinite number using isinf() function print(math.isinf(z))

輸出

執行上述程式後,將生成以下輸出:

True
True
False

Python 中無限值和有限值的比較

將無限值與有限值進行比較的想法非常簡單。因為正無限始終大於任何自然數,而負無限始終小於負數

示例

# importing numpy module import numpy as np # creating a positive infinite integer using numpy.inf x = np.inf # creating a negative infinite integer using -numpy.inf y = -np.inf # positive finite integer z = 40 # negative finite integer k = -40 # creating a function to compare two numbers(positive and negative infinity) # by accepting 2 numbers as arguments def compareNumbers(p, q): # checking if p is greater than q i,e, a first number greater than the second if p>q: # printing True if it is greater print("True") else: # printing False if it is Not greater(less) print("False") # calling the compareNumbers() by passing any of the 2 # above defined variables for comparison compareNumbers(x, y) compareNumbers(x, z) compareNumbers(y, k) compareNumbers(x, k) compareNumbers(y, z)

輸出

執行上述程式後,將生成以下輸出:

True
True
False
True
False

結論

在本文中,我們學習瞭如何使用多種方法在 Python 中表示無限。我們還學習瞭如何比較無限值和有限值,以及如何確定給定整數是否為無限。

更新於:2022年11月9日

10K+ 瀏覽量

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告