Exponentiation operator precedence

Nathan White nw at nwhite.net
Thu Aug 27 18:07:16 UTC 2015

Anecdotal evidence via a quick github search

A significant number of the usages are unit tests. Many others are repeats
from popular libraries like D3.

The most extreme use case I could find:

On Thu, Aug 27, 2015 at 11:19 AM, Steve Fink <sphink at gmail.com> wrote:

> On 08/27/2015 09:25 AM, Dean Tribble wrote:
>> Ideally syntax proposals should include some frequency information to
>> motivate any change. Is there an easy search to estimate the frequency of
>> Math.pow? In my application codebase (financial app with only modest JS
>> use), there are very few uses, and there are as many uses of Math.sin as
>> there are of Math.pow.
> Frequency relative to what, though? If code that does nontrivial math is a
> very small proportion of total JS code, and yet the exponentiation operator
> makes that code much more readable, then what is the conclusion? I would
> argue that ** precedence confusion is irrelevant to code that isn't going
> to use Math.pow in the first place. So it's a question of whether ** is a
> big enough readability win in code that computes exponents.
>> Anecdotally, my eyes caught on: "-Math.pow(2,-10*a/1)" (from a charting
>> library) which makes me not want to have to review code where I'm worried
>> about the precedence of exponentiation.
> I'd have to write that out: -2**(-10*a/1). That doesn't seem too bad.
> For myself, I do very much prefer Math.sqrt(a**2 + b**2) to
> Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)). The verbosity and uneven
> density of notation is really bothersome -- for any equation like the
> second one, I guarantee that I'll rewrite it on paper to figure out what
> it's saying. (Ok, maybe not for that specific formula, but even there I'll
> mentally render it.) I would not need to do so with the first one. Jumping
> between prefix and infix is jarring.
> Then again, I could make the same argument for Math.sqrt(a**2 + b**2) vs
> (a**2 + b**2) ** 0.5. And I don't like the second one much. But people
> don't interchange those when handwriting formulas, either.
> Math.sqrt(a.pow(2) + b.pow(2)) is an interesting middle point. I initially
> thought it struck the right balance, but seeing it written out, it still
> looks far inferior to me.
> A more complex example might help:
>   a * (b - a)**(x - 1/2 * (b - a)**2)
> vs
>   a * Math.pow(b - a, x - 1/2 * Math.pow(b - a, 2))
> vs
>   a * (b - a).pow(x - 1/2 * (b - a).pow(2))
> For me, the middle one is a mess. I can't make sense of it, and I can't
> spot the common (b - a) expression at all. The first one is as readable as
> such formulas ever are when written out with ASCII text. The third one is
> somewhere in between. I can see the common (b - a), and perhaps if I got
> more used to seeing .pow I could mentally make use of it without writing it
> on paper, but for now I cannot. Part of the problem is that I can easily
> translate "x**2" into "x squared", but "x.pow(2)" is raising x to the power
> of 2.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150827/3812cb96/attachment.html>

More information about the es-discuss mailing list