tags:

views:

58

answers:

1

Sometimes I write a float like:

0.1

and sometimes I add an f, like:

0.1f

It often seems that both ways work. I picked the f up from some sample code. But what's that actually good for? I'm sure it means "float", but do I really need that?

+6  A: 

I believe it is indicating that the number is a float (i.e. single-precision floating point) as opposed to a double (double-precision floating point), which is the default, i.e. no prefix. You might be able to use either interchangeably because the compiler can do implicit conversion, but by appending the suffix you're being explicit, and quite possibly has some performance benefits in avoiding the conversion too.

I don't have any direct knowledge of Obective-C, but this is the case for several other C-style languages (e.g. C#, Java, and C itself), and I would imagine it would be exactly the same here.

Noldorin
That is indeed the case. The only real issue is speed (when used in arithmetic gcc will convert from float to double and back)
rpetrich
@rpetrich: Yeah, I was suspecting that. The compiler is typically clever enough to do the conversion for you, though it's not always most efficient.
Noldorin