Silly Pet Peeve or something I should be doing...
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.
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.
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.
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.
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?
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?
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.
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.
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.
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:
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.
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.
It won't do anything so helpful.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| (Probably silly) OpenGL/XCode question | stardark | 4 | 3,298 |
Jul 4, 2006 12:09 AM Last Post: OneSadCookie |
|

