Skip to main content

Float-point basic

1. Storage Format (IEEE 754-2008)​

Formathalf-precisionsingle-precisiondouble-precision
width16-bit32-bit64-bit
Sign1-bit1-bit1-bit
Exponent5-bit8-bit11-bit
Mantissa10-bit23-bit52-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: F

Example of 16-bits half-precision floating-point number calculation process F

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​

TypeSignExponentMantissaHexadecimal expression
Positive Zero00000000000000000x0000
Negative Zero10000000000000000x8000
Positive Denormal000000non-zero0x0001-0x03FF
Negative Denormal100000non-zero0x8001-0x83FF
Positive Normal000001~11110any value0x0400-0x7BFF
Negative Normal100001~11110any value0x8400-0x9BFF
Positive Infinity01111100000000000x7C00
Negative Infinity11111100000000000xFC00
QNaN0/1111111xxxx0x7C01-0x7DFF
SNaN0/1111110xxxx(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
    • 运算时会触发异常
    • 用于初始化未初始化的变量