% 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 Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

% This file defines the mathematical functions and operators.
%
% Version 1.4142135 25/8/2008

% add function.
%
\pgfmathdeclarefunction{add}{2}{%
	\begingroup%
    \pgfmath@x=#1pt\relax%
    \pgfmath@y=#2pt\relax%
    \advance\pgfmath@x by\pgfmath@y%
    \pgfmath@returnone\pgfmath@x%
  \endgroup%
}


% subtract function.
%
\pgfmathdeclarefunction{subtract}{2}{%
	\begingroup%
    \pgfmath@x=#1pt\relax%
    \pgfmath@y=#2pt\relax%
    \advance\pgfmath@x by-\pgfmath@y%
    \pgfmath@returnone\pgfmath@x%
  \endgroup%
}

% neg function.
%
\pgfmathdeclarefunction{neg}{1}{%
	\begingroup%
		\pgfmath@x=-#1pt\relax%
		\edef\pgfmathresult{\pgfmath@tonumber{\pgfmath@x}}%
		\pgfmath@smuggleone\pgfmathresult%
	\endgroup%
}
% change the precedence for neg.
% Otherwise, unary minus and exponentiation won't have the correct precedence
% (test '-2^2')
\def\pgfmath@operation@neg@precedence{500}

% multiply function.
%
\pgfmathdeclarefunction{multiply}{2}{%
  \begingroup%
    \pgfmath@x=#1pt\relax%
    \pgfmath@y=#2pt\relax%
    \pgfmath@x=\pgfmath@tonumber{\pgfmath@y}\pgfmath@x%
    \pgfmath@returnone\pgfmath@x%
  \endgroup%
}

% divide function.
%
\newif\ifpgfmath@divide@period

\pgfmathdeclarefunction{divide}{2}{%
	\begingroup%
		\pgfmath@x=#1pt\relax%
		\pgfmath@y=#2pt\relax%
		\let\pgfmath@sign=\pgfmath@empty%
		\ifdim0pt=\pgfmath@y%
			\pgfmath@error{You've asked me to divide `#1' by `#2', %
				but I cannot divide any number by `#2'}%				
		\fi%
		\afterassignment\pgfmath@xa%
		\c@pgfmath@counta\the\pgfmath@y\relax%
		\ifdim0pt=\pgfmath@xa%
			\divide\pgfmath@x by\c@pgfmath@counta%
		\else%
			\ifdim0pt>\pgfmath@x%
				\def\pgfmath@sign{-}%
				\pgfmath@x=-\pgfmath@x%
			\fi%
			\ifdim0pt>\pgfmath@y%
				\expandafter\def\expandafter\pgfmath@sign\expandafter{\pgfmath@sign-}%
				\pgfmath@y=-\pgfmath@y%
			\fi%
			\ifdim1pt>\pgfmath@y%
				\pgfmathreciprocal@{\pgfmath@tonumber{\pgfmath@y}}%
				\pgfmath@x=\pgfmath@sign\pgfmathresult\pgfmath@x%
			\else%
				\def\pgfmathresult{0}%
				\pgfmath@divide@periodtrue%
				\c@pgfmath@counta=0\relax%
				\pgfmathdivide@@%
				\pgfmath@x=\pgfmath@sign\pgfmathresult pt\relax%
			\fi%
		\fi%
		\pgfmath@returnone\pgfmath@x%
	\endgroup%	
}

\def\pgfmath@small@number{0.00002}

\def\pgfmathdivide@@{%
	\let\pgfmath@next=\relax%
	\ifdim\pgfmath@small@number pt<\pgfmath@x%
		\ifdim\pgfmath@small@number pt<\pgfmath@y%
			\ifdim\pgfmath@y>\pgfmath@x%
				\ifpgfmath@divide@period%
					\expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmathresult.}%
					\pgfmath@divide@periodfalse%
				\fi%
				\pgfmathdivide@dimenbyten\pgfmath@y%
				\ifdim\pgfmath@y>\pgfmath@x%
					\expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmathresult0}%
				\fi%
			\else%
				\c@pgfmath@counta=\pgfmath@x%
				\c@pgfmath@countb=\pgfmath@y%
				\divide\c@pgfmath@counta by\c@pgfmath@countb%
				\pgfmath@ya=\c@pgfmath@counta\pgfmath@y%
				\advance\pgfmath@x by-\pgfmath@ya%
				\def\pgfmath@next{%
					\toks0=\expandafter{\pgfmathresult}%
					\edef\pgfmathresult{\the\toks0 \the\c@pgfmath@counta}%
				}%
				\ifpgfmath@divide@period
				\else
					% we are behind the period. It may happen that the
					% result is more than one digit - in that case,
					% introduce special handling:
					\ifnum\c@pgfmath@counta>9 %
						\expandafter\pgfmathdivide@advance@last@digit\pgfmathresult CCCCC\@@
						\advance\c@pgfmath@counta by-10 %
						\ifnum\c@pgfmath@counta=0
							\let\pgfmath@next=\relax
						\fi
					\fi
				\fi
				\pgfmath@next
			\fi%
			\let\pgfmath@next=\pgfmathdivide@@%
		\fi%
	\fi%
	\pgfmath@next%
}

% advances the last digit found in the number. Any missing digits are
% supposed to be filled with 'C'.
\def\pgfmathdivide@advance@last@digit#1.#2#3#4#5#6#7\@@{%
	\pgfmath@ya=\pgfmathresult pt %
	\if#2C%
		\pgfmath@xa=1pt %
	\else
		\if#3C%
			\pgfmath@xa=0.1pt %
		\else
			\if#4C%
				\pgfmath@xa=0.01pt %
			\else
				\if#5C%
					\pgfmath@xa=0.001pt %
				\else
					\if#6C%
						\pgfmath@xa=0.0001pt %
					\else
						\pgfmath@xa=0.00001pt %
					\fi
				\fi
			\fi
		\fi
	\fi
	\advance\pgfmath@ya by\pgfmath@xa
	\edef\pgfmathresult{\pgfmath@tonumber@notrailingzero\pgfmath@ya}%
}%

{
\catcode`\p=12
\catcode`\t=12
\gdef\Pgf@geT@NO@TRAILING@ZERO#1.#2pt{%
	#1.%
	\ifnum#2=0 \else #2\fi
}
}
\def\pgfmath@tonumber@notrailingzero#1{\expandafter\Pgf@geT@NO@TRAILING@ZERO\the#1}


\def\pgfmathdivide@dimenbyten#1{%
	\edef\pgfmath@temp{\pgfmath@tonumber{#1}}%
	\expandafter\pgfmathdivide@@dimenbyten\pgfmath@temp\@@#1\@@}
\def\pgfmathdivide@@dimenbyten#1.#2\@@#3\@@{%
	\pgfmath@tempcnta=#1\relax%
	\divide\pgfmath@tempcnta by10\relax%
	\pgfmath@tempcntb=\pgfmath@tempcnta%
	\multiply\pgfmath@tempcnta by-10\relax%
	\advance\pgfmath@tempcnta by#1\relax%
	#3=\the\pgfmath@tempcntb.\the\pgfmath@tempcnta#2pt\relax%
}


% reciprocal function.
%
\pgfmathdeclarefunction{reciprocal}{1}{%
	\begingroup%
		\expandafter\pgfmath@x#1pt\relax%
		\ifdim\pgfmath@x=0pt\relax%
			\pgfmath@error{You asked me to calculate `1/#1', but I cannot divide any number by zero}{}%
		\fi%
		\edef\pgfmath@reciprocaltemp{\pgfmath@tonumber{\pgfmath@x}}%
		\expandafter\pgfmathreciprocal@@\pgfmath@reciprocaltemp0000000\pgfmath@}
\def\pgfmathreciprocal@@#1.#2#3#4#5#6#7\pgfmath@{%
		\c@pgfmath@counta#2#3#4#5#6\relax%
		% If the number is an integer, use TeX arithmatic.
		\ifnum\c@pgfmath@counta=0\relax%
			\pgfmath@x1pt\relax%
			\divide\pgfmath@x#1\relax%
		\else%
			\ifnum#1>100\relax%
				\c@pgfmath@counta#1#2#3#4\relax%
				\c@pgfmath@countb1000000000\relax%
				\divide\c@pgfmath@countb\c@pgfmath@counta%
				\c@pgfmath@counta\c@pgfmath@countb%
				\divide\c@pgfmath@counta10000\relax%
				\pgfmath@x\c@pgfmath@counta pt\relax%
				\multiply\c@pgfmath@counta-10000\relax%
				\advance\c@pgfmath@countb\c@pgfmath@counta%
				\pgfmath@y\c@pgfmath@countb pt\relax%
				\divide\pgfmath@y1000000\relax%		
				\advance\pgfmath@x\pgfmath@y%
			\else%
				\c@pgfmath@counta#1#2#3#4#5#6\relax%
				\c@pgfmath@countb1000000000\relax%
				\divide\c@pgfmath@countb\c@pgfmath@counta%
				\c@pgfmath@counta\c@pgfmath@countb%
				\divide\c@pgfmath@counta10000\relax%
				\pgfmath@x\c@pgfmath@counta pt\relax%
				\multiply\c@pgfmath@counta-10000\relax%
				\advance\c@pgfmath@countb\c@pgfmath@counta%
				\pgfmath@y\c@pgfmath@countb pt\relax%
				\pgfmath@y.1\pgfmath@y% Yes! This way is more accurate. Go figure...
				\pgfmath@y.1\pgfmath@y%	
				\pgfmath@y.1\pgfmath@y%	
				\pgfmath@y.1\pgfmath@y%		
				\advance\pgfmath@x\pgfmath@y%
			\fi%
		\fi%
		\pgfmath@returnone\pgfmath@x%
	\endgroup%
}

% div function.
%
\pgfmathdeclarefunction{div}{2}{%
	\begingroup%
		\pgfmathdivide@{#1}{#2}%
		\ifdim\pgfmathresult pt<0pt\relax%
			\pgfmathceil@{\pgfmathresult}%
		\else%
			\pgfmathfloor@{\pgfmathresult}%
		\fi%
		\afterassignment\pgfmath@gobbletilpgfmath@%
		\c@pgfmath@counta=\pgfmathresult\relax\pgfmath@%
		\edef\pgfmathresult{\the\c@pgfmath@counta}%
		\pgfmath@smuggleone\pgfmathresult%
	\endgroup%
}

% mod function.
%
\pgfmathdeclarefunction{mod}{2}{%
  \begingroup%
    \pgfmath@xa#1pt%
    \pgfmath@xb#2pt%
    \pgfmath@x\pgfmath@xa%
    \c@pgfmath@counta\pgfmath@xa%
    \c@pgfmath@countb\pgfmath@xb%
    \divide\c@pgfmath@counta\c@pgfmath@countb%
    \multiply\pgfmath@xb\c@pgfmath@counta%
    \advance\pgfmath@x-\pgfmath@xb%
    \pgfmath@returnone\pgfmath@x%
  \endgroup%
}

% Mod function (never negative).
%
\pgfmathdeclarefunction{Mod}{2}{%
	\begingroup%
		\pgfmathmod@{#1}{#2}%
		\pgfmath@x=\pgfmathresult pt\relax%
		\ifdim\pgfmath@x<0pt\relax%
			\advance\pgfmath@x by#2pt\relax%
		\fi%
		\edef\pgfmathresult{\pgfmath@tonumber{\pgfmath@x}}%
		\pgfmath@smuggleone\pgfmathresult%
	\endgroup%
}

% abs function.
%
\pgfmathdeclarefunction{abs}{1}{%
	\begingroup%
		\expandafter\pgfmath@x#1pt\relax%
		\ifdim\pgfmath@x<0pt\relax%
			\pgfmath@x-\pgfmath@x%
		\fi%
	\pgfmath@returnone\pgfmath@x%
	\endgroup%
}

% sign(x)
\pgfmathdeclarefunction{sign}{1}{%
  \begingroup
    \ifdim#1pt>0sp %
      \def\pgfmathresult{1}%
    \else
      \ifdim#1pt=0sp %
        \def\pgfmathresult{0}%
      \else
        \def\pgfmathresult{-1}%
      \fi
    \fi
    \pgfmath@smuggleone\pgfmathresult
  \endgroup
}

% e constant.
%
\pgfmathdeclarefunction{e}{0}{\def\pgfmathresult{2.718281828}}

% ln function.
%
% This is based on an algorithm due to Rouben Rostamian and
% uses coefficients contributed by Alain Matthes.
%
\pgfmathdeclarefunction{ln}{1}{%
	\begingroup%
		\expandafter\pgfmath@x#1pt\relax%
		\ifdim\pgfmath@x>0pt\else%
			\pgfmath@error{I cannot calculate the logarithm of `#1'}{}%
		\fi%		
		\c@pgfmath@counta=0\relax%
		\ifdim\pgfmath@x>2pt\relax%
			\ifdim\pgfmath@x<128pt\relax%
				\ifdim\pgfmath@x<8pt\relax%
					\ifdim\pgfmath@x<4pt\relax%
						\divide\pgfmath@x by2\relax%
						\c@pgfmath@counta=1\relax%
					\else%
						\divide\pgfmath@x by4\relax%
						\c@pgfmath@counta=2\relax%
					\fi%
				\else%
					\ifdim\pgfmath@x<32pt\relax%
						\ifdim\pgfmath@x<16pt\relax%
							\divide\pgfmath@x by8\relax%
							\c@pgfmath@counta=3\relax%
						\else%
							\divide\pgfmath@x by16\relax%
							\c@pgfmath@counta=4\relax%
						\fi%
					\else%
						\ifdim\pgfmath@x<64pt\relax%
							\divide\pgfmath@x by32\relax%
							\c@pgfmath@counta=5\relax%
						\else%
							\divide\pgfmath@x by64\relax%
							\c@pgfmath@counta=6\relax%
						\fi%
					\fi%
				\fi%
			\else%
				\ifdim\pgfmath@x<2048pt\relax%
					\ifdim\pgfmath@x<512pt\relax%
						\ifdim\pgfmath@x<256pt\relax%
							\divide\pgfmath@x by128\relax%
							\c@pgfmath@counta=7\relax%
						\else%
							\divide\pgfmath@x by256\relax%
							\c@pgfmath@counta=8\relax%
						\fi%
					\else%
						\ifdim\pgfmath@x<1024pt\relax%
							\divide\pgfmath@x by512\relax%
							\c@pgfmath@counta=9\relax%
						\else%
							\divide\pgfmath@x by1024\relax%
							\c@pgfmath@counta=10\relax%
						\fi%
					\fi%
				\else%
					\ifdim\pgfmath@x<8192pt\relax%
						\ifdim\pgfmath@x<4096pt\relax%
							\divide\pgfmath@x by2048\relax%
							\c@pgfmath@counta=11\relax%
						\else%
							\divide\pgfmath@x by4096\relax%
							\c@pgfmath@counta =12\relax%
						\fi%
					\else%
						\divide\pgfmath@x by8192\relax%
						\c@pgfmath@counta=13\relax%
					\fi%
				\fi%
			\fi%	
		\else%
			\ifdim\pgfmath@x<1pt\relax%
				\ifdim\pgfmath@x>0.0078125pt\relax% 2^-7
					\ifdim\pgfmath@x>0.125pt\relax% 2^-3
						\ifdim\pgfmath@x>0.5pt\relax% 2^-1
							\multiply\pgfmath@x by2\relax%
							\c@pgfmath@counta=-1\relax%
						\else%
							\multiply\pgfmath@x by4\relax%
							\c@pgfmath@counta=-2\relax%
						\fi%
					\else%
						\ifdim\pgfmath@x>0.03125pt\relax% 2^-5
							\ifdim\pgfmath@x>0.0625pt\relax%
								\multiply\pgfmath@x by8\relax%
								\c@pgfmath@counta=-3\relax%
							\else%
								\multiply\pgfmath@x by16\relax%
								\c@pgfmath@counta=-4\relax%
							\fi%
						\else%
							\ifdim\pgfmath@x>0.015625pt\relax% 2^-6
								\multiply\pgfmath@x by32\relax%
								\c@pgfmath@counta=-5\relax%
							\else%
								\multiply\pgfmath@x by64\relax%
								\c@pgfmath@counta=-6\relax%
							\fi%
						\fi%
					\fi%
				\else%
					\ifdim\pgfmath@x>0.000244140625pt\relax% 2^-11
						\ifdim\pgfmath@x>0.001953125pt\relax% 2^-9
							\ifdim\pgfmath@x>0.00390625pt\relax% 2^-8
								\multiply\pgfmath@x by128\relax%
								\c@pgfmath@counta=-7\relax%
							\else%
								\multiply\pgfmath@x by256\relax%
								\c@pgfmath@counta=-8\relax%
							\fi%
						\else%
							\ifdim\pgfmath@x>0.0009765625pt\relax% 2^-10
								\multiply\pgfmath@x by512\relax%
								\c@pgfmath@counta=-9\relax%
							\else%
								\multiply\pgfmath@x by1024\relax%
								\c@pgfmath@counta=-10\relax%
							\fi%
						\fi%
					\else%
						\ifdim\pgfmath@x>0.0001220703125pt\relax% 2^13
							\ifdim\pgfmath@x>0.0002441406256pt\relax% 2^12
								\multiply\pgfmath@x by2048\relax%
								\c@pgfmath@counta=-11\relax%
							\else%
								\multiply\pgfmath@x by4096\relax%
								\c@pgfmath@counta=-12\relax%
							\fi%
						\else%
							\multiply\pgfmath@x by8192\relax%
							\c@pgfmath@counta=-13\relax%
						\fi%
					\fi%
				\fi%
			\fi%	
		\fi%
		%
		% Use A+(B+(C+(D+(E+F*x)*x)*x)*x)*x
		%
		% where:
		% A = -2.787927935
		% B =  5.047861502
		% C = -3.489886985
		% D =  1.589480044
		% E = -.4025153233
		% F =  0.04300521312
		%
		\edef\pgfmath@temp{\pgfmath@tonumber{\pgfmath@x}}%
		\pgfmath@x=0.04300521312\pgfmath@x%
		\advance\pgfmath@x by-.4025153233pt\relax%
		\pgfmath@x=\pgfmath@temp\pgfmath@x%
		\advance\pgfmath@x by1.589480044pt\relax%
		\pgfmath@x=\pgfmath@temp\pgfmath@x%
		\advance\pgfmath@x by-3.489886985pt\relax%
		\pgfmath@x=\pgfmath@temp\pgfmath@x%
		\advance\pgfmath@x by5.047861502pt\relax%
		\pgfmath@x=\pgfmath@temp\pgfmath@x%
		\advance\pgfmath@x by-2.787927935pt\relax%
		\advance\pgfmath@x by\c@pgfmath@counta pt\relax%
		\pgfmath@x=0.6931471806\pgfmath@x%	
		\pgfmath@returnone\pgfmath@x%
	\endgroup%
}

% log2 function.
%
\pgfmathdeclarefunction{log2}{1}{%
	\begingroup%
		\pgfmathln@{#1}%
		\pgfmath@x=\pgfmathresult pt\relax%
		\pgfmath@x=1.442695041\pgfmath@x% 1/ln(2).
		\pgfmath@returnone\pgfmath@x%
	\endgroup%
}

% log10 function
%
\pgfmathdeclarefunction{log10}{1}{%	
	\begingroup%
		\pgfmathln@{#1}%
		\pgfmath@x=\pgfmathresult pt\relax%
		\pgfmath@x=0.43429441\pgfmath@x% 1/ln(10).
		\pgfmath@returnone\pgfmath@x%
	\endgroup%
}

% This is needed since \pgfmathlog2 and \pgfmathlog10 are not legal macro names
\expandafter\let\expandafter\pgfmathlogtwo\csname pgfmathlog2\endcsname
\expandafter\let\expandafter\pgfmathlogtwo@\csname pgfmathlog2@\endcsname
\expandafter\let\expandafter\pgfmathlogten\csname pgfmathlog10\endcsname
\expandafter\let\expandafter\pgfmathlogten@\csname pgfmathlog10@\endcsname

% exp function.
%
\pgfmathdeclarefunction{exp}{1}{%	
  \begingroup
    \pgfmath@xc=#1pt\relax
    \pgfmath@yc=#1pt\relax
    \ifdim\pgfmath@xc<-9pt
      \pgfmath@x=1sp\relax
    \else
      \ifdim\pgfmath@xc<0pt
        \pgfmath@xc=-\pgfmath@xc
      \fi
      \pgfmath@x=1pt\relax
      \pgfmath@xa=1pt\relax
      \pgfmath@xb=\pgfmath@x
      \pgfmathloop%
        \divide\pgfmath@xa by\pgfmathcounter
        \pgfmath@xa=\pgfmath@tonumber\pgfmath@xc\pgfmath@xa%
        \advance\pgfmath@x by\pgfmath@xa
      \ifdim\pgfmath@x=\pgfmath@xb
      \else
        \pgfmath@xb=\pgfmath@x
      \repeatpgfmathloop%
      \ifdim\pgfmath@yc<0pt
        \pgfmathreciprocal@{\pgfmath@tonumber\pgfmath@x}%
        \pgfmath@x=\pgfmathresult pt\relax
      \fi
    \fi
    \pgfmath@returnone\pgfmath@x%
  \endgroup
}

% sqrt function.
%
\pgfmathdeclarefunction{sqrt}{1}{%	
	\begingroup%
		\expandafter\pgfmath@x#1pt\relax%
		\ifdim\pgfmath@x<0pt\relax%
			\pgfmath@error{I cannot calculate the square-root of the negative number `#1'}{}%
		\else%
			\ifdim\pgfmath@x<10pt\relax%
				\def\pgfmath@zeros{0}%
				\def\pgfmath@targetiterations{1}%
			\else%
				\ifdim\pgfmath@x<100pt\relax%
					\def\pgfmath@zeros{}%
					\def\pgfmath@targetiterations{1}%
				\else%
					\ifdim\pgfmath@x<1000pt\relax%
						\def\pgfmath@zeros{0}%
						\def\pgfmath@targetiterations{2}%
					\else%
						\ifdim\pgfmath@x<10000pt\relax%
							\def\pgfmath@zeros{}%
							\def\pgfmath@targetiterations{2}%
					\else%
							\def\pgfmath@zeros{0}%
							\def\pgfmath@targetiterations{3}%
		\fi\fi\fi\fi\fi%
		\edef\pgfmath@temp{\pgfmath@zeros\pgfmath@tonumber{\pgfmath@x}}%
		\expandafter\pgfmath@sqrt@\pgfmath@temp\pgfmath@%
}
\def\pgfmath@sqrt@#1.#2\pgfmath@{\pgfmath@@sqrt@#1#2\pgfmath@empty\pgfmath@empty\pgfmath@}

\def\pgfmath@@sqrt@#1#2{%
		\c@pgfmath@countb=#1#2\relax%
		\ifnum\c@pgfmath@countb>35\relax%
			\ifnum\c@pgfmath@countb>63\relax%
				\ifnum\c@pgfmath@countb>80\relax%
					\c@pgfmath@counta=9\relax%
				\else%
					\c@pgfmath@counta=8\relax%
				\fi%
			\else%
				\ifnum\c@pgfmath@countb>48\relax%
					\c@pgfmath@counta=7\relax%
				\else%
					\c@pgfmath@counta=6\relax%
				\fi%
			\fi%
		\else%
			\ifnum\c@pgfmath@countb>15\relax%
				\ifnum\c@pgfmath@countb>24\relax%
					\c@pgfmath@counta5\relax%
				\else%
					\c@pgfmath@counta=4\relax%
				\fi%
			\else%
				\ifnum\c@pgfmath@countb>3\relax%
					\ifnum\c@pgfmath@countb>8\relax%
						\c@pgfmath@counta=3\relax%
					\else%
						\c@pgfmath@counta=2\relax%
					\fi%
				\else%
					\ifnum\c@pgfmath@countb>0\relax%
						\c@pgfmath@counta=1\relax%
					\else%
						\c@pgfmath@counta=0\relax%
					\fi%
				\fi%
			\fi%
		\fi%
		\edef\pgfmath@root{\the\c@pgfmath@counta}%
		\edef\pgfmath@rootspecial{\the\c@pgfmath@counta}%
		\multiply\c@pgfmath@counta by-\c@pgfmath@counta\relax%
		\advance\c@pgfmath@counta by#1#2\relax%
		\edef\pgfmath@remainder{\the\c@pgfmath@counta}%
		\pgfmath@@@sqrt@%
}

\def\pgfmath@@@sqrt@#1#2{%
		\ifx\pgfmath@empty#1%
			\edef\pgfmath@remainder{\pgfmath@remainder00}%
			\def\pgfmath@tokens{\pgfmath@empty\pgfmath@empty}%
		\else%
			\ifx\pgfmath@empty#2%
				\edef\pgfmath@remainder{\pgfmath@remainder#10}%
				\def\pgfmath@tokens{\pgfmath@empty\pgfmath@empty}%
			\else%
				\edef\pgfmath@remainder{\pgfmath@remainder#1#2}%
				\def\pgfmath@tokens{}%
		\fi\fi%
		\c@pgfmath@counta=\pgfmath@rootspecial\relax%
		\multiply\c@pgfmath@counta by20\relax%
		\c@pgfmath@countb=\c@pgfmath@counta%
		\multiply\c@pgfmath@countb by6\relax%
		\advance\c@pgfmath@countb by36\relax%
		\c@pgfmath@countc=\c@pgfmath@counta%
		\multiply\c@pgfmath@countc by2\relax%
		\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax% 
			\advance\c@pgfmath@countb by-\c@pgfmath@countc%
			\advance\c@pgfmath@countb by-20\relax%
			\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
				\advance\c@pgfmath@countb by-\c@pgfmath@countc%
				\advance\c@pgfmath@countb by-12\relax%
				\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
					\advance\c@pgfmath@countb by-\c@pgfmath@counta%
					\advance\c@pgfmath@countb by-3\relax%
					\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
						\def\pgfmath@digit{0}%
					\else%
						\def\pgfmath@digit{1}%
					\fi%
				\else%
					\advance\c@pgfmath@countb by\c@pgfmath@counta%
					\advance\c@pgfmath@countb by5\relax%
					\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
						\def\pgfmath@digit{2}%
					\else%
						\def\pgfmath@digit{3}%
					\fi%
				\fi%
			\else%
				\advance\c@pgfmath@countb by\c@pgfmath@counta%
				\advance\c@pgfmath@countb by9\relax%
				\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
					\def\pgfmath@digit{4}%
				\else%
					\def\pgfmath@digit{5}%
				\fi%
			\fi%
		\else%
			\advance\c@pgfmath@countb by\c@pgfmath@countc%
			\advance\c@pgfmath@countb by28\relax%
			\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
				\advance\c@pgfmath@countb by-\c@pgfmath@counta%
				\advance\c@pgfmath@countb by-15\relax%
				\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
					\def\pgfmath@digit{6}%
				\else%
					\def\pgfmath@digit{7}%
				\fi%
			\else%
				\advance\c@pgfmath@countb by\c@pgfmath@counta%
				\advance\c@pgfmath@countb by17\relax%
				\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
					\def\pgfmath@digit{8}%
				\else%
					\def\pgfmath@digit{9}%
				\fi%
			\fi%
		\fi%
		\edef\pgfmath@rootspecial{\pgfmath@rootspecial\pgfmath@digit}%
		\advance\c@pgfmath@counta by\pgfmath@digit\relax%
		\multiply\c@pgfmath@counta by-\pgfmath@digit\relax%
		\advance\c@pgfmath@counta by\pgfmath@remainder\relax%
		\edef\pgfmath@remainder{\the\c@pgfmath@counta}%
		\c@pgfmath@counta=\pgfmath@targetiterations\relax%
		\advance\c@pgfmath@counta by-1\relax%
		\edef\pgfmath@targetiterations{\the\c@pgfmath@counta}%
		\ifnum\c@pgfmath@counta=0\relax%
			\edef\pgfmath@root{\pgfmath@root.\pgfmath@digit}%
		\else%
			\edef\pgfmath@root{\pgfmath@root\pgfmath@digit}%
		\fi%
		\ifnum\c@pgfmath@counta=-4\relax% -n+1 digits of precision.
			\let\pgfmath@next\pgfmath@sqrt@end%
		\else%
			\let\pgfmath@next\pgfmath@@@sqrt@%
		\fi%
		\expandafter\pgfmath@next\pgfmath@tokens%
}

\def\pgfmath@sqrt@end#1\pgfmath@{%
		\edef\pgfmathresult{\pgfmath@root}%
		\pgfmath@smuggleone\pgfmathresult%
	\endgroup}

% \pgfmathpow
%
% Calculates #1 ^ #2
%
\pgfmathdeclarefunction{pow}{2}{%	
  \begingroup%
    \pgfmath@xa=#1pt%
    \pgfmath@xb=#2pt%
    \ifdim\pgfmath@xa=0pt\relax%
      \ifdim\pgfmath@xb=0pt\relax%
        \pgfmath@x=1pt\relax%
      \else%
        \pgfmath@x=0pt\relax%
      \fi%
    \else%
      \afterassignment\pgfmath@x%
      \expandafter\c@pgfmath@counta\the\pgfmath@xb\relax%
      \ifnum\c@pgfmath@counta<0\relax%
        \c@pgfmath@counta=-\c@pgfmath@counta%
        \pgfmathreciprocal@{#1}%
        \pgfmath@xa=\pgfmathresult pt\relax%
      \fi
      \ifdim\pgfmath@x=0pt\relax%
        \pgfmath@x=1pt\relax%
        \pgfmathloop%
          \ifnum\c@pgfmath@counta>0\relax%
            \ifodd\c@pgfmath@counta%
              \pgfmath@x=\pgfmath@tonumber{\pgfmath@x}\pgfmath@xa%
            \fi
            \ifnum\c@pgfmath@counta>1\relax%
              \pgfmath@xa=\pgfmath@tonumber{\pgfmath@xa}\pgfmath@xa%
            \fi%
            \divide\c@pgfmath@counta by2\relax%
        \repeatpgfmathloop%
      \else%
        \pgfmathln@{#1}%
        \pgfmath@x=\pgfmathresult pt\relax%
        \pgfmath@x=\pgfmath@tonumber{\pgfmath@xb}\pgfmath@x%
        \pgfmathexp@{\pgfmath@tonumber{\pgfmath@x}}%
        \pgfmath@x=\pgfmathresult pt\relax%
      \fi%
    \fi              
    \pgfmath@returnone\pgf@x%
  \endgroup%
}

% factorial function.
%
\pgfmathdeclarefunction{factorial}{1}{%	
	\begingroup%
		\afterassignment\pgfmath@gobbletilpgfmath@%
		\c@pgfmath@counta=#1\relax\pgfmath@%
		\pgfmath@x=1pt\relax%
		\pgfmathloop%
		\ifnum\c@pgfmath@counta<2\relax%
		\else%
			\multiply\pgfmath@x by\c@pgfmath@counta%
			\advance\c@pgfmath@counta by-1\relax%
		\repeatpgfmathloop%
		\edef\pgfmathresult{\pgfmath@tonumber{\pgfmath@x}}%
		\pgfmath@smuggleone\pgfmathresult%
	\endgroup%
}

% Local Variables:
% coding: undecided-unix
% End:
