1 00:00:07,260 --> 00:00:07,590 Hello. 2 00:00:08,220 --> 00:00:11,700 In this lecture, we're going to talk about Question 22. 3 00:00:12,120 --> 00:00:15,360 What is the difference between double and decimal? 4 00:00:15,930 --> 00:00:22,440 This is a question that you can hear quite often during interviews, especially in the financial sector. 5 00:00:22,830 --> 00:00:29,580 After this lecture, you will have a clear understanding of why the difference between double and decimal 6 00:00:29,580 --> 00:00:31,890 is that double is our floating point. 7 00:00:31,890 --> 00:00:36,490 Binary number one decimal is a floating point decimal number. 8 00:00:36,810 --> 00:00:41,430 What matters most for us is that double is optimized for performance. 9 00:00:41,610 --> 00:00:44,250 Well, this email is optimized for precision. 10 00:00:44,730 --> 00:00:48,180 First, let's understand the floating point parts. 11 00:00:48,690 --> 00:00:56,670 Floating point numbers cannot be integers, but numbers like half one, third, one fourth, etc. We 12 00:00:56,670 --> 00:01:00,300 can actually represent such numbers using integers. 13 00:01:00,570 --> 00:01:08,430 If you use the concept of Mandisa and Exponent Mandisa, give some skilled representation of the number 14 00:01:08,430 --> 00:01:15,120 one exponent since, well, the scale is, in other words, where the decimal point will be. 15 00:01:15,570 --> 00:01:20,280 That's why they are called floating point numbers as the point is moving. 16 00:01:20,760 --> 00:01:29,610 This probably sounds a bit mysterious, so let's try to represent number 324 0.56 with monetizable and 17 00:01:29,610 --> 00:01:30,480 exponent. 18 00:01:30,960 --> 00:01:38,340 The pattern is simple we multiply the Mandisa by the base of the system raised to the power of the exponent 19 00:01:38,610 --> 00:01:42,930 10 to the power of minus two is zero point zero one. 20 00:01:43,080 --> 00:01:45,660 So the result is 224. 21 00:01:45,660 --> 00:01:47,840 That 56 as expected. 22 00:01:48,270 --> 00:01:53,430 As you can see, we manage to represent our floating point number with integers only. 23 00:01:53,970 --> 00:02:00,570 We said that a double is a binary floating point number, which means the base of the system it uses 24 00:02:00,570 --> 00:02:02,520 is to four decimal. 25 00:02:02,760 --> 00:02:03,510 It is done. 26 00:02:04,020 --> 00:02:07,890 All right, here comes the problem with floating point numbers. 27 00:02:08,160 --> 00:02:15,020 If the monkeys are limited precision and it does in computers, or even if we try to write it down on 28 00:02:15,030 --> 00:02:20,700 a piece of paper, it means we can't represent some numbers in a perfectly precise way. 29 00:02:21,120 --> 00:02:27,540 For example, if you wanted to write one third as a floating point number on a piece of paper, you 30 00:02:27,540 --> 00:02:32,370 would fill it with 0.3 free, free, free, free and so on. 31 00:02:32,550 --> 00:02:35,040 But finally, you would run out of paper. 32 00:02:35,490 --> 00:02:37,410 Well, there are no you would, right? 33 00:02:37,650 --> 00:02:44,070 It would be some approximation of the number that is actually one third, the same as with the paper. 34 00:02:44,220 --> 00:02:50,580 You can run out of bits when representing such a number in C Sharp, which leads to representation that 35 00:02:50,580 --> 00:02:51,690 is not precise. 36 00:02:52,140 --> 00:02:56,880 Double is a binary floating point number, occupying 64 bits. 37 00:02:57,240 --> 00:03:03,210 One bit is reserved for the sign, so to know whether the number is positive or negative. 38 00:03:03,390 --> 00:03:09,030 Fifty two bits are reserved for monkeys and 11 bits are reserved for the exponent. 39 00:03:09,390 --> 00:03:16,170 We said that numbers, like one third, are impossible to be represented in the decimal number system, 40 00:03:16,170 --> 00:03:20,340 with finite number of digits in the binary system. 41 00:03:20,700 --> 00:03:24,570 An example of such unrepresentative number is one tenth. 42 00:03:24,940 --> 00:03:29,130 Let's see some code that will show the lack of precision of taboos. 43 00:03:30,160 --> 00:03:35,530 From the point of view of real world mathematics, this should evaluate to true. 44 00:03:35,890 --> 00:03:39,910 Let's see what the computer's mathematics has to say about that. 45 00:03:41,200 --> 00:03:47,770 Well, that's obviously not correct, but it shouldn't be that surprising, given that we already established 46 00:03:47,770 --> 00:03:52,840 that the floating point numbers are not represented precisely in the computer's memory. 47 00:03:53,470 --> 00:04:00,550 And here's an important note since doubles are approximations of numbers and are not very precise. 48 00:04:00,670 --> 00:04:04,600 We should avoid simply comparing them with the quality operator. 49 00:04:05,050 --> 00:04:10,090 Think of it like this in real life, you are measuring borrowed to build something. 50 00:04:10,450 --> 00:04:12,820 You want the boards to be of the same length. 51 00:04:13,180 --> 00:04:16,450 You measure them carefully and you are happy to see that. 52 00:04:16,450 --> 00:04:23,020 Both have the length of exactly 273 centimeters and zero millimeters. 53 00:04:23,320 --> 00:04:30,280 So you can say to yourself, yes, those boards are equal and move on to constructing a shed for gardening 54 00:04:30,280 --> 00:04:30,880 tools. 55 00:04:31,240 --> 00:04:33,220 But are they truly equal? 56 00:04:33,700 --> 00:04:38,800 I bet if you measure them with some advanced machinery, it would actually turn out. 57 00:04:38,800 --> 00:04:46,540 Not one of them is two hundred seventy three point zero one, while the other is 273 point zero four 58 00:04:46,540 --> 00:04:47,770 centimeters long. 59 00:04:48,010 --> 00:04:52,480 So they are not exactly equal, but they are equal enough for your needs. 60 00:04:52,870 --> 00:04:56,890 It's the same thing which doubles when checking for the equality. 61 00:04:56,920 --> 00:05:02,680 We should rather check if the difference between them is so small that we don't actually care about 62 00:05:02,680 --> 00:05:03,010 it. 63 00:05:03,370 --> 00:05:08,830 So the simplest implementation of a method checking the equality could be this. 64 00:05:21,360 --> 00:05:27,690 In this matter, I simply check if the absolute of the difference is smaller than the tolerance. 65 00:05:28,080 --> 00:05:34,320 Let's see what will be printed from this line now through has been printed. 66 00:05:34,350 --> 00:05:39,090 Because the difference between those two numbers is smaller than the given tolerance. 67 00:05:40,440 --> 00:05:48,150 One more note about Dubbo's a variable of the type can have a specific value called a noun, not a number. 68 00:05:48,510 --> 00:05:55,020 It is reserved for representing undefined mathematical operations like, for example, dividing zero 69 00:05:55,020 --> 00:05:55,800 by zero. 70 00:06:01,310 --> 00:06:04,880 As you can see, this gave a result, which is not a number. 71 00:06:06,170 --> 00:06:06,800 All right. 72 00:06:07,100 --> 00:06:09,260 That closes the topic of doubles. 73 00:06:09,500 --> 00:06:12,920 But before we move on to decimals, let's mention floats. 74 00:06:13,280 --> 00:06:15,920 Float acts exactly the same as double. 75 00:06:16,250 --> 00:06:20,290 The only difference is that float is scored on 42 minutes. 76 00:06:20,490 --> 00:06:22,520 Well, double is on 64. 77 00:06:23,000 --> 00:06:25,550 So Double simply has a larger range. 78 00:06:26,180 --> 00:06:26,840 All right. 79 00:06:27,050 --> 00:06:33,260 We learned that doubles are not very precise, and we must be careful when making assumptions about 80 00:06:33,260 --> 00:06:33,590 them. 81 00:06:34,070 --> 00:06:40,100 For example, the equality of two double numbers may not be the same as it would be in a real world 82 00:06:40,100 --> 00:06:42,800 mathematics for some scenarios. 83 00:06:42,830 --> 00:06:46,970 We need precision and we can't make any compromises about that. 84 00:06:47,360 --> 00:06:52,220 For example, imagine a banking system that cross-check some transactions. 85 00:06:52,520 --> 00:06:58,940 It must be sure that after the money transfer, the amount that was taken from your account is exactly 86 00:06:58,940 --> 00:07:02,870 the same as the amount that was added to the receiver's account. 87 00:07:03,230 --> 00:07:05,990 If you use double for representing money. 88 00:07:06,080 --> 00:07:07,550 This could lead to errors. 89 00:07:07,880 --> 00:07:13,940 Both amounts would be represented as some approximation, and they could not be exactly equal. 90 00:07:14,210 --> 00:07:18,380 That's why, for representing money, we should always use decimals. 91 00:07:18,770 --> 00:07:21,320 Decimal is optimized for precision. 92 00:07:21,620 --> 00:07:28,640 It occupies more memory than the double because a smaller range and operations on it are slower, but 93 00:07:28,640 --> 00:07:30,110 it guarantees precision. 94 00:07:30,530 --> 00:07:37,270 Of course, like other C-sharp numeric type, because its size, limitations and country presents numbers 95 00:07:37,280 --> 00:07:39,050 smaller or larger than this. 96 00:07:39,230 --> 00:07:44,630 But within this range, the operations will give correct results without any surprises. 97 00:07:45,080 --> 00:07:50,510 First, let's see the code that we had before, but this time let's use decimals. 98 00:07:57,760 --> 00:08:04,750 As you can see, this time, nothing unexpected happens, so we gained precision. 99 00:08:04,900 --> 00:08:06,250 But at what price? 100 00:08:06,520 --> 00:08:08,860 First, let's measure the performance. 101 00:08:09,970 --> 00:08:17,410 Here are defined amateur doing some operations on Dubbo's, and he was exactly the same method, but 102 00:08:17,410 --> 00:08:18,640 using decimals. 103 00:08:19,000 --> 00:08:25,060 Let's see how both of them will do when the number of iterations is far million. 104 00:08:27,270 --> 00:08:28,020 Yikes. 105 00:08:28,320 --> 00:08:33,150 Calculating decimals took almost seven times longer than doubles. 106 00:08:34,200 --> 00:08:35,430 There is one more thing. 107 00:08:35,670 --> 00:08:42,540 Doubles are not only faster, but they also have much larger range while occupying less memory. 108 00:08:43,230 --> 00:08:49,890 As you can see, doubles rent is just ridiculous, which makes them perfect for some scientific users. 109 00:08:50,130 --> 00:08:58,050 When one often operates on very small or very large, numbers, also double takes only eight bytes while 110 00:08:58,070 --> 00:08:59,580 decimal take 16. 111 00:09:00,390 --> 00:09:06,960 In general, we should use doubles when representing some numbers coming from nature, like physical 112 00:09:06,960 --> 00:09:10,860 measurements, which by definition are not perfectly precise. 113 00:09:11,220 --> 00:09:17,220 That was our right to represent things like land speed, position on the map and such. 114 00:09:17,850 --> 00:09:19,320 Of their great performance. 115 00:09:19,560 --> 00:09:26,700 They are widely used in some industrial applications, games, etc. Decimals are much slower. 116 00:09:26,940 --> 00:09:30,060 They occupy more memory and have a smaller range. 117 00:09:30,300 --> 00:09:35,650 But they guarantee precision will use them when we can't allow any approximations. 118 00:09:35,880 --> 00:09:43,320 For example, when representing money points in games or other human made concepts that we need to represent 119 00:09:43,320 --> 00:09:44,160 precisely. 120 00:09:44,730 --> 00:09:50,880 Let's summarize the topic of double versus decimal doubles are optimized for performance. 121 00:09:51,120 --> 00:09:53,520 Decimals are optimized for precision. 122 00:09:54,120 --> 00:09:57,660 Decimals have much worse performance than doubles. 123 00:09:58,140 --> 00:10:04,260 They also have a smaller range, so they are not good for representing really small or really large 124 00:10:04,260 --> 00:10:04,980 numbers. 125 00:10:05,370 --> 00:10:09,450 Because of that, decimals shall be used when we care about precision. 126 00:10:09,540 --> 00:10:15,870 For example, when we want to compare two sums of money and tell if they are exactly equal or not, 127 00:10:16,230 --> 00:10:18,780 doubles are less precise, but faster. 128 00:10:19,050 --> 00:10:25,230 They are perfect for representing numbers that are not human made, but rather come from nature or physics, 129 00:10:25,410 --> 00:10:31,140 like the speed of a car or the length of a wave when talking to doubles for equality. 130 00:10:31,380 --> 00:10:35,520 We should only check if they are close to each other within some tolerance. 131 00:10:36,180 --> 00:10:42,420 During the interview, you can hear questions like What is the difference between double and float? 132 00:10:42,870 --> 00:10:49,980 The only difference is that double occupies 64 bits of memory, while float occupies 42, giving double 133 00:10:49,980 --> 00:10:51,150 our larger range. 134 00:10:51,510 --> 00:10:54,240 Except for that, they work exactly the same. 135 00:10:54,780 --> 00:11:01,980 What is the and then in the end, the means not a number, and it's a special value that double or float 136 00:11:01,980 --> 00:11:02,580 can be. 137 00:11:02,850 --> 00:11:10,020 It's reserved for representing results of undefined mathematical operations like dividing zero by zero 138 00:11:10,020 --> 00:11:12,150 or infinity by infinity. 139 00:11:12,720 --> 00:11:16,050 What numeric type should we use to represent money? 140 00:11:16,620 --> 00:11:20,100 What we present in money, we should always use decimals. 141 00:11:20,610 --> 00:11:26,670 All right, that's it for this lecture and in general, the topic of representing numbers. 142 00:11:27,000 --> 00:11:29,760 Thanks for watching and see you in the next lecture.