double to DWORD: strange result

Ah hello.

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)

TIA
LVL 19
mrwad99Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
DWORD dwTimeRemaining2 = DWORD( (1.0 - dProgress) / (dProgress) ) * (dwTimeTakenForProgressSoFar);
DWORD dwTimeRemaining = ( (1.0 - dProgress) / (dProgress) ) * (dwTimeTakenForProgressSoFar);

short answer : your cast is in the wrong place, it should have been :

DWORD dwTimeRemaining2 = (DWORD) (( (1.0 - dProgress) / (dProgress) ) * (dwTimeTakenForProgressSoFar));

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
long answer :

DWORD dwTimeRemaining2 = DWORD( (1.0 - dProgress) / (dProgress) ) * (dwTimeTakenForProgressSoFar);

Let's take this step by step :

1.0 - dProgress    --> float + double = double (0.4970326409495549)
(1.0 - dProgress) / (dProgress)    --> double / double = double (0.98820058997050149017498977558497)
DWORD( (1.0 - dProgress) / (dProgress) )     --> DWORD(double) = DWORD (explicit cast) (0)
DWORD( (1.0 - dProgress) / (dProgress) ) * (dwTimeTakenForProgressSoFar)      --> DWORD * DWORD = DWORD (0)
DWORD dwTimeRemaining2 = DWORD( (1.0 - dProgress) / (dProgress) ) * (dwTimeTakenForProgressSoFar);    -->   DWORD(DWORD) = DWORD (implicit cast) (0)


And the second one :

DWORD dwTimeRemaining = ( (1.0 - dProgress) / (dProgress) ) * (dwTimeTakenForProgressSoFar);

step by step (first 2 steps are same) :

( (1.0 - dProgress) / (dProgress) ) * (dwTimeTakenForProgressSoFar)    --> double * DWORD = double (36564.410029498525637964796686419)
DWORD dwTimeRemaining = ( (1.0 - dProgress) / (dProgress) ) * (dwTimeTakenForProgressSoFar);    --> DWORD(double) = DWORD  (implicit cast)  (36564)
mrwad99Author Commented:
Thanks :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.