Double vs Decimal in C# - Why They Are Different and How They Work
Read full transcript 21 segments
-
In C# both a double and decimal will In C# both a double and decimal will hold what appears to be the same value. hold what appears to be the same value. hold what appears to be the same value. But these are two very different But these are two very different But these are two very different variable types. So let's understand why variable types. So let's understand why variable types. So let's understand why do you use one over the other as well as do you use one over the other as well as do you use one over the other as well as how both get stored. Now videos like how both get stored. Now videos like how both get stored. Now videos like these are paid for by people who these are paid for by people who these are paid for by people who purchase training at imtimcorey.com. purchase training at imtimcorey.com. purchase training at imtimcorey.com. If you're looking to improve your If you're looking to improve your If you're looking to improve your development skills, keep up with AI, or development skills, keep up with AI, or development skills, keep up with AI, or even just learn to have fun building even just learn to have fun building even just learn to have fun building games, head over to imtimcorey.com games, head over to imtimcorey.com games, head over to imtimcorey.com and check it out. And if you want the and check it out. And if you want the and check it out. And if you want the source code from today's video, click source code from today's video, click source code from today's video, click the link in the description. Finally, if the link in the description. Finally, if the link in the description. Finally, if you want to see something specific you want to see something specific you want to see something specific covered either here or in a paid course, covered either here or in a paid course, covered either here or in a paid course, head over to the newly revamped head over to the newly revamped head over to the newly revamped suggestion sites at suggestion sites at suggestion sites at suggestions.imtimcorey.com suggestions.imtimcorey.com suggestions.imtimcorey.com to vote on upcoming topics. Okay, so to vote on upcoming topics. Okay, so to vote on upcoming topics. Okay, so this I already have the code written for this I already have the code written for this I already have the code written for this this um video. this this um video. this this um video. And there's a lot here, okay? So we're And there's a lot here, okay? So we're And there's a lot here, okay? So we're going to go through a lot and a lot of going to go through a lot and a lot of going to go through a lot and a lot of this is just showing you what's this is just showing you what's this is just showing you what's happening, okay? So a lot here, I'll happening, okay? So a lot here, I'll happening, okay? So a lot here, I'll give you the source code again, that's give you the source code again, that's give you the source code again, that's linked down in the description, but I linked down in the description, but I linked down in the description, but I want to run through this and talk about want to run through this and talk about want to run through this and talk about double versus decimal. And here's the double versus decimal. And here's the double versus decimal. And here's the basic example that everyone sees and basic example that everyone sees and basic example that everyone sees and freaks out about. So the basic example freaks out about. So the basic example freaks out about. So the basic example is 0.1 + 0.2. If we're in double, we get is 0.1 + 0.2. If we're in double, we get is 0.1 + 0.2. If we're in double, we get 0.30000 0.30000 0.30000 for a number of them and then four. As for a number of them and then four. As for a number of them and then four. As but in decimal, we get 0.1 + 0.2 = 0.3, but in decimal, we get 0.1 + 0.2 = 0.3, but in decimal, we get 0.1 + 0.2 = 0.3, which is precisely what we're expecting.
-
which is precisely what we're expecting. which is precisely what we're expecting. Now you may look at this and go, "Well, Now you may look at this and go, "Well, Now you may look at this and go, "Well, you know, 0.1 + 0.2, why isn't that just you know, 0.1 + 0.2, why isn't that just you know, 0.1 + 0.2, why isn't that just 0.3? Why does it have all this extra 0.3? Why does it have all this extra 0.3? Why does it have all this extra stuff?" We're going to understand that stuff?" We're going to understand that stuff?" We're going to understand that in this video and understand why even in this video and understand why even in this video and understand why even though you get a little bit of though you get a little bit of though you get a little bit of imprecision here, imprecision here, imprecision here, we still want to use double almost all we still want to use double almost all we still want to use double almost all the time when we need a number that has the time when we need a number that has the time when we need a number that has a decimal point it. a decimal point it. a decimal point it. So, let's start off with understanding So, let's start off with understanding So, let's start off with understanding how a decimal is stored. This is the how a decimal is stored. This is the how a decimal is stored. This is the decimal type. decimal type. decimal type. Now, this is going to get a bit Now, this is going to get a bit Now, this is going to get a bit technical, but think of this like the um technical, but think of this like the um technical, but think of this like the um computer science education you kind of computer science education you kind of computer science education you kind of missed maybe. So, missed maybe. So, missed maybe. So, a decimal is stored in 128 bits. a decimal is stored in 128 bits. a decimal is stored in 128 bits. 96 bits, okay, right here, 96 bits 96 bits, okay, right here, 96 bits 96 bits, okay, right here, 96 bits represent an integer. represent an integer. represent an integer. An integer is a whole number. An integer is a whole number. An integer is a whole number. One bit represents the sign. Now, a sign One bit represents the sign. Now, a sign One bit represents the sign. Now, a sign is either positive or negative.
-
is either positive or negative. is either positive or negative. And then eight scale bits. And then eight scale bits. And then eight scale bits. So, this is the scale. We're going to So, this is the scale. We're going to So, this is the scale. We're going to see that in just a minute. And a scale see that in just a minute. And a scale see that in just a minute. And a scale is from 0 to 28. This is um how to move is from 0 to 28. This is um how to move is from 0 to 28. This is um how to move the decimal point around. the decimal point around. the decimal point around. And then finally, there is 16 reserved And then finally, there is 16 reserved And then finally, there is 16 reserved bits. So, all of this adds up together bits. So, all of this adds up together bits. So, all of this adds up together to um to um to um to be our 128 bits. to be our 128 bits. to be our 128 bits. So, I think actually I'm missing a So, I think actually I'm missing a So, I think actually I'm missing a couple of bits here. Um couple of bits here. Um couple of bits here. Um it's more than 16 bits. it's more than 16 bits. it's more than 16 bits. There's some extra. 16 plus all the There's some extra. 16 plus all the There's some extra. 16 plus all the rest, okay? There's actually two rest, okay? There's actually two rest, okay? There's actually two different places of reservation. different places of reservation. different places of reservation. So, in any event, So, in any event, So, in any event, let's figure out how this is actually let's figure out how this is actually let's figure out how this is actually stored on disk. Because remember, when a stored on disk. Because remember, when a stored on disk. Because remember, when a computer stores data, it stores it in computer stores data, it stores it in computer stores data, it stores it in bits. And what are bits? Bits are ones bits. And what are bits? Bits are ones bits. And what are bits? Bits are ones and zeros. and zeros. and zeros. So, So, So, we have, you know, 1 1 0 1. You know, we have, you know, 1 1 0 1. You know, we have, you know, 1 1 0 1. You know, like that's four bits. Okay? So, like that's four bits. Okay? So, like that's four bits. Okay? So, let's see how we're storing a decimal let's see how we're storing a decimal let's see how we're storing a decimal number number number on the hard drive or in memory, on the hard drive or in memory, on the hard drive or in memory, actually.
-
actually. actually. So, if we're going to store 1.123.45 as a decimal, you would store 1 2 3 4 5. as a decimal, you would store 1 2 3 4 5. Okay, that's the integer we're storing. Okay, that's the integer we're storing. Okay, that's the integer we're storing. Now, notice we just dropped decimal Now, notice we just dropped decimal Now, notice we just dropped decimal entirely. entirely. entirely. Now, we also store the sign, again, Now, we also store the sign, again, Now, we also store the sign, again, positive or negative. We store zero for positive or negative. We store zero for positive or negative. We store zero for positive, meaning there's no there's no positive, meaning there's no there's no positive, meaning there's no there's no minus sign here. minus sign here. minus sign here. And that goes in the flags area. And that goes in the flags area. And that goes in the flags area. Also in the flags area, we store a scale Also in the flags area, we store a scale Also in the flags area, we store a scale of two. What does two mean? Well, notice of two. What does two mean? Well, notice of two. What does two mean? Well, notice that the the decimal point here, we that the the decimal point here, we that the the decimal point here, we moved it one two places and then put it moved it one two places and then put it moved it one two places and then put it right here. right here. right here. So, you move it two, therefore the scale So, you move it two, therefore the scale So, you move it two, therefore the scale is two. is two. is two. So, now this results in 1 2 3 4 5, this So, now this results in 1 2 3 4 5, this So, now this results in 1 2 3 4 5, this is the formula to get back to the is the formula to get back to the is the formula to get back to the number, 1 2 3 4 5 * 10 to the power of number, 1 2 3 4 5 * 10 to the power of number, 1 2 3 4 5 * 10 to the power of -2. -2. -2. Okay? So, the this is the scale right Okay? So, the this is the scale right Okay? So, the this is the scale right here. So, right there is the scale, I here. So, right there is the scale, I here. So, right there is the scale, I set two, we just put a negative in front set two, we just put a negative in front set two, we just put a negative in front of it, and then we have of it, and then we have of it, and then we have no negative sign up front because we had no negative sign up front because we had no negative sign up front because we had this was a positive, therefore we don't this was a positive, therefore we don't this was a positive, therefore we don't do anything, and we have our integer do anything, and we have our integer do anything, and we have our integer here.
-
here. here. So, 1 2 3 4 5, or 12,345, So, 1 2 3 4 5, or 12,345, So, 1 2 3 4 5, or 12,345, * 10 to the negative second power. That * 10 to the negative second power. That * 10 to the negative second power. That results in 1 results in 1 results in 1 123.45. 123.45. 123.45. Okay. So, here's another example. Okay. So, here's another example. Okay. So, here's another example. -123.45. -123.45. -123.45. Well, it's all the same, pretty much. Well, it's all the same, pretty much. Well, it's all the same, pretty much. It's 1 2 3 4 5 is stored as an integer. It's 1 2 3 4 5 is stored as an integer. It's 1 2 3 4 5 is stored as an integer. The scale is still two, we are still The scale is still two, we are still The scale is still two, we are still moving that decimal point two to the moving that decimal point two to the moving that decimal point two to the right. right. right. And the sign is what changed, it's a And the sign is what changed, it's a And the sign is what changed, it's a one. one. one. So, now it's -1 2 3 4 5 * 10 to the So, now it's -1 2 3 4 5 * 10 to the So, now it's -1 2 3 4 5 * 10 to the power of -2. power of -2. power of -2. So, notice here, this sign right here is So, notice here, this sign right here is So, notice here, this sign right here is not about not about not about this sign, they're two different signs, this sign, they're two different signs, this sign, they're two different signs, okay? okay? okay? The one to the right, the power, that's The one to the right, the power, that's The one to the right, the power, that's based upon how many decimal points you based upon how many decimal points you based upon how many decimal points you move to the right. move to the right. move to the right. Okay. So, let's do one more. Okay. So, let's do one more. Okay. So, let's do one more. So, we have So, we have So, we have 0.1 0.1 0.1 as a decimal. Well, we're storing one as a decimal. Well, we're storing one as a decimal. Well, we're storing one >> [clears throat] >> [clears throat] >> [clears throat] >> in the value position.
-
>> in the value position. >> in the value position. We're storing zero because of the We're storing zero because of the We're storing zero because of the positive in the sign position, and we're positive in the sign position, and we're positive in the sign position, and we're the scale is one. Why? Because we moved the scale is one. Why? Because we moved the scale is one. Why? Because we moved this decimal one to the right. So, 1 * this decimal one to the right. So, 1 * this decimal one to the right. So, 1 * 10 to the -1. Okay? And so, you you 10 to the -1. Okay? And so, you you 10 to the -1. Okay? And so, you you might be saying, "Man, Thomas, there's a might be saying, "Man, Thomas, there's a might be saying, "Man, Thomas, there's a lot of math here. Why are we doing all lot of math here. Why are we doing all lot of math here. Why are we doing all of this?" of this?" of this?" I want you to understand how it's I want you to understand how it's I want you to understand how it's stored, okay? Now, you don't have to stored, okay? Now, you don't have to stored, okay? Now, you don't have to remember all of this. Again, you got the remember all of this. Again, you got the remember all of this. Again, you got the source code if you want it, but I want source code if you want it, but I want source code if you want it, but I want to at least get you an understanding of to at least get you an understanding of to at least get you an understanding of how this works. how this works. how this works. Because now, let's talk about double. Because now, let's talk about double. Because now, let's talk about double. Double is stored differently. Double is stored differently. Double is stored differently. We're storing one sign bit, so that's We're storing one sign bit, so that's We're storing one sign bit, so that's again positive or negative, again positive or negative, again positive or negative, zero being positive, one being negative, zero being positive, one being negative, zero being positive, one being negative, 11 exponent bits, and 52 significands 11 exponent bits, and 52 significands 11 exponent bits, and 52 significands that are bits, okay? This is the that are bits, okay? This is the that are bits, okay? This is the fraction. fraction. fraction. Now, what does this really mean? Let's Now, what does this really mean? Let's Now, what does this really mean? Let's look at an example, because it's getting look at an example, because it's getting look at an example, because it's getting a bit more complicated, but I will tell a bit more complicated, but I will tell a bit more complicated, but I will tell you this is faster. Okay. So, we're you this is faster. Okay. So, we're you this is faster. Okay. So, we're talking about double now, which does talking about double now, which does talking about double now, which does have that rounding error. We're going to have that rounding error. We're going to have that rounding error. We're going to see why.
-
see why. see why. >> [snorts] >> [snorts] >> [snorts] >> So, let's look at a binary >> So, let's look at a binary >> So, let's look at a binary representation of 0.1. representation of 0.1. representation of 0.1. So, binary, the representation of 0.1 is So, binary, the representation of 0.1 is So, binary, the representation of 0.1 is this number right here. this number right here. this number right here. So, 0. And I've got 0001 So, 0. And I've got 0001 So, 0. And I've got 0001 1001 1001 1001 1001. 1001. 1001. And we got the repeating pattern here. And we got the repeating pattern here. And we got the repeating pattern here. Why? Well, Why? Well, Why? Well, remember that we're storing in binary. remember that we're storing in binary. remember that we're storing in binary. Binary is ones and zeros. Binary is ones and zeros. Binary is ones and zeros. Well, how do you store point one, which Well, how do you store point one, which Well, how do you store point one, which is a fraction? And that fraction isn't is a fraction? And that fraction isn't is a fraction? And that fraction isn't easily divisible easily divisible easily divisible between a one and a zero. between a one and a zero. between a one and a zero. It's kind of like in base 10 or how we It's kind of like in base 10 or how we It's kind of like in base 10 or how we normally think of numbers, if we were to normally think of numbers, if we were to normally think of numbers, if we were to try and calculate what is 1 / 3. try and calculate what is 1 / 3. try and calculate what is 1 / 3. Well, that equals 0. Well, that equals 0. Well, that equals 0. 3 3 3 3 3 3 3 3 3 3 repeating, right? Like we can never 3 repeating, right? Like we can never 3 repeating, right? Like we can never stop saying three and eventually we say, stop saying three and eventually we say, stop saying three and eventually we say, well, either we say four, which isn't well, either we say four, which isn't well, either we say four, which isn't quite right because we're not we're quite right because we're not we're quite right because we're not we're rounding up from a three, or we just rounding up from a three, or we just rounding up from a three, or we just um end it right here.
-
um end it right here. um end it right here. But our our But our our But our our division just kind of or our yeah, our division just kind of or our yeah, our division just kind of or our yeah, our division ends up at this repeating division ends up at this repeating division ends up at this repeating number. So, we have this repeating number. So, we have this repeating number. So, we have this repeating pattern pattern pattern as well in our point one. We got this as well in our point one. We got this as well in our point one. We got this 1001 1001 1001 1001 1001 that just keeps going. Now, I 1001 1001 that just keeps going. Now, I 1001 1001 that just keeps going. Now, I put spaces in here to try and make this put spaces in here to try and make this put spaces in here to try and make this a little easier to read, but obviously a little easier to read, but obviously a little easier to read, but obviously the binary doesn't actually have spaces. the binary doesn't actually have spaces. the binary doesn't actually have spaces. So, to store a 0.1 as a double, you So, to store a 0.1 as a double, you So, to store a 0.1 as a double, you would get a sign of zero because it's would get a sign of zero because it's would get a sign of zero because it's positive. positive. positive. We get We get We get 1019 1019 1019 stored in the exponent area, okay? stored in the exponent area, okay? stored in the exponent area, okay? Because what we're actually looking for Because what we're actually looking for Because what we're actually looking for is and let's let's is and let's let's is and let's let's um look at this count here. We need to um look at this count here. We need to um look at this count here. We need to move this decimal point to the move this decimal point to the move this decimal point to the till it's one after the one, okay? So, till it's one after the one, okay? So, till it's one after the one, okay? So, we're going to put it right we're going to put it right we're going to put it right here. here. here. >> [snorts] >> [snorts] >> [snorts] >> Which means we have to move it one two >> Which means we have to move it one two >> Which means we have to move it one two three four positions to the right. Which three four positions to the right. Which three four positions to the right. Which means -4.
-
means -4. means -4. We take that -4, we add We take that -4, we add We take that -4, we add 1023. 1023. 1023. You may say that's an arbitrary number. You may say that's an arbitrary number. You may say that's an arbitrary number. Well, yes and no. What that number Well, yes and no. What that number Well, yes and no. What that number represents is the very middle of the represents is the very middle of the represents is the very middle of the overall binary value for a a double. overall binary value for a a double. overall binary value for a a double. So, it's right in the middle. So, it's right in the middle. So, it's right in the middle. So, what that means is we have 0 to 123, So, what that means is we have 0 to 123, So, what that means is we have 0 to 123, 123 to whatever the the value is in the 123 to whatever the the value is in the 123 to whatever the the value is in the top. But, that puts it in the middle. top. But, that puts it in the middle. top. But, that puts it in the middle. That way it can it can figure out That way it can it can figure out That way it can it can figure out positives and negatives for the sign for positives and negatives for the sign for positives and negatives for the sign for the exponent. the exponent. the exponent. Now, why we have a sign the exponent we Now, why we have a sign the exponent we Now, why we have a sign the exponent we want to also have a sign up here. want to also have a sign up here. want to also have a sign up here. Because we need to know we're moving to Because we need to know we're moving to Because we need to know we're moving to the right or to the left. Because the the right or to the left. Because the the right or to the left. Because the decimal point could be with a move to decimal point could be with a move to decimal point could be with a move to the left. We're going to see that in a the left. We're going to see that in a the left. We're going to see that in a bit in a minute. But, for right now bit in a minute. But, for right now bit in a minute. But, for right now we're moving -4. So, -4 + 123 gives us we're moving -4. So, -4 + 123 gives us we're moving -4. So, -4 + 123 gives us 119. 119. 119. Now, we need to calculate the fraction. Now, we need to calculate the fraction. Now, we need to calculate the fraction. So, the fraction is everything after So, the fraction is everything after So, the fraction is everything after this this this this dividing line. We put the decimal this dividing line. We put the decimal this dividing line. We put the decimal point here. So, everything after this we point here. So, everything after this we point here. So, everything after this we need to have 52 places where I capture.
-
need to have 52 places where I capture. need to have 52 places where I capture. Which let's do some counting here. So, Which let's do some counting here. So, Which let's do some counting here. So, we got four we got four we got four + 8 that is 12. Then we have 20. Check + 8 that is 12. Then we have 20. Check + 8 that is 12. Then we have 20. Check our math here. Um our math here. Um our math here. Um 28, then 36, then 44. 28, then 36, then 44. 28, then 36, then 44. Then we have 52. So, we end right here. Then we have 52. So, we end right here. Then we have 52. So, we end right here. Okay? So, all of this from from this Okay? So, all of this from from this Okay? So, all of this from from this left mark to this right mark left mark to this right mark left mark to this right mark all of that is the fraction that we're all of that is the fraction that we're all of that is the fraction that we're going to store. But again, this is a going to store. But again, this is a going to store. But again, this is a repeating number. So, what do you do repeating number. So, what do you do repeating number. So, what do you do when you have a repeating number? when you have a repeating number? when you have a repeating number? You look at the position right after the You look at the position right after the You look at the position right after the repetition or right right after you're repetition or right right after you're repetition or right right after you're done and say, "Okay, we have a one done and say, "Okay, we have a one done and say, "Okay, we have a one here." Remember, it's ones and zeros here." Remember, it's ones and zeros here." Remember, it's ones and zeros that all there is. that all there is. that all there is. So, do we need to round? Well, yes we do So, do we need to round? Well, yes we do So, do we need to round? Well, yes we do because this is a one. because this is a one. because this is a one. So, we add one to this spot right here So, we add one to this spot right here So, we add one to this spot right here which gives us zero, carry the one which gives us zero, carry the one which gives us zero, carry the one and one. So, the last four numbers is 1 and one. So, the last four numbers is 1 and one. So, the last four numbers is 1 0 0 0 1 0 instead of 1 0 0 1 because of the 1 0 instead of 1 0 0 1 because of the 1 0 instead of 1 0 0 1 because of the round.
-
round. round. So, that's this right here. So, that's this right here. So, that's this right here. So, this whole thing is the fraction. So, this whole thing is the fraction. So, this whole thing is the fraction. That results in this number. That results in this number. That results in this number. 1.1 I'm not going to read the whole 1.1 I'm not going to read the whole 1.1 I'm not going to read the whole thing. thing. thing. Um Um Um times two binary times two binary times two binary to the power of -4, to the power of -4, to the power of -4, okay? okay? okay? So, So, So, again, the reason why it says -4 here is again, the reason why it says -4 here is again, the reason why it says -4 here is because it did the calculation because it did the calculation because it did the calculation backwards. We stored 1 0 1 9. It knows backwards. We stored 1 0 1 9. It knows backwards. We stored 1 0 1 9. It knows that 1 0 2 3 is the the middle point and that 1 0 2 3 is the the middle point and that 1 0 2 3 is the the middle point and so it's okay. so it's okay. so it's okay. 1 0 1 9 minus 1 0 1 3 is -4. Therefore, 1 0 1 9 minus 1 0 1 3 is -4. Therefore, 1 0 1 9 minus 1 0 1 3 is -4. Therefore, that's our exponent. that's our exponent. that's our exponent. So, 2 to the power of -4 So, 2 to the power of -4 So, 2 to the power of -4 times this number right here. That gives times this number right here. That gives times this number right here. That gives us us us in theory, 0.1 but it's not quite 0.1 in theory, 0.1 but it's not quite 0.1 in theory, 0.1 but it's not quite 0.1 because of this rounding right here, because of this rounding right here, because of this rounding right here, okay? We rounded here. We ended up with okay? We rounded here. We ended up with okay? We rounded here. We ended up with the 1 0 instead of 0 1.
-
the 1 0 instead of 0 1. the 1 0 instead of 0 1. That's where we get a little bit of the That's where we get a little bit of the That's where we get a little bit of the the round at that last place. the round at that last place. the round at that last place. Okay? Okay? Okay? So, that's how we store 0.1. Let's do So, that's how we store 0.1. Let's do So, that's how we store 0.1. Let's do -0.1. Well, it's just the sign. -0.1. Well, it's just the sign. -0.1. Well, it's just the sign. That's really the only thing that That's really the only thing that That's really the only thing that changes. changes. changes. So, when we look at the the storage So, when we look at the the storage So, when we look at the the storage here, we see the sign is is one because here, we see the sign is is one because here, we see the sign is is one because it's negative. it's negative. it's negative. But, we still have the -4. So, it's But, we still have the -4. So, it's But, we still have the -4. So, it's still the 1 0 1 9. We still have the still the 1 0 1 9. We still have the still the 1 0 1 9. We still have the same uh fraction. same uh fraction. same uh fraction. So, we still get the times 2 to the -4 So, we still get the times 2 to the -4 So, we still get the times 2 to the -4 power. But, our number here, we start power. But, our number here, we start power. But, our number here, we start with a negative one or negative the with a negative one or negative the with a negative one or negative the beginning. I also want to point out, beginning. I also want to point out, beginning. I also want to point out, we're starting with a one here, we're starting with a one here, we're starting with a one here, but that's not actually stored in the in but that's not actually stored in the in but that's not actually stored in the in the actual double. Like we have the the the actual double. Like we have the the the actual double. Like we have the the sign of 01, we have the 1019 for the sign of 01, we have the 1019 for the sign of 01, we have the 1019 for the exponent, we have the fraction, but the exponent, we have the fraction, but the exponent, we have the fraction, but the fraction doesn't contain this first one.
-
fraction doesn't contain this first one. fraction doesn't contain this first one. This is actually a bonus value we get This is actually a bonus value we get This is actually a bonus value we get because we can assume that since we because we can assume that since we because we can assume that since we moved this decimal point over until it's moved this decimal point over until it's moved this decimal point over until it's right here, right here, right here, between the after the first one, we can between the after the first one, we can between the after the first one, we can assume this one exists. assume this one exists. assume this one exists. So therefore, we can add this is So therefore, we can add this is So therefore, we can add this is actually the 53rd bit. actually the 53rd bit. actually the 53rd bit. Okay? So it's kind of a a bonus value Okay? So it's kind of a a bonus value Okay? So it's kind of a a bonus value there, just a little bit of extra. there, just a little bit of extra. there, just a little bit of extra. Okay. So that's how you represent -0.1 Okay. So that's how you represent -0.1 Okay. So that's how you represent -0.1 in for in binary for double. in for in binary for double. in for in binary for double. Okay. Okay. Okay. Let's look at one more. Let's look at Let's look at one more. Let's look at Let's look at one more. Let's look at 123.45. 123.45. 123.45. Well, Well, Well, first of all, we have numbers to the first of all, we have numbers to the first of all, we have numbers to the left of the decimal point. So guess left of the decimal point. So guess left of the decimal point. So guess what? We need to count how many. 1 2 3 4 what? We need to count how many. 1 2 3 4 what? We need to count how many. 1 2 3 4 5 6. We're going to put the decimal 5 6. We're going to put the decimal 5 6. We're going to put the decimal point right there. point right there. point right there. So therefore, we put um six here and add So therefore, we put um six here and add So therefore, we put um six here and add it to 1023 and get 1029. Again, we're a it to 1023 and get 1029. Again, we're a it to 1023 and get 1029. Again, we're a positive, so zero. And then the positive, so zero. And then the positive, so zero. And then the remaining actually starts from our remaining actually starts from our remaining actually starts from our decimal, so it starts right here and it decimal, so it starts right here and it decimal, so it starts right here and it goes this direction. Okay? So we're not goes this direction. Okay? So we're not goes this direction. Okay? So we're not going to go through the whole thing going to go through the whole thing going to go through the whole thing here, but that's that's our here, but that's that's our here, but that's that's our one dot. Again, that one is a bonus. And one dot. Again, that one is a bonus. And one dot. Again, that one is a bonus. And we have our remainder.
-
we have our remainder. we have our remainder. We multiply that by two to the sixth We multiply that by two to the sixth We multiply that by two to the sixth power. And that gives us back our number power. And that gives us back our number power. And that gives us back our number or close to our number for that double or close to our number for that double or close to our number for that double value. value. value. Okay. So this is how decimal versus Okay. So this is how decimal versus Okay. So this is how decimal versus double stores the information in binary. double stores the information in binary. double stores the information in binary. Again, computers work with binary, ones Again, computers work with binary, ones Again, computers work with binary, ones and zeros, in their memory. So, when and zeros, in their memory. So, when and zeros, in their memory. So, when you're talking about storing you're talking about storing you're talking about storing information, you're not storing the information, you're not storing the information, you're not storing the actual decimal number. You're not actual decimal number. You're not actual decimal number. You're not storing this, storing this, storing this, what you're storing is is well, like what you're storing is is well, like what you're storing is is well, like this. It's actually this. It's actually this. It's actually like this data right here, which like this data right here, which like this data right here, which represents this. represents this. represents this. So, So, So, it's a little different. So, that's why it's a little different. So, that's why it's a little different. So, that's why when we're storing decimal numbers, it's when we're storing decimal numbers, it's when we're storing decimal numbers, it's kind of hard because binary doesn't kind of hard because binary doesn't kind of hard because binary doesn't just, you know, put a dot. You got to just, you know, put a dot. You got to just, you know, put a dot. You got to convert from base 10 to base 2, and that convert from base 10 to base 2, and that convert from base 10 to base 2, and that causes some fractional issues. causes some fractional issues. causes some fractional issues. And they do things differently because And they do things differently because And they do things differently because decimal actually stores an integer on decimal actually stores an integer on decimal actually stores an integer on the hard drive, and then it moves the the hard drive, and then it moves the the hard drive, and then it moves the decimal point around. Whereas, double decimal point around. Whereas, double decimal point around. Whereas, double actually tries to store a value that actually tries to store a value that actually tries to store a value that represents the the decimal number, represents the the decimal number, represents the the decimal number, the the number that it has a decimal the the number that it has a decimal the the number that it has a decimal point, uh which is a double. Um but, point, uh which is a double. Um but, point, uh which is a double. Um but, because of that, we have some rounding because of that, we have some rounding because of that, we have some rounding differences in at in doubles.
-
differences in at in doubles. differences in at in doubles. Okay. Okay. Okay. So, let's also talk about size So, let's also talk about size So, let's also talk about size differences. differences. differences. A double takes up 8 bytes. A double takes up 8 bytes. A double takes up 8 bytes. Remember, a a bit Oh, I'm sorry, 1 byte Remember, a a bit Oh, I'm sorry, 1 byte Remember, a a bit Oh, I'm sorry, 1 byte is 8 bits. is 8 bits. is 8 bits. So, 64 bits go into a double, whereas So, 64 bits go into a double, whereas So, 64 bits go into a double, whereas 128 bits go into a decimal. Takes up 128 bits go into a decimal. Takes up 128 bits go into a decimal. Takes up twice the size to store twice the size to store twice the size to store the the information about a decimal. So, the the information about a decimal. So, the the information about a decimal. So, if we had 10 million values, a we'd have if we had 10 million values, a we'd have if we had 10 million values, a we'd have doubles take up 76 megabytes, whereas doubles take up 76 megabytes, whereas doubles take up 76 megabytes, whereas decimals would take up 153 megabytes. decimals would take up 153 megabytes. decimals would take up 153 megabytes. Now, do you have 10 million variables Now, do you have 10 million variables Now, do you have 10 million variables for doubles versus decimals? Maybe. Um for doubles versus decimals? Maybe. Um for doubles versus decimals? Maybe. Um but, but, but, you know, either way, it just shows the you know, either way, it just shows the you know, either way, it just shows the size difference. Yes, this is tiny when size difference. Yes, this is tiny when size difference. Yes, this is tiny when you're talking about one or two, but you're talking about one or two, but you're talking about one or two, but there are other reasons why we should there are other reasons why we should there are other reasons why we should think about double versus decimal think about double versus decimal think about double versus decimal differently. differently. differently. So, let's talk about the speed. So, a So, let's talk about the speed. So, a So, let's talk about the speed. So, a double, double, double, um and I should have labeled this um and I should have labeled this um and I should have labeled this better, but the double takes 23 better, but the double takes 23 better, but the double takes 23 milliseconds to milliseconds to milliseconds to um do calculations on that 10 million um um do calculations on that 10 million um um do calculations on that 10 million um doubles versus decimal, same 10 million doubles versus decimal, same 10 million doubles versus decimal, same 10 million uh numbers, but does calculations on as uh numbers, but does calculations on as uh numbers, but does calculations on as a decimal, it takes 210 milliseconds.
-
a decimal, it takes 210 milliseconds. a decimal, it takes 210 milliseconds. Almost an order of magnitude different Almost an order of magnitude different Almost an order of magnitude different in terms of speed. Okay? in terms of speed. Okay? in terms of speed. Okay? Then we're talking about the overall Then we're talking about the overall Then we're talking about the overall benefits. benefits. benefits. So, the overall benefits of a double So, the overall benefits of a double So, the overall benefits of a double comes down to speed. They're a lot comes down to speed. They're a lot comes down to speed. They're a lot faster. faster. faster. They take up less memory size. They have They take up less memory size. They have They take up less memory size. They have a massive range compared to decimal. So, a massive range compared to decimal. So, a massive range compared to decimal. So, the the amount of uh space inside of a the the amount of uh space inside of a the the amount of uh space inside of a double is so much larger than decimal. double is so much larger than decimal. double is so much larger than decimal. And then finally, And then finally, And then finally, CPUs, almost every CPU has uh CPUs, almost every CPU has uh CPUs, almost every CPU has uh floating-point optimization or floating-point optimization or floating-point optimization or floating-point processing in there that floating-point processing in there that floating-point processing in there that allows for some significant speed allows for some significant speed allows for some significant speed advantages for processing things like advantages for processing things like advantages for processing things like add, subtract, divide, multiply with add, subtract, divide, multiply with add, subtract, divide, multiply with doubles doubles doubles compared to decimals. compared to decimals. compared to decimals. So, So, So, doubles are faster. Doubles are smaller. doubles are faster. Doubles are smaller. doubles are faster. Doubles are smaller. Doubles have a larger range. Doubles have a larger range. Doubles have a larger range. So, what why do we even have decimals? So, what why do we even have decimals? So, what why do we even have decimals? Precision. Precision. Precision. So, one way to talk about this is um So, one way to talk about this is um So, one way to talk about this is um if you're talking about money, you want if you're talking about money, you want if you're talking about money, you want to be very precise, right? You don't to be very precise, right? You don't to be very precise, right? You don't want your bank account to have money want your bank account to have money want your bank account to have money drift out of it over time. You want to drift out of it over time. You want to drift out of it over time. You want to be very precise.
-
be very precise. be very precise. So, when you're talking about things So, when you're talking about things So, when you're talking about things like how fast did my car go? Well, like how fast did my car go? Well, like how fast did my car go? Well, you're measuring that, but are you are you're measuring that, but are you are you're measuring that, but are you are you super precise? Are you precise down you super precise? Are you precise down you super precise? Are you precise down to the 10,000 decimal point? No, you're to the 10,000 decimal point? No, you're to the 10,000 decimal point? No, you're you're saying it went 60 miles an hour. you're saying it went 60 miles an hour. you're saying it went 60 miles an hour. Well, that's a rounding issue, actually. Well, that's a rounding issue, actually. Well, that's a rounding issue, actually. Well, for those kind of things, double Well, for those kind of things, double Well, for those kind of things, double works just fine, which also is why you works just fine, which also is why you works just fine, which also is why you find double in so many games, because if find double in so many games, because if find double in so many games, because if we're talking about, you know, what we're talking about, you know, what we're talking about, you know, what angle to go at, use double. Because yes, angle to go at, use double. Because yes, angle to go at, use double. Because yes, it's not exact, but it's really close. it's not exact, but it's really close. it's not exact, but it's really close. And that's good enough, because of the And that's good enough, because of the And that's good enough, because of the fact we have CPU optimization, the fact we have CPU optimization, the fact we have CPU optimization, the smaller memory size, the faster smaller memory size, the faster smaller memory size, the faster processing. So, what are the drawbacks? processing. So, what are the drawbacks? processing. So, what are the drawbacks? Well, for double, you get some rounding. Well, for double, you get some rounding. Well, for double, you get some rounding. Again, in the very beginning we talked Again, in the very beginning we talked Again, in the very beginning we talked about, we saw that that simple example about, we saw that that simple example about, we saw that that simple example where .1 + .2 = .3000000004. Okay? Uh because there's a little bit of Okay? Uh because there's a little bit of rounding. rounding. rounding. But, But, But, that's we we give up a little bit of that's we we give up a little bit of that's we we give up a little bit of precision for the speed, memory size, precision for the speed, memory size, precision for the speed, memory size, massive range, and the optimizations.
-
massive range, and the optimizations. massive range, and the optimizations. What are the drawbacks of decimal? Well, What are the drawbacks of decimal? Well, What are the drawbacks of decimal? Well, it's they're slower. They take up more it's they're slower. They take up more it's they're slower. They take up more memory, and there's a smaller range of memory, and there's a smaller range of memory, and there's a smaller range of numbers in 7 decimal. numbers in 7 decimal. numbers in 7 decimal. So, when you're talking about decimals, So, when you're talking about decimals, So, when you're talking about decimals, when it comes to processing, again, when it comes to processing, again, when it comes to processing, again, there's CPU optimization for doubles, there's CPU optimization for doubles, there's CPU optimization for doubles, there's not for decimals. So, the CPU there's not for decimals. So, the CPU there's not for decimals. So, the CPU was doing multiple steps was doing multiple steps was doing multiple steps of manual processing to figure out a of manual processing to figure out a of manual processing to figure out a decimal and figure out the operations decimal and figure out the operations decimal and figure out the operations for a decimal, versus possibly even one for a decimal, versus possibly even one for a decimal, versus possibly even one step for a double. Okay? So, when to step for a double. Okay? So, when to step for a double. Okay? So, when to use? For doubles, almost all use? For doubles, almost all use? For doubles, almost all calculations. If you're not using cal- calculations. If you're not using cal- calculations. If you're not using cal- if you're not using calculation, if if you're not using calculation, if if you're not using calculation, if you're trying to um you're trying to um you're trying to um you know, measure something, you're you know, measure something, you're you know, measure something, you're almost always using double. Uh again, almost always using double. Uh again, almost always using double. Uh again, think about this. When you're measuring think about this. When you're measuring think about this. When you're measuring speed, it's miles per hour. We're not speed, it's miles per hour. We're not speed, it's miles per hour. We're not talking about super fractional things. talking about super fractional things. talking about super fractional things. And even if you had fractions of, you And even if you had fractions of, you And even if you had fractions of, you know, 60.356521, that can still fit in a double, and the that can still fit in a double, and the rounding issue will be barely rounding issue will be barely rounding issue will be barely noticeable. Again, it's that last bit noticeable. Again, it's that last bit noticeable. Again, it's that last bit that may or may not be rounded.
-
that may or may not be rounded. that may or may not be rounded. And then, you know, if we're talking And then, you know, if we're talking And then, you know, if we're talking about the temperature, we're talking about the temperature, we're talking about the temperature, we're talking about, you know, about, you know, about, you know, the distance that you went. All these the distance that you went. All these the distance that you went. All these things are calculations things are calculations things are calculations that you don't have you're you're never that you don't have you're you're never that you don't have you're you're never going to be ex- exactly precise. going to be ex- exactly precise. going to be ex- exactly precise. Okay? And then, decimal. Okay? And then, decimal. Okay? And then, decimal. You'd want to use that for money and You'd want to use that for money and You'd want to use that for money and other counted items that need precision. other counted items that need precision. other counted items that need precision. So, if you're counting things So, if you're counting things So, if you're counting things specifically, specifically, specifically, uh [snorts] again, money's a great uh [snorts] again, money's a great uh [snorts] again, money's a great example because we have dollars and example because we have dollars and example because we have dollars and cents. Okay? If you're in the US, it's cents. Okay? If you're in the US, it's cents. Okay? If you're in the US, it's it's dollars and cents. We have, you it's dollars and cents. We have, you it's dollars and cents. We have, you know, 100 cents equals $1. And so, you know, 100 cents equals $1. And so, you know, 100 cents equals $1. And so, you say, you know, I have $23.15. say, you know, I have $23.15. say, you know, I have $23.15. You don't want it to be $23.15 You don't want it to be $23.15 You don't want it to be $23.15 points or 15.0000 points or 15.0000 points or 15.0000 1 cent. You want it to be 15 cents, 1 cent. You want it to be 15 cents, 1 cent. You want it to be 15 cents, okay? So, that's where those counted okay? So, that's where those counted okay? So, that's where those counted things need that precision. Okay? So, things need that precision. Okay? So, things need that precision. Okay? So, that's double versus decimal. that's double versus decimal. that's double versus decimal. These These conclusions are ones you've These These conclusions are ones you've These These conclusions are ones you've probably heard before. You know, use probably heard before. You know, use probably heard before. You know, use decimal for money and double for almost decimal for money and double for almost decimal for money and double for almost everything else.
-
everything else. everything else. But, now you know why. Because it's not But, now you know why. Because it's not But, now you know why. Because it's not just a, you know, because Tim told me to just a, you know, because Tim told me to just a, you know, because Tim told me to or because I was told that in a or because I was told that in a or because I was told that in a textbook. Now, you know how it's stored textbook. Now, you know how it's stored textbook. Now, you know how it's stored on disk and why specifically you should on disk and why specifically you should on disk and why specifically you should choose one over the other. choose one over the other. choose one over the other. So, So, So, there's a source code for it. Again, if there's a source code for it. Again, if there's a source code for it. Again, if you if you want go to more depth and you if you want go to more depth and you if you want go to more depth and understand a bit more about why it's understand a bit more about why it's understand a bit more about why it's doing what it's doing, you can look at doing what it's doing, you can look at doing what it's doing, you can look at this, you can read the results, you can this, you can read the results, you can this, you can read the results, you can try it out for yourself, use different try it out for yourself, use different try it out for yourself, use different numbers. I did create um numbers. I did create um numbers. I did create um some things like down here we have quick some things like down here we have quick some things like down here we have quick benchmark with it takes a row count of benchmark with it takes a row count of benchmark with it takes a row count of 10,000. Um 10,000. Um 10,000. Um what you can do is just or I'm sorry, 10 what you can do is just or I'm sorry, 10 what you can do is just or I'm sorry, 10 million. Um you can change that to 20 million. Um you can change that to 20 million. Um you can change that to 20 million, see what happens. Um up here we million, see what happens. Um up here we million, see what happens. Um up here we have the representing the um the binary have the representing the um the binary have the representing the um the binary representation of a number. Well, I just representation of a number. Well, I just representation of a number. Well, I just pass a number in, so you can change that pass a number in, so you can change that pass a number in, so you can change that and see what happens and look at it and see what happens and look at it and see what happens and look at it actually count out to figure out how the actually count out to figure out how the actually count out to figure out how the number would be then stored number would be then stored number would be then stored on disk. Okay? So, all this you can play on disk. Okay? So, all this you can play on disk. Okay? So, all this you can play around with. There's the the methods around with. There's the the methods around with. There's the the methods down below. This is all just in one down below. This is all just in one down below. This is all just in one program.cs, [clears throat] program.cs, [clears throat] program.cs, [clears throat] but um this is now but um this is now but um this is now a much better understanding for you of a much better understanding for you of a much better understanding for you of double versus decimal. If you may ask, double versus decimal. If you may ask, double versus decimal. If you may ask, you know, what about float? Well, float you know, what about float? Well, float you know, what about float? Well, float is just double, but smaller, okay? I is just double, but smaller, okay? I is just double, but smaller, okay? I think float is 32-bit think float is 32-bit think float is 32-bit versus decimal or double is 64-bit, versus decimal or double is 64-bit, versus decimal or double is 64-bit, okay? Uh long is 128-bit. So, it's just okay? Uh long is 128-bit. So, it's just okay? Uh long is 128-bit. So, it's just about how much storage space you have about how much storage space you have about how much storage space you have for that integer or that number, okay?
-
for that integer or that number, okay? for that integer or that number, okay? So, um that's the only real difference So, um that's the only real difference So, um that's the only real difference when it comes to uh float, double, and when it comes to uh float, double, and when it comes to uh float, double, and uh uh long. Okay? So, decimal is for the uh uh long. Okay? So, decimal is for the uh uh long. Okay? So, decimal is for the precision. Almost everything else use precision. Almost everything else use precision. Almost everything else use double. If you use the math library, if double. If you use the math library, if double. If you use the math library, if you use the math library from Microsoft you use the math library from Microsoft you use the math library from Microsoft for doing calculations, it's doubles. for doing calculations, it's doubles. for doing calculations, it's doubles. Because Because Because that's the thing we have the most speed that's the thing we have the most speed that's the thing we have the most speed on it. They're much much faster on it. They're much much faster on it. They're much much faster uh to use, okay? Thanks for watching and uh to use, okay? Thanks for watching and uh to use, okay? Thanks for watching and as always, I am Tim Corey.
Summary
This transcript explains the difference between C# `double` and `decimal` variable types, highlighting their storage methods and precision issues, particularly with the common 0.1 + 0.2 example. The key takeaway is that while `double` offers broader usage, `decimal` is crucial for financial calculations where exact precision is paramount. The video is a paid training resource from imtimcorey.com, offering source code and further learning opportunities.