[Numpy-discussion] bug in numpy.mean() ?
Zachary Pincus
zachary.pincus@yale....
Tue Jan 24 13:05:50 CST 2012
> You have a million 32-bit floating point numbers that are in the
> thousands. Thus you are exceeding the 32-bitfloat precision and, if you
> can, you need to increase precision of the accumulator in np.mean() or
> change the input dtype:
>>>> a.mean(dtype=np.float32) # default and lacks precision
> 3067.0243839999998
>>>> a.mean(dtype=np.float64)
> 3045.747251076416
>>>> a.mean(dtype=np.float128)
> 3045.7472510764160156
>>>> b=a.astype(np.float128)
>>>> b.mean()
> 3045.7472510764160156
>
> Otherwise you are left to using some alternative approach to calculate
> the mean.
>
> Bruce
Interesting -- I knew that float64 accumulators were used with integer arrays, and I had just assumed that 64-bit or higher accumulators would be used with floating-point arrays too, instead of the array's dtype. This is actually quite a bit of a gotcha for floating-point imaging-type tasks -- good to know!
Zach
More information about the NumPy-Discussion
mailing list