# Confused about a static_cast problem. This should be very basic

### Question

• Hello, I have a problem figuring out static_casting from my book which only tells me to understand how this example works, yet doesn't provide me the steps to figure it out. Go figure. I looked on wikipedia but couldn't figure it out either. Perhaps one of you can shine some light on this topic:

static_cast<int>(7.8 +
static_cast<double(15/2)) = static_cast<int>(7.8 + 7.0)
= static_cast<int>(14.8)
=14

ok so this is doing the exact opposite of what I would think it will do. The first line:

static_cast<int>(7.8 + makes me assume that "OK This is going to turn the floating point number 7.8 into a integer of 7.

Then the 2nd line does the opposite of what I would expect as well. Mainly I think
static_cast<double(15/2)) would give me Double floating point number which in this case would only be 7.5. WRONG. Apparently it's only 7.

Finally at the very end, the number is displayed as a integer.

Any ideas? Thanks.

cout << "static_cast<int>(7.8 + static_cast<double>(15) / 2) = "
<< static_cast<int>(7.8 + static_cast<double>(15) / 2 )
<< endl;
This gives me a reply of:
static_cast<int>(7.8 + static_cast<double>(15) / 2) = 15

cout << "static_cast<int>(7.8 + static_cast<double>(15 / 2)) = "
<< static_cast<int>(7.8 + static_cast<double>(15 / 2 ))
<< endl;
This gives me a reply of:
static_cast<int>(7.8 + static_cast<double>(15 / 2)) = 14

If these could be explained as well I'd appreciate it.  I understand that the order of precedence is changed but I'm not sure how that is affecting this outcome.  Thanks again.
Sunday, September 23, 2007 11:42 PM

•  AliRaz wrote:
 The first line: static_cast(7.8 + makes me assume that "OK This is going to turn the floating point number 7.8 into a integer of 7

The static_cast<int> ( expression ) statement casts the value of the expression into an int after the expression has been evaluated.  It does not change each sub-component of the expression before evaluating the expression.

It does not turn 7.8 into 7 but rather the statement static_cast<int> ( 7.8 + static_cast<double>(15) / 2 ) will first evaluate the expression '7.8 + static_cast<double>(15) / 2' and will then cast the result of that evaluation into an int.

So, what is the value of '7.8 + static_cast<double>(15) / 2' and what is its value after being cast to an int?

 Then the 2nd line does the opposite of what I would expect as well. Mainly I think static_cast

Again, remember that statiic<double>( expression ) will first evaluate the expression and then convert the result into a double.  So what is the value of the expression 15 / 2 and why?

(hint: what is the type of 15?  What is the type of 2?)

Monday, September 24, 2007 1:38 AM

### All replies

•  AliRaz wrote:
 The first line: static_cast(7.8 + makes me assume that "OK This is going to turn the floating point number 7.8 into a integer of 7

The static_cast<int> ( expression ) statement casts the value of the expression into an int after the expression has been evaluated.  It does not change each sub-component of the expression before evaluating the expression.

It does not turn 7.8 into 7 but rather the statement static_cast<int> ( 7.8 + static_cast<double>(15) / 2 ) will first evaluate the expression '7.8 + static_cast<double>(15) / 2' and will then cast the result of that evaluation into an int.

So, what is the value of '7.8 + static_cast<double>(15) / 2' and what is its value after being cast to an int?

 Then the 2nd line does the opposite of what I would expect as well. Mainly I think static_cast

Again, remember that statiic<double>( expression ) will first evaluate the expression and then convert the result into a double.  So what is the value of the expression 15 / 2 and why?

(hint: what is the type of 15?  What is the type of 2?)

Monday, September 24, 2007 1:38 AM
• Thanks for the hints Mr. Frank Boyne, it is now clarified.

(int)(7.8 + (double)(15) /2)
(int)(7.8 + (15.0)/2)
(int)(7.8 + 7.5)
(int)(15.3)
15

(int)(7.8 + (double)(15/2))
(int)(7.8 + (double)(7))
(int)(7.8 + 7.0)
(int)(14.8)
14

Monday, September 24, 2007 4:27 PM