I want to perform some standard operations on numpy float32
arrays in python 3, however I'm seeing some strange behavior when working with numpy sum()
. Here's an example session:
Python 3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
import numpy as np
np.__version__
Out[3]: '1.12.1'
a = np.ones(10).astype(np.float32)
np.sum(a).dtype
Out[5]: dtype('float32')
(np.sum(a)+1).dtype
Out[6]: dtype('float64')
(np.sum(a)+1.).dtype
Out[7]: dtype('float64')
(a+1).dtype
Out[8]: dtype('float32')
Any reason why adding a scalar to the result of a sum (which seems to have dtype
of float32
) would cast it back to float64
? To be clear, I know that I can explicitly cast the scalar to float32
, however as the last line shows, numpy still respects float32
when adding a scalar to an array. Any explanations or suggestions how to keep things as float32
without explicit casting?