What's the use of suffix `f` on float value

I am wondering what the difference is between these two variables in C:

float price = 3.00;

and

float price = 3.00f;

What is the use of suffix f in this case?

75696 次浏览

3.00 is a double, 3.00f is a float.

3.00 is interpreted as a double, as opposed to 3.00f which is seen by the compiler as a float.

The f suffix simply tells the compiler which is a float and which is a double.

See MSDN (C++)

That's because the default type of a floating point numeric literal - the characters 3.00 is double not float. To make this compile you have to add the suffix f (or F).

Often the difference isn't important, as the compiler will convert the double constant into a float anyway. However, consider this:

template<class T> T min(T a, T b)
{
return (a < b) ? a : b;
}


float x = min(3.0f, 2.0f); // will compile
x = min(3.0f, 2);   // compiler cannot deduce T type
x = min(3.0f, 2.0); // compiler cannot deduce T type

In addition to what has already been said, keeping track of 1.0 versus 1.0f is more important than many people realize. If you write code like this:

float x;
...
float y = x * 2.0;

Then x will be promoted to a double, because 2.0 is a double. The compiler is not allowed to optimize that promotion away or it would violate the C standard. The calculation takes place with double precision, and then the result is then implicitly truncated into a float. This means that the calculation will be slower (though more accurate) than it would have been if you had written 2.0f or 2.

Had you written 2, the constant would be of int type, which would be promoted to a float, and the calculation would have been done with "float precision". A good compiler would warn you about this promotion.

Read more about the "usual arithmetic conversion" rules here:

http://msdn.microsoft.com/en-us/library/3t4w2bkb%28v=vs.80%29.aspx

Because by unsuffixed floating-point literals are doubles, and rounding means that even small literals can take on different values when rounded to float and double. This can be observed in the following example:

float f=0.67;
if(f == 0.67)
printf("yes");
else
printf("no");

This will output no, because 0.67 has a different value when rounded to float than it does when rounded to double. On the other hand:

float f=0.67;
if(f == 0.67f)
printf("yes");
else
printf("no");

outputs yes.

The suffix can be specified using either upper or lowercase letters.

Try this also:

printf(" %u %u\n", sizeof(.67f), sizeof(.67));

Check @codepade

Adding few more combination of comparisons between float and double data types.

int main()
{
// Double type constant(3.14) converts to Float type by
// truncating it's bits representation
float a = 3.14;
// Problem: float type 'a' promotes to double type and the value
// of 'a'  depends on how many bits added to represent it.
if(a == 3.14)
std::cout<<"a: Equal"<<std::endl;
else
std::cout<<"a: Not Equal"<<std::endl;


float b = 3.14f; // No type conversion
if(b == 3.14)    // Problem: Float to Double conversion
std::cout<<"b: Equal"<<std::endl;
else
std::cout<<"b: Not Equal"<<std::endl;


float c = 3.14; // Double to Float conversion (OK even though is not a good practice )
if(c == 3.14f)  // No type conversion
std::cout<<"c: Equal"<<std::endl;  // OK
else
std::cout<<"c: Not Equal"<<std::endl;


float d = 3.14f;
if(d == 3.14f)
std::cout<<"d: Equal"<<std::endl; // OK
else
std::cout<<"d: Not Equal"<<std::endl;


return 0;
}

Output:

 a: Not Equal
b: Not Equal
c: Equal
d: Equal