浮点数的存储方式
根据小数点是否固定,计算机中的数据分为浮点和定点,其中浮点数存储多遵从IEEE标准,包含:s符号位 + exp指数 + frac尾数,根据位数不同浮点数分为double(64bit), float(32bit), fp16(16bit)等,如下图所示:
以fp16为例,fp16使用两个字节存储数据: s i g n = 1 sign = 1 sign=1, e x p o n e n t = 5 exponent = 5 exponent=5, f r a c t i o n = 10 fraction = 10 fraction=10,则其表示的浮点数为:
d a t a = { ( − 1 ) s i g n × 2 ( e x o n e n t − 15 ) × ( 1 + f r a c t i o n 1024 ) , e x p o n e n t ≠ 0 ( − 1 ) s i g n × 2 − 14 × ( 0 + f r a c t i o n 1024 ) , e x p o n e n t = 0 data=\left\{ \begin{aligned} &(-1)^{sign}\times2^{(exonent -15)}\times(1+\frac{fraction}{1024}),&\quad exponent \neq0 \\ &(-1)^{sign}\times2^{-14}\times(0+\frac{fraction}{1024}),&\quad exponent=0 \end{aligned} \right. data=⎩
⎨
⎧