Thread: Warning message truncation 'double' to 'float'

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    5

    Warning message truncation 'double' to 'float'

    Hi,

    I'm getting this warning message when I compile my C program. I'm learning for my intro to programming class, so I'm a newbie.

    The message is the subject of this posting.

    Warning: Trucation from 'double' to 'float'

    The code for the function I'm getting the message in is.

    float emp_calc(char code, float hrs, float bns)
    { //this is where my trouble lies
    float made;
    float g = 9.1; //get the warning "truncation from 'double' to 'float' here
    float s = 12.5;
    float m = 14.25;
    switch(pay_code)
    {
    case 'G':
    made = (float)hrs * g + bns;
    break;
    case 'S':
    made = (float)hrs * s + bns;
    break;
    case 'M':
    made = (float)hrs * m + bns;
    break;
    default:
    printf("\nError: You must input G, S, or M for the pay_code");
    }
    return(made);
    }

    I have no idea what the message means, but I figured it had to do with forcing the right side of my assignments to float, so I added the (float) before it. But I have no idea whether I'm using it right.

    Thanks one and all for your help.

    Tojam

  2. #2
    Unregistered
    Guest
    Try this:

    float g = 9.1f;
    float s = 12.5f;
    float m = 14.25f;

    Than get rid of your type casts I think! Not 100% sure though.

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    ok that warning means the compiler is resizing one type(doulbe) to a smaller one(float). you compiler appearently by default treats typed float point numbers as double precision, and is warning you that there is a possibility of data loss in the conversion from double to float!

    you could do several things to remedie this some may be possible or not some may just be dumb...

    1. Ignore it!(not neccessarily a good idea)

    2. somehow tell the compiler to treat typed float point numbers as floats not doubles(not always possible)

    3. tell yout compiler to ignore these truncation warnings(possible bad idea)

    4. use a type cast as you have( recommended)

    5. use doubles instead of floats

    6. specify that its a float by putting an f after the number like 9.1f( if possible this is what i recomend)

    ect... ect... use option 4 or 6.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    5
    Thank you for your help.

    I tossed the f after the float variables, and then for similar problems with assignment I found that I could do this

    int i = 4;
    float x = 3.2;
    float a;
    float r = 5.55;


    a = x * r * (float)i;

    I was just using the property improperly.

    Thanks one and all once again.

    Tojam

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Or

    a = (float)(x * r * i);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  2. Im stuck....
    By dAzed in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 04:50 PM
  3. Display list not displaying?
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 09-19-2004, 06:47 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM