float->long conversion

Some strange behavior here…

What do you say about this? Here are two code snippets and the resulting
machine code:


DWORD a = 100;
DWORD b = 1;
float c = 0.04;
float e;
e = a * b * c;
DWORD d = 0;
d = e;

9: DWORD a = 100;
00401028 mov dword ptr [ebp-4],64h
10: DWORD b = 1;
0040102F mov dword ptr [ebp-8],1
11: float c = 0.04;
00401036 mov dword ptr [ebp-0Ch],3D23D70Ah
12: float e;
13: e = a * b * c;
0040103D mov eax,dword ptr [ebp-4]
00401040 imul eax,dword ptr [ebp-8]
00401044 mov dword ptr [ebp-1Ch],eax
00401047 mov dword ptr [ebp-18h],0
0040104E fild qword ptr [ebp-1Ch]
00401051 fmul dword ptr [ebp-0Ch]
00401054 fstp dword ptr [ebp-10h]
14: DWORD d = 0;
00401057 mov dword ptr [ebp-14h],0
15: d = e;
0040105E fld dword ptr [ebp-10h]
00401061 call __ftol (0040113c)
00401066 mov dword ptr [ebp-14h],eax

And the other one:

DWORD a = 100;
DWORD b = 1;
float c = 0.04;
float e;
DWORD d = 0;
^^^^^^ This line has been moved upwards…
e = a * b * c;
d = e;

9: DWORD a = 100;
00401028 mov dword ptr [ebp-4],64h
10: DWORD b = 1;
0040102F mov dword ptr [ebp-8],1
11: float c = 0.04;
00401036 mov dword ptr [ebp-0Ch],3D23D70Ah
12: float e;
13: DWORD d = 0;
0040103D mov dword ptr [ebp-14h],0
14: e = a * b * c;
00401044 mov eax,dword ptr [ebp-4]
00401047 imul eax,dword ptr [ebp-8]
0040104B mov dword ptr [ebp-1Ch],eax
0040104E mov dword ptr [ebp-18h],0
00401055 fild qword ptr [ebp-1Ch]
00401058 fmul dword ptr [ebp-0Ch]
0040105B fst dword ptr [ebp-10h]
^^^^ Different instruction
15: d = e;
!!! fld instruction missing here !!!
0040105E call __ftol (0040113c)
00401063 mov dword ptr [ebp-14h],eax

The first example gives desired (and expected) result, d = 4. In the second
example, d = 3. If d is not initialized to 0, in both cases d = 3 at the end.
If c is put to be, say 0.05, both cases give d = 5.

I know that real numbers cannot be stored exactly as ‘0.04’, and there’s an
article in MSDN (Q48928) describing truncating errors in this type of
conversion, but getting different result because I move one line of code is
really weird…

Env: WinNT 4.0 SP6a, VC 6.0, SP3.

Marko
ICQ: 5990814

Q: How many Microsoft executives does it take to change a light bulb?
A: We can see no need for uninstallation and have therefore made no
provision for light bulbs to be removed.