[Numpy-discussion] float32 to float64 casting

Nathaniel Smith njs at pobox.com
Sat Nov 17 11:47:39 EST 2012

On Fri, Nov 16, 2012 at 9:53 PM, Gökhan Sever <gokhansever at gmail.com> wrote:
> Thanks for the explanations.
> For either case, I was expecting to get float32 as a resulting data type.
> Since, float32 is large enough to contain the result. I am wondering if
> changing casting rule this way, requires a lot of modification in the NumPy
> code. Maybe as an alternative to the current casting mechanism?
> I like the way that NumPy can convert to float64. As if these data-types are
> continuation of each other. But just the conversation might happen too early
> --at least in my opinion, as demonstrated in my example.
> For instance comparing this example to IDL surprises me:
> I16 np.float32(5555)*5e38
> O16 2.7774999999999998e+42
> I17 (np.float32(5555)*5e38).dtype
> O17 dtype('float64')

In this case, what's going on is that 5e38 is a Python float object,
and Python float objects have double-precision, i.e., they're
equivalent to np.float64's. So you're multiplying a float32 and a
float64. I think most people will agree that in this situation it's
better to use float64 for the output?


More information about the NumPy-Discussion mailing list