iDevGames Forums
Silly Pet Peeve or something I should be doing... - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Silly Pet Peeve or something I should be doing... (/thread-4742.html)



Silly Pet Peeve or something I should be doing... - WhatMeWorry - Nov 30, 2005 01:05 AM

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.


Silly Pet Peeve or something I should be doing... - OneSadCookie - Nov 30, 2005 01:50 AM

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.


Silly Pet Peeve or something I should be doing... - WhatMeWorry - Nov 30, 2005 09:45 AM

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?


Silly Pet Peeve or something I should be doing... - Zekaric - Nov 30, 2005 02:19 PM

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.


Silly Pet Peeve or something I should be doing... - OneSadCookie - Nov 30, 2005 02:21 PM

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.


Silly Pet Peeve or something I should be doing... - akb825 - Nov 30, 2005 02:22 PM

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.


Silly Pet Peeve or something I should be doing... - TomorrowPlusX - Nov 30, 2005 02:34 PM

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.


Silly Pet Peeve or something I should be doing... - OneSadCookie - Nov 30, 2005 02:35 PM

It won't do anything so helpful.