Remove overcomplicated integer divisions

Believe it or not, there are still some remnants of the ActionScript
coding standards in the codebase! And one of them sometimes pops up
whenever an integer division happens.

As it so happens, it seems like division in ActionScript automatically
produces a decimal number. So to prevent that, the game sometimes
subtracts off the remainder of the number to be divided before
performing the division on it.

Thus, we get statements that look like

    (a - (a % b)) / b

And probably more parentheses surrounding it too, since it would be
copy-pasted into yet another larger expression, because of course it
would.

`(a % b)` here is subtracting the remainder of `a` divided by `b`, using
the modulo operator, before it gets divided by `b`. Thus, the number
will always be divisible by `b`, so dividing it will mathematically not
produce a decimal number.

Needless to say, this is unnecessary, and very unreadable. In fact, when
I saw these for the first time, I thought they were overcomplicated
_modulos_, _not_ integer division! In C and C++, dividing an integer by
an integer will always result in an integer, so there's no need to do
all this runaround just to divide two integers.

To find all of these, I used the command

    rg --pcre2 '(.+?).+?-.+?(?=\1).+?%.+?([\d]+?).+?\/.+?(?=\2)'

which basically matches expressions of the form 'a - a % b / b', where
'a' and 'b' are identical and there could be any characters in the
spaces.
This commit is contained in:
Misa
2021-09-24 17:21:46 -07:00
parent 6192269128
commit 891ca527f9
7 changed files with 23 additions and 33 deletions

View File

@@ -871,8 +871,8 @@ void customlevelclass::findstartpoint(void)
else
{
//Start point spawn
int tx=(customentities[testeditor].x-(customentities[testeditor].x%40))/40;
int ty=(customentities[testeditor].y-(customentities[testeditor].y%30))/30;
int tx=customentities[testeditor].x/40;
int ty=customentities[testeditor].y/30;
game.edsavex = ((customentities[testeditor].x%40)*8)-4;
game.edsavey = (customentities[testeditor].y%30)*8;
game.edsaverx = 100+tx;