Hello,

Why wont the following code assign 0.5 to variable (double type):

Thanks for help!Code:`unsigned int num = 1;`

double d = num/2;

Printable View

- 11-05-2008l2usimple question about double
Hello,

Why wont the following code assign 0.5 to variable (double type):

Code:`unsigned int num = 1;`

double d = num/2;

- 11-05-2008Daved
You're dividing integers, so the result will be an integer.

1 and 2 are integer literals. 1.0 and 2.0 are double literals.

As long as one of the two operands is a double, the result will be a double, so the solution is to use 2.0 instead of 2. - 11-05-2008l2u
Thanks for explanation!