12

I am following a tutorial on how to make a game with SDL. At a certain point in the tutorial I need to calculate the game's FPS. The tutorial does the following:

caption << "Average Frames Per Second: " << frame / ( fps.get_ticks() / 1000.f );

Now, I know exactly what the code does, except for the part where it divides by 1000.f. I've been looking but just can't find what .f means.

So my question is, what does .f mean? And why is it there?

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
SimplyZ
  • 898
  • 2
  • 9
  • 22

6 Answers6

21

1000 is an int literal.

1000. is a double literal.

1000.f is a float literal.

Luc Danton
  • 34,649
  • 6
  • 70
  • 114
9

It means this is a float constant rather than a double constant. As for the effect, on most C++ compilers, it will have no effect as the float will be converted to a double to do the divide.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
7

.f makes the number float type.

Just see this interesting demonstration:

float a = 3.2;
if ( a == 3.2 )
    cout << "a is equal to 3.2"<<endl;
else
    cout << "a is not equal to 3.2"<<endl;

float b = 3.2f;
if ( b == 3.2f )
    cout << "b is equal to 3.2f"<<endl;
else
    cout << "b is not equal to 3.2f"<<endl;

Output:

a is not equal to 3.2
b is equal to 3.2f

Do experiment here at ideone: http://www.ideone.com/WS1az

Try changing the type of the variable a from float to double, see the result again!

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Isn't the `f` in the `b` assignment redundant? – naught101 Aug 01 '13 at 07:25
  • @naught101: Yes. It is redundant practically. If you don't use it, then `3.2` which is `double` will convert to `float` anyway. BTW, that is not *assignment*; that is *initialization*. – Nawaz Aug 01 '13 at 08:25
  • Ah, thanks. I have no idea about C++. Presumably compilers would be smart enough to avoid the conversion, and you'd end up with the same compiled code, right? – naught101 Aug 01 '13 at 09:03
5

It is telling you the literal 1000. should be treated as a float. Look here for details.

linuxuser27
  • 7,183
  • 1
  • 26
  • 22
5

It means 1000.f is treated as a float.

Ivan
  • 10,052
  • 12
  • 47
  • 78
4

A floating point literal can have a suffix of (f, F, l or L). "f and F" specify a float constant and "l and L" a double.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73