What do the > < signs in numpy dtype mean?

前端 未结 3 1321
野的像风
野的像风 2021-01-01 21:55

What is the difference between dtype=\'f\', dtype=\'f4\', dtype=\'>f4\', dtype\'? The syntax is not explained

相关标签:
3条回答
  • 2021-01-01 22:28

    Endian-ness.

    < = little-endian (LSB first)

    > = big-endian (MSB first)

    https://docs.scipy.org/doc/numpy/reference/generated/numpy.dtype.byteorder.html

    0 讨论(0)
  • 2021-01-01 22:30

    I just had the same question and tried to search the answer online.

    'f' is the shorthand for 'float32'.

    'f4' also means 'float32' because it has 4 bytes and each byte has 8 bits.

    Similarly, 'f8' means 'float64' because 8*8 = 64.

    For the difference between '>f4' and '<f4', it is related to how the 32 bits are stored in 4 bytes.

    ('>')Big Endian Byte Order: The most significant byte (the "big end") of the data is placed at the byte with the lowest address. The rest of the data is placed in order in the next three bytes in memory.

    ('<')Little Endian Byte Order: The least significant byte (the "little end") of the data is placed at the byte with the lowest address. The rest of the data is placed in order in the next three bytes in memory.

    Please see this link for more detailed information: https://chortle.ccsu.edu/AssemblyTutorial/Chapter-15/ass15_3.html

    0 讨论(0)
  • By looking up the data type object you can see that the '>' and '<' reference the Endianess of the datatype

    https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

    >>> dt = np.dtype('>H') # big-endian unsigned short
    >>> dt = np.dtype('<f') # little-endian single-precision float
    

    f is a single-precision floating point number and in your case it uses 4 bytes (4 x 8 = 32 bits).

    dtype='<f4'
    

    Makes dtype a 32 bit single-precision floating point number using little endian order of bytes.

    More on Endianness can be found using wiki https://en.wikipedia.org/wiki/Endianness

    0 讨论(0)
提交回复
热议问题