engine
engine copied to clipboard
Use exponentiation operator instead of Math.pow
JavaScript now support the exponentiation operator (**
). This essentially does the same thing as Math.pow
. There is an ESLint rule called prefer-exponentiation-operator
(which we currently have disabled) - and it makes the argument for the rule as follows:
Introduced in ES2016, the infix exponentiation operator ** is an alternative for the standard Math.pow function.
Infix notation is considered to be more readable and thus more preferable than the function notation.
So this PR is a suggestion to poll opinion as to whether we adopt officially **
.
I confirm I have read the contributing guidelines and signed the Contributor License Agreement.
I prefer Math.pow
. **
looks like something that is easy to mistype or mis-read
Personally I like removing fluff from code, to make it as expressive in least amount of space. Never even realized that **=
is possible, I like that one.
There are three cases of resMult = 1 / 2 ** mipLevel;
which could become resMult = 0.5 ** mipLevel;
to get rid of the division and its just a bit shorter (not sure if its easier to understand).
@kungfooman **
takes precedence over /
so you can't make that substitution.
@willeastcott Its a special case, you can also write 2 ** -mipLevel
.
Haha, oh yeah, you're right.
I'm not sure whats easiest on the eye, I read 0.5 ** mipLevel
as "half it a bunch of times" and it makes most sense to me to generate the [1, 0.5, 0.25, 0.125, ...]
sequence.
Not sure I prefer ** given the extra complication of operator precedence, but it's not a strong objection.
I feel the same. To me the code is harder to understand, if the brackets are missing. Perhaps we should have brackets around it (apart from a trivial code).
Exponentiation is also right-associative (unlike multiply and divide) but it's on common to have code like 2 ** 3 ** 4
so this is a small issue.
I also checked ES5 compilation, and its transpiled to Math.pow
.. so that's great.
I'm approving, but I'm not 100% behind this.
This doesn't seem to be overwhelmingly superior to Math.pow
so I think I'll close this for now.