I have a query regarding conversion of what is essentially a double to a DWORD. Consider the following code:
double dProgress = 0.50296735905044510;
DWORD dwTimeTakenForProgressSoFar = 37001;
DWORD dwTimeRemaining2 = DWORD( (1.0 - dProgress) / (dProgress) ) * (dwTimeTakenForProgressSoFar); // In milliseconds
DWORD dwTimeRemaining = ( (1.0 - dProgress) / (dProgress) ) * (dwTimeTakenForProgressSoFar); // In milliseconds
dwTimeRemaining will be implicitly cast to a DWORD. Correct ? dwTimeRemaining2 will be a DWORD due to the explicit cast.
When I run this code, dwTimeRemaining2 is 0. dwTimeRemaining, on the other hand, is 36564.
Why did I add the cast to dwTimeRemaining2 ? Simple: the compiler moaned
" warning C4244: 'initializing' : conversion from 'double' to 'DWORD', possible loss of data"
if I did not. Indeed, it still says this about dwTimeRemaining.
Why are the two answers so different ?
(NOte: this bizarreness only occurs when dProgress >= 50.0)