# internals Digest 23 Nov 2013 00:01:03 -0000 Issue 3138

- Thread: https://externals.io/message/70310
- Original archive: https://news-web.php.net/php.internals/70310
- Started by Walter Parker on 2013-11-23T03:51:31+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #70308 — Walter Parker — 2013-11-23T01:47:46+00:00

URL: https://externals.io/message/70310#70308

The Wikipedia page on Operator associativity, at
http://en.wikipedia.org/wiki/Operator_associativity, says the
following:

Associativity is only needed when the operators in an expression have
the same precedence. Usually + and - have the same precedence.
Consider the expression 7 - 4 + 2. The result could be either (7 - 4)
+ 2 = 5 or 7 - (4 + 2) = 1. The former result corresponds to the case
when + and - are left-associative, the latter to when + and - are
right-associative.

In order to reflect normal mathematical usage, addition, subtraction,
multiplication, and division operators are usually left-associative
while an exponentiation operator (if present) is right-associative.
Any assignment operators are also typically right-associative. To
prevent cases where operands would be associated with two operators,
or no operator at all, operators with the same precedence must have
the same associativity.


Walter


---------- Forwarded message ----------
>
> From: Sherif Ramadan <theanomaly.is@gmail.com>
> To: Jakub Zelenka <bukka@php.net>
> Cc: Nikita Popov <nikita.ppv@gmail.com>, Tjerk Meesters <tjerk.meesters@gmail.com>, PHP Internals <internals@lists.php.net>
> Date: Fri, 22 Nov 2013 19:00:59 -0500
> Subject: Re: [PHP-DEV] Power function as operator
> 
>
> > […]
>
>
> Right, that would be why I'm not *entirely* convinced.
>



-- 
The greatest dangers to liberty lurk in insidious encroachment by men
of zeal, well-meaning but without understanding.   -- Justice Louis D.
Brandeis

---

## #70309 — Sherif Ramadan — 2013-11-23T02:02:44+00:00

URL: https://externals.io/message/70310#70309 · In reply to: #70308

> The Wikipedia page on Operator associativity, at
> http://en.wikipedia.org/wiki/Operator_associativity, says the
> following:
>
> Associativity is only needed when the operators in an expression have
> the same precedence. Usually + and - have the same precedence.
> Consider the expression 7 - 4 + 2. The result could be either (7 - 4)
> + 2 = 5 or 7 - (4 + 2) = 1. The former result corresponds to the case
> when + and - are left-associative, the latter to when + and - are
> right-associative.
>
> In order to reflect normal mathematical usage, addition, subtraction,
> multiplication, and division operators are usually left-associative
> while an exponentiation operator (if present) is right-associative.
> Any assignment operators are also typically right-associative. To
> prevent cases where operands would be associated with two operators,
> or no operator at all, operators with the same precedence must have
> the same associativity.
>
>
> Walter
>
>
> ---------- Forwarded message ----------
> > […]
> tjerk.meesters@gmail.com>, PHP Internals <internals@lists.php.net>
> > […]
>
>
>
> --
> The greatest dangers to liberty lurk in insidious encroachment by men
> of zeal, well-meaning but without understanding.   -- Justice Louis D.
> Brandeis
>
> --
> 
> 
>
>

I think that's about the worst possible example for operator associativity
there is. Trying to distinguish order of operations in operators of the
same precedence is a moot point since math tells us they should be carried
out in the order they presented in. 7 - 4 + 2 should always be ((7 - 4) +
2) in any sane implementation of an operator precedence parser.

As for exponentiation being right-associative, I also disagree with that
article. The exponent operator doesn't have to be right associative. It
just has to have a higher precedence than multiplication. I would suggest
correcting the false claims made in that wikipedia article before quoting.

---

## #70310 — Walter Parker — 2013-11-23T03:51:31+00:00

URL: https://externals.io/message/70310#70310 · In reply to: #70309

If we want the the exponent operator in PHP code to model mathematical
notation, then right associative is a better match than left. The
issue is is two exponent operators  in between 3 numbers. The rule
about left to right is taught at time in a student's learning when all
operators are left associative. Later, when right associative
operators are taught, then then rule is modified/ changed to a more
complete version (left is left and right is right). What the Wikipedia
article says is that all operators of the same precedence should have
the same associative value. Your statement that math says all
operators should be done the order present means that there are not
math operators that are right associative. This is not true.
Assignment and function composition are right associative.

As for why the exponent operator SHOULD (not must, should) be right
assocative, let me give some reasons why:

First, when doing calculations, a left-associative exponential
operation is less useful than right assocaitive.
Fortran has it right to left
Python has it right to left
Others that have it are Bash, Perl, Scilab, Mathematica, and Maple
Google calculator has it right to left
My pocket calculator has it right to left

ADA makes it not associative [A**B**C is not a valid statement, it
must be written as (A**B)**C or A**(B**C)
TCL made it left, then later suggested that this was a mistake and
that it should actually be left
Matlab appears to be the major exception as it is left to right.


An example,
if it is left associative, then A**B **C means the same as A ** (B * C)
if it is right associative then A**B**C means A ** (B**C)

To me this stacking of the operator looks like the natural and normal
way to right it.

If you think the Wikipedia article is wrong or poorly written, I'd
suggest you find a few "cites" and rewrite the article to meet your
standards. I don't disagree with the statement that the exponential
operator is right associative, so I'm not interested in changing the
article.

If you can give a reason why left associative exponential operator
would work better in PHP (or math in general) than a right associative
one, please share the math theory or use cases with us. If this is a
case "well, it was the first rule I learned, so it is better than then
second rule", you are not going to be persuasive. At best, it might
make the parser and grammar simpler to write by hand (but people have
not done that for decades in CS). Even then, all you really need to do
use Dijkstra's Shunting-yard algorithm.


>
>
>
> 
> > […]
>
>
> I think that's about the worst possible example for operator associativity
> there is. Trying to distinguish order of operations in operators of the same
> precedence is a moot point since math tells us they should be carried out in
> the order they presented in. 7 - 4 + 2 should always be ((7 - 4) + 2) in any
> sane implementation of an operator precedence parser.
>
> As for exponentiation being right-associative, I also disagree with that
> article. The exponent operator doesn't have to be right associative. It just
> has to have a higher precedence than multiplication. I would suggest
> correcting the false claims made in that wikipedia article before quoting.
>



-- 
The greatest dangers to liberty lurk in insidious encroachment by men
of zeal, well-meaning but without understanding.   -- Justice Louis D.
Brandeis

---

## #70312 — Sanford Whiteman — 2013-11-23T04:30:51+00:00

URL: https://externals.io/message/70310#70312 · In reply to: #70310

> Matlab appears to be the major exception as it is left to right.

It seems VB, VBScript (ASP), and VB.NET's exponentiation op is
left-associative.

Together, those account for a terrific amount of code in the wild,
well outstripping the lines of production code -- if not the
associated developer skills -- of many of your examples.

Anyway, my take is it's unnecessary to have anything but `pow()` if it's
taken this long for this to be brought up.

-- Sandy
