# Thread: automatic conversion of int, float, etc.

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

2. 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

3. 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. Originally Posted by jackson6612
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. Originally Posted by Inanna
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.

Popular pages Recent additions