\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#123: Adding an Exponentiation Operator to the [expr] Command}
\date{December 16, 2002}
\author{Arjen Markus, Donal K. Fellows}
\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 \#123}&\textbf{Adding an Exponentiation Operator to the [expr] Command}\\\hline
Author&

Arjen Markus \tipmail{arjen.markus}{wldelft.nl}
\par
Donal K. Fellows \tipmail{donal.k.fellows}{man.ac.uk}
\\
Created&Monday, \(\text{16}^{\text{th}}\) December 2002\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.6 $\\
Keywords&mathematics, evaluation\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes to add a new operator to the operators recognised by the [expr] command: the exponentiation operator. This operator will enhance the functionality of the current \textit{pow()} function by returning a result that depends on the type of its operands. It will also make complicated formulae more readable.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Introduction}

Currently Tcl's [expr] command uses the exponentiation function \textit{pow()} to calculate such expressions as ``2 to the power 10''. The drawback of this is twofold:
\begin{itemize}
\item{}
Expressions using several exponentiations become difficult to read. For instance, a third-degree polynomial looks like:
\begingroup\small\begin{verbatim}
 2.0*pow($x,3) - 1.2*pow($x,2) + 3.0*$x + 4.0
\end{verbatim}
\endgroup

or:
\begingroup\small\begin{verbatim}
 2.0*$x*$x*$x - 1.2*$x*$x + 3.0*$x + 4.0
\end{verbatim}
\endgroup
\item{}
The result of raising an integer to an integer power is a double: 2 to the power 10 is 1024.0, not 1024.
\end{itemize}

Other languages, like for instance FORTRAN, use an operator instead of a function. Two operators are commonly found: ** and \textasciicircum{}. As the latter already has a meaning within the [expr] command, we propose to add the ``**'' operator instead. The above example would become:
\begingroup\small\begin{verbatim}
 2.0*$x**3 - 1.2*$x**2 + 3.0*$x + 4.0
\end{verbatim}
\endgroup

\section{Mathematical Details}

The implementation of the exponentiation operator will have the following properties (below we refer to the expression \textit{\$x**\$y}):

If \textit{x} and \textit{y} are both integers (ordinary or wide):
\begin{itemize}
\item{}
The result is of the same type as the widest operand
\item{}
An error is raised if the operation makes no mathematical sense, \textit{0**(-1)} for instance.
\item{}
If \textit{x} has the value 0, then:
\begin{itemize}
\item{}
if \textit{y \tiprangle{} 0}, the result is 0
\item{}
if \textit{y \tiplangle{} 0}, the result is an error
\item{}
if \textit{y == 0}, the result is 1
\end{itemize}
\item{}
If \textit{x} has the value 1, then the result is always 1
\item{}
If \textit{y} has the value 0, the result is always 1
\item{}
If \textit{x} has a negative value lower than -1 and \textit{y \tiplangle{} 0}, the result is 0
\item{}
If \textit{x} has the value -1, then depending on whether \textit{y} is even or odd, the result is 1 or -1 (respectively.)
\item{}
For all other combinations, the value is ``\textit{x} raised to the power \textit{y}''
\item{}
When evaluating this, no attention is paid to overflow, even though the result might fit into a wide integer (though of course the result will be a wide integer if either operand was wide.) This is in accordance with the type model used in other [expr] operators.
\end{itemize}

If either \textit{x} or \textit{y} is a double, the C function \textit{pow()} is used to compute the result.

The following expressions are parsed and evaluated in accordance with all other operators:
\begingroup\small\begin{verbatim}
 $x ** $y ** $z ==> ($x ** $y ) ** $z
 $x ** -1       ==> ($x ** (-1))
\end{verbatim}
\endgroup

The precedence of the exponentiation operator is thus \textit{higher} than the multiplication, division and remainder operations and lower than the unary operations, in accordance with common definitions.

\section{Sample Implementation}

\url{http://sf.net/tracker/?func=detail&aid=655176&group_id=10894&atid=310894}

\section{Copyright}

This document is placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
