Fahrenheit to celsius different formula

The code part was easy but I never remember formulas and I’m terrible at math.

When I google for the formula to convert fahrenheit to celsius I can find tons of examples including c and c++ code examples but everywhere its
(fahrenheit - 32) * (5.0/9.0)
Mosh example
(fahrenheit - 32) / 1.8
gives the same answer but I’m wondering where did it come from?

Note my first example gives an incorrect answer of 0 no matter what temp I enter if I don’t use doubles and add the .0 in (5.0/9.0) so the second one is better but I don’t get the math.

So now I understand the second one

1 Like

The actual formula to convert fahrenheit to celsius can also be written as (fahrenheit - 32) * (100/180) which can be simplified to (fahrenheit - 32) * (5.0/9.0). You can also perform the inverse operation on the parentheses, where 9/5 is 1.8 which you divide from the parentheses; notice how the first example multiples both parentheses and the second divides them.

As for your error, if you are always returning 0 I would say it is either a casting issue (since you are multiplying an integer by a double) or you are incorrectly setting the variable you are trying to return

The basic answer is math. Dividing by x is the same as multiplying by 1/x. Conversely, dividing by y is the same as multiplying by 1/y.

So, applying here where x is 5/9, the division equivalent is to divide by 1/(5/9) which is equal to 9/5 or 1.8. So multiplying by 5/9 is the same as dividing by 9/5 (or 1.8).

Before we had computers and calculators people had to count fractions on paper to get decimals. It is basically same as using synonyms, or another language 1/2 is the same as 0.5. 9/5 is the same as 1 and 4/5 (1 full 5s and 4 extra, 5 goes into 9 fully once) and 4/5 is the same as 0.8, 1full and 4/5 parts is 1.8.

Think 5 as a whole apple. You divide the apple in 5 equal pieces, 4/5 is not quite the whole apple, but 5/5 is. 5/5 == 1 whole apple so 5/5 + 4/5 = 9/5. Nine pieces, each one fifth of an apple, but together 1 whole and 4 fifths. Four fifths also happens to be 80%. What is 100 divided into 5 piles? Five piles of 20. So what is 4 fifths? take four of those piles of 20; 4*20=80

Five piles of 20 = 100% Four piles of 20 = 80%

This is why when we do the fraction calculations, we have to first turn the parts to be same. You do not get correct answer if you count 8/10 and 5/6 together. The latter value must be same first before you can do any calculations with them.