what does the .f in 1000.f mean? c++ [duplicate]
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?
1000
is an int
literal.
1000.
is a double
literal.
1000.f
is a float
literal.
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.
.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!
It is telling you the literal 1000.
should be treated as a float
. Look here for details.
It means 1000.f is treated as a float.
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
.
精彩评论