Thread: Use of 'f' letter in float variables or constants

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    37

    Use of 'f' letter in float variables or constants

    I see that in different books one writer uses the f at the end of every float and another writer in his book omit the 'f' letter completely.
    What is the best strategy?
    Thnx.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Be specific in your coding.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    float a = 1.0f;
    double b = 1.0;
    Unless you have a really good reason to use float, always use doubles.

    40 years ago, when C was invented, memory was scarce and CPU power was limited (and seldom had FPUs). The tradeoff between floats and doubles was a useful choice.

    Except for low-end embedded micros, an FPU is pretty standard and there is plenty of memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    Run this little program to see a difference:

    Code:
    #include <stdio.h>
    
    int main (void)
    {
     float f = 0.1f; 
    
     if (f == 0.1)
       printf("Equal\n");
     else
       printf("Not equal\n");
    
     if (f == 0.1f)
       printf("Equal\n");
     else
       printf("Not equal\n");
    }
    It prints this:

    Not equal
    Equal


    The 0.1 in the first if test is converted to a double by the compiler, so it is a more accurate representation of the infinitely repeating binary equivalent of decimal 0.1.

    (Yes, I know this example is a bit contrived, and I'm comparing floating-point values for equality (usually frowned upon), but you get my point: behavior can differ with or without the 'f' suffix.)

    BTW, the line "float f = 0.1f;" could just as well have left off the 'f'; in this case, the suffix does not matter -- the compiler converts to a float.

    (For related reading, see my article When Floats Don’t Behave Like Floats - Exploring Binary .)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Model Rocket Altitude predictor...
    By kalor_alros in forum C++ Programming
    Replies: 11
    Last Post: 09-04-2009, 12:27 AM
  2. Reflective Factory Design
    By Shamino in forum Game Programming
    Replies: 4
    Last Post: 12-16-2005, 06:50 PM
  3. Replies: 6
    Last Post: 11-12-2005, 11:57 AM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Why does it work in Visual Studio and Not Borland
    By MonteMan in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2002, 09:36 PM