I’m writing a code that needs to run on a server using an old version of python (2.4?) with Numeric instead of numpy, I can’t do anything about that. To test the code, I run it with numpy.oldnumeric
I start with an array of float32’s, and I store values to them. My values are in the 1.0e50-1.0e60 range, and the array keeps storing them as ‘inf’. Even casting a 1.0e39, leads to ‘inf’. Aren’t floats supposed to max out closer to 1.0e108 ?! How can I preserve these values?
....
import numpy.oldnumeric as N
data = N.zeros(10, 'f')
....
for i in range(10): data[i] = (1.0e38)*pow(10.0,i)
print data[i]
gives
[ 9.99999993e+36 9.99999968e+37 inf inf
inf inf inf inf
inf inf]
Solution:
Single precision float (float32) is has a smaller limit than I thought (~3e38), thanks @aka.nice, so I switched from ‘f’ (float32) to ‘dtype=N.float64’, which has enough capacity.
You are using single precision floats…
In IEEE 754 single precision, the limit of exponent is 127, giving a max float value of about 2 * 2^127, that is approximately
or 3.40282346×10^38 see http://en.wikipedia.org/wiki/IEEE_754
The exact value is 2^128 – 2^104 = 340282346638528859811704183484516925440
Use IEEE 754 double precision (64 bits), the limit is 2^1024-2^971, that is about 1.7976931348623157e308