Caution with numpy float32 images
There is an interesting thread on the numpy discussion group about an unexpected result of taking the mean (or sum) of a large array of 32-bit floating point numbers. If you exceed the 32-big float precision then you will get the wrong answer on some platforms. Takeaway: use float64 or specify the precision of the accumulator.
Here are some examples:
In [1]: d = np.ones((1000, 1000), dtype=np.float32) * 10 In [2]: d.mean() Out[2]: 10.0 In [3]: d = np.ones((1000, 1000), dtype=np.float32) * 100 In [4]: d.mean() Out[4]: 98.684352000000004 In [5]: d.mean(dtype=np.float64) Out[5]: 100.0
3 Comments
The default is .... by Mike (2012-01-28)
Reply
Re: Article by Tom Aldcroft (2012-02-02)
Reply