Thật mỉa mai vì nó có vẻ vô cùng được định nghĩa là một số không xác định có thể là một giá trị tích cực hoặc âm. Tất cả các hoạt động số học được thực hiện trên một giá trị vô hạn luôn dẫn đến một số vô hạn, nói rằng đó là tổng, phép trừ, nhân hoặc bất kỳ hoạt động nào khác. Trong thế giới khoa học máy tính, Infinity thường được sử dụng để đo lường hiệu suất và tối ưu hóa các thuật toán thực hiện tính toán trên Một ứng dụng quy mô lớn.
In the world of computer science, infinity is generally used to measure performance and optimize algorithms that perform computations on a large scale application.
Đại diện cho Infinity như một số nguyên trong khái niệm Pythonthe đại diện cho Infinity như một số nguyên vi phạm định nghĩa của chính vô cực. Tính đến năm 2020, không có cách nào để thể hiện vô cùng như một số nguyên trong bất kỳ ngôn ngữ lập trình nào cho đến nay. Nhưng trong Python, vì nó là một ngôn ngữ động, các giá trị nổi có thể được sử dụng để thể hiện một số nguyên vô hạn. Người ta có thể sử dụng float [‘inf,] như một số nguyên để thể hiện nó là vô cùng. Dưới đây là danh sách các cách người ta có thể đại diện cho vô cực trong Python. & NBSP;
The concept of representing infinity as an integer violates the definition of infinity itself. As of 2020, there is no such way to represent infinity as an integer in any programming language so far. But in python, as it is a dynamic language, float values can be used to represent an infinite integer. One can use float[‘inf’] as an integer to represent it as infinity. Below is the list of ways one can represent infinity in Python.
1. Sử dụng phao [‘inf,] và float [‘-inf,]:
Vì vô cùng có thể là cả tích cực và tiêu cực, chúng có thể được biểu diễn dưới dạng phao [‘inf,] và float [‘-inf,] tương ứng. Mã dưới đây cho thấy việc thực hiện nội dung được thảo luận ở trên:
Python3
positive_infinity
=
float
[
____1010
Positive Infinity: inf Negative Infinity: -inf2
[
Positive Infinity: inf Negative Infinity: -inf4
Positive Infinity: inf Negative Infinity: -inf5
Positive Infinity: inf Negative Infinity: -inf6
=
float
[
Positive Infinity: Infinity Negative Infinity: -Infinity0
Positive Infinity: inf Negative Infinity: -inf1
Positive Infinity: inf Negative Infinity: -inf2
[
Positive Infinity: Infinity Negative Infinity: -Infinity4
Positive Infinity: Infinity Negative Infinity: -Infinity5
Output:
Positive Infinity: inf Negative Infinity: -inf
2. Sử dụng mô -đun toán Python từ:
Mô -đun toán học Python cũng có thể được sử dụng để đại diện cho các số nguyên vô hạn. Mã dưới đây cho thấy cách nó được thực hiện: & nbsp;
Python3
Positive Infinity: Infinity Negative Infinity: -Infinity6
Positive Infinity: Infinity Negative Infinity: -Infinity7
positive_infinity
=
Positive Infinity: inf Negative Infinity: -inf0
Positive Infinity: inf Negative Infinity: -inf2
[
Positive Infinity: inf Negative Infinity: -inf4
Positive Infinity: inf Negative Infinity: -inf5
Positive Infinity: inf Negative Infinity: -inf6
=
Positive Infinity: inf Negative Infinity: -inf7
Positive Infinity: inf Negative Infinity: -inf0
Positive Infinity: inf Negative Infinity: -inf2
[
Positive Infinity: Infinity Negative Infinity: -Infinity4
Positive Infinity: Infinity Negative Infinity: -Infinity5
Output:
Positive Infinity: inf Negative Infinity: -inf
3. Sử dụng mô -đun thập phân Python sườn:
Mô-đun thập phân Python sườn cũng có thể được sử dụng để biểu diễn các giá trị nổi vô hạn. & NBSP; Nó được sử dụng làm số thập phân [‘vô cực] cho dương tính và thập phân [‘-vô cực] cho giá trị vô hạn âm. & NBSP;
It is used as Decimal[‘Infinity’] for positive and Decimal[‘-Infinity’] for negative infinite value.
Mã dưới đây cho thấy việc triển khai của nó: & nbsp; & nbsp;
Python3
True True False3
True True False4
Positive Infinity: Infinity Negative Infinity: -Infinity6
True True False6
positive_infinity
=
True True False9
True True True False False0
Positive Infinity: inf Negative Infinity: -inf1
Positive Infinity: inf Negative Infinity: -inf2
[
Positive Infinity: inf Negative Infinity: -inf4
Positive Infinity: inf Negative Infinity: -inf5
Positive Infinity: inf Negative Infinity: -inf6
=
True True False9
True True True False False9
Positive Infinity: inf Negative Infinity: -inf1
Positive Infinity: inf Negative Infinity: -inf2
[
Positive Infinity: Infinity Negative Infinity: -Infinity4
Positive Infinity: Infinity Negative Infinity: -Infinity5
Output:
Positive Infinity: Infinity Negative Infinity: -Infinity
4. Sử dụng thư viện Python sườn Numpy:
Mô -đun Numpy Python cũng có thể được sử dụng để biểu diễn các giá trị vô hạn. Nó được sử dụng làm np.inf cho dương và -np.inf cho giá trị vô hạn âm. Việc sử dụng thư viện Numpy để biểu diễn giá trị vô hạn được hiển thị trong mã bên dưới: & nbsp;np.inf for positive and -np.inf for negative infinite value. The use of Numpy library for representing an infinite value is shown in the code below:
Python3
Positive Infinity: Infinity Negative Infinity: -Infinity6
positive_infinity
6positive_infinity
=
positive_infinity
9
Positive Infinity: inf Negative Infinity: -inf2
[
Positive Infinity: inf Negative Infinity: -inf4
Positive Infinity: inf Negative Infinity: -inf5
Positive Infinity: inf Negative Infinity: -inf6
=
Positive Infinity: inf Negative Infinity: -inf7
positive_infinity
9Positive Infinity: inf Negative Infinity: -inf2
[
Positive Infinity: Infinity Negative Infinity: -Infinity4
Positive Infinity: Infinity Negative Infinity: -Infinity5
Output:
Positive Infinity: inf Negative Infinity: -inf
Kiểm tra xem một số là vô hạn trong kiểm tra Pythonto xem một số đã cho có vô hạn hay không, người ta có thể sử dụng phương thức isinf [] của thư viện toán học trả về giá trị boolean. Mã dưới đây cho thấy việc sử dụng phương thức isinf []: & nbsp;
To check if a given number is infinite or not, one can use isinf[] method of the math library which returns a boolean value. The below code shows the use
of isinf[] method:
Python3
Positive Infinity: Infinity Negative Infinity: -Infinity6
positive_infinity
6Positive Infinity: Infinity Negative Infinity: -Infinity6
Positive Infinity: Infinity Negative Infinity: -Infinity7
positive_infinity
=
Positive Infinity: inf Negative Infinity: -inf0
Positive Infinity: inf Negative Infinity: -inf6
=
Positive Infinity: inf Negative Infinity: -inf7
Positive Infinity: inf Negative Infinity: -inf0
Positive Infinity: inf Negative Infinity: -inf
Positive Infinity: inf Negative Infinity: -inf2
[
7Positive Infinity: inf Negative Infinity: -inf2
[
9Positive Infinity: inf Negative Infinity: -inf2
Positive Infinity: inf Negative Infinity: -inf01
Output:
True True False
3. Sử dụng mô -đun thập phân Python sườn:
The concept of comparing an infinite value to finite values is
as simple as it gets. As positive infinity is always bigger than every natural number and negative infinity is always smaller than negative numbers. For better understanding look into the code below:
Python3
Positive Infinity: Infinity Negative Infinity: -Infinity6
positive_infinity
6positive_infinity
=
Positive Infinity: inf Negative Infinity: -inf0
Positive Infinity: inf Negative Infinity: -inf6
=
Positive Infinity: inf Negative Infinity: -inf7
Positive Infinity: inf Negative Infinity: -inf0
Positive Infinity: inf Negative Infinity: -inf
3. Sử dụng mô -đun thập phân Python sườn:
Mô-đun thập phân Python sườn cũng có thể được sử dụng để biểu diễn các giá trị nổi vô hạn. & NBSP; Nó được sử dụng làm số thập phân [‘vô cực] cho dương tính và thập phân [‘-vô cực] cho giá trị vô hạn âm. & NBSP;
Mã dưới đây cho thấy việc triển khai của nó: & nbsp; & nbsp;
True True False3
True True False4
Positive Infinity: Infinity Negative Infinity: -Infinity6
True True False6
Positive Infinity: inf Negative Infinity: -inf20
Positive Infinity: inf Negative Infinity: -inf29
Positive Infinity: inf Negative Infinity: -inf30
Positive Infinity: inf Negative Infinity: -inf23
Positive Infinity: inf Negative Infinity: -inf2
[
Positive Infinity: inf Negative Infinity: -inf34
Positive Infinity: inf Negative Infinity: -inf1
Positive Infinity: inf Negative Infinity: -inf36
Positive Infinity: inf Negative Infinity: -inf37
Positive Infinity: inf Negative Infinity: -inf38
Positive Infinity: inf Negative Infinity: -inf39
Positive Infinity: inf Negative Infinity: -inf40
Output:
True True True False False