我想知道从float32到float16的numpy强制转换,因为当我使用astype从float32到float16转换8193这样的数字时,它会输出8192而10000的float32会转换为10000的float16。
import numpy as np
a = np.array([8193], dtype=np.float32)
b = a.astype(np.float16)
答案 0 :(得分:3)
The IEEE 754-2008 16-bit base 2 format, aka binary16, doesn't give you a lot of precision. What do you expect from 16 bits? :) 1 bit is the sign bit, 5 bits are used for the exponent, and that leaves 10 bits to store the normalised 11 bit mantissa, so anything > 2**11 == 2048 has to be quantized.
According to Wikipedia, integers between 4097 and 8192 round to a multiple of 4, and integers between 8193 and 16384 round to a multiple of 8.