gmqcc icon indicating copy to clipboard operation
gmqcc copied to clipboard

Inconsistent math with multiple assignments to the same variable

Open terencehill opened this issue 4 years ago • 2 comments

float myfloat, res;
res = (myfloat = 4) - (myfloat = 3); // res == 1
res = (myfloat = 4, myfloat) - (myfloat = 3, myfloat); // res == 0

Both operations should produce the same result, if I'm not mistaken, whereas the former returns 1, the latter 0.

The same operations in C produce the same result (0) at least in this online compiler: https://onlinegdb.com/rJTVFGcJO

terencehill avatar Jan 25 '21 21:01 terencehill

I spotted this inconsistent behavior using Xonotic's vec2 macro:

noref vector _vec2;
#define vec2(...) EVAL(OVERLOAD(vec2, __VA_ARGS__))
#define vec2_1(v) (_vec2 = (v), _vec2.z = 0, _vec2)
#define vec2_2(x, y) (_vec2_x = (x), _vec2_y = (y), _vec2)

vec2(v1) - vec2(v2) always returns '0 0 0' regardless of the values of v1 and v2, so, assuming '0 0 0' is the correct result, operations with more than one call to vec2 in a single assignment should be avoided in the qc code.

terencehill avatar Jan 25 '21 21:01 terencehill

After some discussions we think that this should emit a warning but is otherwise not really a "bug". Given C's definition of sequencing, the result types of assignments and, in fact, some compilers warning about code with multiple assignments to the same variable in one expression, I think that's the best way to go. IMO our results make just as much sense as C's. In fact, if anything, I'd consider the 2nd one to be off, but that one does match C...

Blub avatar Jan 26 '21 08:01 Blub