logo

파이썬 인피니티(inf)

아이러니하게 보일 수도 있지만 무한은 다음과 같이 정의됩니다. 정의되지 않은 숫자 이는 양수 또는 음수 값일 수 있습니다. 무한한 값에 대해 수행되는 모든 산술 연산은 합계, 빼기, 곱셈 또는 기타 연산과 같이 항상 무한한 숫자로 이어집니다.

컴퓨터 과학의 세계에서 무한대는 일반적으로 성능을 측정하고 대규모 애플리케이션에서 계산을 수행하는 알고리즘을 최적화하는 데 사용됩니다.



Python에서 무한대를 정수로 표현하기

무한대를 정수로 표현한다는 개념은 무한대 자체의 정의를 위반합니다. 2020년 현재까지 어떤 프로그래밍 언어에서도 무한대를 정수로 표현하는 방법은 없습니다.

하지만 파이썬 , 동적 언어이기 때문에 float 값은 무한한 정수를 나타내는 데 사용될 수 있습니다. . 하나는 사용할 수 있습니다 플로트('inf') 무한대로 표현하기 위해 정수로 표현합니다. 다음은 Python에서 무한대를 표현할 수 있는 방법 목록입니다.

1. float('inf') 및 float('-inf') 사용

무한대는 양수일 수도 있고 음수일 수도 있으므로 각각 float('inf') 및 float('-inf')로 표시할 수 있습니다.



아래 코드는 위에서 논의한 내용의 구현을 보여줍니다.

파이썬3






# 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)>

>

>

산출:

np.zeros
Positive Infinity: inf Negative Infinity: -inf>

2. Python의 Math 모듈 사용하기

Python의 수학 모듈은 무한한 정수를 나타내는 데에도 사용할 수 있습니다.

Pythons math.inf 상수는 양의 무한대를 반환하고 -math.inf는 음의 무한대를 반환합니다.

아래 코드는 수행 방법을 보여줍니다.

파이썬3




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>

또한 읽어보세요 : 예를 들어 Golang의 math.Inf() 함수

3. Python의 Decimal 모듈 사용

Python의 10진수 모듈은 무한한 부동 소수점 값을 나타내는 데에도 사용할 수 있습니다.

다음과 같이 사용됩니다. 소수('무한대') 긍정적이고 십진수('-무한대') 음의 무한 값의 경우.

아래 코드는 구현을 보여줍니다.

파이썬3




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 라이브러리를 사용하는 방법은 아래 코드에 나와 있습니다.

파이썬3




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() 메소드의 사용을 보여줍니다.

파이썬3




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))>

>

>

산출:

javatpoint 자바
True True False>

또한 읽어보세요: Python의 numpy.isinf()

Python의 무한 값과 유한 값 비교

무한한 값을 유한한 값과 비교하는 개념은 매우 간단합니다. 양의 무한대는 항상 모든 자연수보다 크고 음의 무한대는 항상 음수보다 작습니다.

더 나은 이해를 위해 아래 코드를 살펴보십시오.

파이썬3




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>그리고:> >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>

프로그래밍에서 무한대를 사용하는 것은 매우 까다롭지만 Python에서는 이를 매우 쉽게 만들었습니다. Python inf는 3가지 이상의 메서드와 함께 사용할 수 있으므로 Python을 매우 사용자 친화적으로 만듭니다.

이제 Python에서 infinity(inf)를 사용하고 솔루션에 사용할 수 있기를 바랍니다.