[SciPy-User] [SciPy-user] difference between different mean()s

Pauli Virtanen pav+sp at iki.fi
Fri Sep 11 04:18:58 EDT 2009


(Please keep this discussion on the list, thanks!)

Fri, 11 Sep 2009, n.l.o wrote:
> Fri 11 Sep 2009, Pauli Virtanen wrote:
> > >>> a.mean(dtype=np.float64)
> > 93.617742029825848
>
> Thanks for the very informative reply.
> So if I understand correctly, I should use the 'a.mean()' method, since 
> it uses the same
> dtype; 'float32'?

No, you should specify a higher-accuracy accumulator, or use the ndimage 
routine. 93.6177 is the more correct answer.

This is a generic floating point issue: if you do (in C)

	float item[LARGENUM];
	float c;
	for (k = 0; k < N; ++k) {
		c += item[k];
	}
	c /= N;

you get a less accurate answer than with

	float item[LARGENUM];
	double c;
	for (k = 0; k < N; ++k) {
		c += item[k];
	}
	c /= N;

because of accumulated loss of precision in the + operations.

-- 
Pauli Virtanen




More information about the SciPy-User mailing list