## digitalmars.D - Should ^^ (opPow) be left- or right-associative?

- KennyTM~ <kennytm gmail.com> Dec 05 2009
- bearophile <bearophileHUGS lycos.com> Dec 05 2009
- dsimcha <dsimcha yahoo.com> Dec 05 2009
- Don <nospam nospam.com> Dec 05 2009
- "Nick Sabalausky" <a a.a> Dec 05 2009
- Tim Matthews <tim.matthews7 gmail.com> Dec 05 2009
- Don <nospam nospam.com> Dec 05 2009
- Tim Matthews <tim.matthews7 gmail.com> Dec 05 2009

Currently in D (r283) opPow is left-associative: import std.stdio; void main() { // prints 19683 instead of 7625597484987: writeln(3.0 ^^ 3.0 ^^ 3.0); } But the mathematical convention is a^b^c == a^(b^c). Languages which the power operator is right-associative (3^3^3==7e12): - Python, Haskell, Ruby, Perl, Mathematica, Bash. Languages which the power operator is left-associative (3^3^3==19683): - Octave/MATLAB, Excel, BASIC.

Dec 05 2009

KennyTM~:But the mathematical convention is a^b^c == a^(b^c). Languages which the power operator is right-associative (3^3^3==7e12): - Python, Haskell, Ruby, Perl, Mathematica, Bash.

Doing things as in mathematical convention, Mathematica and Python sounds better. This was just the release V.0.1 of the built-in pow, it needs several improvements :-) Bye, bearophile

Dec 05 2009

== Quote from bearophile (bearophileHUGS lycos.com)'s articleThis was just the release V.0.1 of the built-in pow, it needs several

See also bug 3577. ^^ should have higher precedence than * and / to be consistent w/ mathematical convention. http://d.puremagic.com/issues/show_bug.cgi?id=3577

Dec 05 2009

bearophile wrote:KennyTM~:But the mathematical convention is a^b^c == a^(b^c). Languages which the power operator is right-associative (3^3^3==7e12): - Python, Haskell, Ruby, Perl, Mathematica, Bash.

Doing things as in mathematical convention, Mathematica and Python sounds better. This was just the release V.0.1 of the built-in pow, it needs several improvements :-) Bye, bearophile

Fortran too. Here's a link from the TCL language, with a nice explanation of the rationale for choosing right associativity. http://www.tcl.tk/cgi-bin/tct/tip/274.html

Dec 05 2009

"Don" <nospam nospam.com> wrote in message news:hfei3u$kvk$1 digitalmars.com...bearophile wrote:KennyTM~:But the mathematical convention is a^b^c == a^(b^c). Languages which the power operator is right-associative (3^3^3==7e12): - Python, Haskell, Ruby, Perl, Mathematica, Bash.

Doing things as in mathematical convention, Mathematica and Python sounds better. This was just the release V.0.1 of the built-in pow, it needs several improvements :-) Bye, bearophile

Fortran too. Here's a link from the TCL language, with a nice explanation of the rationale for choosing right associativity. http://www.tcl.tk/cgi-bin/tct/tip/274.html

Quick summary for those who don't want to dig through that whole page: (a ^^ b) ^^ c == a ^^ (b * c) <-- For all real values of a, b, and c So kinda useless, but... a ^^ (b ^^ c) == .... a ^^ (b ^^ c) So more useful.

Dec 05 2009

Don wrote:bearophile wrote:

Doing things as in mathematical convention, Mathematica and Python sounds better. This was just the release V.0.1 of the built-in pow, it needs several improvements :-) Bye, bearophile

Fortran too. Here's a link from the TCL language, with a nice explanation of the rationale for choosing right associativity. http://www.tcl.tk/cgi-bin/tct/tip/274.html

Don I thought you were the one who created the patch http://d.puremagic.com/issues/show_bug.cgi?id=3481 What was your reason for the choice of left associativity? To cite wikipedia: http://en.wikipedia.org/wiki/Associativity#Notation_for_non-associative_operations "x^{y^z}=x^{(y^z)}.\, The reason exponentiation is right-associative is that a repeated left-associative exponentiation operation would be less useful. Multiple appearances could (and would) be rewritten with multiplication: (x^y)^z=x^{(yz)}.\,"

Dec 05 2009

Tim Matthews wrote:Don wrote:

Doing things as in mathematical convention, Mathematica and Python sounds better. This was just the release V.0.1 of the built-in pow, it needs several improvements :-) Bye, bearophile

Fortran too. Here's a link from the TCL language, with a nice explanation of the rationale for choosing right associativity. http://www.tcl.tk/cgi-bin/tct/tip/274.html

Don I thought you were the one who created the patch http://d.puremagic.com/issues/show_bug.cgi?id=3481 What was your reason for the choice of left associativity?

It was a quick patch to determine if Walter could be swayed to include it, if he didn't need to implement it. I didn't get any positive feedback about it until it was put into svn.

Dec 05 2009

Don wrote:It was a quick patch to determine if Walter could be swayed to include it, if he didn't need to implement it. I didn't get any positive feedback about it until it was put into svn.

Ok thanks a lot for getting a truly important operator implemented. I tried to give my feedback in the ng post back in august http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=95596

Dec 05 2009