Thread: Constant type question

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    10

    Constant type question

    I am currently learning and started with Beginning C 3rd Edition by Ivor Horton from Apress.

    On page 51
    Code:
    void main() {
      float plank_length = 10.0f;
      float piece_count = 4.0f;
      float piece_length = 0.0f;
      
      piece_length = plank_length/piece_count;
      printf("A plank %f feet long can be cut into %f pieces %f feet long.", plank_length, piece_count, piece_length);
    
    }
    I am trying to understand what the 'f' does at the end when I declare the variable. On page 50, it says "To write a constant of type float, you append an f to the number to distinguish if from type double; however, did not I initialized the variable as a float?

    I am not getting this.. and require some help.

    float is 4 bytes
    double is 8 bytes

    In addition, each type offers different level of precision.

    Thank you
    Last edited by jason_jackal; 02-12-2018 at 03:20 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,547
    I am trying to understand what the 'f' does at the end when I declare the variable.
    You have the answer to that in the next sentence:
    On page 50, it says "To write a constant of type float, you append an f to the number to distinguish if from type double;
    A floating point constant defaults to a double, if you're trying to store the value into a float it will be implicitly converted from a double to a float. Appending the 'f' tells the compiler that the constant is a type float not a type double eliminating the need for the implicit conversion,

    I am not getting this..
    A float requires 4 bytes of memory of storage, a double requires 8 bytes of memory in order to get roughly twice the precision.

    By the way unless you have some special reason you should prefer double instead of float so that you get the higher precision and less issues with round off problems.
    Last edited by jimblumberg; 02-12-2018 at 03:51 PM.

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    10
    Hi Jim,
    A floating point constant defaults to a double, if you're trying to store the value into a float it will be implicitly converted from a double to a float. Appending the 'f' tells the compiler that the constant is a type float not a type double eliminating the need for the implicit conversion,
    I did not know float was not a float by default and in fact was a double. I assumed it would be 'float' since I declared it as a float.

    So just so I understand...

    Code:
    float plank_length = 10.0;
    Is a double that is taking up 8 bytes of memory. 15 decimal precision.

    Code:
    float plank_length = 10.0f;
    Is truly saying it is a float and allocating 4 bytes. 7 decimal precision.

    Code:
    const float plank_length = 10.0;
    Is this the same thing? I might be getting ahead of myself.

    Thank you.
    JJ

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,547
    Is a double that is taking up 8 bytes of memory. 15 decimal precision.
    No, the constant is a double that is implicitly converted to a float when initializing the variable plank_length that is a float that takes 4 bytes of memory. The implicit conversion from a double to a float can be the cause of errors.

    Is truly saying it is a float and allocating 4 bytes. 7 decimal precision.
    Yes, with no implicit conversion required.

    Is this the same thing? I might be getting ahead of myself.
    No, this is creating a float constant from a double.

  5. #5
    Registered User
    Join Date
    Feb 2018
    Posts
    6
    Hey there,
    The "f" there stands just kind of works as an explicit declaration for the compiler so that it doesn't implicitly assigns the number as a double. Because by default the compiler tends to assign the decimal numbers as a double. It is valid that you're getting confused that despite writing "float plank_length=10.0;" why do you still need that clarification for the compiler. The "f" there is just a way of saying "Hello Mr. Compiler I would like this declaration to always remain a float all the time, even though I know double is more precise. So, please don't implicitly upcast it. Thank you." (The compiler really does appreciate good manners.)
    Hope this helps. Pardon me if I got it wrong.
    Cheers.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,767
    I think you might be getting confused because you're not using the word literal anyway.

    Code:
    /* 'f' is a constant of type float
     * 10.0f is a literal constant of type float
     * 20.0 is a literal constant (or just literal for short) of type double because the default type for real number literals -- those with decimal places -- is double
     * 20.0f is a literal of type float because we told the compiler we want a float and not the default type of double
     */
    float f = 10.0f;  /* Declare a variable of type float and assign 10.0f (literal float of value 10.0f) to 'f'
    f = 20.0;          /* The compiler will do an implicit cast from double to float when assigning because 20.0 is a double and f is a float */
    f = 20.0f;         /* Here we're explicitly saying 20.0f is a float */
    
    const float foo = 30.0f; /* foo is a const float, 30.0f is a literal constant of type float */
    const float bar = 40.0; /* bar is a const float, 40.0 is a literal constant of type double */
    I wouldn't say that floats have a precision of 7 decimal places. A float normally has 24 bits of precision (IEEE 754) but this is really up to the compiler at the end of the day. A double would normally have 53-bit precision if the implementation is using IEEE 754. Note that I haven't said 7 and 14 at all because that's not the way it works (single precision floating point [float] can normally represent 7.22 decimal digits and double 15.95), and explaining how it works is way beyond what I want to do in a forum post

    Here's what the C Standard says
    F.2 Types

    1 The C floating types match the IEC 60559 formats as follows:

    The float type matches the IEC 60559 single format.
    The double type matches the IEC 60559 double format.
    The long double type matches an IEC 60559 extended format,[357]) else a non-IEC 60559 extended format, else the IEC 60559 double format.

    Any non-IEC 60559 extended format used for the long double type shall have more precision than IEC 60559 double and at least the range of IEC 60559 double.[358])

    Recommended practice

    2 The long double type should match an IEC 60559 extended format.

    Footnotes

    [357] ''Extended'' is IEC 60559's double-extended data format. Extended refers to both the common 80-bit and quadruple 128-bit IEC 60559 formats.

    [358] A non-IEC 60559 long double type is required to provide infinity and NaNs, as its values include all double values.
    Note that it uses the word "recommend[ed]"

    Edit: Oh, this is what's said in the C Standard about IEC 60559
    The IEC 60559 floating-point standard is specifically Binary floating-point arithmetic for microprocessor systems, second edition (IEC 60559:1989), previously designated IEC 559:1989 and as IEEE Standard for Binary Floating-Point Arithmetic (ANSI/IEEE 754-1985)
    Further reading:
    IEEE 754 - Wikipedia
    Last edited by Hodor; 02-13-2018 at 12:41 AM.

  7. #7
    Registered User
    Join Date
    Feb 2018
    Posts
    10
    Thank you all for the help. I am excited to be learning C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cast a constant to a type
    By Ducol in forum C Programming
    Replies: 3
    Last Post: 12-04-2015, 12:30 PM
  2. integer constant is too large for 'long' type
    By rehman12390 in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2012, 06:12 AM
  3. Replies: 4
    Last Post: 12-22-2011, 01:41 PM
  4. What type is a macro constant?
    By Vespasian in forum C Programming
    Replies: 15
    Last Post: 09-08-2011, 11:10 AM
  5. integer constant is too large for type
    By Abda92 in forum C++ Programming
    Replies: 8
    Last Post: 02-09-2008, 11:47 AM

Tags for this Thread