跳转至

Lecture 6: Floating Point

Warning

"95% of the folks out there are completely clueless about floating point".

– James Gosling, 1998-02-28

上课时我爱搭不理,做作业发现要逐帧学习🤡

- Andrew Joker Hu🐷

现在才发现课前这句话说的太对了,Lecture 6 是目前为止我认为最难的一节课,也在课后花费很多时间才将其完全弄清楚

这一节的笔记风格切换回“国内课堂”,跳过前言和引入,以及思维指导,我们直接上结论:

alt text

alt text

C
1
1 1000 0001 111 0000 0000 0000 0000 0000

We know the bias = -(2^(k-1)-1) = -127

  1. Signal Bit is 1, so -
  2. 1000 0001 is the exponent, which is 129 in decimal, so 2^(129-127) = 2^2 = 4
  3. Significand equals to 0.111

So the result is (-1) * (4) * (1.111)_2 = -7.5

Special Presentation

Representation for 0

C
1
2
3
4
+0: 0 00000000 00000000000000000000000
-0: 1 00000000 00000000000000000000000

They are all representation of 0
  • exponent all zeroes
  • significand all zeroes

Representation for Not a Number

What do I get if I calculate sqrt(-4.0) or 0/0?

  • If ∞ not an error, these shouldn’t be either
  • Called Not a Number (NaN)
  • Exponent = 255, Significand equals to nonzero

Representation for Denorms

Problem: There’s a gap among representable FP numbers around 0

Smallest representable pos num:

\(a = {1.000...000}_2 * 2^{-126} = 2^{-126}\)

Second smallest representable pos num:

\(b = {1.000...001}_2 * 2^{-126} = 2^{-126} + 2^{-149}\)

alt text

Summary

alt text

alt text