[Numpy-discussion] Viewing a float64 array with a float32 array

Zachary Pincus zachary.pincus@yale....
Wed Mar 21 10:19:38 CDT 2012


> Hi,
> 
> Is it possible to have a view of a float64 array that is itself float32?
> So that:
> 
>>>> A = np.arange(5, dtype='d')
>>>> A.view(dtype='f')
> 
> would return a size 5 float32 array looking at A's data?

I think not. The memory layout of a 32-bit IEEE float is not a subset of that of a 64-bit float -- see e.g. the first table in:
http://steve.hollasch.net/cgindex/coding/ieeefloat.html

This would work for int8/int16 or other int types (so long as the number doesn't exceed the range of the smaller type), but AFAIK not floats.

Note how the subset relationship works for the int8/int16 case, but not float32/float64:

str(numpy.array(100,dtype=numpy.int8).data)
'd'

str(numpy.array(100,dtype=numpy.int16).data)
'd\x00'

str(numpy.array(-100,dtype=numpy.int16).data)
'\x9c\xff'

str(numpy.array(-100,dtype=numpy.int8).data)
'\x9c'

str(numpy.array(100,dtype=numpy.float32).data)
'\x00\x00\xc8B'

str(numpy.array(100,dtype=numpy.float64).data)
'\x00\x00\x00\x00\x00\x00Y@'


Zach





More information about the NumPy-Discussion mailing list