1

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • I might be wrong, because I'm a newbie myself, but I am guessing that a 8bit floating point value would just be too small to make sense to have, since largest value of float 16 is `6.55040e+04` and smallest is `-6.55040e+04` – Sabsa Jul 20 '22 at 10:24
  • To print specifications yourself you can use: `fi16 = np.finfo(np.float16)` and `print(fi16)` – Sabsa Jul 20 '22 at 10:25
  • @Sabsa I gues so. – Ali A.M.Hassani Jul 20 '22 at 10:52
  • 1
    We can already see that a question has answers, you shouldn't edit the title to indicate that. If one or more answers have been helpful, consider voting on them and/or marking one of them as "accepted". See: [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – mkrieger1 Jul 20 '22 at 14:38
  • @mkrieger1 Actually, the answer is in the comments. – Ali A.M.Hassani Jul 20 '22 at 14:47

2 Answers2

1

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
0

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
chubercik
  • 534
  • 1
  • 5
  • 13
  • This does not answer the question to why floats are 16 but ints 8 – Sabsa Jul 20 '22 at 10:57
  • Not an expert on the subject myself, but [this answer from a different SO question](https://stackoverflow.com/a/40507235/14664861) seems to be going over why there's no float8 pretty well. – chubercik Jul 20 '22 at 11:24