f suffix on floats revisited
It's been a while (several years?) since I've seen discussion on this anywhere, including here. One of the things I've been noticing is how many extra keystrokes I'm putting in by typing things like 10.1f or 0.4f, over and over and over... and over and over... The reason I put that f there all the time is because I read many years ago that it tells the compiler to generate code that loads a float directly instead of loading a double and then converting to float, which is slower.
Just how much would leaving that "f" out affect performance? How really important is it to type 10.0f when I could just type 10 and let the compiler deal with it?
Is it really necessary to use the "f" suffix in every case, or are there certain situations which help the compiler more than others?
This thread isn't geared toward a specific platform, but are some platform/compiler combinations more sensitive to this than others, or do they all pretty much assume a double for floating point constants without the "f"?
I've decided that I totally do not give a crap about being "proper" to identify things like parameters as being floats, when there's a prototype that can easily be command-clicked on for a declaration. Just about every situation I can think of should be pretty clear what the application of the constant is, and quite frankly, the code is cleaner and easier to read without the "f" and extra decimal points. So the only real purpose I can still see behind the "f" is to clue the compiler in on what I'm trying to do.
Just how much would leaving that "f" out affect performance? How really important is it to type 10.0f when I could just type 10 and let the compiler deal with it?
Is it really necessary to use the "f" suffix in every case, or are there certain situations which help the compiler more than others?
This thread isn't geared toward a specific platform, but are some platform/compiler combinations more sensitive to this than others, or do they all pretty much assume a double for floating point constants without the "f"?
I've decided that I totally do not give a crap about being "proper" to identify things like parameters as being floats, when there's a prototype that can easily be command-clicked on for a declaration. Just about every situation I can think of should be pretty clear what the application of the constant is, and quite frankly, the code is cleaner and easier to read without the "f" and extra decimal points. So the only real purpose I can still see behind the "f" is to clue the compiler in on what I'm trying to do.
From what I've read, the 'f' suffix is really only advantageous in graphics programming. The advantage is that no conversion is necessary before a push across the bus if you have the suffix in place. I, like yourself, have been doing it more out of habit than anything else.
I'm pretty sure it's just an implied typecast, so I don't think it would really affect performance that much. Like Talyn was saying, avoiding the conversion might be advantageous for time-critical pieces of code, like graphics or physics algorithms, but for the most part, I'd say the effect is negligible.
K&R says that all literals with a decimal in it are doubles unless a suffix is included, so I'd assume that almost all C-based compilers out there would follow this rule.
I also want to note that some strongly-typed languages, like Java, will require the implied type of a literal to exactly match the variable type. So something like "float f = 10.4" will compile in C/C++, but will cause a type mismatch error in Java. However, you can use integer literals without a hitch: "float f = 10" works fine. Go figure...
In general, though, I've used the 'f' suffix mostly out of habit, like both of you.
AnotherJake Wrote:This thread isn't geared toward a specific platform, but are some platform/compiler combinations more sensitive to this than others, or do they all pretty much assume a double for floating point constants without the "f"?
K&R says that all literals with a decimal in it are doubles unless a suffix is included, so I'd assume that almost all C-based compilers out there would follow this rule.
I also want to note that some strongly-typed languages, like Java, will require the implied type of a literal to exactly match the variable type. So something like "float f = 10.4" will compile in C/C++, but will cause a type mismatch error in Java. However, you can use integer literals without a hitch: "float f = 10" works fine. Go figure...
In general, though, I've used the 'f' suffix mostly out of habit, like both of you.
Since when was "Fred" a placeholder variable?
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Help with Floats | clapton541 | 1 | 1,863 |
Apr 26, 2007 09:56 PM Last Post: PowerMacX |
|
| Comparing Floats Without == | Nick | 13 | 4,389 |
Nov 18, 2006 02:12 PM Last Post: akb825 |
|
| Cocoa Event Loop/NSTimer revisited | Fenris | 6 | 4,593 |
Oct 29, 2005 11:27 PM Last Post: maaaaark |
|

