skip to content
[Jimjimu Notes]

Computer Organization: Floating Point

/ 4 min read

Table of Contents

Course index · Previous: Number Representation · Next: RISC-V Assembly

小数点不需要fixed,而是floating

Pasted image 20260424234057

符号位+指数+尾数

Pasted image 20260424234141

Underflow(下溢) 是指计算结果的绝对值太小,无限接近于 0,以至于超出了当前浮点数格式所能表示的最小非零值的范围。 underflow可能会导致数字清零

IEEE 754

Pasted image 20260424234722 Pasted image 20260424234816

IEEE754指数为什么使用Bias Notation

Pasted image 20260424234847

根据bias encoding,一般bias取值为(2N11)-(2^{N-1} -1 ) 在这里就是127-127 ,也就是说指数会存储真实数字加上127,最后计算的时候就要减去127 注意:Exponent为0和11111111时为特殊情况,后面介绍

Pasted image 20260424235632

Special Numbers

表示0与无穷大

Pasted image 20260425002032 Pasted image 20260425002151

NaN

Pasted image 20260425003245

Denorms 非规格化

Pasted image 20260425003303

由上面的2126>>21492^{-126} >> 2^{-149} 我们发现0到最小数之间有一个很大的gap,而罪魁祸首是我们省略了尾数的先导“1”,因此,在exponent=0且significand0significand \not = 0的时候我们取消这个先导1,尾数变为(0.significand)2(0.significand)_2

IEEE 754 强行规定:所有非规格化数的真实指数固定为 1−Bias(此处bias为正数)。对于单精度来说,就是 1−127=−126

Pasted image 20260425003317

此时非规格化的最小正数为223×2126=21492^{-23} \times 2^{-126} = 2^{-149},次小数为2×21492 \times 2^{-149},接下去是3×21493\times 2^{-149} ,以此类推 到尾数全为1的时候,接下去再往下数指数位+1,就是(1+1×223)×2126,(1+2×223)×2126...(1+1 \times 2^{-23}) \times 2^{-126},(1+2 \times 2^{-23}) \times 2^{-126}... 到尾数全为1的时候,接下去再往下数指数位+1(此时exponent变为0b10 :

(1+1×223)×2125,(1+2×223)×2125...(1+1 \times 2^{-23}) \times 2^{-125},(1+2 \times 2^{-23}) \times 2^{-125}...

我们发现stride从 21492^{-149} 变成了 21482^{-148} 也就是说,每次exponent加1,stride就会翻倍。

浮点数在数轴上的分布特征:越靠近 0,浮点数分布得越密集(stride 越小);数字的绝对值越大,浮点数分布得越稀疏(stride 越大)。 这种步长在计算机科学中被称为 ULP (Unit in the Last Place)

总结

Pasted image 20260425003653

(神奇之处在于这个表格从上到下是除了sign外的31位二进制递增的,所表示的数也在递增)

例1: (1)IEEE754单精度如何表示1? (2)什么时候IEEE754单精度能表示的相邻的两个数间隔为1?

Rounding and Addition

浮点数并不满足加法结合律

Pasted image 20260425130811

Precision and Accuracy

Pasted image 20260425114545

Rounding舍入

Pasted image 20260425114608 Pasted image 20260425114630

规则:round to even,举例: Value Binary Rounded Action Rounded Value 2 3/32 10.000112 10.002 (<1/2—down) 2 2 3/16 10.001102 10.012 (>1/2—up) 2 ¼ 2 7/8 10.111002 11.002 ( 1/2—up) 3 2 5/8 10.101002 10.102 ( 1/2—down) 2 1/2

FP Addition

Pasted image 20260425114703 Pasted image 20260425131545 Pasted image 20260425131527

Course index · Previous: Number Representation · Next: RISC-V Assembly

Backlinks