% Copyright 2007 by Mark Wibrow
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.
%

\section{Evaluating Mathematical Expressions}

The easiest way of using \pgfname's mathematical engine is to provide
a mathematical expression given in the usual infix notation (such as
|1cm+4*2cm/5.5| or |2*3+3*sin(30)|). This expression can be parsed by
the mathematical engine and the result be placed in a dimension
register, a counter, or a macro. Supported are infix mathematical
operations involving integers and non-integers, with or without
units.

It should be noted that all 
calculations must not exceed $\pm16383.99999$ at \emph{any} point, 
because the underlying algorithms relie on \TeX{} dimensions. This
means that many of the underlying algorithms are necessarily
approximate. It also means that some of the algorithms are not very
fast. \TeX{} is, after all, a typesetting language and not ideally
suited to relatively advanced mathematical operations. However, it is
possible to change the algorithms as described in
Section~\ref{pgfmath-reimplement}. 

In the present section, the high-level macros for parsing an
expression are explained first, then the syntax for expression is
explained.


\subsection{Commands for Parsing Expressions}

\label{pgfmath-registers}

\label{pgfmath-parsing}

The basic command for invoking the parser of \pgfname's mathematical
engine is the following:

\begin{command}{\pgfmathparse\marg{expression}}
  This macro parses \meta{expression} and returns the result without
  units in  the macro |\pgfmathresult|.

  \example |\pgfmathparse{2pt+3.5pt}| will set |\pgfmathresult| to the
  text |5.5|.

  In the following, the special properties of this command are
  explained. The exact syntax of mathematical expressions is explained
  in Section~\ref{pgfmath-syntax}. Note that unlike the rest of the  
  manual, the examples show the result of the calculation (that is,
  the value of the macro |\pgfmathresult|), not what is displayed in
  the document.  

  \begin{itemize}
  \item
    The result stored in the macro |\pgfmathresult| is a decimal
    \emph{without units}. This is true regardless of whether the
    \meta{expression} contains any unit specification. But, any units
    specified will be converted to points first.
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{2pt+3.4pt}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{2cm+3.4cm}
\end{codeexample}

  \item If no units are specified \emph{at any point} in the 
    expression, the result will be multiplied by the value in 
    |\pgfmathresultunitscale|, which can be a number or a dimension 
    (which will be converted to points). By default it is set to 1, 
    but can be changed with |\pgfmathsetresultunitscale|. Note that 
    the result will still be a number \emph{without} units.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{2pt+3.4pt}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathsetresultunitscale{1cm}
\pgfmathparse{2+3.4}
\end{codeexample}

    \pgfmathsetresultunitscale{1pt}
    
  \item You can check whether an expression contained a unit using 
    the \TeX-if |\||ifpgfmathunitsdeclared|. After a call
    of |\pgfmathparse| this if will be true exactly if some unit was
    encountered in the expression.
    
  \item The parser handles numbers with or without units regardless
    of the operation.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{54pt/3cm*2.1} 
\end{codeexample}

  \item the parser can cope with \TeX{} registers, including those 
    preceded by |\the|.

    \makeatletter

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgf@x=12.34pt
\c@pgf@counta=5
\pgfmathparse{\pgf@x+\c@pgf@counta*6}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgf@x=56.78pt
\pgfmathparse{\pgf@x+\the\pgf@x}
\end{codeexample}

  \item \TeX{} dimension registers can be multiplied without the |*| 
    operator by preceding them with a number (\emph{not} a function),
     or a count register.
	 
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\c@pgf@counta=-4
\pgf@x=10pt
\pgfmathparse{.5\pgf@x-\c@pgf@counta\pgf@x}%
\end{codeexample}

  \item Parenthesis can be used to group operations.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{(4pt+0.5)*3}
\end{codeexample}

  \item functions are recognized, so it is possible to parse
    |sin(.5*pi r)*60|, which means ``the sine of $0.5$ times $\pi$ 
    radians, multiplied by 60''. The argument of most functions can
    be any expression.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{sin(pi/2 r)*60}
\end{codeexample}

  \item Scientific notation in the form |1.234e+4| is recognised (but
  the restriction on the range of values still applies). The exponent
  symbol can be upper or lower case (i.e., |E| or |e|). 
  
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{1.234567891e-2}
\end{codeexample}
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{1.234567891e4}
\end{codeexample}
  \end{itemize}
\end{command}

\begin{command}{\pgfmathqparse\marg{expression}}
  This macro is similar to |\pgfmathparse|: it parses 
  \meta{expression} and returns the result in the macro 
  |\pgfmathresult|. It differs in two respects. Firstly, 
  |\pgfmathqparse| does not parse functions or scientific
  notation. 
  Secondly, numbers in \meta{expression} \emph{must}
  specify a \TeX{} unit (except in such instances as |0.5\pgf@x|), 
  which greatly simplifies the problem of parsing 
  of non-integers. As a result of these restrictions |\pgfmathqparse| 
  is about twice as fast as |\pgfmathparse|. Note that the result 
  will still be a number \emph{without} units.	
\end{command}

\begin{command}{\pgfmathsetresultunitscale\marg{number or dimension}}
  Sets the value in |\pgfmathresultunitscale|, which scales the result
  of an expression parsed with |\pgfmathparse|, if that expression
  contains no units \emph{at any point}. The argument can be an integer,
  non-integer or a dimension, but the result will still be a number 
  \emph{without} units. Note, that this will affect |\pgfmathsetlength| 
  and friends, but not if the expression starts with |+| (which
  switches parsing off). By default the value in
  |\pgfmathresultunitscale| is 1. 
\end{command}

Instead of the |\pgfmathparse| macro you can also wrapper commands,
whose usage is very similar to their cousins in the \calcname{} 
package. The only difference is that the expressions can be any
expression that is handled by |\pgfmathparse|.

For all of the following commands, if \meta{expression} starts with
|+|, no parsing is done and a simple assignment or increment is done
using normal \TeX\ assignments or increments. This will be orders of
magnitude faster than calling the parser. 

\begin{command}{\pgfmathsetlength\marg{dimension register}\marg{expression}}
  Sets the length of the \TeX{} \meta{dimension register}, to the value
  (in points) specified by \meta{expression}. The \meta{expression}
  will be parsed using |\pgfmathparse|.
\end{command}

\begin{command}{\pgfmathaddtolength\marg{dimension register}\marg{expression}}
  Adds the value (in points) of \meta{expression} to the \TeX{} 
  \meta{dimension register}. 
\end{command}

\begin{command}{\pgfmathsetcount\marg{count register}\marg{expression}}
  Sets the value of the \TeX{} \meta{count register}, to the 
  \emph{truncated} value specified by \meta{expression}. 
\end{command}

\begin{command}{\pgfmathaddtocount\marg{count register}\marg{expression}}
  Adds the \emph{truncated} value  of \meta{expression} to the \TeX{} 
  \meta{count register}.
\end{command}

\begin{command}{\pgfmathsetcounter\marg{counter}\marg{expression}}
  Sets the value of the \meta{counter}, to the \emph{truncated} value 
  specified by \meta{expression}. 
\end{command}

\begin{command}{\pgfmathaddtocounter\marg{counter}\marg{expression}}
  Adds the \emph{truncated} value  of \meta{expression} to 
  \meta{counter}.
\end{command}

% \begin{command}{\pgfmathnewcounter\marg{counter}}
%   This is simply a version of the \LaTeX{} macro |\newcounter|, 
%   implemented to maintain consistency (consistency is good,
%   inconsistency is evil). Considering |\pgfmathnewcounter{foo}|, this
%   creates a new count register |\c@foo|, and a macro |\thefoo|, which
%   returns the value in |\c@foo|.
% \end{command}

\begin{command}{\pgfmathsetmacro\marg{macro}\marg{expression}}
  Defines \meta{macro} as the  value of \meta{expression}. The result
  is a decimal \emph{without} units.
\end{command}



\subsection{Syntax for mathematical expressions}

\label{pgfmath-syntax}

The syntax for the expressions recognized by |\pgfmathparse| and
friends is based on the syntax recognized by \textsc{matlab}. The
following operations and functions are currently recognized:

\begin{math-operator}{\mvar{x}\ +\ \mvar{y}}
	Adds \mvar{y} to \mvar{x}.
	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{4+2pt}
\end{codeexample}
\end{math-operator}

\begin{math-operator}{\mvar{x}\ -\ \mvar{y}}
	Subtracts \mvar{y} from  \mvar{x}.
	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{155.35-4cm}
\end{codeexample}
\end{math-operator}
\begin{math-operator}{\mvar{x}\ *\ \mvar{y}}
	Multiplies \mvar{x} by  \mvar{y}.
	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{3.9pt*4.56}
\end{codeexample}

\end{math-operator}
\begin{math-operator}{\mvar{x}\ /\ \mvar{y}}
	Divides \mvar{x} by  \mvar{y}.
	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{-31.6pt/17}
\end{codeexample}

\end{math-operator}
\begin{math-operator}{\mvar{x}\ {\char94}\ \mvar{y}} 

Raises \mvar{x} to the power \mvar{y}. \mvar{y} should be an integer, but it can be negative.

{
\catcode`\^=7

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{2.3^4}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{2^-4}
\end{codeexample}
}
\end{math-operator}

\begin{math-operator}{\mvar{x}\ ==\ \mvar{y}} 

	This evaluates to |1| if \mvar{x} equals \mvar{y}, or |0| if \mvar{x}
	does not equal \mvar{y}. 
	Note that equalities (and inequalities) are evaluated left to right, 
	and are only evaluated when another equality (or inequality) operator 
	is scanned, or the end of the current group or parse is reached. So 
	|5+4==3+2==9| results in |0| because |5+4| does not equal |3+2|, 
	resulting in zero, and the second equality is therefore evaluating 
	|0==9|.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{3*5 == 15}
\end{codeexample}

\end{math-operator}


\begin{math-operator}{\mvar{x}\ >\ \mvar{y}} 

	This evaluates to |1| if \mvar{x} is greater than \mvar{y}, or |0| if 
	\mvar{x} is smaller or equal to \mvar{y}.
	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{17 > 4.2*1.97+4}
\end{codeexample}

\end{math-operator}

\begin{math-operator}{\mvar{x}\ <\ \mvar{y}}

	This evaluates to |1| if \mvar{x} is smaller than \mvar{y}, or |0| if
	\mvar{x} is greater or equal to \mvar{y}.
	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{2 < -5.2/-3.6-2}
\end{codeexample}

\end{math-operator}

\begin{math-function}{mod(\mvar{x},\mvar{y})}
	This evaluates \mvar{x} modulo \mvar{y}. This function cannot be 
	nested inside itself or the functions |max|, |min| or |veclen|.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{mod(20,6)}
\end{codeexample}

\end{math-function}

\begin{math-function}{max(\mvar{x},\mvar{y})}
	This evaluates to the maximum of \mvar{x} or \mvar{y}. This function 
	cannot be nested inside itself or the functions |min|, |mod| or 
	|veclen|.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{max(17,23)}
\end{codeexample}

\end{math-function}

\begin{math-function}{min(\mvar{x},\mvar{y})}
	This evaluates to the minimum of \mvar{x} or \mvar{y}. This function 
	cannot be nested inside itself or the functions |max|, |mod| or 
	|veclen|.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{min(17,23)}
\end{codeexample}

\end{math-function}

\begin{math-function}{abs(\mvar{x})}

	Evaluates the absolute value of $x$.
	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{abs(-5)}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{-abs(4*-3)}
\end{codeexample}

\end{math-function}

\begin{math-function}{round(\mvar{x})}

	Rounds \mvar{x} to the nearest integer. It uses ``asymmetric half-up'' 
	rounding. So |1.5| is rounded to |2|, but |-1.5| is rounded to |-2| 
	(\emph{not} |0|).

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{round(32.5/17)}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{round(398/12)}
\end{codeexample}

\end{math-function}

\begin{math-function}{floor(\mvar{x})}

	Rounds \mvar{x} down to the nearest integer. 
	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{floor(32.5/17)}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{floor(398/12)}
\end{codeexample}

\end{math-function}

\begin{math-function}{ceil(\mvar{x})}

	Rounds \mvar{x} up to the nearest integer. 

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{ceil(32.5/17)}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{ceil(398/12)}
\end{codeexample}

\end{math-function}

\begin{math-function}{exp(\mvar{x})}
{
\catcode`\^=7

	Maclaurin series for $e^\textrm{\mvar{x}}$. 
}	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{exp(1)}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{exp(2.34)}
\end{codeexample}

\end{math-function}

\begin{math-function}{pow(\mvar{x},\mvar{y})}

 Raises \mvar{x} to the power \mvar{y}. \mvar{y} should be an integer, but it can be negative. 

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{pow(2,7)}
\end{codeexample}

\end{math-function}

\begin{math-function}{sqrt(\mvar{x})}

 Calculates $\sqrt{\textrm{\mvar{x}}}$.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{sqrt(10)}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{sqrt(8765.432)}
\end{codeexample}

\end{math-function}

\begin{math-function}{veclen(\mvar{x},\mvar{y})}

	Evaluates the Euclidean distance from |(0,0)| to |(|\mvar{x}|,|\mvar{y}|)|. 
	This function cannot be nested inside itself, or the functions |max|,
	|min| or |mod|.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{veclen(15,14)}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{veclen(3,4)}
\end{codeexample}

\end{math-function}

\begin{math-constant}{pi}

	The constant $\pi=3.14159$.
	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{pi}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{pi r}
\end{codeexample}

\end{math-constant}

\begin{math-operator}{\mvar{x}\ r}

	This converts \mvar{x} from radians to degrees. Note that |r| will 
	evaluate any preceding series of multiplication or division 
	\emph{before} conversion, but not other operations. So |3*4/6r| 
	converts 2 radians to degrees, but |3-4+6r|, converts 6 radians to
	degrees and adds the result to |-1|.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{2*pi r-pi r}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{2*pi/8 r}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{sin(3*pi/2r)*60}
\end{codeexample}

\end{math-operator}

\begin{math-function}{rad(\mvar{x})}

	Convert \mvar{x} to radians. \mvar{x} is assumed to be in degrees.
	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{rad(90)}
\end{codeexample}

\end{math-function}

\begin{math-function}{deg(\mvar{x})}

	Convert \mvar{x} to degrees. \mvar{x} is assumed to be in radians.
	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{deg(3*pi/2)}
\end{codeexample}

\end{math-function}

\begin{math-function}{sin(\mvar{x})}

	Sine of \mvar{x}. By employing the |r| operator, \mvar{x} can be in 
	radians.
	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{sin(60)}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{sin(pi/3 r)}
\end{codeexample}

\end{math-function}

\begin{math-function}{cos(\mvar{x})}

	Cosine of \mvar{x}. By employing the |r| operator, \mvar{x} can be in 
	radians.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{cos(60)}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{cos(pi/3 r)}
\end{codeexample}

\end{math-function}

\begin{math-function}{tan(\mvar{x})}

	Tangent of \mvar{x}. By employing the |r| operator, \mvar{x} can be in 
	radians.
	
\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{tan(45)}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{tan(2*pi/8 r)}
\end{codeexample}

\end{math-function}

\begin{math-function}{asin(\mvar{x})}

	Arcsine of \mvar{x}. The result is in degrees.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{asin(0.7071)}
\end{codeexample}

\end{math-function}

\begin{math-function}{acos(\mvar{x})}

	Arccosine of \mvar{x} in degrees.  

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{acos(0.5)}
\end{codeexample}

\end{math-function}

\begin{math-function}{atan(\mvar{x})}

	Arctangent of $x$ in degrees. 

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{atan(1)}
\end{codeexample}

\end{math-function}

\begin{math-function}{rnd}

	Generates a pseudo-random number between 0 and 1.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{rnd}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{2*rnd}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{-rnd+5}
\end{codeexample}

\end{math-function}

\begin{math-function}{rand}

	Generates a pseudo-random number between -1 and 1.

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{rand}
\end{codeexample}

\begin{codeexample}[post=\tt\footnotesize\pgfmathresult]
\pgfmathparse{rand*15}
\end{codeexample}

\end{math-function}
