www.digitalmars.com         C & C++   DMDScript  

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

reply KennyTM~ <kennytm gmail.com> writes:
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
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
next sibling parent dsimcha <dsimcha yahoo.com> writes:
== Quote from bearophile (bearophileHUGS lycos.com)'s article
 This 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
prev sibling parent reply Don <nospam nospam.com> writes:
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
next sibling parent "Nick Sabalausky" <a a.a> writes:
"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
prev sibling parent reply Tim Matthews <tim.matthews7 gmail.com> writes:
Don wrote:
 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

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
parent reply Don <nospam nospam.com> writes:
Tim Matthews wrote:
 Don wrote:
 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

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
parent Tim Matthews <tim.matthews7 gmail.com> writes:
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