1 00:00:00,000 --> 00:00:00,310 2 00:00:00,310 --> 00:00:01,750 >> DAVID MALAN: Let's now blow your mind. 3 00:00:01,750 --> 00:00:06,500 It turns out in the real world 1 divided by 10 is indeed 1/10, or 0.1. 4 00:00:06,500 --> 00:00:10,370 But in computers that only have a finite number of bits with which to 5 00:00:10,370 --> 00:00:14,290 represent numbers, you can't always represent numbers like 1/10 with 6 00:00:14,290 --> 00:00:15,500 perfect precision. 7 00:00:15,500 --> 00:00:18,640 In other words, computers sometimes have to make judgment calls and not 8 00:00:18,640 --> 00:00:22,740 necessarily represent the number you want as precisely as you intend. 9 00:00:22,740 --> 00:00:27,020 >> For instance, suppose I go back into this program and change the 0.1 to, 10 00:00:27,020 --> 00:00:32,073 oh, 0.28, thereby indicating that I'd like printf to printf to 11 00:00:32,073 --> 00:00:34,350 28 places of precision. 12 00:00:34,350 --> 00:00:39,330 Let's now save and compile the program, this time with make floats2. 13 00:00:39,330 --> 00:00:41,910 Run it with dot slash floats2. 14 00:00:41,910 --> 00:00:49,980 And, dear God, this time I see not 0.1, but 0.10000000, which is pretty 15 00:00:49,980 --> 00:00:51,070 good so far. 16 00:00:51,070 --> 00:00:57,830 But then, 14901161193847656250. 17 00:00:57,830 --> 00:00:58,880 >> Well, what's going on? 18 00:00:58,880 --> 00:01:02,280 Well, it turns out that a float is typically stored inside of a computer 19 00:01:02,280 --> 00:01:03,500 with 32 bits. 20 00:01:03,500 --> 00:01:07,340 32 is obviously a finite number, which implies that you can only represent 21 00:01:07,340 --> 00:01:11,050 with 32 bits a finite number of floating point values. 22 00:01:11,050 --> 00:01:14,980 Unfortunately, that means that the computer cannot represent all possible 23 00:01:14,980 --> 00:01:18,110 floating point numbers, or real numbers, that exist in the world, 24 00:01:18,110 --> 00:01:19,980 because it only has so many bits. 25 00:01:19,980 --> 00:01:23,940 >> And so what the computer's apparently done in this case is represent 1/10 to 26 00:01:23,940 --> 00:01:26,880 the closest possible floating point value that it can. 27 00:01:26,880 --> 00:01:31,050 But if we look, as we have here, to 28 decimal places, we start to see that 28 00:01:31,050 --> 00:01:31,970 imprecision. 29 00:01:31,970 --> 00:01:34,480 So this is a problem with no perfect solution. 30 00:01:34,480 --> 00:01:38,060 We can use a double instead of a float, which tends to use 64 bits as 31 00:01:38,060 --> 00:01:39,410 opposed to 32. 32 00:01:39,410 --> 00:01:42,290 But of course, 64 is also finite, so the problem will 33 00:01:42,290 --> 00:01:43,630 remain even with doubles. 34 00:01:43,630 --> 00:01:46,323