logo

Python infinit (inf)

Per irònic que pugui semblar, l'infinit es defineix com un nombre indefinit que pot ser un valor positiu o negatiu. Totes les operacions aritmètiques que es realitzen sobre un valor infinit sempre donen lloc a un nombre infinit, diem que sigui suma, resta, multiplicació o qualsevol altra operació.

En el món de la informàtica, l'infinit s'utilitza generalment per mesurar el rendiment i optimitzar algorismes que realitzen càlculs en una aplicació a gran escala.



Representar l'infinit com un nombre enter en Python

El concepte de representar l'infinit com un nombre enter viola la pròpia definició d'infinit. A partir del 2020, fins ara no hi ha cap manera de representar l'infinit com un nombre enter en cap llenguatge de programació.

Però en Python , com que és un llenguatge dinàmic, els valors flotants es poden utilitzar per representar un nombre enter infinit . Un pot utilitzar float('inf') com un nombre enter per representar-lo com a infinit. A continuació es mostra la llista de maneres en què es pot representar l'infinit a Python.

1. Utilitzant float(‘inf’) i float(‘-inf’)

Com que l'infinit pot ser tant positiu com negatiu, es poden representar com a float(‘inf’) i float(‘-inf’) respectivament.



El codi següent mostra la implementació del contingut comentat anteriorment:

Python 3




dividint una cadena en c++



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

>

>

Sortida:

Positive Infinity: inf Negative Infinity: -inf>

2. Ús del mòdul matemàtic de Python

El mòdul matemàtic de Python també es pot utilitzar per representar nombres enters infinits.

Pythons math.inf constant retorna infinit positiu i -math.inf retorna infinit negatiu.

El codi següent mostra com es fa:

Python 3


'abc' està en números'



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

>

>

Sortida:

Positive Infinity: inf Negative Infinity: -inf>

Llegeix també : math.Inf() Funció a Golang amb exemples

3. Ús del mòdul decimal de Python

El mòdul decimal de Python també es pot utilitzar per representar valors flotants infinits.

S'utilitza com Decimal ('Infinit') per positiu i Decimal('-Infinit') per valor infinit negatiu.

El codi següent mostra la seva implementació:

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

>

>

Sortida:

Positive Infinity: Infinity Negative Infinity: -Infinity>

4. Ús de la biblioteca Numpy de Python

El mòdul Numpy de Python també es pot utilitzar per representar valors infinits. S'utilitza com np.inf per positiu i -np.inf per valor infinit negatiu. L'ús de la biblioteca Numpy per representar un valor infinit es mostra al codi següent:

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

c
>

>

Sortida:

Positive Infinity: inf Negative Infinity: -inf>

Comprovar si un nombre és infinit a Python

Per comprovar si un nombre donat és infinit o no, es pot utilitzar per tu () mètode de la biblioteca matemàtica que retorna un valor booleà. El codi següent mostra l'ús del mètode isinf():

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

nombre a cadena java
>

>

Sortida:

True True False>

Llegeix també: numpy.isinf() a Python

Comparació de valors infinits amb valors finits a Python

El concepte de comparar un valor infinit amb valors finits és tan senzill com es fa. Com que l'infinit positiu és sempre més gran que tots els nombres naturals i l'infinit negatiu sempre és més petit que els nombres negatius.

Per a una millor comprensió, mireu el codi següent:

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>i:> >print>(>'True'>)> >else>:> >print>(>'False'>)> > compare(a, b)> compare(a, c)> compare(a, d)> compare(b, c)> compare(b, d)>

>

>

Sortida:

True True True False False>

Utilitzar l'infinit a la programació és molt complicat, però Python ho va fer molt fàcil. Python inf es pot utilitzar amb més de 3 mètodes, cosa que fa que Python sigui molt fàcil d'utilitzar.

Espero que ara pugueu utilitzar infinity (inf) a Python ara i utilitzar-lo per a solucions.