Previous      Up      Previous     Course Home   e-mail

3.1 Farenheit to Centigrade

We can convert a temperature F, expressed in degrees on the Farenheit scale, to degrees Centigrade by the formula: which renders in code as


double f ;

double c = (5.0 / 9.0) * ( f - 32.0);
Note, by the way, the use of the constants "5.0" and "9.0" rather than just "5" and "9". That’s actually important. 5.0/9.0 is a floating point calculation that yields a floating-point answer of approximately 0.5556. But 5/9 is an integer calculation that truncates any fractional part and actually returns an answer of 0.

So if we had written


double f ;

double c = (5 / 9) * ( f - 32);
then, no matter how hot it gets on the Farenheit scale, we would think it’s still freezing in Centigrade!

 Previous      Up      Previous     Course Home   e-mail