1
import numpy as np
from io import StringIO
data = StringIO("1 2 3\n 4 5 6")
np.genfromtxt(data, dtype=(int, float, int), names="a")

array([(1, 2.0, 3), (4, 5.0, 6)],
      dtype=[('a', '<i8'), ('f0', '<f8'), ('f1', '<i8')])

The above code is from NumPy official documentation. https://numpy.org/doc/stable/user/basics.io.genfromtxt.html#setting-the-names

When I ran it on my local PC, the result was different. The '<f8' in the second element was the same, but I got '<i4' at the first and the third elements instead of '<i8' as in the example.

What makes the differenct? I ran it on Windows 10 PC of i7-6700 CPU. Might it depend on the hardware?

user67275
  • 1
  • 9
  • 38
  • 64
  • It's very unlikely to depend on the hardware. It might depend on the OS, or Python version. – Barmar Feb 08 '22 at 05:26

1 Answers1

0

Regarding the documentation, the int type decays to the np.int_ one which is not portable between platforms. It is likely equal to np.int32 on x86-64 Windows machines and np.int64 on x86-64 Linux ones for example. Indeed, the size of default native integers is defined by the C ABI. The thing is there is no standard C ABI but many ones each related to a set of platforms. The OS, the processor architecture and the compiler are the three parameters that generally define the ABI. A part of the ABI for the target platform defines the data model. There are many of them, even for 64-bit systems, but only 2 are widely used nowadays: LLP64 and LP64. Based on this, we can conclude that the default Numpy integer size is:

32-bit on 64-bit Windows when using a standard MinGW build
64-bit on 64-bit Windows when using a non-standard Cygwin build
64-bit on most 64-bit Unix-like systems (eg. Linux, macOS, BSD)
32-bit on all 32-bit mainstream processors

It is generally better to specify the size of the type so to avoid platform dependent issues (eg. overflows) and better control memory usage of your program on different platforms it can be executed.

Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59