¿Es posible establecer un número a NaN o infinito?


Es posible establecer un elemento de una matriz a NaN en Python?

Además, ¿es posible establecer una variable a +/- infinito? Si es así, ¿hay alguna función para comprobar si un número es infinito o no?

Author: nbro, 2011-03-26

4 answers

Fundido desde cadena usando float():

>>> float('NaN')
nan
>>> float('Inf')
inf
>>> -float('Inf')
-inf
>>> float('Inf') == float('Inf')
True
>>> float('Inf') == 1
False
 185
Author: marcog,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-25 22:25:46

Sí, puede usar numpy por eso.

import numpy as np
a = arange(3,dtype=float)

a[0] = np.nan
a[1] = np.inf
a[2] = -np.inf

a # is now [nan,inf,-inf]

np.isnan(a[0]) # True
np.isinf(a[1]) # True
np.isinf(a[2]) # True
 56
Author: Olivier Verdier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-26 00:19:43

¿Es posible establecer un elemento de una matriz a NaN en Python?

Sí, siempre se puede incluir un NaN o inf en un python list (y similar). Sin embargo, si desea incluirlo en un array (por ejemplo array.array o numpy.array), entonces el tipo de la matriz debe ser float o complex!

>>> import math
>>> import numpy as np

>>> arr = np.ones(1, float)  # float array
>>> arr[0] = math.nan
>>> arr
array([ nan])

>>> [math.nan, math.inf, -math.inf]  # python list
[nan, inf, -inf]

El math-el módulo contiene constantes para nan y inf (desde Python 3.5) así como algunas funciones (disponibles desde Python 2.6) para comprobar estos:

math.isnan comprueba si el valor es nan:

>>> math.isnan(math.nan)   # nan
True

math.isinf comprueba si el valor es infinito (positivo o negativo):

>>> math.isinf(math.inf)   # positive infinity
True
>>> math.isinf(-math.inf)  # negative infinity
True

Y una función que puede comprobar si un número es no inf o nan: math.isfinite (disponible para Python 3.2+)

>>> math.isfinite(math.inf)
False
>>> math.isfinite(math.nan)
False
>>> math.isfinite(11.2)
True
 8
Author: MSeifert,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-24 10:41:09

Cuando use Python 2.4, intente

inf = float("9e999")
nan = inf - inf

Me enfrento al problema cuando estaba portando el simplejson a un dispositivo embebido que ejecuta Python 2.4, float("9e999") lo solucionó. No use inf = 9e999, necesita convertirlo de cadena. -inf da el -Infinity.

 1
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-01 00:54:26