Tip:
Highlight text to annotate it
X
Last time, we talked about the different
C# data types we have to represent integers.
This time, we're going to talk about the
C# data types we have to represent real numbers.
Before we start that conversation, here's the
reminder of the Module 3 learning objectives.
So before we actually start talking about specific data types, let's talk
about a problem that we have as we try to represent real numbers with a computer.
The problem is that we already know N equal 2 to the b.
It's also the case that real world is a continuous domain.
In other words, between 0 and 1, we have an infinite number of numbers.
What that tells us, given N equal 2 to the
b is that if we're going to precisely represent every
number in the real world, then we're going to need an
infinite number of bits for the numbers between 0 and 1.
And then, another infinite number of bits for
the numbers between 1 and 2, and so on.
And despite the fact that memory is getting cheaper all the
time, we do not have that many bits available to us.
So we are making choices as we represent those real numbers.
And, in fact, what happens is a number of real numbers get represented
in the same sequence of bits in the computer because we have some rounding.
We're doing approximation,
rather than precise representation of those numbers.
So it's important that you'll understand that because when we try to represent
the real number, we can't represent all real numbers with the bits we have.
So some of the real numbers that exist in the real
world get represented with the same sequence of bits in the computer.
When we try to represent real numbers in the computer, we actually
have a couple of different kinds of real numbers that we're going to represent.
And we'll talk about each of those separately.
First of all, we're going to talk about floating point numbers.
And we get two different data types in C# for
floating point numbers We get float and we get double.
And the difference
between those two data types is that they
use different numbers and bits for their representations.
So we already know what that means.
We know that floating point numbers, which use fewer bit, can represent a smaller
range of numbers and, in fact, can
represent a smaller range of exponents as well.
We're going to find, because XNA uses float almost exclusively, we're going
to find that we're going to use float in large
part for real number representations in the programs we write.
But we have these two choices, and the operations
we have available to us, are as you'd expect.
Let's take a look at a short program that uses floats.
New Project, another Console Application
[SOUND]
and I'll call this one floating point numbers.
Zooming in before I forget, and saving as well.
We'll just save right there. And again,
adding comments.
So, we're going to
declare a couple
of different
variables here.
Let's say one of the areas where
floating point numbers tend to be really useful is when we calculate averages.
So let's actually calculate an average.
And we're going to first declare variables that store
the numbers we're going to use in our calculation.
I'll make them both integers.
So the first will be something you may
have seen in the past in your gaming experience.
We'll call it score.
Then, we'll say it's 1356. That may be good.
That may be bad. It depends, but there is a score, 1356.
We'll also have a total seconds played.
Because of those gamers like to optimize our points per second and
so, we'll have a total number of seconds played here as well.
And let's make that 10,000.
Notice I didn't put any commas in this literal.
They don't go there.
Just the pure number. So, I've got a score.
I've got a total number of seconds played.
And now I'd like to calculate how many points per second
I'm actually earning in this game.
So, I'll say calculate and display result, and I'm going to put this
result into a floating point number. So float
pointsPerSecond
is a good name.
And the pointsPerSecond are the number of points, so that score
divided by the number of seconds. So that's total seconds played.
And we'll do our output.
And I will add that
blank line that I
always looked at.
So when I run this with Ctrl + F5, oh
dear, pointsPerSecond is zero and that doesn't make any sense to us, right?
PointsPerSecond has got to be more than
zero because we have more than zero points.
Remember, though, from last time, when we divide two integers using division,
we actually get the whole number result.
And because our average is less than one point per second, the score
divided by the total seconds played is, in fact, 0 using integer division.
What we want to do is force this to do floating point division.
And so we're going to use something called
the typecast, that says, turn this into a float.
So we need either score or total seconds played.
to be treated as a float for
the calculation to yield a float, because we're
trying to force this division operator to do
a floating point division not an integer division.
So the way I can tell score to be treated as a
float in the calculation, is I add the word float in front
of it, inside parenthesis.
And that is a type cast, that says
temporarily, right here, treat score as a float.
It does not convert the score variable to a float
permanently or anything like that, just temporary for this calculation.
Once I do that though and I run again, I see that I now get
0.1356, which is as we expected. So, if this
is one of those things that burns beginning programmers a lot.
You heard about integer division last time, and you sort
of maybe nodded your head and said, sure, I've got it.
And, in fact, it can still come back to bite you.
So we have done, we've used a float to store a floating point
number, it has a decimal point in it. So that's a good thing.
It's very useful to us.
And that's all we're actually going to do in code today for this lecture.
So I've saved, and I've closed.
Now, that means, since we just talked about floats, that you
Probably have an in-lecture quiz to do and of course you do.
So go ahead and do that now.
As I mentioned, floating point numbers,
float, and double have inaccuracies in them.
They are an approximation of the continuous real world of numbers.
And so there is some rounding.
There's some instances, many instances of multiple numbers in the
real world being represented with the same sequence of bits.
So we get that inaccuracy.
That can't be helped because N equal 2 to the b.
The good news, well, it's sort of a dual-edged sword, a dual-edged
sword of cutting, the good news is that we have the
capability to have perfect precision, for numbers with decimal points.
The bad news is that comes at a cost. And the
cost is that we have to reduce the range of numbers we can represent.
But it's important, sometimes, to have that perfect
accuracy when we're talking about currency, when we're talking
about the stock market, if you're talking about cash
register machines we do not want to allow rounding.
Even for fractions of a cent, or whatever your currency is.
So, sometimes, we need perfect accuracy.
And remember, this is not tricking an equal to the b.
This is not magic in any way.
All we're doing is reducing the range of the numbers we can represent.
So that we can represent with perfect precision, all those numbers.
And so, you know, we have this data type in C# called decimal.
That gives us that perfect accuracy.
At the cost of a reduced range of numbers we can represent.
And, you know, this is up to 28 decimal places, so
it's not like this won't work for your local cash register.
But it's good to avoid the rounding.
So, given that discussion about decimals, you have another in lecture quiz.
I really hope you picked the correct answer on that previous quiz.
Alright.
So the recap for today's lecture is just as we have a variety of
C# data types we could use to represent integers, we also have a
variety of data types we can use in C# to represent real numbers.
And those are the three data types that we talked
about today. There are also value types.
Remember the memory location is the, the bits at
the memory location are actually the value of the variable?
But we still have a few more value types to talk about in addition
to the numeric types that we talked about last time and this time.
So in the next lecture, we will finish off
Module 3 by talking about the other value types that are available to us in C#.