Silly Pet Peeve or something I should be doing...

Member
Posts: 321
Joined: 2004.10
Post: #1
Isn't it redundant to have a leading zero, a decimal point, AND a "f"?

Is there anything gained by including the f?

functionNameChangedToProtectTheInnocent(0.0f,0.0f,0.5f,0.1f,0.0f,0.1f);

my code seems to compile and run fine without the f.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
f says "single precision constant". That is, 0.0f is of type float, where 0.0 is of type double

With the f, there's a good chance that GCC will generate an "lfs" (load floating-point single-precision [4 bytes]) instruction.

Without it, it'll more than likely generate an "lfd" (load floating-point double-precision [8 bytes]) followed by an "frsp" (floating-point round to single-precision) instruction, which is a lot slower.

IOW, yes, it's probably a good idea to use f religiously for single-precision floating point constants.
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #3
Ok, understood. Thanks! But, at least for function invocations, isn't the function prototype going to specify whether this is a float or a double? (Here I did go into
void functionNameChangedToProtectTheInnocent(float ,float, float ,float ,float,float); and change one parameter to double. And
gcc did complain flag an error.

Maybe it is redundant here, but other places your explainatin holds true?
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #4
If you set the compiler's warning level high enough (I usually set it to 'pedantic') you should get warnings along the lines of "converting double to float, possible loss of data." Things like may be important to you if loss of precision is going to be a factor.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
I doubt it's redundant, even in the parameter list to a function. I think it will still start with a double and round it off to a float.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #6
With optimizations enabled, I'm sure that gcc will catch that you're putting a double constant into a float and make it a double for you.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #7
When I sharked some code a while back, Shark made it clear that GCC wasn't automatically converting "x.y" constants into floats.

Now, that said, I only ever shark my debug builds, since I get so much more legible info that way. A fully optimized release might not have these issues. Nonetheless, I always add the 'f', since 99.99% of the time I use floats.

Note: If you're concerned about this stuff, you might want to use the single precision math functions too, like cosf(), sinf(), fabs(), etc, since they don't require double conversions. Consider, if you were to call:

Code:
float b = 30.0f;
float a = cos( b );

You'd get two implicit conversions, once converting 'b' from a float to a double, and once converting the return from cos() into a float.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
It won't do anything so helpful.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  (Probably silly) OpenGL/XCode question stardark 4 3,781 Jul 4, 2006 12:09 AM
Last Post: OneSadCookie