View Single Post
Old 10-17-2004, 03:21 PM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
what's going on here is the order of type promotions is getting shifted around.

Code:
/* #1 */
average =  total / gradeCounter;
/* #2 */
average = static_cast< double >( total ) / gradeCounter;
in line #1, what you are doing is taking one int (total), and dividing it by another int (gradeCounter). that result is then cast to a double and stored into average. so, in integer math, 11 / 2 = 5. this is then cast to a double, and average is 5.0.

in line #2, the first thing that happens is that "total" is promoted to a double. now, you can't divide a double by an int, so "gradeCounter" is promoted to a double too. 11.0 / 2.0 = 5.5. the result is already double, so no promotion is necessary.

now, perhaps someone better at c++ than i can explain to us the value of static_cast over a plain cast.
joe_bruin is offline   Reply With Quote