1. Member
Join Date
Jan 2011
Posts
2
Rep Power
0

## Adding an integer to a double?

int y = 3;
double z= y+4.0;

Will the right hand side of the bottom line return a double, or will this be an error? I know y is an integer, but I believe I heard something like if you if you write double z=5, then you'll get 5.0 as an output, but I am not sure.

Basically will the code work?

2. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8
Originally Posted by tokoolio
Basically will the code work?
Yes. y+4 will work too....

3. Originally Posted by tokoolio
Basically will the code work?

yes, the result will be a double.

4. Member
Join Date
Jan 2011
Posts
2
Rep Power
0
What if I had int z= y+4.5; would it just return y+4? Or perhaps int z = double y, would y just be rounded down?

Thanks btw.
Last edited by tokoolio; 01-15-2011 at 09:42 PM.

5. would it just return y+4?
No.
Or perhaps int z = double y, would y just be rounded down?
Yes.
:D

6. Member
Join Date
Jan 2011
Posts
3
Rep Power
0
Presence of any double in a expression results in promotion of all the variables in that expression to a double.
So yes the code will work returning a double value.