declare

14 - Declaring and Using Floating-Point Variables in Java



Sharing buttons:

hello I'm Jason with code learner comm

in this lesson we're going to learn

about floating-point numbers and how to

do math with floating-point numbers now

if you remember in the previous few

sections we've dealt with integer

variables so if we want to define a

variable that contains the number 5 I

might have an int and I called a

variable Jason and later on I can assign

the number 5 to variable Jason integers

are basically whole numbers that's all

they can do they don't have any

knowledge of decimal points or anything

like that you can't put the number 2.5

into an integer so what if you have to

do some manipulations or calculations

with decimal points or if you just need

to store a decimal point numbers into

variables well you need to have a

different variable type for that in the

most common one that you're going to use

in Java is going to be called double now

the reason it's called a double is kind

of beyond the scope of this discussion

but if you look up here in the commets

the double is a 64-bit number and it can

hold numbers with decimal points it's

really all you need to know so for my

advice is that for every program you

write if you need a decimal point you

declare it as a double now as we get

later on into the course we're going to

learn that there are different types of

variables that can hold decimal points

basically they're just different number

of bits and the reason that you care

about that is because of course if you

define a million variables that are each

64 bits long it's going to take up more

memory in your computer if your numbers

are not that if you don't have a need

for that many numbers after the decimal

point then you can choose from some of

Java's other floating-point

representations and you can you can get

away with a 32-bit number and so on so

that's kind of getting beyond the scope

of what I want to discuss here I just

want to let you know there are other

kinds of variables that can store

decimal points the one that you need to

be concerned about right now is this one

it's the most common one that you're

going to use especially on modern

computers with tons and tons of memory

all right so it is called the w declare

it by putting the keyword double in

front and in this case to illustrate

what we're talking about let's say we're

going to convert inches to centimeters

so we want to convert inches to

centimeters now if you remember there

are two point five four centimeters in

every inch so we're going to need

deal with decimals so let's declare a

variable call inches and let's declare

another double called centimeters like

this so here we have declared both of

those guys and then in the inches we

will set this guy equal to twelve point

seven all right so we have 12 point

seven inches the question is we want to

calculate how many centimeters we have

so what we need to do is we need to take

and we're going to assign we're going to

be converting to centimeters right like

this and so we're going to take the

number of inches we have and we're gonna

multiply it by two point five four

because that's the conversion factor

this is how many centimeters are in

every single inch so we take the number

of inches we have times that number that

gives us the number of centimeters this

will go and save that off now typically

now we have the value of centimeters

from the conversion in there typically

we want to print it out so let's go and

do system dot out dot print Ln like this

and inside of here let's say let's take

away the quotes actually will say inches

right that's how many inches and we'll

do a plus we'll do a space inches is

equal to put a space there come out here

I'll hit a plus sign like this

centimeters and I put a plus sign here

and I'll open a quote space centimeters

okay so there's a couple of things I

want to point out to make sure you

understand make sure you understand this

line when we go into the print statement

this is there's no quotes around it so

it's going to print what however many

value of inches we have so twelve point

seven should come out here and then we

put a space so twelve point seven inches

is equal to space and then we put the

calculation so it'll be whatever this

number comes out to be and then we put

in another space centimeters so it

should say twelve point seven inches is

equal to however many centimeters now

one thing I want to point out while I'm

you know kind of here is that sometimes

these print statements can get really

long you see how it kind of scrolled off

and if I had a much longer sentence it

would be off the page here so in in Java

you can split these these long lines

anywhere you want so for instance I can

put

I can put the cursor there and I can hit

an enter and if I want to make it

readable I can tab in so Java doesn't

consider the line terminated until it

hits the semicolon so pretty much

anywhere in here I can just hit enter

and bring it to the next line and the

line is gonna continue to be read by

Java until it hits the semicolon so this

might be a little bit more easier for us

to read especially if we're rolling off

the screen like that

so let's go ahead and hit enter and

let's go ahead and hit run and then we

have twelve point seven inches is equal

to thirty two point two five seven nine

nine nine six centimeters now one thing

I really want to spend just a few

seconds here pointing out here is that

we've declared these guys as double

precision floating point numbers and

we've done multiplication between one

number that contains a decimal with

another number that contains a decimal

and the answer that we get is thirty two

point two five seven nine nine nine nine

nine and so on so when you see a

repeating decimal like this you really

need to make sure it's correct now I

want to make sure you understand that

for instance in this case if you go get

a calculator out and type twelve point

seven times two point five four you're

not going to get 32 point two five seven

nine nine nine nine you're going to get

thirty-two point two five eight so this

digit here will be rounded to an eighth

so the caution I'm trying to throw here

is the same caution you're going to get

if you read any book in Java if you're

doing calculations that require

extremely accurate numbers then you

really shouldn't do it like this there

are special numbers in the Java library

that will handle all of the decimal

calculations perfectly the reasons this

does not match reality from a calculator

perfectly is the way in which binary

multiplication works on floating point

numbers so there's a Java package out

there that's better suited for for very

good precision so this is perfectly fine

for this example because thirty-two

point two five seven most people would

run them around this up to two five

eight anyway is perfectly fine for an

illustration of how we're going to

multiply floating-point numbers together

it's perfectly fine for most

applications if you're writing you know

depending on what you're doing but if

you're writing an application that's

going to calculate people's bank account

balances or if you're going to do any

kind of

where every single decimal point really

is important then you don't want to run

into these problems with with this sort

of multiplication or division of

floating-point numbers and so later on

I'll show you the the way to get around

that there's certain variables and and

classes in Java that you can use that

will take care of all of these decimal

points here so the point I'm trying to

bring here is that when you see a bunch

of nines like this you can pretty much

assume that when it did the calculation

there was some kind of remainder that

wasn't quite right and that's just based

on the default way in which the

calculations are done you know I can

show you something else if I change this

variable from twelve point seven if I

change it to just 12 so 12 inches times

two point five four and I run it then

I'm gonna get thirty point forty eight

centimeters this is exactly correct

so this thirty point forty eight is

actually correct you run into problems

frequently when you have both numbers

multiplied or divided together or from

one another that have decimal points in

them and it has to do with the way in

which the decimal point is represented

inside so when you do calculations when

you get out three or four decimal points

sometimes they can diverge from

exactness so this is perfectly fine for

illustrating the point but what if

you're doing some kind of program with

extreme precision you just need to be

aware of that and in those cases you

would use some of the other Java class

libraries that we're going to get to

later that are designed to handle

decimal point calculations very

precisely all right so we've done a

calculation here where we've multiplied

two guys together we've got a

floating-point answer which is stored in

a double precision variable now one more

thing I want to show you before I go on

let's declare an integer let's declare

an integer that's going to be number one

and let's declare another double it's

going to be number two I just want to

show you a difference in something here

real quick all right so we've got all of

this stuff working as usual and then

down below this print statement I'm

going to do a couple of different

calculations so let's assign to number

one the following let's assign 12/5

there and we're going to put the result

into number one and then in number two

assign the same thing 12/5 the only

difference see this is 12/5 we know

we're going to get a decimal answer this

does not divide evenly but in one case

we're assigning it into an integer so we

know that's not going to work right

because integers cannot store decimal

coins in the other case we're going to

assign it to a double so I'm just going

to show you really quickly that that

leads to the results that we care about

so let's do system system dot out dot

print oh yeah and in the first case in

the first case let's just do it this way

number one right and let's go down here

system dot out dot println number two so

all we're doing is we we have this first

statement of calculation here we're

gonna get a carriage return line feed

and then we're gonna print the kid the

result of this division here and then

the result of this division here right

underneath it and we should see a

difference so let's go in and just for

kicks inside of here let's go and do a

newline backslash n so we get a space

between and before I go and calculate

everything I want to make this twelve

point zero here and five point zero so

in the first case we're taking twelve

which is an integer we're dividing by

five which is an integer and we're

storing whatever happens here into an

integer variable here we're also taking

12 and dividing by five but we're going

to put decimal points in there just to

have the types match up so this is a

floating-point number this is a

floating-point double and we're dividing

them and we're putting it into a double

precision variable that's capable of

storing decimal points so we're going to

hit save and we'll hit run and we'll see

the difference here so underneath our

original print statement the first

division is going to give us just 2 and

the second division since it's stored in

a double variable is going to give us

two point four so what I'm trying to

tell you here and just kind of

illustrate by showing is that when you

do division here of integers or of whole

numbers like this and we know that

there's a decimal remainder but since

we're storing it in an integer it all

gets truncated it gets lost so just the

two survives when we do the same

calculation and store the result into a

double then the whole calculation is

carried forward and we get

two-point-four so just be aware if in

your programs you're using and and and

have a need to use integers and whole

numbers make sure to store those in

integers if you have a need and you're

using decimal points store them in

double you have type of variables you

can add subtract multiply and divide

them you can use the system.out.print

functions as we've been doing before so

what I'd like you to do now as you

understand this as go on to the

exercises and solve those problems and

write those programs and build your

skills with this in Java