We help IT Professionals succeed at work.

double to DWORD: strange result

mrwad99
mrwad99 asked
on
Medium Priority
1,737 Views
Last Modified: 2008-02-01
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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2009
Commented:
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));

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2009

Commented:
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)

Author

Commented:
Thanks :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.