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 ----------
Quoted text · 11 lines
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 operatorHi,
I'm not entirely sure that making it non-associative is a great idea either, but I have to agree that it does find some middle ground. Right-associative exponent operator didn't seem right to me either.
I think there is a good mathematical reason for right associative exponent. It calls tetration or power tower. Please look at following sources for more details:
http://math.stanford.edu/circle/notes08f/bignumbers.pdf http://en.wikipedia.org/wiki/Tetration
http://mathworld.wolfram.com/PowerTower.htmlRegards
Jakub
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
Quoted text · 20 lines
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 operatorHi,
I'm not entirely sure that making it non-associative is a great idea either, but I have to agree that it does find some middle ground. Right-associative exponent operator didn't seem right to me either.
I think there is a good mathematical reason for right associative exponent. It calls tetration or power tower. Please look at following sources for more details:
http://math.stanford.edu/circle/notes08f/bignumbers.pdf http://en.wikipedia.org/wiki/Tetration
http://mathworld.wolfram.com/PowerTower.htmlRegards
Jakub
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--
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.
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 [ABC is not a valid statement, it must be written as (AB)C or A(BC)
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 AB C means the same as A ** (B * C) if it is right associative then ABC 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.
Quoted text · 22 lines
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 operatorHi,
I'm not entirely sure that making it non-associative is a great idea either, but I have to agree that it does find some middle ground. Right-associative exponent operator didn't seem right to me either.
I think there is a good mathematical reason for right associative exponent. It calls tetration or power tower. Please look at following sources for more details:
http://math.stanford.edu/circle/notes08f/bignumbers.pdf http://en.wikipedia.org/wiki/Tetration
http://mathworld.wolfram.com/PowerTower.htmlRegards
Jakub
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--
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
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