14x - Learn Java - Floating Point Variables - Exercise 1

preview_player
Показать описание

In this lesson we will practice writing java code that declares and uses floating point variables for use in calculations.
Рекомендации по теме
Комментарии
Автор

I think since only one of the numbers needs to be double in order for the whole thing to be stored as double you can just write 34 degrees celcius without .0 and it will be stored naturally as a double and it will cascade down to the farenheit equation and the answer.

EDIT
That hypothesis is "wrong" every multiplication or division need to involve at least 1 double number with .0 or it will be stored as integer and you will lose accuracy.
In this case since 9/5 is inside brackets you need to make one of them or both as a double in order for the result to be stored as double.

Or you can avoid the whole .0 business if you write it with celcius first so every arthimetic past that will have the double stored because we started with celcius which is stored as a double:

celcius*9/5+32
no brackets.

Program is executed sequentially from left to right so it happens like this:
1. celcius*9 > stored as double
2. result divided by 5 > stored as double
3. add 32 to it
So the double is preserved.

If you start with (9/5) that will be stored as integer so we have already lost accuracy when we proceed to the next multiplication.

joeroganpodfantasy
Автор

Hi, how can we get the exercise file??

nouraldeenhe