0#
I'm in accord with the comments about
using;
switch statements (or ever if/else if as it's just two conditions to check); and I agree with giblit that you could factor your code to use more functions (though not in their choice.)
(I would also wrap the over long lines (14, 22, and 29). The need for the 80 column rule has gone, not we don't use primitive text editors. But 155 char long lines are way too long! (In this case you can take advantage of the fact that the compiler's preprocessing phase concatentes all adjacent string literals, even across line breaks!
e.g.
1 2 3 4
|
char msg[] = "Hello"
" "
"world"
"!";
|
looks the same to the compiler as
char msg[] = "Hello world!";
)
1#
As giblit has already mentioned, you should use double rather than float for most cases. An exception is when you're using a large vector (or C-style array).
As you might have noticed, all the standard library calls work with double, so you save the compiler from having to cast back and forth between float and double.
And "on x86 processors, at least, float and double will each be converted to a 10-byte real by the FPU for processing."
Float vs Double Performance
http://stackoverflow.com/questions/417568/float-vs-double-performance
2#
But I would keep the local variables in the conversions functions.
Elimination of the local variable is a case of premature optimization as both
1 2 3 4
|
float convertCtoF (float celsius) {
float faren=(9.0/5.0)*(celsius+32);
return faren;
}
|
and
1 2 3
|
float convertCtoF (float celsius) {
return (9.0/5.0)*(celsius+32);
}
|
result in
identical object code for the optimized build.
The advantage of the form with the local is that if you set a breakpoint on the return, you can see the value of
faren in the debugger watch window. (In the one without the local, I just see
celsius; with the local I see both
celsius and
faren.)
Also, having the function's code all on one line also prevents the watch window from working (at least with Visual Studio 2010.)
Andy
PS Both versions of the function cause a compilation warning as it's using double literals rather than float literals. If you want a float literal, rather tham a double literal, then you need to use an f suffix.
1 2 3 4 5
|
// no warnings :-)
float convertCtoF (float celsius) {
float faren=(9.0f/5.0f)*(celsius+32);
return faren;
}
|