\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#274: Right-Associativity for the Exponentiation Operator}
\date{September 15, 2006}
\author{Arjen Markus, David Smith, Richard Suchenwirth, Don Porter, S\'{e}rgio Loureiro}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#274}&\textbf{Right-Associativity for the Exponentiation Operator}\\\hline
Author&

Arjen Markus \tipmail{arjen.markus}{wldelft.nl}
\par
David Smith \tipmail{David.Smith}{synopsis.com}
\par
Richard Suchenwirth \tipmail{richard.suchenwirth-bauersachs}{siemens.com}
\par
Don Porter \tipmail{dgp}{users.sf.net}
\par
S\'{e}rgio Loureiro \tipmail{srgloureiro}{gmail.com}
\\
Created&Friday, \(\text{15}^{\text{th}}\) September 2006\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.9 $\\
Keywords&Tcl, expr\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP clarifies and corrects the associativity behaviour of the exponentation operator that was introduced in \cite{tip123}.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{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.

\cite{tip123} 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.

\section{Rationale}

A left-associative exponentiation operator is less useful than a right-associative operator, as pointed out in the Wikipedia article \url{http://en.wikipedia.org/wiki/Associative} . To illustrate, a left-associative exponentiation operator behaves like this:

\begin{quote}
\textit{x} \textbf{**} \textit{y} \textbf{**} \textit{z} = (\textit{x} \textbf{**} \textit{y}) \textbf{**} \textit{z} = \textit{x} \textbf{**} (\textit{y} * \textit{z})
\end{quote}

Whereas a right-associative exponentiation operator behaves like this:

\begin{quote}
\textit{x} \textbf{**} \textit{y} \textbf{**} \textit{z} = \textit{x} \textbf{**} (\textit{y} \textbf{**} \textit{z})
\end{quote}

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

\section{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:
\begingroup\small\begin{verbatim}
 2 ** 3 ** 4 = 2 ** (3 ** 4) = 2 ** 81 = 2417851639229258349412352
\end{verbatim}
\endgroup

(and not 4096, if left-associativity is used)

\section{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.

\section{Copyright}

This document is placed in the public domain

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#1}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#123]{tip123}
Arjen Markus,
Donal K. Fellows,
\emph{Adding an Exponentiation Operator to the [expr] Command},
on-line at \url{http://purl.org/tcl/tip/123.html}
\end{thebibliography}
\end{document}
