Exponentiation operator precedence
Jason Orendorff
jason.orendorff at gmail.com
Thu Aug 27 20:17:15 UTC 2015
Don't rely on github searches to turn up representative examples. It
doesn't work that well. Here's my educated guess as to how ** will be
used.
The most common use will be to square numbers.
a²
a**2
Math.pow(a, 2)
a.pow(2)
Currently you might write `a * a`, which is kind of lame.
So where's the benefit? If this trivial thing is the most common use
of exponentation, why bother?
The ability to look at an expression and understand it at a glance is
a big deal.
x² + y² > limit
x**2 + y**2 > limit
Math.pow(x, 2) + Math.pow(y, 2) > limit
x.pow(2) + y.pow(2) > limit
A big big deal. It's the reason we have arithmetic operators in JS in
the first place.
Exponentiation is common when computing easing functions, curves for
graphics, interest, simulations, random stuff in games. Nth roots are
fairly common too (`apr**(1/12)`). In all of these cases, the user is
doing the same thing: translating a mathematical formula they wish to
use from "math" to JS. It is not extra hard to translate such a
formula using Math.pow(), but it is harder to read once you're done.
You have to mentally translate back to "math".
-j
More information about the es-discuss
mailing list