Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 942
Alex Avatar asked Feb 17 '11 08:02

Alex


People also ask

Why do you put F after a float?

Why is it used? The "F" indicates that the literal numeric value it is appended to is a float value. This is necessary information for the compiler and if not present can lead to errors or the compiler otherwise interpreting the number incorrectly.

Why do we use F in C# float?

Why is the f is required at the end of this number? Probably, because otherwise it would be treated as double . 0.58 (without the f suffix) is a literal of type double and one cannot assign a double value to a float, just like one cannot assign an int value to a string.

What is float f in C?

The f suffix simply tells the compiler which is a float and which is a double . See MSDN (C++)


5 Answers

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

like image 26
Lundin Avatar answered Oct 06 '22 02:10

Lundin


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++)

like image 130
James Avatar answered Oct 06 '22 03:10

James


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

like image 30
Grijesh Chauhan Avatar answered Oct 06 '22 01:10

Grijesh Chauhan


3.00 is a double, 3.00f is a float.

like image 45
Erik Avatar answered Oct 06 '22 01:10

Erik


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
like image 39
SridharKritha Avatar answered Oct 06 '22 03:10

SridharKritha