sfall-team / sfall

sfall - Engine modifications for Fallout 2
https://sfall-team.github.io/sfall/
GNU General Public License v3.0
342 stars 40 forks source link

Interpreter: Subtracting negative int value from a float value produces unexpected result #488

Closed phobos2077 closed 1 year ago

phobos2077 commented 1 year ago

display_msg(""+(0.2000 - (-1)));

This code prints "-4294967296.00000" but expected "-0.80000"

phobos2077 commented 1 year ago

Seems like vanilla issue, error must be somewhere here:

0046A2C0 loc_46A2C0:                                  ; op_sub_+DCj
0046A2C0            mov     eax, [esp+34h+var_20]
0046A2C4            xor     ebp, ebp
0046A2C6            mov     dword ptr [esp+34h+var_34], eax
0046A2C9            mov     dword ptr [esp+34h+var_34+4], ebp
0046A2CD            fild    [esp+34h+var_34]
0046A2D0            fsubr   [esp+34h+var_24]
0046A2D4
0046A2D4 loc_46A2D4:                                  ; op_sub_+E6j
0046A2D4            fstp    [esp+34h+var_28]
0046A2D8            mov     ebx, [esp+34h+var_28]
0046A2DC            lea     edx, [ecx+24h]
0046A2DF            mov     eax, [ecx+1Ch]

var_20 is the second (integer) operand. Seems like it should convert integer to float but it does something like reinterpret_cast<float> instead.

phobos2077 commented 1 year ago

Turns out on September 11, 2014 I fixed the same issue in op_div and op_mult (on SVN repo), but apparently missed op_sub. Surprising this had to wait 9 years to get fixed.