• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1493
  • Last Modified:

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
0
mrwad99
Asked:
mrwad99
  • 2
1 Solution
 
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));
0
 
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)
0
 
mrwad99Author Commented:
Thanks :)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now