In this lecture, we will explore a little more deeply how we read the documentation.
And this is an important skill that every programmer need.
You never reach the point where you don't have to look something up in the documentation,
whether it's the C-sharp documentation or the Unity documentation,
you are going to spend time through out your programming life looking up
information from the documentation that goes with code that others have provided to you.
So we're going to explore that a little more here,
but you're going to have to just practice.
You're going to have to just learn how to do this by doing it,
time and time again.
So let's go to MonoDevelop and solve
a very small problem that requires us to explore the documentation a little bit.
So here's our MonoDeveloped project that I created and commented,
including this comment that says calculate and print the cosine of 45 degrees.
So that is the problem that we're trying to solve.
Let's actually declare a float and we'll just call it cosine.
And we're going to need to set it to something,
specifically the cosine of 45 degrees.
Let's actually just set it to something right now.
And this is a temporary thing,
I want to show you another important detail about using float.
So let's say I set it to point eight even though that's incorrect.
And I tried to compile it with f8.
I actually get an error here that says the
literal of type double cannot be explicitly converted to float.
And it even tells us the answer,
it says use an 'F' suffix to create a literal.
So when the compiler sees a decimal number like this,
it assumes it's a double.
And one of the things we're not allowed to do,
is to put a larger data type like a double into
a smaller data type like a float because to do the B = N,
and we know that we will potentially lose some precision when we move from
a larger data type to a smaller data type
because the smaller data type can't represent as many things.
The message here says to use an 'F' suffix and you certainly can.
I tend to use lowercase f instead.
So now we can compile fine.
We get a warning that says cosine is assigned to but its value is never used,
so let's go ahead and use it.
Console.Writeline cosine of 45 degrees,
and we'll control f5 and of course we get our output, but it's a lie.
So now we have to figure out how we can actually calculate the cosine of 45 degrees.
And it's a good time to go look in the documentation.
Now, unfortunately, one of the things you have to know if you're going to
search is to search on something and you have to know what to search for.
So let's actually search on cosine because that's the thing we're trying to figure out.
I'll just hit enter here.
And it turns out that there's a whole bunch of
answers but the math.cosine method is the one we want.
So I'm going to click on it and that brings us right to the method.
Now another way we could get to this method,
is by actually looking at the math class.
So I'm going to scroll down to the bottom here and click Math Class.
So we could end up here.
Now the Math Class is a very useful class that has lots of
stuff in it that we will regularly use as we do math in our games,
including calculating cosine and including
one other piece that we'll get to in a few minutes.
So we could also get to cosine by finding
the cosine method from the Math Class and clicking it.
And this brings us back to the same page we were at.
Now, there are pieces in
the documentation that we need to understand to be able to use it.
First of all, we see that it's static and that means just like console.writeline,
it means that we will call math.cos by
putting the class name math dot in front of the method name.
The second thing we see is that it actually
returns something and that's good for us, right?
We're trying to calculate the cosine of an angle and it returns something to us.
And so we need to know that it's going
to return a double to us because we're going to store that double somewhere.
The method name comes next,
and then between the parentheses anything we're supposed to provide.
And if you race through reading this documentation, you'll say,
"Okay 'd' is an angle,
great", and you won't read the rest of it because you're in a big rush.
And so let's go use the call to this method in our code.
So instead of this,
we will say Math.Cosine,
the angle and we said we were going to do 45 degrees.
When I compile, it says wow this looks a lot like the previous error message,
although not exactly but it does say we can't implicitly convert double to a float.
Back in the documentation,
this says the method returns a double,
and here in the code,
we're trying to put it into a float.
So we can use the typecast that we
used when we were doing calculations in the previous lecture,
to say I want to typecast the double that you return to me into a float.
And I have to do that explicitly because this is a double so it's a bigger data type.
So pushing it into a smaller data type,
that means we could do some precision.
I'm allowed to do it,
I just have to explicitly do it.
Now, I'm going to control_f5.
And if you don't know what the cosine of 45 is,
you'll say wow this is great.
I got a number and I must have it correct.
One of the important things to do when you're doing math
and checking your code to make sure it's working properly,
is you need to know the right answer
before you can actually tell if your code is working properly.
And I can tell you that's not the cosine of 45 degrees.
So, we go back to the documentation and read this full comment,
that says an angle measured in radians.
We're not providing an angle measured in radians,
we're providing an angle measured in degrees.
So how do we convert from degrees to radians?
Well, you need to know that 360 degrees is the same as 2 pi radians,
so 180 degrees is the same as pi radians.
And so we need to multiply this times
pi divided by 180 and that will convert it to radians for us.
And you might say well I know pi, it's 3.1415.
And while we could get this to work,
there's a better way to use pi in our games
because back in the Math Class and this is the class not the cosine documentation,
we can see past all these methods,
we can see that math actually provides us a constant for pi.
So this is the value we get when we use Math.PI.
So let's just use Math.PI instead of hard coding it in our code.
And the way I do it,
is I say Math.PI.
Now I'll make sure I build fine, build successful,
I control_f5, and I get 0.707 which is in fact the cosine of 45 degrees.
So I'm done with this particular problem.
And we learned a couple of different things.
Importantly, we learned about type casting something that gets returned from a method,
but we also learn that we can read
the documentation to find out information about a particular method,
like what does it return,
what does it require as information we
pass in and we need to pay attention to all that stuff.
So that was a very small exercise in exploring C-Sharp documentation.
But you're going to be doing that,
as long as you program,
you're going to actually be looking stuff up in the documentation,
so you get lots of opportunities to practice doing that as you program.