engine icon indicating copy to clipboard operation
engine copied to clipboard

Use exponentiation operator instead of Math.pow

Open willeastcott opened this issue 2 years ago • 7 comments

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.

willeastcott avatar Jun 26 '22 21:06 willeastcott

I prefer Math.pow. ** looks like something that is easy to mistype or mis-read

yaustar avatar Jun 27 '22 08:06 yaustar

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 avatar Jun 27 '22 12:06 kungfooman

@kungfooman ** takes precedence over / so you can't make that substitution.

willeastcott avatar Jun 27 '22 12:06 willeastcott

@willeastcott Its a special case, you can also write 2 ** -mipLevel.

kungfooman avatar Jun 27 '22 13:06 kungfooman

Haha, oh yeah, you're right.

willeastcott avatar Jun 27 '22 13:06 willeastcott

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.

kungfooman avatar Jun 27 '22 13:06 kungfooman

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.

mvaligursky avatar Jun 29 '22 10:06 mvaligursky

This doesn't seem to be overwhelmingly superior to Math.pow so I think I'll close this for now.

willeastcott avatar Sep 01 '22 20:09 willeastcott