YouTube2Text

[CS61C FA20] Lecture 06.5 - Floating Point: Floating Point Discussion — Transcript

by CS 61C Departmental · 2,502 words · 400 segments · language en · Watch on YouTube

Full transcript

  1. 0:00and welcome back now let's have a
  2. 0:02discussion about some of the issues of
  3. 0:04floating point
  4. 0:06so first there's some fallacies we want
  5. 0:08to
  6. 0:09dispel um is add associative you know ad
  7. 0:14addition should be associative right
  8. 0:15that's you know one plus two plus three
  9. 0:17whether you add the one and two first or
  10. 0:19the two and three
  11. 0:20first it should be the same number it
  12. 0:22should be six
  13. 0:23well is that true for floating point add
  14. 0:26here's an example
  15. 0:26so let's make x a really small
  16. 0:30uh a really big negative number so minus
  17. 0:321.5 times 10 to the 38.
  18. 0:34let's make y exactly the opposite but a
  19. 0:37positive number
  20. 0:38and let z just be one and we even saw
  21. 0:40how to do one in a previous video
  22. 0:43let's first add y and z together when
  23. 0:46you add y and z together get this really
  24. 0:48big number
  25. 0:49and this really small number and the
  26. 0:51number of bits that you can play with is
  27. 0:53really 23
  28. 0:54bits total if you include the
  29. 0:56normalization there's like 24 total bits
  30. 0:58from like left most bit to the rightmost
  31. 1:00bit that's a one
  32. 1:01and the problem is you don't have enough
  33. 1:03bits at least in the floating point and
  34. 1:05a float
  35. 1:06to store both the one and this really
  36. 1:08big number over here
  37. 1:10so you can't do that um so when that
  38. 1:13then when that when this right most let
  39. 1:14me get my thing when this when this adds
  40. 1:16together that one goes away
  41. 1:18i can't store this one and that one the
  42. 1:20bigger one wins
  43. 1:21we lose the one well that means it's as
  44. 1:24if the z wasn't added at all
  45. 1:26and i get x plus y which is zero
  46. 1:30well now let's try it again seeing if
  47. 1:32it's associative or not let's try
  48. 1:35the leftmost one
  49. 1:38so the leftmost one says
  50. 1:42hear the leftmost guy
  51. 1:45and x and y and they cancel and then you
  52. 1:49have one and so
  53. 1:50these two are not equal so therefore in
  54. 1:53summary floating point add
  55. 1:54is not associative really important that
  56. 1:57you saw that
  57. 1:59and the reason for that is we you just
  58. 2:01have a fixed number of bits you can't do
  59. 2:03more than that if you have a really big
  60. 2:04number and really small stuff you can't
  61. 2:06keep both those guys alive you could
  62. 2:08imagine a third
  63. 2:09maybe you wanted to invent a third way
  64. 2:11to have big numbers and small numbers
  65. 2:13and have two pieces of them and
  66. 2:14i mean that's kind of like having two
  67. 2:16separate numbers anyway but you can't a
  68. 2:17single number can't have b both really
  69. 2:19big and really small
  70. 2:21so that's i mean this bullet says
  71. 2:24exactly what i said
  72. 2:25the number is so much bigger than one
  73. 2:26that it can't the representation cannot
  74. 2:28store both of them at once and so it
  75. 2:29loses it in one
  76. 2:30keeps it in the other because they
  77. 2:31cancel each other out and one is not
  78. 2:33equal to zero
  79. 2:36i wanted to give you a definition just
  80. 2:38so that you're using the right words as
  81. 2:39you're talking about floating point
  82. 2:40numbers
  83. 2:41um so precision is if this is
  84. 2:44any representation um it's the number of
  85. 2:47bits we're throwing at the problem so
  86. 2:49precision oh i want more precision
  87. 2:50throw more bits of the problem oh okay
  88. 2:52float only has this number of bits well
  89. 2:54double that or quadruple that
  90. 2:55you'll get more precision the more bits
  91. 2:57you have the closer you can get to the
  92. 2:59actual number you're trying to get to
  93. 3:01accuracy is the distance between any
  94. 3:04number that you have coded and the
  95. 3:06original
  96. 3:06actual number so his example high
  97. 3:09precision
  98. 3:10a lot of bits permits high accuracy but
  99. 3:12doesn't guarantee it
  100. 3:13here's an example here float pi equals
  101. 3:163.14
  102. 3:17well there's going to be lots of bits
  103. 3:2020 you know 32 bits associated for that
  104. 3:23but you're still pretty far in terms of
  105. 3:25accuracy from the actual value of pi
  106. 3:27now you could have done more i could
  107. 3:28have initialized it to a closer number
  108. 3:30but that
  109. 3:30typical example says i got a lot of bits
  110. 3:33but it isn't
  111. 3:34close to the actual number so that has
  112. 3:36high precision
  113. 3:37low accuracy in that case
  114. 3:40now let's talk about rounding so
  115. 3:42rounding is this complicated thing
  116. 3:44uh where you're doing some calculation
  117. 3:48and then you figure out okay well i'm
  118. 3:49not exactly at
  119. 3:50one or the other i'm not exactly at a
  120. 3:52floating point number so do i go
  121. 3:54the one above or the one below that's
  122. 3:56the idea of this
  123. 3:57and there's some hardware that usually
  124. 4:00carries with
  125. 4:00the usually floating point hardware has
  126. 4:03a couple of extra bits
  127. 4:04on the far right side that's called
  128. 4:06rounding bits
  129. 4:07and those extra bits can be used to
  130. 4:09figure out where do i really want to go
  131. 4:11the floating point number above
  132. 4:13or the one below in terms of that so
  133. 4:14this is the 32 bits and there's extra
  134. 4:16bits on the right side of that
  135. 4:17that's useful so there are modes when
  136. 4:20i'm in the middle let's say i'm some you
  137. 4:21know the rounding bits say that there's
  138. 4:23something in here that's not a zero down
  139. 4:24here my rounding bits means there
  140. 4:25actually is something to the right of
  141. 4:26that
  142. 4:28you could either there's four rounding
  143. 4:29modes you could either round
  144. 4:31always toward positive infinity always
  145. 4:33to the right on the number line
  146. 4:35or always to the left of the number line
  147. 4:36always towards minus infinity
  148. 4:38or just truncating which actually means
  149. 4:40always rounding it towards zero
  150. 4:42or here's the more clever way you could
  151. 4:45have it in an
  152. 4:46unbiased way rather than always bigger
  153. 4:48or always smaller
  154. 4:49or always towards zero you could
  155. 4:51sometimes go up and sometimes go down
  156. 4:54and that means when you're midway well
  157. 4:56this by the way these examples i'm
  158. 4:57showing you are all examples
  159. 4:59in decimal this is not the same as
  160. 5:01binary but you're obviously working with
  161. 5:03binary we're working with floating point
  162. 5:04calculations so in the first example you
  163. 5:07round up that means the 2.001 well you
  164. 5:10round toward the next
  165. 5:11that's in the middle between two and
  166. 5:12three that would round up to three
  167. 5:14okay and minus two would round to two we
  168. 5:16always round to the right
  169. 5:17the same idea always round left one
  170. 5:19point nine nine nine rounds to one even
  171. 5:20though you're almost at two you go back
  172. 5:22to one you lose it
  173. 5:24and minus 1.99 also goes down also goes
  174. 5:26negative
  175. 5:27so here's the case when you're in the
  176. 5:29middle so normal rounding
  177. 5:31um 2.4 again this is in decimal
  178. 5:34is less than halfway so it round down to
  179. 5:362. 2.6 we're
  180. 5:38more than halfway round up 2.5 is
  181. 5:42exactly in the middle
  182. 5:43so that was going to round toward the
  183. 5:45even number so that rounds to
  184. 5:472 but 3.5 would round to 4. 4
  185. 5:51nice so think about this i've got some
  186. 5:55binary equivalent of this but this is
  187. 5:56now in binary
  188. 5:57so i've got some binary numbers here
  189. 5:59okay 0
  190. 6:011 0 1. here are the rounding areas okay
  191. 6:05so now
  192. 6:07what's halfway between this by the way
  193. 6:09if i have many bits here let's say i
  194. 6:11have let's say i have three bits let's
  195. 6:12just say i have three bits
  196. 6:14those bits would be from zero zero zero
  197. 6:17to one one
  198. 6:18one what's halfway in the middle in
  199. 6:20binary
  200. 6:21remember what half is this yeah it'd be
  201. 6:24a one
  202. 6:26and zeros that's halfway in the middle
  203. 6:29so when you're that way that's when this
  204. 6:31k when when the rounding vowel
  205. 6:33bits are one and all zeros that's
  206. 6:35halfway in the middle
  207. 6:36and then you're making a decision in
  208. 6:37this particular mode which way to go
  209. 6:40well you round toward the even number so
  210. 6:43you ask is that an even number no that's
  211. 6:46a one that's an odd number
  212. 6:47so therefore i'm going to round not
  213. 6:49toward that number but toward the one
  214. 6:50above
  215. 6:51and that would round up so this would
  216. 6:53round to 0
  217. 6:541 1 0. okay
  218. 6:57if i had 0 1
  219. 7:01zero and then here's the rounding guys
  220. 7:03and i were one
  221. 7:04zero i would round toward the even i
  222. 7:06would round down
  223. 7:07which means around down toward the even
  224. 7:09and i would just cross it off and that
  225. 7:11would be the closest even
  226. 7:12does that make sense so you round toward
  227. 7:14even in binary
  228. 7:15which means you have to be a one zero
  229. 7:17zero zero zero zero zero in the rounding
  230. 7:19bits and you round toward the even
  231. 7:20number if it's a one there you round up
  232. 7:22you add one to it if it's a zero
  233. 7:23you just truncate those rounding bits
  234. 7:25okay that way we kind of round
  235. 7:27both ways and that's kind of that's
  236. 7:29really we really like that as a default
  237. 7:30mode so that's the default mode for the
  238. 7:31system
  239. 7:32because it kind of is fair it doesn't
  240. 7:33always go make things bigger or make
  241. 7:35things smaller
  242. 7:36um however irs if you're calculating my
  243. 7:38my return
  244. 7:39can you round up always just don't round
  245. 7:43the middle so now you know when you see
  246. 7:46these errors here's this wonderful comic
  247. 7:48strip i found online
  248. 7:50uh and here is a robot and it says
  249. 7:53welcome to the secret robot internet
  250. 7:55prove that you're a human point one plus
  251. 7:570.2 0.3004
  252. 8:01we can represent powers of two perfectly
  253. 8:03fractional powers are two perfectly in
  254. 8:05floating point
  255. 8:05you know because it's zero in the
  256. 8:06significant and some value of the
  257. 8:08exponent
  258. 8:09but but because we're decimal we can't
  259. 8:12do point one or point two
  260. 8:13very easily because it's one tenth this
  261. 8:15ten thing is different there's ten
  262. 8:16fingers with a problem
  263. 8:18so this is an example of point three if
  264. 8:20you type point three into my simulator
  265. 8:22you get 0.300 and it's not exactly
  266. 8:26o4 maybe that's a double but in the
  267. 8:27float in a single precision or 32 bits
  268. 8:30of a float we get 0.3001 so this is
  269. 8:32exactly why
  270. 8:33you would see this error
  271. 8:37addition let's talk about addition now
  272. 8:38we're just talking about elements of
  273. 8:39this
  274. 8:40rounding and addition and truncating and
  275. 8:42all those things
  276. 8:43so addition uh is a little bit harder
  277. 8:46uh because you can't just add the
  278. 8:48significance um you have to denormalize
  279. 8:51to match the exponent you have to kind
  280. 8:52of line them up okay
  281. 8:54to match the exponents then you add the
  282. 8:57significance to get the resulting guy
  283. 8:59and then you have to keep the same
  284. 9:00exponent and then normalize to get
  285. 9:01the end so you have to kind of shift it
  286. 9:03do something and then shift it back
  287. 9:04whatever the result is they may cancel
  288. 9:06each other out to make something
  289. 9:07you may have a big number and a big
  290. 9:08number but it turns out that they cancel
  291. 9:10most of the big stuff and the small
  292. 9:11values of what's left then you have to
  293. 9:13renormalize to get that right if the
  294. 9:14signs
  295. 9:15differ you just do a subtraction instead
  296. 9:18this is how you do casting you've seen
  297. 9:20casting when you do malik
  298. 9:22when you go to the godfather you have to
  299. 9:24cast that un
  300. 9:25you know uninitialized void star space
  301. 9:28into whatever type you want so that you
  302. 9:29know
  303. 9:30you know pointer plus plus knows how
  304. 9:31much to jump that's the reason we do
  305. 9:33that
  306. 9:34and also to check you know whether
  307. 9:35you're whether you're matching these
  308. 9:36right and everything ever the pointers
  309. 9:38work out
  310. 9:40type tracking obviously
  311. 9:43here if you've got a floating point
  312. 9:45number you want to convert to an integer
  313. 9:46just say
  314. 9:46int if you remember in python it was it
  315. 9:49was in
  316. 9:50parentheses there's a function call to
  317. 9:51that as a method call to that here you
  318. 9:53just type cache with the parenthesis in
  319. 9:54so here's 3.14159 times some float
  320. 9:58that's obviously a float and now you
  321. 9:59cast that to an end to figure what the
  322. 10:01closest energy
  323. 10:02that might be conversely you do the same
  324. 10:04thing on the other side if you have an
  325. 10:05integer number you want to float convert
  326. 10:07it like that so you say
  327. 10:08float of that integer expression and now
  328. 10:10you're able to work with that so if i
  329. 10:11have f as a float i wanna
  330. 10:13f plus equal another float well if i
  331. 10:15have i which is int you have to convert
  332. 10:17it to a float so
  333. 10:18that the compiler will be happy so
  334. 10:21now you might ask yourself well if i go
  335. 10:23from an into a flow back to an int
  336. 10:24do i get the same number does this
  337. 10:26always print true
  338. 10:30pause the video and think about it talk
  339. 10:32to your neighbor and then come on back
  340. 10:35and we're back if i equal equal int a
  341. 10:39float of i
  342. 10:40same thing ain't
  343. 10:43no free lunch there are integers
  344. 10:48that the float can't handle
  345. 10:51so there are certain integers in fact i
  346. 10:54even showed you one i think it was
  347. 10:55a little bit more than 16 million that
  348. 10:58has no
  349. 10:59float equivalent you know you can count
  350. 11:01up to four billion in in floats and
  351. 11:03integers
  352. 11:04unsigned in numbers but floats the first
  353. 11:06number you can't do we saw that before
  354. 11:0816 million plus one i can't do that
  355. 11:11number 16 million plus one is
  356. 11:13that can't be done so i mean two to the
  357. 11:15two to the fourteen two to the 24
  358. 11:18plus one can't be done i showed you
  359. 11:20before
  360. 11:21so we do that it's going to snap it to
  361. 11:24the closest float
  362. 11:25when you go back to int it's the back
  363. 11:27end the same end i can do that it'll
  364. 11:28either be
  365. 11:29not think about rounding modes that
  366. 11:31number is two to the 24
  367. 11:33plus one can't be done so does it snap
  368. 11:35to the fourth part does it do 22 24
  369. 11:38or 2 to the 24 plus 2 well this is an
  370. 11:41even number so it'll round to even
  371. 11:43so that's an exact case of that rounding
  372. 11:45mode there that's going to be
  373. 11:462 to the 20. try it 2 to the 24
  374. 11:50plus 1. see what it says that'd be
  375. 11:52really fun to play with
  376. 11:53okay can't be done sorry
  377. 11:57double precision so let's talk about
  378. 11:59double precision could that do it you
  379. 12:00want to ask yourself look at the
  380. 12:02double precision could double precision
  381. 12:03do that as well
  382. 12:06float to int to float does this always
  383. 12:09return true
  384. 12:10pause the video and think about it and
  385. 12:12come on back
  386. 12:14and we're back ain't no free lunch
  387. 12:19what if the float is 1.5 this is easiest
  388. 12:21one 1.5
  389. 12:22obviously there's no int that does 1.5
  390. 12:24and you go back it's going to be
  391. 12:26the integer representation that'd be
  392. 12:27either one or two what do you think it
  393. 12:29does
  394. 12:301.5 round to even
  395. 12:33the closest even number is 2. so that
  396. 12:36should in theory be
  397. 12:37two when it comes back so that should
  398. 12:39not be the same that's an obvious one
  399. 12:41why that doesn't work we'll see the next
  400. 12:42video

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 06.5 - Floating Point: Floating Point Discussion by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,502 words across 400 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.