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

if you change a variable in an expression and use it twice gives undetermined result ( a = i++ + i;) #224

Closed
rioj7 opened this issue Dec 30, 2023 · 0 comments

Comments

@rioj7
Copy link

rioj7 commented Dec 30, 2023

In collide.js there are 2 lines with this issue

d3-force/src/collide.js

Lines 52 to 53 in c3e73cf

l = (r - (l = Math.sqrt(l))) / l * strength;
node.vx += (x *= l) * (r = (rj *= rj) / (ri2 + rj));

in line 52 what value of l is used in / l * strength

in line 53 what value of rj is used in ri2 + rj and it no longer is a radius it is a radius2

The compiler/interpreter is free to choose whichever sub expression to calculate first. Thus the resulting value depends on the compiler/interpreter used.

Better to rewrite the code to make it unambiguous what is calculated:

l = Math.sqrt(l);
l = (r - l) / l * strength;
rj2 = rj * rj;
node.vx += (x *= l) * (f = rj2 / (ri2 + rj2));
node.vy += (y *= l) * f;
data.vx -= x * (f = 1 - f);
data.vy -= y * f;

In the function r is used as a distance between center points. Re-using the name for a correction factor is confusing.

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

No branches or pull requests

2 participants