What is the difference between
unsigned int n = (n2-n1)/n3 + 1;
and
double N = (n2-n1)/n3 + 1;
unsigned int n = (unsigned int)(N);
where n1
, n2
, and n3
are double
s? I am asking this because with n1=3
, n2=4
and n3=0.1
, n
gets 10 in the first case and 11 in the second (at least on my computer). The correct answer is 11 so the result of the first one is wrong. I am guessing it has something to do with truncation to unsigned int
. Is that right? Is the right hand side expression in the first case not calculated as double
first, before being cast into unsigned int
(in which case it would be equivalent to the second piece of code)? Also, is there a way to get the shorter expression (the first one) to work?