YouTube2Text

[CS61C FA20] Lecture 06.2 - Floating Point: Floating Point — Transcript

by CS 61C Departmental · 3,100 words · 500 segments · language en · Watch on YouTube

Full transcript

  1. 0:00and welcome back when last we left our
  2. 0:03hero we saw that
  3. 0:04maybe there's an idea of not just
  4. 0:07locking in these six bits and saying
  5. 0:09that the binary point is
  6. 0:10two in from the right but maybe there's
  7. 0:12another input on the side that can
  8. 0:14actually
  9. 0:15change where that binary point is and
  10. 0:18that second input
  11. 0:20is the aha with floating point
  12. 0:23so again before we've locked in fixed
  13. 0:26point we fix it there
  14. 0:28but what if i were able to also pass in
  15. 0:31another input which tells you where the
  16. 0:34binary point might be
  17. 0:35and that allows the binary point two
  18. 0:37drum roll please
  19. 0:40float float it's not fixed
  20. 0:43it can float and that beautiful idea of
  21. 0:46it floating back and forth
  22. 0:47is the power of this representation it's
  23. 0:50like another level of abstraction rather
  24. 0:51than locking it here
  25. 0:52i'm now abstracting away and saying what
  26. 0:54if it's more general now i can now move
  27. 0:56it where it is
  28. 0:57so that's just really amazing so that's
  29. 0:59a good example
  30. 1:01here's 0.1640625 it's in binary
  31. 1:05if i represent this in five bits and i
  32. 1:07choose where to put the binary point
  33. 1:08okay
  34. 1:09so here is this i've got point zero it
  35. 1:11says here point zero
  36. 1:13zero 10101 at 101.01 and then all the
  37. 1:17zeros
  38. 1:19so all i kind of need to know see the
  39. 1:22guys in blue
  40. 1:23all the need to know is i need to
  41. 1:25somehow send
  42. 1:26these five bits if i could somehow send
  43. 1:28those five bits i need to send the zeros
  44. 1:30before it or zero's after it
  45. 1:31or over here i don't need to send those
  46. 1:33it's just default it's always zero
  47. 1:35i need to i need i almost need to send
  48. 1:37you where the energy is where are the
  49. 1:38ones and zeros that are interesting
  50. 1:40but i kind of need to freeze it on the
  51. 1:41left side and the right side with ones
  52. 1:42and the zeros outside of it
  53. 1:44so if i could just send that set and
  54. 1:47with my
  55. 1:47other input that's the key idea i tell
  56. 1:50you oh
  57. 1:50the binary point should be two to the
  58. 1:52left of the leftmost
  59. 1:54one so i would say like you know somehow
  60. 1:56minus two
  61. 1:57sitting at minus two that tells you it's
  62. 1:59over there
  63. 2:00and on this guy i'm sending 10101.
  64. 2:04so that's kind of cool so you can
  65. 2:05imagine taking two inputs this
  66. 2:07is the energy i say what the energy is
  67. 2:09and this is
  68. 2:10where the the floating point would be
  69. 2:13and that's amazing that's amazing
  70. 2:17so that is the idea we're gonna have
  71. 2:20some fields to our representation one
  72. 2:22field tells you kind of where the energy
  73. 2:24is
  74. 2:24and one field tells you in a way the
  75. 2:26exponent tells you where it is it's
  76. 2:28it's multiplied times two to the what
  77. 2:30right it's times two to the
  78. 2:31minus two here's two to the minus two
  79. 2:33because it's two over okay it's the
  80. 2:35quarter this is a times a quarter
  81. 2:36so what it's saying is it's saying
  82. 2:40i want to be able to have this exponent
  83. 2:42for where that moves every time i move
  84. 2:43it to the left it's multiplying by a
  85. 2:44factor of two so it's really what two to
  86. 2:46the what
  87. 2:47two to the zero it says it's exactly
  88. 2:48where you think it is to the one says
  89. 2:50move it you know move it one to the
  90. 2:51to the left and now i have a zero to the
  91. 2:53right etcetera
  92. 2:54is that cool or one to the right move
  93. 2:56one to the right i guess what is but one
  94. 2:57to the right okay
  95. 2:59so because the binary point
  96. 3:02where it is it's different from the
  97. 3:03actual stored bits what i call the
  98. 3:05energy
  99. 3:06now i can do large and small numbers now
  100. 3:08i've got the first thing i can do really
  101. 3:09small numbers or really big numbers
  102. 3:11or big and small and i got it so let's
  103. 3:13now review scientific notation
  104. 3:15in decimal back in the old days now
  105. 3:18we're in i think we're probably in
  106. 3:20physics class maybe ap maybe chemistry
  107. 3:22back in high school 9th or 10th grade
  108. 3:24if you remember here are some words just
  109. 3:26to remind you what these words mean
  110. 3:28i had a decimal point that's the obvious
  111. 3:30thing i've got the radix which we call
  112. 3:32the base
  113. 3:33i've got the exponent and i've got the
  114. 3:35mantissa on the left
  115. 3:36okay so if i live in normal form
  116. 3:41normal form says that i always have a
  117. 3:44single
  118. 3:45digit to the left of the decimal point
  119. 3:48so i don't have
  120. 3:49if i want to store 1 times 10 to the
  121. 3:50minus ninth okay so
  122. 3:52or a nano something i wouldn't store it
  123. 3:55as
  124. 3:550.1 times 10 to the 8th and i would not
  125. 3:58store it as 10 or 10 to the minus 10
  126. 4:00even though they're equivalent
  127. 4:00mathematically i would not do that
  128. 4:02because they're not in normalized form
  129. 4:04so we're going to go with a normalized
  130. 4:06form there
  131. 4:07so that's pretty cool so let's see what
  132. 4:11that looks like when we go to
  133. 4:13binary same idea
  134. 4:16three out of those four words are
  135. 4:17exactly the same mentis is there the
  136. 4:20exponents up there the base is base two
  137. 4:22not base ten
  138. 4:22and now it's the binary point we call
  139. 4:26this
  140. 4:27floating point numbers officially
  141. 4:28floating point numbers and if you've
  142. 4:30ever seen the phrase
  143. 4:32in c float that's it
  144. 4:35this is what happens we're storing this
  145. 4:37this story in this way we're storing in
  146. 4:39this way
  147. 4:40in normalized form where on the left is
  148. 4:42always going to be a one
  149. 4:44that's the important thing always going
  150. 4:45to be one to the left of the binary
  151. 4:47point
  152. 4:48in normalized form this is it i feel
  153. 4:50like i
  154. 4:51always it's such a delight to teach
  155. 4:53somebody something new for the first
  156. 4:54time and this is it this is
  157. 4:55the this is the beginning of what we
  158. 4:57call the ieee floating point
  159. 4:59designation so here's normalized form
  160. 5:02you're gonna say but dan
  161. 5:04if every number starts at one point
  162. 5:06something you might say to me
  163. 5:07why do you have to transmit it why would
  164. 5:09you store the one on the left if it's
  165. 5:11one on the left
  166. 5:12always there's always going to be one on
  167. 5:13the left unless it's all zeros there's
  168. 5:15gonna be a leading one
  169. 5:16why store it where it's always gonna be
  170. 5:18one and you're like
  171. 5:20i'm you're right so we actually don't
  172. 5:22store it we only store
  173. 5:23the number to the right which is the
  174. 5:25significant
  175. 5:26so we're one point the one point is
  176. 5:29always kind of default there
  177. 5:32yyyy is our exponent so this exponent
  178. 5:37goes here
  179. 5:38here's a significant there and you're
  180. 5:40going to say well dan we probably should
  181. 5:41have some negative numbers too
  182. 5:43and you know to make this easy what
  183. 5:45we're going to do is
  184. 5:46let's actually use a sine magnitude
  185. 5:48model so that left is a sine bit
  186. 5:51so there we go s is your sign bit bloop
  187. 5:55exponent are these eight bits there that
  188. 5:57returns two to the y
  189. 5:58and the significant are the x's
  190. 6:00represented there with 23 bit that's a
  191. 6:02lot
  192. 6:02one point 23 more bits that's quite a
  193. 6:04bit actually
  194. 6:06it's pretty cool and by the way what's
  195. 6:08this last bit controlling
  196. 6:10what's that last bit if it's always one
  197. 6:12point something that last bit is two to
  198. 6:14the minus 23.
  199. 6:15so it's one plus two to the minus if i
  200. 6:18only have a one here and have zeros all
  201. 6:20in here
  202. 6:20that last guy is two to the minus 23 if
  203. 6:22you think about that 23 bits there on
  204. 6:24the right it's that it's one point this
  205. 6:25to the minus 23. remember there was
  206. 6:27there was four bits to the right it was
  207. 6:28once one sixteenth it was two minus four
  208. 6:30well 23 bits across
  209. 6:32two to the minus 23 is the guy all the
  210. 6:33way on the right what can we do
  211. 6:36well this just this nothing special
  212. 6:39nothing more than that no
  213. 6:40special cases which we're going to see
  214. 6:41in later lectures
  215. 6:43we can get to 1.2 times 10 to the minus
  216. 6:4638
  217. 6:46really small numbers and up to 3.4
  218. 6:50times 10 to the 38. that's amazing this
  219. 6:53is
  220. 6:53really cool now you're going to say well
  221. 6:55dan
  222. 6:56what if they're too large what if i mean
  223. 6:58that's pretty good
  224. 6:59that's pretty good and we're going to
  225. 7:01see the ain't no free lunch coming up
  226. 7:03back again but we'll talk about that in
  227. 7:05a second so what if they're bigger than
  228. 7:06that what if they're either bigger than
  229. 7:08the high side or
  230. 7:09smaller than the low side well that's
  231. 7:11overflow
  232. 7:12you know i want to restore a number
  233. 7:14bigger than that i what if i double it
  234. 7:15and double that
  235. 7:16and i can't always in forever do that
  236. 7:18double that double that i got a fixed
  237. 7:19bit
  238. 7:20bit width at some point it's going to be
  239. 7:22bigger than i can store
  240. 7:23we call that overflow what if it's even
  241. 7:25smaller than it what if it's half of
  242. 7:26that half of that on the negative side
  243. 7:28half
  244. 7:29it should be double a negative number
  245. 7:31i'm sorry if you double the negative
  246. 7:32number and keep
  247. 7:33pushing the left the left side it's also
  248. 7:35overflow
  249. 7:36some people call it negative overflow
  250. 7:37but it's also overflow on the big sides
  251. 7:40on the ends towards infinity always
  252. 7:42overflow sometimes the left they call it
  253. 7:43negative overflow
  254. 7:45what if they're too small aha
  255. 7:49underflow you thought that the negative
  256. 7:51infinity was
  257. 7:52under flow it's not if that's overflow
  258. 7:54that's not a flow underflow is towards
  259. 7:55zero what if i take a number and have it
  260. 7:57and have it and have it
  261. 7:59and it keeps getting smaller and smaller
  262. 8:00at some point i reach the limit
  263. 8:02of what i can do and one past
  264. 8:05that is underflow i would probably would
  265. 8:07say it's zero even though there is some
  266. 8:09number nope
  267. 8:10the closest number that i can store to
  268. 8:12what you're asking me for is zero
  269. 8:13so i kind of underflowed to zero and
  270. 8:15that's that's a problem because it means
  271. 8:17that
  272. 8:17oh i made some fractional amount but
  273. 8:19then it says zero but it's not really
  274. 8:20zero it's close to zero that's an issue
  275. 8:22so again underflow is close to zero
  276. 8:25overflow
  277. 8:26is on the sides we'll just call it
  278. 8:27overflow in 621c rather than negative
  279. 8:29overflow but that's right what would
  280. 8:31help think about what would help reduce
  281. 8:33the chances of overflow and underflow
  282. 8:36throw more bits at it right throw more
  283. 8:38bits at the problem
  284. 8:40so here is the ieee 754 floating point
  285. 8:44standard used in every computer you have
  286. 8:46access to
  287. 8:47that's it sign bit
  288. 8:50eight exponent bits 23 significant bits
  289. 8:54that's great by the way just a refresher
  290. 8:56one is going to mean
  291. 8:57a negative number and 0 is going to be a
  292. 8:59positive number
  293. 9:01so that's pretty cool now we mentioned
  294. 9:04this before to pack more bits the
  295. 9:06leading one is going to be implicit
  296. 9:07remember that's normalized form and why
  297. 9:09store that one if it's always going to
  298. 9:10be a one so
  299. 9:11that's pretty cool if i actually want
  300. 9:13more
  301. 9:14if i want i want to have more resolution
  302. 9:16i want to reduce the chance of
  303. 9:18overflow or underflow throw more bits of
  304. 9:20the problem call it a double
  305. 9:22so we'll have actually a double for our
  306. 9:24significant for that i have 52 bits for
  307. 9:26that
  308. 9:28so what's always true is the significand
  309. 9:30is between
  310. 9:31zero and one for normalized numbers
  311. 9:33because if you remember it's one point
  312. 9:36all those bits 23 or 52. think about
  313. 9:38that one point this
  314. 9:40so this the this the significant is
  315. 9:42point
  316. 9:43something goes from zero if it's all
  317. 9:45zeros to all ones but it's still a
  318. 9:46fractional number
  319. 9:48it's probably with 23 it is one
  320. 9:51over two to the 23 away from one so it
  321. 9:53is
  322. 9:54um i don't know how to say that it's a
  323. 9:57it's a
  324. 9:57number very close to one how close is it
  325. 9:59to one one
  326. 10:01over two to the 23 away from 1. that's
  327. 10:04how it is
  328. 10:05and uh here's the interesting thing 0
  329. 10:09has no leading one we said this is weird
  330. 10:10case that 0 has no leading 1
  331. 10:12so we're going to reserve the exponent
  332. 10:14value of all zeros
  333. 10:16just for zero which is great so now i
  334. 10:19have
  335. 10:19exponent of zero so i wanna store zero
  336. 10:22there's a zero
  337. 10:23there's a zero and ah wait wait sine bit
  338. 10:26do you remember our issue with sine bits
  339. 10:28with sine magnitude i got two zeros
  340. 10:33what do you think we got two zeros here
  341. 10:35too
  342. 10:37so here's the other part that's
  343. 10:39interesting
  344. 10:40if zero is the smallest exponent
  345. 10:44in terms of the bid pattern and we go up
  346. 10:47wouldn't it be nice if all zeros
  347. 10:51here's the thing all zeroes were the
  348. 10:54smallest one and we kept going up
  349. 10:55continually not having to wrap like
  350. 10:57remember signed
  351. 10:57it starts in the middle goes up and then
  352. 10:59it snaps over here and does this thing
  353. 11:01i want it to be zero all zeros is the
  354. 11:02smallest one and go up
  355. 11:04but if i want the small here's the part
  356. 11:06that's a little weird i want that
  357. 11:07smallest one
  358. 11:08to be in the negative because i want to
  359. 11:11do fractional numbers right i want that
  360. 11:13to be two to the
  361. 11:14negative number so if i want all zero
  362. 11:16bit pattern just might be a negative
  363. 11:17how does this going to work okay
  364. 11:22so here's the designer the designers
  365. 11:24went back to the drawing board they said
  366. 11:26we want to use if i have no floating
  367. 11:28port hardware by the way back in the day
  368. 11:29they used to have
  369. 11:30explicit floating point hardware and we
  370. 11:33still have that
  371. 11:34but if i didn't in the early days of
  372. 11:36machines they didn't have special
  373. 11:37purpose floating power they just had a
  374. 11:38normal cpu
  375. 11:39but they want to be able to use the cpu
  376. 11:41to compare and do things to do some
  377. 11:43manipulations with
  378. 11:44floating point numbers so they came up
  379. 11:46with the idea that
  380. 11:48bigger integer exponents are bigger
  381. 11:49numbers the smaller ones are fractional
  382. 11:51numbers
  383. 11:52and they thought to themselves they said
  384. 11:54think about the odometer the only way to
  385. 11:55do that is that bias notation
  386. 11:58so therefore kind of a really small bit
  387. 12:00patterns very few numbers and low low
  388. 12:02bid patterns like you're driving the
  389. 12:04binary odometer a little bit
  390. 12:05that should be a negative number and a
  391. 12:07high thing should be a really big
  392. 12:08positive number well that's a bias model
  393. 12:11if you think about that that takes this
  394. 12:12you know zero and this and here's
  395. 12:14all ones and if you shift them down by
  396. 12:16some offset
  397. 12:17then this is going to be overall a
  398. 12:19negative number and this will be a
  399. 12:20positive number
  400. 12:21if this is from 0 to 55 then roughly
  401. 12:23this would be kind of
  402. 12:24128 to minus 128 ish somewhere on there
  403. 12:27and that's pretty good so we'll shift it
  404. 12:29down by some bias and we'll get what we
  405. 12:31want
  406. 12:32love this so bias notation
  407. 12:35and the bias is subtracted and we've got
  408. 12:37eight bits you remember how we did eight
  409. 12:38bits before
  410. 12:39eight bits is two to the n minus one
  411. 12:42minus one
  412. 12:43so two to the n minus one so eight bits
  413. 12:44two to the n minus one eight
  414. 12:46n is eight that's 128 minus one is 127.
  415. 12:50so we call the bias 127. what that
  416. 12:52really means is you subtract 127 from
  417. 12:55the value of that so you look at the
  418. 12:57unsigned value
  419. 12:58subtract 127 and you've got your actual
  420. 13:01number
  421. 13:02so that's it so there's our equation
  422. 13:03let's take a look at our equation now
  423. 13:06this is actually a kind of interesting
  424. 13:07thing minus 1 to the s when s is 0 minus
  425. 13:101 to the 0
  426. 13:10is 1. so that's just a normal positive
  427. 13:12number if s is 1 it's minus 1 to the 1
  428. 13:14which is minus number so actually that's
  429. 13:16kind of a cool way to think about the
  430. 13:17sign bit it's minus 1 to the sign bit
  431. 13:20times 1 plus significant remember this
  432. 13:23there's
  433. 13:23there's the implicit one times
  434. 13:262 to the exponent whatever the raw value
  435. 13:29is
  436. 13:29minus your 127 bias that's it
  437. 13:33folks that is the ieee 754
  438. 13:37floating point there's still some more
  439. 13:38details there but that's pretty cool and
  440. 13:40double is exactly the same
  441. 13:41it's just you have more bits for the
  442. 13:43significant and more bits for the
  443. 13:44exponent but exactly the same equation
  444. 13:47that's pretty cool and quad
  445. 13:50quad is bigger than that same idea just
  446. 13:52more again bits for the exponent
  447. 13:54more bits for the significance let me
  448. 13:55introduce you to my colleague professor
  449. 13:57velvel
  450. 13:59kahan he's the father of the floating
  451. 14:02point standard uc berkeley
  452. 14:05ieee standard 754 for binary floor
  453. 14:07arithmetic
  454. 14:08earn him the turing award the nobel
  455. 14:12prize in computer science
  456. 14:14for being the leader of this boy
  457. 14:17floating point
  458. 14:18space the ecosystem was chaotic before
  459. 14:22his team of folks said let's kind of
  460. 14:24normalize this so that
  461. 14:26if i calculate something on one computer
  462. 14:28and bring it to the other computer i've
  463. 14:28got the different number
  464. 14:29this was happening all around them and
  465. 14:31so i would try to compare oh right to
  466. 14:32the trajectory and it landed here and
  467. 14:34yours that
  468. 14:34why because the way the algorithms were
  469. 14:37the same but the way the floating point
  470. 14:38was done below the line
  471. 14:40in the hardware was different on every
  472. 14:41machine he said that's crazy let's have
  473. 14:43a standard
  474. 14:44let's have a way that my calculation can
  475. 14:46move to your computer remember porting
  476. 14:47was an issue
  477. 14:48well it's big indian small indian all
  478. 14:50these weird things that are different
  479. 14:52about this answer different sizes
  480. 14:53it's even worse back in the day in the
  481. 14:56floating point space your machine was
  482. 14:58completely different from our machine we
  483. 14:59couldn't trust our numbers
  484. 15:01so the scientific community said we got
  485. 15:02to centralize this he led the team to
  486. 15:05centralize it came up with ieee standard
  487. 15:07754
  488. 15:08and won the turing award back in 1994.
  489. 15:10amazing right
  490. 15:11amazing work so we're going to see now
  491. 15:14more details about this we're going to
  492. 15:15see
  493. 15:16does that cover everything well that's
  494. 15:18almost 90
  495. 15:19of it in terms of intellectually 90 of
  496. 15:20this but there's a lot of interesting
  497. 15:22ideas now that you know the basics of
  498. 15:24floating point where can we go with that
  499. 15:26we're gonna see that in the next set of
  500. 15:27videos see you there

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 06.2 - Floating Point: Floating Point by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 3,100 words across 500 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.