Search
Should ^^ (opPow) be left- or right-associative?
Dec 05, 2009
KennyTM~
Dec 05, 2009
bearophile
Dec 05, 2009
dsimcha
Dec 05, 2009
Don
Dec 05, 2009
Nick Sabalausky
Dec 06, 2009
Tim Matthews
Dec 06, 2009
Don
Dec 06, 2009
Tim Matthews
```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.

```
```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
```
```== Quote from bearophile (bearophileHUGS@lycos.com)'s article
> This was just the release V.0.1 of the built-in pow, it needs several
improvements :-)

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
```
```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" <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.

```
```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)}.\,"

```
```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.
```
```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
```