In this lecture, we'll talk about a number of the data types we can use in C++, to represent floating point numbers. Floating point numbers, are numbers with the decimal point. They have a fractional part. Before we talk about specific C++ data types, we should talk about a big problem that we have. Because numbers in the real world, particularly numbers that we call real numbers, those numbers that have a decimal point have an infinite number of values. We know that two to be equal N. When we try to translate or represent those infinite numbers in what's called the discrete domain, using bits, we can't do it unless we have an infinite number of bits. Because the only way to represent an infinite number of unique things is with an infinite number of bits. What we do is we decide that we are going to represent a range of real numbers, with the same sequence of bits in the computer. When we do that, we lose some precision. We've got rounding. We're rounding every single one of these real numbers to a particular floating-point number in the computer. In general, if we use a enough bits for whatever application we're doing, that works out to be okay. But it's important for you to understand the difference between real numbers in the continuous domain and floating point numbers in the discrete domain inside our computer. For floating point numbers, there are a number of data types that we regularly use. They're float, double and long double. Floats are what we will typically use throughout the courses in this specialization. They each have a different number of bits allocated to them, and as we know, that means that the data types with more bits can represent a wider range of numbers. The operations on these floating point data types are pretty much as you'd expect, so let's actually go take a look at some code that uses floating point data types. I've already created my Visual Studio project, and I called it floating point data types, and I've added my copyright notice and my Java doc comment above the main function. Here's what we're going to do in this code. We're going to calculate and print points per second. I know some of you may be like, "Well, no, it should be DPS, damage per second." But let's say I played an action game and I earned a score and I played for a certain number of seconds, and now I want to calculate and print the average. I need a variable called score. That's whatever score I happened to score during that game-play session. Let's say that score is 1360, and scores are typically integers, so I'm going to make this an integer. Let's say that I kept track of the total seconds played as an integer as well, just as we did in our integer example. Int total seconds played, and let's say that was 10,000 seconds. That's not too bad. Now I want to calculate and print my points per second. Let's start by doing it a naive, but feels correct, way to do that calculation. I know that I want to put this into a float because I'm going to divide score by total seconds played, and so I know that my numerator is going to be smaller than my denominator. My points per second is going to be some decimal number less than one. I'll say here, points per second, and I know I'm making a mistake here, but I'm going to do it anyway, is score divided by total seconds played. Then I'll print that out in the typical way with a label because I'm not submitting this to the auto-grader, and I don't think I want to say pinte, I think I want to say points per second, and I'll send the value of the variable to the output stream as well, and my new line character. But then when I run it, I end up with zero, as my point per second, and I end up with my zero because here I'm dividing an integer by an integer. This division does integer division, which as we know, gives us the quotient, and if the numerator is smaller than the denominator, the quotient is zero. Now you could say, well, I know how to solve this, I'll just make score a float. But that doesn't make sense. If you think about this, score is a whole number, we know that from our game. Changing the data type of score to be a float just to make it all work, is not intuitive based on how the game actually would play. We don't want to do that. Same argument for total seconds played, I said we stored that or kept track of that as a whole number. When you declare variables, they should always be the correct datatype, not an incorrect data type, just to try to trick the compiler to do what you wanted to do. There's a better way to solve this problem, and the better way to solve this problem is to make one of these or both, but one will be sufficient, be treated as a float. The way I'm going to do that is I'm going to do something called the typecast. I'm going to say, treat score as a float, just for this calculation. This is a temporary thing, it doesn't change the underlying datatype of score. Score is still an int. It just says for this calculation, treat score as a float. As soon as we do that, this division becomes floating point division, and that's just what we want because we have a fractional part in our division result. If I Control F5 now, you can see points per second is 0.136. Hopefully you can see that that is the correct answer based on score and total seconds played. The important lessons from this short example are, we always want to declare variables to be of the correct datatype. We don't want to try to trick the compiler, we just use the right datatype, and we want to typecast if we need to, to force the math to work right. I will say before we leave the code, that you might be tempted to do this. You might say, well, if typecasting score is good, then typecasting the whole result must be even better. But that's not true. If I Control F5, you'll see points per second is back to zero. Because what happens is this part does integer division, so you get zero and then you say treat that zero as a float, but who cares? The other moral of the story here is that when you do typecasting, you should typecast as close to the place that you need to typecast as you can. Instead of saying, "I'll just typecast the whole result," that doesn't work in this case, we need to type cast one or both of these variables, and it's your choice. We could have typecast this to float instead, we could have typecast both of them to float. But doing just score makes the division be floating point division, which is just what we need in this particular example. To recap, in this lecture, you learned the difference between the continuous and the discrete domains, and you also learned about some of the floating point data types we can use in C++.