1

In python, when I want to fill a huge array with consecutive numbers, I use numpy's arange function. However, as soon as numbers start to get big and I convert my array to float32, the numbers seem to jump randomly. As soon as I use float64, everything works as expected, but I want to know, where this error stems from exactly. I suppose, this happens because large numbers can't be represented exactly. However, numpy.finfo(numpy.float32) tells me, that the maximum float32 is about 3.4028235e+38 which I am not nearly close to. In my case, the last number up to which everything is fine seems to be

16777216 = 2**24

Can someone tell me, why this is the case?

The code to reproduce the behaviour is for example:

dat = np.arange(64*1026*1026).reshape((64,1026,1026)).astype(np.float32)
Dschoni
  • 3,714
  • 6
  • 45
  • 80
  • [This](http://stackoverflow.com/questions/12596695/why-does-a-float-variable-stop-incrementing-at-16777216-in-c) question has an answer, that seems to be applicable here. 2**24 is the biggest number that can be exactly represented, with all smaller numbers being exactly represented as well. The weired jumps in the resulting array are therefore simple rounding errors in unexact representations. – Dschoni Jan 05 '17 at 13:35
  • It's not quite true to say that `2**24` is the biggest number that can be exactly represented: there are many larger values (`2**25`, for a start) that can be exactly represented in a `float32`. But `2**24` is the largest exactly representable integer for which *all* smaller integers are also exactly representable, which I suspect is what you meant. (BTW, for `float64`, the corresponding value is `2**53`.) – Mark Dickinson Jan 06 '17 at 12:05
  • Yes, that's exactly what I meant. I'll update my answer. – Dschoni Jan 09 '17 at 12:10

0 Answers0