View mode: basic / threaded / horizontal-split · Log in · Help
December 05, 2009
Should ^^ (opPow) be left- or right-associative?
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.
December 05, 2009
Re: Should ^^ (opPow) be left- or right-associative?
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
December 05, 2009
Re: Should ^^ (opPow) be left- or right-associative?
== 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
December 05, 2009
Re: Should ^^ (opPow) be left- or right-associative?
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
December 05, 2009
Re: Should ^^ (opPow) be left- or right-associative?
"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.
December 06, 2009
Re: Should ^^ (opPow) be left- or right-associative?
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)}.\,"
December 06, 2009
Re: Should ^^ (opPow) be left- or right-associative?
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.
December 06, 2009
Re: Should ^^ (opPow) be left- or right-associative?
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
Top | Discussion index | About this forum | D home