Int's and doubles.

int n = 0;

n = (3 / 4 * 4 - 21 / 7.0) * 2 + 10 - 3.0;

cout << n << endl;

So, I thought when ever you have an int + double = double?
but when I put it in C9 and used the compiler, i got 1 rather than 1.0 like i thought it would be.

Can someone enlighten me in this topic please! Thank you!
the type of n won't be changed, it is an int and will allways be an int.
The thing that changes is the value of n because it is rounded after you calculated everything.
if everything is converted to the "finer" type then you get less computation errors (1st line, implicitly converted to double)
If everything would be converted to an integer before computing you'd get bigger errors (2nd line, explicitly converted to int)

For example:
1
2
n = 3 * 2.5; // n = 7
n = 3 * (int)2.5; // n = 6 
if you want a number to have say 1.0 at the end say you did 16 / 5 it normally rounds to a whole number instead use float.
that will hopefully help =)
The type of the expression (3 / 4 * 4 - 21 / 7.0) * 2 + 10 - 3.0 is double.Then the result will be converted into int and assigned to n,whose type is int.
Try this:
cout<<showpoint<<3 / 4 * 4 - 21 / 7.0) * 2 + 10 - 3.0<<endl;
Topic archived. No new replies allowed.