>> DAVID J. MALAN: As you probably know, temperatures are measured using
different scales in different parts of the world.
For instance, 212 degrees Fahrenheit is 100 degrees Celsius and 32 degrees
Fahrenheit is 0 degrees Celsius.
Wouldn't it be nice if we had a computer program that allowed us to
convert from, say, Fahrenheit to Celsius?
>> Well, we can write that program.
Let's first start with include cs50.h, so that we can use a function called
get float, which, like get int, gets a number from the user, but this time a
floating point number-- one with a decimal point.
>> Let's also include standard io.h so that we have access to
functions like print f.
And let's also declare main in the usual way.
>> Let's next prompt the user for a temperature in Fahrenheit.
Let's now actually get that temperature from the user, first by
declaring a variable called f of type float.
A float, again, meaning a variable that stores a floating point value,
one with a decimal point.
>> Let's assign it the return value of get float.
And then let's do a bit of arithmetic on it, first declaring another float
called C for Celsius and store in C the result of some common arithmetic.
5.0 divided by 9.0 times F minus 32.0.
>> Let's now print the result of this computation.
percent f--
signifying a placeholder for floating point value--
comma C to print out the temperature in Celsius.
Let's save my final.
Compile with F to C. Run the program with dot slash F to C.
>> And let's try those common temperatures.
212 degrees in Fahrenheit is 100 degrees Celsius.
32 degrees in Fahrenheit is 0 degrees in Celsius.
>> Now lets refine this program a little bit by not printing quite as many 0's
after that decimal place.
To do this, I'm going to go back to line 11.
And rather than just specify percent f, I'm going to instead specify, say,
0.1 f, informing print f that I only want to print a floating point value
to one value after the decimal place.
>> Let's resave my program.
Recompile it with make F to C. Then rerun it with dot slash F to C. And
let's retry with, say, 212, which gives me 100.0.
>> Now it's worth noting that I did something very deliberately in line 9.
Notice how I wrote 5 is 5.0, 9 as 9.0, and even 32 as 32.0.
Well, the first two of those values were very deliberately chosen to be
floating point values, not just because of consistency with the rest
of my program--
which clearly involves floating point values--
but because it turns out that C, If you divide an int by another int, the
resulting answer you're going to get is itself an int, even if that means
having to throw away everything after the decimal point.
>> In other words, if I change this 5.0 to 5 or this 9.0 to 9 and then resave
my program, recompile with make F to C, and then re-run it with dot slash F
to C and type in an input of like 212, notice that the answer I'm going to
get this time is actually quite wrong.
0.0 is not the correct degree in Celsius as 212 Fahrenheit.
>> Well, what's going on?
Well, in line 9, because 5 is now an integer and because 9 is now an
integer, the result mathematically should be 0.5555 and so on.
But because the result, according to C's rules, has to be an int, that
0.5555 gets thrown away, leaving us with just 0.
>> So in the end, I end up multiplying quite accidentally 0 times f minus
32.0, which is no matter what always going to give me 0.
So do keep in mind, any time using floating point values in proximity of
ints, you might not necessarily get the answer so you expect.
And so take care to use, as I did in the first case, floating point values
throughout to avoid any such issues.