Thread: Why use numerical type literals?

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Why use numerical type literals?

    I use openGL and often I'd see code (and thus repeat it) such as

    Code:
    GLfloat color[] = {1.0f, 1.0f, 1.0f, 1.0f};
    I'm wondering, can a type be inferred by the compiler from the regular use of 1.0 without the f that would warrant assigning a float literal to a type that is expecting a float?

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    1.0 is a double, 1.0f is a float.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by indigo0086 View Post
    I use openGL and often I'd see code (and thus repeat it) such as

    Code:
    GLfloat color[] = {1.0f, 1.0f, 1.0f, 1.0f};
    I'm wondering, can a type be inferred by the compiler from the regular use of 1.0 without the f that would warrant assigning a float literal to a type that is expecting a float?
    The compiler doesn't try to do anything like that. 1.0 is treated as a double when scanned. The literal value is then cast to float during the initialization. The reason the 'f' is explicitly specified is because the result of 1.2345f, for instance, might be a different value than (float)1.2345.

    Also, it allows the compiler to warn you if you accidentally use a literal value which is beyond the precision of a float.

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    ah, thanks for the clarification.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM