Thread: automatic conversion of int, float, etc.

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    automatic conversion of int, float, etc.

    Hi

    Today the instructor marked the following line of the code wrong (I have shorten it):

    Code:
    int d = float a + int b + int c;
    He said that you are adding up different data types which is not correct. You must first convert 'float a' into an int.

    In the past I have several times written things like:
    Code:
    int b;
    float a = b / 10;
    He didn't tell me then that I was wrong. I believe the compiler takes care of such small details and automatic conversion takes place.

    Now see this code from a book:
    Code:
    void intfrac(float dumnumber, long& dumintpart, float& dumfracpart)
       {
    
       long temp = static_cast<long>(dumnumber);
       dumintpart = temp;
       dumfracpart = dumnumber - static_cast<float>(dumintpart);
    
       }
    I don't see there was real need for casting 'dumintpart' into float because it will automatically will be converted into a float to do the subtraction from 'dumnumber' which is a float. Please let me hear your opinion. Thank you.

    EDIT: I have made an edit; it appears in orange.
    Last edited by jackson6612; 06-01-2011 at 08:02 AM. Reason: unintended error
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    anyways it is wrong because you have this:
    int d = float a + int b + int c;
    it should be at least

    float a
    int b
    int c
    int d = a + b+ c ;
    or
    int d = (int) a+b+c
    You ended that sentence with a preposition...Bastard!

  3. #3
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    float a = (int b)/10;
    This should not even compile. Casting with parentheses looks like this
    Code:
    float a = (int)b / 10;
    or this
    Code:
    float a = int(b) / 10;
    Anything else, including your first line with no parentheses will fail to compile.

    The lines above will compile, but will not work the right way because both b and 10 are int. Say b is 12, 12 / 10 should be 1.2. But if both operands are int, the precision will be sliced off and 12 / 10 is 1. One of the operands has to be a float type or there will be no precision.
    Code:
    float a = (int)b / 10.0f;
    If one of the operands is a float type, the other will be widened too.

    I believe the compiler takes care of such small details and automatic conversion takes place.
    Sometimes. Narrowing conversions are dangerous, like when float is converted to long, because you are slicing off data. Widening conversions are safer, but sometimes it is better to show that you know what is happening with a cast.

    I don't see there was real need for casting 'dumintpart' into float because it will automatically will be converted into a float to do the subtraction from 'dumnumber' which is a float.
    You are right, dumintpart will be widened to float, and letting the conversion happen implicitly will not change the result.

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by jackson6612 View Post
    Hi

    Today the instructor marked the following line of the code wrong (I have shorten it):

    Code:
    int d = float a + int b + int c;
    He said that you are adding up different data types which is not correct. You must first convert 'float a' into an int.

    In the past I have several times written things like:
    Code:
    int b;
    float a = b / 10;
    He didn't tell me then that I was wrong. I believe the compiler takes care of such small details and automatic conversion takes place.

    Now see this code from a book:
    Code:
    void intfrac(float dumnumber, long& dumintpart, float& dumfracpart)
       {
    
       long temp = static_cast<long>(dumnumber);
       dumintpart = temp;
       dumfracpart = dumnumber - static_cast<float>(dumintpart);
    
       }
    I don't see there was real need for casting 'dumintpart' into float because it will automatically will be converted into a float to do the subtraction from 'dumnumber' which is a float. Please let me hear your opinion. Thank you.

    EDIT: I have made an edit; it appears in orange.
    they're both wrong; you have have been docked in both cases.

    in the case of
    Code:
    float a;
    int b, c;
    int d = a+b+c;
    a is implicitly truncated to 0 decimal places; not rounded. this is probably not what you wanted. if it is, then using an explicit cast is good practice.

    in the case of
    Code:
    int i = 8;
    float f = i/10;
    f will evaluate to 0, which is not the result of 8/10 in floating point.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Inanna View Post
    Casting with parentheses looks like this
    Code:
    float a = (int)b / 10;
    or this
    Code:
    float a = int(b) / 10;
    Technically that second one is not a cast, it's a direct constructor call. This is also why it is not possible in C, as C does not have constructors.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conversion to ieee from a float
    By srcsrc13 in forum C Programming
    Replies: 5
    Last Post: 10-04-2010, 04:41 PM
  2. int to float conversion
    By dereach in forum C Programming
    Replies: 6
    Last Post: 09-26-2008, 03:35 AM
  3. float to double conversion
    By cdalten in forum C Programming
    Replies: 2
    Last Post: 04-12-2006, 08:05 AM
  4. Fastest float to int conversion
    By Pea in forum C Programming
    Replies: 21
    Last Post: 01-26-2005, 11:08 PM
  5. Conversion from Float to String
    By laragon9 in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2001, 02:33 PM