Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent math with multiple assignments to the same variable #196

Open
terencehill opened this issue Jan 25, 2021 · 2 comments
Open

Comments

@terencehill
Copy link
Contributor

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
Copy link
Contributor Author

terencehill commented Jan 25, 2021

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.

@Blub
Copy link
Collaborator

Blub commented Jan 26, 2021

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants