Getting infinities with numpy.exp()
Posted on Sat 24 May 2014 in bugs • Tagged with numpy, python • Leave a comment
So I spent quite a lot of time this week trying to find out why I was getting infinities in one of my codes I'm working on.
In the code I have a big numpy.ndarray, let's call it a, which I
then pass into numpy.exp() and save into another numpy.ndarray,
let's call it b. Everything was working fine until I started detecting
NaN-s somewhere later in the code. I traced the problem back to this
b = numpy.exp(a) step; it turned out b contained inf at some
places. More precisely, b was inf where a was around 90 or
above.
So I ran python and typed:
a = 90
b = np.exp(a)
b 1.2204032943178408e+39
np.isinf(b)
False
OK, this is big, but it's not inf, right? Let's try bigger.
a = 120
b = np.exp(a)
b 1.3041808783936323e+52
np.isinf(b)
False
Still not inf. But hey, what type is b actually?
type(b)
< type 'numpy.float64' >
Everything was clear now. Throughout my code I was using
numpy.float32 as the dtype of my arrays and that was the source of the
problem.
Check this out:
a = np.finfo(np.float32).max
b = np.finfo(np.float32).max * 10
c = np.zeros(2, dtype = 'float32')
c[0] = a
c[1] = b
c
array([ 3.40282347e+38, inf], dtype=float32)
np.isinf(a) False
np.isinf(b) False
np.isinf(c[0]) False
np.isinf(c[1]) True
Why is b not inf and c[1] is?
Because:
type(b)
< type 'numpy.float64' >
type(c[1])
< type 'numpy.float32' >`
Point of story? Be careful what types you're using to store your data, stupid!
