[Numpy-discussion] float32 to float64 casting

Olivier Delalleau shish at keba.be
Fri Nov 16 06:47:42 EST 2012


2012/11/16 Charles R Harris <charlesr.harris at gmail.com>

>
>
> On Thu, Nov 15, 2012 at 8:24 PM, Gökhan Sever <gokhansever at gmail.com>wrote:
>
>> Hello,
>>
>> Could someone briefly explain why are these two operations are casting my
>> float32 arrays to float64?
>>
>> I1 (np.arange(5, dtype='float32')).dtype
>> O1 dtype('float32')
>>
>> I2 (100000*np.arange(5, dtype='float32')).dtype
>> O2 dtype('float64')
>>
>
> This one is depends on the size of the multiplier and is first present in
> 1.6.0. I suspect it is a side effect of making the type conversion code
> sensitive to magnitude.
>
>
>>
>>
>>
>> I3 (np.arange(5, dtype='float32')[0]).dtype
>> O3 dtype('float32')
>>
>> I4 (1*np.arange(5, dtype='float32')[0]).dtype
>> O4 dtype('float64')
>>
>
> This one probably depends on the fact that the element is a scalar, but
> doesn't look right. Scalars are promoted differently. Also holds in numpy
> 1.5.0 so is of old provenance.
>
> Chuck
>

My understanding is that non-mixed operations (scalar/scalar or
array/array) use casting rules that don't depend on magnitude, and the
upcast of int{32,64} mixed with float32 has always been float64 (probably
because the result has to be a kind of float, and float64 makes it possible
to represent exactly a larger integer range than float32). Note that if you
cast 1 into int16 the result will be float32 (I guess float32 can represent
exactly all int16 integers).

-=- Olivier
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121116/68a2699b/attachment.html>


More information about the NumPy-Discussion mailing list