Astropython
Caution with numpy float32 images
25 Jan 2012, 12:37 UTC
(200 words excerpt, click title or image to see full post)
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
Note: All formatting and links have been removed - click title or image to see full article.




