单精度浮点数

来自GSXAB的知识库
单精度浮点数
术语名称 单精度浮点数
英语名称 single-precision floating-point type
别名 single-precision floating-point format, 单精度浮点型, 单精浮点, float, single, float32, FP32, binary32, 32位浮点数, 32-bit floating-point format

单精度浮点数/单精度浮点型(single-precision floating-point number)是浮点型中长 32 二进制位数据类型,符合 IEEE 754 中 binary32 类型的格式(IEEE 754-1985 旧称 single)。根据长度,称为 float32 或 binary32 ,有时也称 single 或 float 。是通用计算中最常用的浮点类型之一。

单精度浮点数在科学计算、计算机图形学、嵌入式系统中广泛应用,提供精度与存储的平衡。尽管精度低于双精度类型,其计算效率更高,适用于大多数实时场景。

定义

单精度浮点数(single-precision floating-point type)或 float32FP32binary32 ,指长度为 32 位、符合 IEEE 754 中 binary32 类型浮点格式的浮点类型。

其中浮点数的 32 位包括符号位 1 位、阶码 8 位、尾数 23 位。

范围

阶码 8 位,因此阶码偏移量为 [math]\displaystyle{ b=2^{7-1}-1=127 }[/math] 。指数范围 [math]\displaystyle{ e_\min = 1-b=-126, e_\max=b=127 }[/math] 。 尾数 23 位,精度 [math]\displaystyle{ p=24 }[/math] ,因此尾数精度为 [math]\displaystyle{ 2^{-23} }[/math] ,尾数取值在 0 ~ [math]\displaystyle{ 1-2^{-23} }[/math] 之间。

其有效精度为 24 位二进制数(含隐藏位),相当于约 7~8 位十进制数的有效数字([math]\displaystyle{ \lg 2^{24} \approx 7.22 }[/math])。

floating_point_binary32_range.svg

如图,单精度浮点数的表示范围,除了 0 和无穷、 NaN 外:

  • 规格化数正数绝对值范围:[math]\displaystyle{ 2^{-126} \approx 1.175 \times 10^{-38} }[/math][math]\displaystyle{ 2^{127} \times (2 - 2^{-23}) \approx 3.403 \times 10^{38} }[/math]
  • 非规格化数正数绝对值范围:[math]\displaystyle{ 2^{-126} \times 2^{-23} = 2^{-149} \approx 1.401 \times 10^{-45} }[/math][math]\displaystyle{ 2^{-126} \times (1 - 2^{-23}) \approx 1.175 \times 10^{-38} }[/math]