python - Automatic float32 promotion in numexpr -


consider following numpy array of dtype float32:

in [29]: x = numpy.arange(10, dtype=numpy.float32) 

when multiply 2 using pytables.expr, float32 array back:

in [30]: tables.expr('x * 2').eval().dtype out[30]: dtype('float32') 

yet when multiply 2.0, float64 array back:

in [31]: tables.expr('x * 2.0').eval().dtype out[31]: dtype('float64') 

is there way specify floating-point literal in above expression in way not cause result promoted float64?

more generally, have expression using float32 arrays, , want ensure result of type float32 (i don't mind float64 being used intermediate calculations, can't afford store results float64). how do this?

i pretty pytables.expr based on numexpr. documentation numexpr notes following promotion in expressions:

in operations implying scalar , array, normal rules of casting used in numexpr, in contrast numpy, array types takes priority. example, if 'a' array of type float32 , 'b' scalar of type float64 (or python float type, equivalent), 'a*b' returns float64 in numexpr, float32 in numpy (i.e. array operands take priority in determining result type). if need keep result float32, sure use float32 scalar too.

so happening. floating point constant responsible promotion 64 bit floats, , solution explicitly specify floating point constants float32.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -