# Thread: implicit int to float type conversion in expression

1. ## 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?

- 2. Originally Posted by megafiddle
Each operator and it's two operands are treated individually for determining type conversion?
Yes: 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 3. > 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 4. Originally Posted by laserlight 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. 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;` 6. Originally Posted by laserlight 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. Originally Posted by ssharish2005 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 