[Numpy-discussion] casting integers to reals

Christopher Barker Chris.Barker@noaa....
Thu Feb 26 17:23:30 CST 2009


Gael Varoquaux wrote:
> nump.arange(0, 10.astype(numpy.float)

I think you meant:

np.arange(0, 10).astype(np.float)

but:

np.arange(0, 10, dtype=np.float)

is a better bet.

> but in this special case you can do:
> 
> numpy.arange(0., 10.)

yup -- however, beware, using arange() with floating point numbers 
doesn't necessarily work as you would like it to, die to floating point 
rounding. np.linspace may be a better bet:

 >>> np.linspace(0, 9, 10)
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])


-Chris





-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov


More information about the Numpy-discussion mailing list