I came across this line in some code and can't find the syntax defined anywhere:
*(float *)csCoord.nX = lImportHeight* .04f; /* magic number to scale font size */
If I remove the f
from .04f
then the compiler gives a warning about possible data loss due to a conversion from 'double' to 'float'. I assume the f
is doing some sort of typecasting.
Has anyone seen this before? Where is this defined in the C standard?
-
The trailing f indicates to the compiler that it is a float type literal, just like "" indicate a literal string and the L suffix indicates a long int.
The C++ spec for literals is the same.
-
Don't know where it's defined in the standard, but you use an
f
to indicate a float, otherwise the compiler assumes it's a double. Similarly, you can specify an integer to be unsigned by adding au
, or a long byl
(lowercase ell), etc. -
Without "f", the quantity 0.04 is a double. When you multiple lImportHeight with a double, you get another double. Then you store it in a float-sized memory location, losing precision. With "f", 0.04f is a float literal. You multiple lImportHeight with it, and you get a float; then the assignment does not cause loss of precision.
-
What exactly is the problem? Does your compiler choke on .04f? Try
(float) .04
Tim : No, it compiles fine. I just had never seen that syntax before and wanted to make sure I understood it.
0 comments:
Post a Comment