Home > Enterprise >  Why do NumPy floating-point types start from "f2" but integer types from "i1"?
Why do NumPy floating-point types start from "f2" but integer types from "i1"?

Time:07-21

I have started learning NumPy recently. My question is why floating numbers start from 'f2', but integer numbers can start from 'i1'?

Code example:

>>> import numpy as np
>>> arr = np.array([x for x in range(10)], dtype='i1')
>>> arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int8)
>>> new_arr = arr.astype('f1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: data type 'f1' not understood
>>> new_arr = arr.astype('f2')
>>> new_arr
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.], dtype=float16)
>>> 

I also know a little bit about showing integer number in binary format.

CodePudding user response:

According to answers from this question:

For the second question: no, there's no float8 type in NumPy. float16 is a standardized type (described in the IEEE 754 standard), that's already in wide use in some contexts (notably GPUs). There's no IEEE 754 float8 type, and there doesn't appear to be an obvious candidate for a "standard" float8 type. I'd also guess that there just hasn't been that much demand for float8 support in NumPy.

Thanks Chubercik who helped me to find the answer.

CodePudding user response:

i1 translates to int8, an 8-bit signed integer; f1 in this case would similarly be float8, for which there is no support in the NumPy library.

From the documentation:

The first character specifies the kind of data and the remaining characters specify the number of bytes per item, except for Unicode, where it is interpreted as the number of characters. The item size must correspond to an existing type, or an error will be raised.

  • Related