Why do I need to cast this variable?

I want my probabilityofWinning variable to output a floating point number, so I have the following line of code, which works.

Note probabilityofWinning is of type double, playerWin and houseWin are both int.

 
probabilityOfWinning = (static_cast<double>(playerWin) / (playerWin + houseWin)) * 100;


What I want to know is why do I have to cast playerWin to get a floating point output? If I don't, and use:

 
probabilityOfWinning = (playerWin / (playerWin + houseWin)) * 100;


it's an int that gets assigned to probabilityOfWinning

Thanks
Last edited on
Because otherwise you'll have integer division which will give an integer result. It would be casted to a double implicitly but you will have a truncated result
So is the way I did it correct?
When the compiler sees the expression "a / b" where "a" and "b" are expressions or terms of type int,
the division is performed using integer division, regardless of what the result is assigned to.
Topic archived. No new replies allowed.