Float-point basic
1. Storage Format (IEEE 754-2008)
| Format | half-precision | single-precision | double-precision |
|---|---|---|---|
| width | 16-bit | 32-bit | 64-bit |
| Sign | 1-bit | 1-bit | 1-bit |
| Exponent | 5-bit | 8-bit | 11-bit |
| Mantissa | 10-bit | 23-bit | 52-bit |
Half-precision floating-point number (16-bit)
The IEEE 754 half-precision format packs 16 bits as: 1 sign bit | 5 exponent bits (bias 15) | 10 mantissa bits.
16-bit half-precision floating-point number layout:
Example of 16-bits half-precision floating-point number calculation process

2. Formula for Floating-Point Number
half-precision floating-point number = (-1)^sign * 2^(exp-15)* (1 + mantissa/1024)
single-precision floating-point number = (-1)^sign * 2^(exp-127)* (1 + mantissa/(2^23))
double-precision floating-point number = (-1)^sign * 2^(exp-1023)* (1 + mantissa/(2^52))
3. Half-precision Encoding Rules for Special Values
| Type | Sign | Exponent | Mantissa | Hexadecimal expression |
|---|---|---|---|---|
| Positive Zero | 0 | 00000 | 0000000000 | 0x0000 |
| Negative Zero | 1 | 00000 | 0000000000 | 0x8000 |
| Positive Denormal | 0 | 00000 | non-zero | 0x0001-0x03FF |
| Negative Denormal | 1 | 00000 | non-zero | 0x8001-0x83FF |
| Positive Normal | 0 | 00001~11110 | any value | 0x0400-0x7BFF |
| Negative Normal | 1 | 00001~11110 | any value | 0x8400-0x9BFF |
| Positive Infinity | 0 | 11111 | 0000000000 | 0x7C00 |
| Negative Infinity | 1 | 11111 | 0000000000 | 0xFC00 |
| QNaN | 0/1 | 11111 | 1xxxx | 0x7C01-0x7DFF |
| SNaN | 0/1 | 11111 | 0xxxx(non-zero) | 0x7E00-0x7FFF |
Explanation: QNaN is a quiet NaN, SNaN is a signaling NaN
1. Zero
- 正零和负零在数值上相等
- 但某些运算会保留符号
- 1/0 = +∞, 1/-0 = -∞
2. Denormal / Subnormal
-
指数位全0,尾数位非0
-
用于表示接近0的极小数
-
公式:(-1)^sign × 2^(-14) × (0 + mantissa/1024)
-
没有隐含的整数部分1
- 最小的正非规格化数
16'h0001 : sign=0, exp=0, man=1
数值 = (-1)^0 × 2^(-14) × (0 + 1/1024) ≈ 5.96e-8- 最大的正非规格化数
16'h03FF : sign=0, exp=0, man=1023
数值 = (-1)^0 × 2^(-14) × (0 + 1023/1024) ≈ 6.10e-5
3. Normal
- 指数位不全0也不全1
- 隐含整数部分1
- 最小正规格化数
16'h0400 // sign=0, exp=1, man=0
数值 = (-1)^0 × 2^(1-15) × (1 + 0/1024) = 2^(-14) ≈ 6.10e-5- 最大正规格化数
16'h7BFF // sign=0, exp=30, man=1023
数值 = (-1)^0 × 2^(30-15) × (1 + 1023/1024) ≈ 65504
4. Infinity
- 任何有限数 / 0 = ±∞
- ∞ + ∞ = ∞
- ∞ - ∞ = NaN
- ∞ × 0 = NaN
- ∞ / ∞ = NaN
5. NaN
- QNaN (Quiet NaN)
- 尾数最高位为1(man[9]=1)
- 运算传播时不会触发异常
- 用于表示无效运算结果
- SNaN (Signaling NaN)
- 尾数最高位为0(man[9]=0),但尾数不全0
- 运算时会触发异常
- 用于初始化未初始化的变量