Tim: All right, so I continue
on with our floating point discussion
of the float and double primitive types.
Let's start out by deleting
the .25 from both our float and our double variables.
And let's actually print out these values.
We'll do it to all three.
So we'll print out the myIntValue
is equal to, and we'll add to myIntValue.
We do the same for the other two.
MyFloatValue, get that equal
to plus myFloatValue.
And the same for the double.
MyDoubleValue equals plus myDoubleValue.
All right, so let's just run
that to see what it looks like.
All right, I can see there that myIntValue
is showing five.
No surprises there,
but the float and the double are showing
the points of 5.0 and 5.0,
both for the float and for the double value.
And obviously, that's because, no surprises,
it's a floating point number.
All right, so let's now try doing some division
to see what happens.
So we're going to go back,
and we're going to put for the first line, line 17,
we're gonna put divide it by two there.
And we're gonna do the same for floats.
So we're gonna put divide it by,
and we're going to put 2f
there because you wanna divide it by a floating point number
and the same for the double.
Divide it by 2d, in this case.
So we're doing a simple division for all three.
Let's actually run that and see what happens.
So you can see what's happened there, now.
The int type has got a value of two,
but both float and the double have got the value of 2.5.
So because the integer is a whole number,
in effect, what's happened is it hasn't handled
the remainder of the division five divided by two,
and we're left with the number of times
that two goes into five, which is two.
But in the case of the float and the double,
obviously, we've got a much more precise answer,
because it can handle the decimal point.
And, obviously, that's the reason
why you'd want to use a floating point number
to be more precise with things like division.
All right, So moving on, let's change
this instead of two to three.
And dividing all three of them by three, this time,
and we'll run the code again.
And, as you can see, for the integer value
we've now got one because three goes into five only once,
and, again, there's no remainder because we're using
an integer, but note, so the precision
for the float and the double value there.
Obviously, we haven't got an exact number
because we're dividing,
but the result for the float value is 1.6666666.
So you can see there that the precision is seven
for a float, but can get up to seven numbers
after the decimal point,
but the double has got up to 16,
and you can see it's actually 16 digits there, 1.6666.
I won't read them all but the last digit is a seven.
So basically 16 digits.
So you can see, hopefully that's really clear now,
that the double is a lot more precise
and has more precision than the float.
All right, and just a reminder again,
that in terms of the number that you're typing in there,
if you're typing in a decimal number 5.25,
for argument's sake,
we can do something like this.
I've got 5.00 divided by 3.00.
We can remove the d, remembering that Java will
automatically look at a floating point number,
and assume it's a double.
You can run that,
and we get exactly the same output as a result.
So you don't really need to specify d
when you're using a literal there,
but it's, again, as I mentioned in the last video,
it's a good habit to get into to do just that.
Now, you might be thinking that given
that the fact that double takes up
double the amount of space,
it's a double precision number compared to the float,
which is a single precision number,
that, perhaps, you should be using the float.
What's interesting, but most definitely, the double
precision number, the double primitive type,
in this case, is recommended.
And there's a number of reasons for it.
Firstly, believe it or not, it's actually faster to process
on many modern computers.
And that's because computers have at the chip level
the functionality, for many computers,
to actually deal with these double numbers
faster than the equivalent float.
Secondly, the Java libraries that we'll get into
later in the course, that the particular math functions
are often patternal-written to process doubles
and not floats, and to return the result as a double.
And you can see the reason that the makers of Java did that.
Obviously, it's more precise, and it can handle
a larger range of numbers.
So you can see why they would have done that.
So, consequently, you'll find that double
was used a lot in the code that's out there, in the wild,
in the various libraries.
And for you to take advantage of these libraries,
you should be using the double.
And the other thing I mentioned, anyway,
is that Java treats double
as the default floating point number
if you don't specify the d or the f.
So for all those reasons,
it's highly recommended that you use the double.
And for that reason, I'm not gonna be using
the float anymore in this course.
Any floating point number code that we use,
will be using the double, moving forward.
Alright, so to end this video,
it's time for a quick little challenge.
So the challenge here is to convert
a given number of pounds to kilogrammes.
So here are the steps that you'll need to take.
Firstly, you'll need to create a variable
with the appropriate type to store
the number of pounds that we want
to convert into kilogrammes.
Then you wanna calculate the result of that.
So, i.e. the number of kilogrammes
based on the contents of the variable above,
then you wanna store that result
into a second variable with the appropriate type.
And third, we wanna print out the result
of your calculation.
And just to help you, as a hint there,
one pound is equal to 0.45359237 of a kilogramme.
So you should be able to take that and figure
out how you would calculate and perform that operation.
You can go through the three steps,
and see what you can come up with.
So pause the video, type your code in.
Give it a test run, try and see if you can get the result.
When you're done, come back and resume the video,
and I'll go over my solution with you.
So pause the video now.
All right, so welcome back.
Hopefully, you managed to solve that.
So let's go through and come up with a solution.
We're gonna be using a double
here for floating point numbers.
And, hopefully, you've taken in my reasoning
for using the double.
So I'm gonna start by typing double numberOfPounds
equals, and I'm just gonna type 200d.
That's just an arbitrary number.
You could have used any number for a calculation.
And next, we're gonna create another variable
called double convertedKilograms.
We set that equal to, the formula will be the number
of pounds we want converted.
All we need to do is multiply it by that number
I gave you, which was 0.45359237d,
and I'm putting a d there.
We don't have to because we're dealing
with a double data type, anyway.
It would be assumed, but we'll do that for consistency.
Semicolon, to end the line,
and we'll print out the results.
We'll put converted kilogrammes equals
plus our convertedKilograms.
All right, so let's actually run this,
and make sure that it works.
And you can see we got a result there 90.718474.
Now, just to be sure that it's going to work,
let's just open a browser.
We're going to Google, and we'll say
200 pounds to kilogrammes.
You can say that's a common question.
We've got the result there, 70 point, sorry, 90.7185.
Looking at our code, 70.71847.
So, obviously, that would be 7185.
Google have just round it up.
So, clearly, our calculation has worked well.
Okay, that's good.
So, hopefully, you managed to solve that challenge.
All right, the other quick thing I wanted to show
you was we're aware now that we can type in a double,
for example, double pi equals 3.1415927d semicolon,
but you can also express a number
like this, using the underscores double
anotherNumber is equal to, we could do 3 underscore 000,
underscore 000 period 4 underscore 567 underscore 890d.
So you can format it in that way, if you want.
I personally don't like that.
I don't like using underscores,
but I'm just showing you that because you may see
that out in the wild when you're coding,
and wonder what it is.
And just to confirm, though,
even though we type it in that format,
if we actually print both of these variables out,
this is purely a thing just for us humans to read,
because when we output it you can see the results
are still stored exactly the same way, 3.1415927.
And I printed out the wrong number.
I was wondering why that number was coming up.
That's the one I was trying
to print out there, anotherNumber.
We'll try it again.
And there's the number.
You can see it's still 3,000,000.456789.
And the zero at the end was ignored
because it's not needed.
All right, so I just wanna
finish off by talking about another important point
about floating point numbers.
So keep in mind that, in general,
float and decimal are great primitive types
for general floating point operations,
but both are not great to use
where precise calculations are required.
Now, this is due to a limitation
with how floating point numbers, in general,
are stored and not a Java-specific problem, as such.
But Java has a solution for this,
the class called BigDecimal that overcomes
this problem with calculation of precise numbers.
Now we will talk about that more later in the course.
You really need to get an understanding of classes,
and so forth, before we move into that.
For now, just keep in the back of your mind
that when precise calculations are necessary,
such as when performing say currency calculations,
then floating point types, like I've used here,
should generally not be used.
But for general calculations,
like you've seen here,
converting kilogrammes and things of that nature,
float and double are probably just fine.
And again, double would be the recommendation.
That would be the floating point type you'd generally use.
We'll discuss this in a lot more detail
in a future video, as we go through the course.
All right, so that's it now for the floating
point data types.
In the next video we'll look at two more primitive types
in Java, namely the char and the Boolean primitive types.
So I'll see you in that next video.