浮点数(floating-point number)是属于有理数中某特定子集的数的数字表示,在计算机中用以近似表示任意某个实数。具体来说,这个实数由一个整数或定点数(即尾数)乘以某个基数(计算机中通常是2)的整数次幂得到,这种表示方法类似于基数为10的科学记数法。
大部分计算机采用二进制(b=2)的表示方法。位(bit)是衡量浮点数所需存储空间的单位,通常为32位或64位,分别被叫作单精度和双精度。
浮点数精度标准有:
IEEE 754
16-bit: Half (binary16)
32-bit: Single (binary32), decimal32
64-bit: Double (binary64), decimal64
128-bit: Quadruple (binary128), decimal128
Extended precision formats
计算机内部表示
Type
Sign
Exponent
Significand field
Total bits
Exponent bias
Bits precision
Number of decimal digits
Half(IEEE 754-2008)
1
5
10
16
15
11
~3.3
Single
1
8
23
32
127
24
~7.2
Double
1
11
52
64
1023
53
~15.9
x86 extended precision
1
15
64
80
16383
64
~19.2
Quad
1
15
112
128
16383
113
~34.0
有一些CPU架构提供更大的浮点数,例如Intel的浮点运算单元8087协处理器(以及其被集成进x86处理器中的后代产品)提供80位长的浮点数,用于存储浮点运算的中间结果。还有一些系统提供128位的浮点数(通常用软件实现)。
对比ARM64与x86_64单精度/双精度浮点运算精度,测试代码如下:
#include
#include
#include
#include
#include
#include