Is float16 datatype for numpy disfunctional?

后端 未结 1 1543
有刺的猬
有刺的猬 2021-01-26 07:11

Example of this major problem:

c=np.array([2600.0])

In [3]: c=c.astype(np.float16)

In [4]: c

Out[4]: array([ 2600.], dtype=float16)

All good

相关标签:
1条回答
  • 2021-01-26 07:54

    A 16-bit float is not a very precise data type. It has only 11 bits of precision. It cannot exactly represent all integers greater than 2048. Between 2048 and 4096, only the even integers have exact representations. Odd integers will be rounded to one of the nearest even integers.

    You can read more about this in the Wikipedia page about half-width floats. Here's the most important passage for your issue:

    Precision limitations on integer values

    • Integers between 0 and 2048 can be exactly represented
    • Integers between 2049 and 4096 round to a multiple of 2 (even number)
    • Integers between 4097 and 8192 round to a multiple of 4
    0 讨论(0)
提交回复
热议问题