[Numpy-discussion] Bug in numpy.mean() revisited

Nathaniel Smith njs@pobox....
Fri Jul 27 05:53:03 CDT 2012

On Fri, Jul 27, 2012 at 5:15 AM, Charles R Harris
<charlesr.harris@gmail.com> wrote:
> I would support accumulating in 64 bits but, IIRC, the function will need to
> be rewritten so that it works by adding 32 bit floats to the accumulator to
> save space. There are also more stable methods that could also be
> investigated. There is a nice little project there for someone to cut their
> teeth on.

So the obvious solution here would be to make the ufunc reduce loop
smart enough that
  x = np.zeros(2 ** 30, dtype=float32)
  np.sum(x, dtype=float64)
does not upcast 'x' to float64's as a whole. This shouldn't be too
terrible to implement -- iterate over the float32 array, and only
upcast each inner-loop "buffer" as you go, instead of upcasting the
whole thing.

In fact, nditer might do this already?

Then using a wide accumulator by default would just take a few lines
of code in numpy.core._methods._mean to select the proper dtype and
downcast the result.


More information about the NumPy-Discussion mailing list