TIP #274: Right-Associativity for the Exponentiation Operator
_____________________________________________________________________________
TIP:	274
Title:	Right-Associativity for the Exponentiation Operator
Version:	$Revision: 1.9 $
Authors:	Arjen Markus <arjen.markus@wldelft.nl>
	David Smith <David.Smith@synopsis.com>
	Richard Suchenwirth <richard.suchenwirth-bauersachs@siemens.com>
	Don Porter <dgp@users.sf.net>
	Sérgio Loureiro <srgloureiro@gmail.com>
State:	Final
Type:	Project
Tcl-Version:	8.5
Vote:	Done
Created:	Fri Sep 15 00:00:00 UTC 2006
Keywords:	Tcl, expr
Abstract
This TIP clarifies and corrects the associativity behaviour of the exponentation operator that was introduced in [123].
Background
The exponentiation operator found in various programming languages has a distinct right associativity. In this way it differs from the other arithmetic operators. Some languages do implement with left-associativity but this is felt to be awkward by many users.
[123] introduced an exponentiation operator into Tcl 8.5 and was quite elaborate in defining its behaviour with respect to integer arguments, as there are quite a few corner cases to be examined.
However, it was vague on the issue of associativity and in fact showed by one example that the operator as implemented then was left-associative.
Rationale
A left-associative exponentiation operator is less useful than a right-associative operator, as pointed out in the Wikipedia article http://en.wikipedia.org/wiki/Associative . To illustrate, a left-associative exponentiation operator behaves like this:
x ** y ** z = (x ** y) ** z = x ** (y * z)
Whereas a right-associative exponentiation operator behaves like this:
x ** y ** z = x ** (y ** z)
Thus, if the operator is left-associative, then the above expression can be rewritten using a single exponentiation.
For this reason many programming languages and mathematical systems, among these Fortran, Perl, Python, Scilab, Mathematica and Maple, have chosen right-associativity. Several others, MATLAB and Octave, which is intended to be compatible to MATLAB, use left-associativity. (Microsoft Excel also uses left-associativity, but this product exhibits more quirky arithmetic behaviour and therefore should not be considered as a serious factor, in at least one author's opinion.)
Proposal
Therefore, given the expectation of programmers, the choice made in many programming languages and mathematical systems and the arguments about the limited usefulness of left-associativity, this TIP proposes to correct the behaviour of the exponentiation operator.
It should be right-associative, so that:
 2 ** 3 ** 4 = 2 ** (3 ** 4) = 2 ** 81 = 2417851639229258349412352
(and not 4096, if left-associativity is used)
Compatibility
No official version of Tcl 8.5 has been released yet. Existing "private" extensions that define an exponentiation operator that we are aware of use the right-associativity as well. Therefore this correction will only enhance compatibility.
Copyright
This document is placed in the public domain
_____________________________________________________________________________
Colophon
TIP AutoGenerator - written by Donal K. Fellows