Thread: implicit int to float type conversion in expression

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

    implicit int to float type conversion in expression

    I can't seem to find a definitive explanation of the rules for implicit int to float type conversion when an expression contains one float value and several int values, and also several operators. All of the examples are of an expression with just operator and two operands.

    From the operation of my program, it appears that the type conversion from int to float only occurs on an operator by operator basis, not on an expression wide basis.

    For example, this relevant part of my code works as expected:
    Code:
    float action[NSTRINGS];
    
    void adjustsetup(int a)    //    a has value of either 1 or -1
    {   ...
        action[string] = action[string] + a / 64.0;
        ...
    }
    while this results in a value of zero for a/64:
    Code:
    float action[NSTRINGS];
    
    void adjustsetup(int a)    //    a has value of either 1 or -1
    {   ...
        action[string] = action[string] + a / 64;
        ...
    }
    So the presence of the float action[string], in the expression does not automatically convert the int a and the constant 64?

    Each operator and it's two operands are treated individually for determining type conversion?

    -
    Last edited by megafiddle; 08-18-2015 at 11:33 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,390
    Quote Originally Posted by megafiddle
    Each operator and it's two operands are treated individually for determining type conversion?
    Yes:
    Quote Originally Posted by C11 Clause 6.3.1.8 Paragraph 1
    Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. For the specified operands, each operand is converted, without change of type domain, to a type whose corresponding real type is the common real type. Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise. This pattern is called the usual arithmetic conversions
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    > I can't seem to find a definitive explanation of the rules for implicit int to float type conversion when an expression contains one float value and several int values, and also several operators. All of the examples are of an expression with just operator and two operands.
    If you think in terms of mathematics and it fundamentals law of arithmetic, in order for the operator to do its operation you need the operands and without it there is no operation. Its the same withint the computing, however big is an expression, you start somewhere in your expression, that is, you start calculating the small part of the expression and result of it will be an operand for another operator. Strictly a operator can only work on 2 operands regardless how big is an expression is and therefore you should able to predict the type conversion as result.

    ~H
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Yes:
    Ok, thanks.

    I guess I had always thought it was an expression wide conversion. I just happened to catch this because I used 64 instead of 64.0. I normally use floating point representations for constants that are involved in floating point calculations.

    Would it be better to use a cast rather than a floating point constant to force conversion? eg:
    Code:
    ... (float)a / 64;
    ... a / 64.0;
    -

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,390
    Quote Originally Posted by megafiddle
    Would it be better to use a cast rather than a floating point constant to force conversion?
    I prefer the use of a floating point constant as it looks "cleaner" to me, perhaps even more so if you wrote:
    Code:
    action[string] += a / 64.0f;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by laserlight View Post
    I prefer the use of a floating point constant as it looks "cleaner" to me, perhaps even more so if you wrote:
    Code:
    action[string] += a / 64.0f;
    I think I will use that. It's very clear that one operand is a float.

    Quote Originally Posted by ssharish2005 View Post
    If you think in terms of mathematics and it fundamentals law of arithmetic, in order for the operator to do its operation you need the operands and without it there is no operation. Its the same withint the computing, however big is an expression, you start somewhere in your expression, that is, you start calculating the small part of the expression and result of it will be an operand for another operator. Strictly a operator can only work on 2 operands regardless how big is an expression is and therefore you should able to predict the type conversion as result.

    ~H
    Thanks. Makes sense.

    -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::shared_ptr<T> implicit conversion from T*
    By Elkvis in forum C++ Programming
    Replies: 25
    Last Post: 05-21-2013, 04:22 PM
  2. Implicit conversion between pointers?
    By nonlinearly in forum C Programming
    Replies: 13
    Last Post: 11-13-2011, 11:02 AM
  3. Implicit conversion problem
    By Elysia in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2008, 01:38 PM
  4. implicit conversion not working
    By Mr_Jack in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2004, 10:50 AM
  5. implicit conversion
    By cj56 in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2003, 11:36 AM