% 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.414213 29/9/2007

% This file defines the mathematical functions and operators.
%
% Adding/redefining extra operators/functions:
%
% Each operator/function XXX has two forms:
%
%
% \pgfmathXXX#1...   a public version which evaluates any
%                    arguments passed to it and passes the
%                    results on to...
%
% \pgfmathXXX@#1...  a non-public version which performs 
%                    required calculation on arguments which
%                    must have already been evaluated (i.e.
%                    *without* dimensions).
% 
% If a function XXX is to be included in the parser, it is 
% recommended, for consistency, that where possible, the 
% pgfmathparser file should define the macro \pgfmath@parseXXX.
% The parser should (ideally) then call \pgfmathXXX@.
%
% It is also recommened that all calculations (where necessary)
% take place within a TeX group. \pgfmath@returnone#1 makes and
% expanded version of #1 global and stores this in \pgfmathresult 
% after the group is ended.
%

\input pgfmathtrig.code.tex% Load the trig. stuff.
\input pgfmathrnd.code.tex%  Load the random stuff.


% \pgfmathadd
%
% Add #1 and #2.
%
\def\pgfmathadd#1#2{%
	\edef\pgfmath@marshal{%
		\noexpand\pgfmathparse{#2}
		\noexpand\let\noexpand\pgfmath@arg\noexpand\pgfmathresult%
		\noexpand\pgfmathparse{#1}%
	}%
	\pgfmath@marshal%
	\pgfmathadd@{\pgfmathresult}{\pgfmath@arg}}
\def\pgfmathadd@#1#2{%
  \begingroup%
    \pgfmath@x#1pt%
    \pgfmath@y#2pt%
    \advance\pgfmath@x by\pgfmath@y%
    \pgfmath@returnone\pgfmath@x%
  \endgroup%
}

% \pgfmathsubtract
%
% Subtract #2 from #1.
%
\def\pgfmathsubtract#1#2{%
	\edef\pgfmath@marshal{%
		\noexpand\pgfmathparse{#2}
		\noexpand\let\noexpand\pgfmath@arg\noexpand\pgfmathresult%
		\noexpand\pgfmathparse{#1}%
	}%
	\pgfmath@marshal%
	\pgfmathsubtract@{\pgfmathresult}{\pgfmath@arg}}
\def\pgfmathsubtract@#1#2{%
  \begingroup%
    \pgfmath@x#1pt%
    \pgfmath@y#2pt%
    \advance\pgfmath@x-\pgfmath@y%
    \pgfmath@returnone\pgfmath@x%
  \endgroup%
}

% \pgfmathmultiply
%
% Multiply #1 by #2.
%
\def\pgfmathmultiply#1#2{%
	\edef\pgfmath@marshal{%
		\noexpand\pgfmathparse{#2}
		\noexpand\let\noexpand\pgfmath@arg\noexpand\pgfmathresult%
		\noexpand\pgfmathparse{#1}%
	}%
	\pgfmath@marshal%
	\pgfmathmultiply@{\pgfmathresult}{\pgfmath@arg}}
\def\pgfmathmultiply@#1#2{%
  \begingroup%
    \pgfmath@x#1pt%
    \pgfmath@y#2pt%
    \pgfmath@x\pgfmath@tonumber{\pgfmath@y}\pgfmath@x%
    \pgfmath@returnone\pgfmath@x%
  \endgroup%
}

% \pgfmathdivide
%
% Divide #1 by #2.
%
\def\pgfmathdivide#1#2{%
	\edef\pgfmath@marshal{%
		\noexpand\pgfmathparse{#2}
		\noexpand\let\noexpand\pgfmath@arg\noexpand\pgfmathresult%
		\noexpand\pgfmathparse{#1}%
	}%
	\pgfmath@marshal%
	\pgfmathdivide@{\pgfmathresult}{\pgfmath@arg}}
\def\pgfmathdivide@#1#2{%
  \begingroup%
    \pgfmath@x#1pt%
    \pgfmath@y#2pt%
    \def\pgfmath@sign{}%
		\ifdim\pgfmath@y=0pt\relax%
			\pgfmath@error{You've asked me to divide `#1' by `#2', %
				but I cannot divide any number by `#2'}%				
		\fi%
		\afterassignment\pgfmath@xa%
		\expandafter\c@pgfmath@counta\the\pgfmath@y\relax%
		%
		% If y is an integer, use TeX arithmatic.
		%
		\ifdim\pgfmath@xa=0pt\relax%
			\divide\pgfmath@x\c@pgfmath@counta\relax%
			\edef\pgfmathresult{\pgfmath@tonumber{\pgfmath@x}}%
			\let\pgfmath@next\pgfmathdivide@@@%
		\else%
			%
			% Simple long division.
			%
			\ifdim\pgfmath@x<0pt\relax%
				\def\pgfmath@sign{-}%
				\pgfmath@x-\pgfmath@x%
			\fi%
			\ifdim\pgfmath@y<0pt\relax%
				\edef\pgfmath@sign{\pgfmath@sign-}%
				\pgfmath@y-\pgfmath@y%
			\fi%
			\pgfmath@ya\pgfmath@y%
			\c@pgfmath@counta0\relax%
			\ifdim\pgfmath@x>\pgfmath@ya%
				\ifdim\pgfmath@ya<1638.4pt\relax%
					\pgfmathmultiply@dimenbyten\pgfmath@ya%
					\ifdim\pgfmath@ya>\pgfmath@x%
						\pgfmathdivide@dimenbyten\pgfmath@ya%
						\c@pgfmath@counta0\relax%
					\else%
						\ifdim\pgfmath@ya<1638.4pt\relax%
							\pgfmathmultiply@dimenbyten\pgfmath@ya%
							\ifdim\pgfmath@ya>\pgfmath@x%
								\pgfmathdivide@dimenbyten\pgfmath@ya%
								\c@pgfmath@counta1\relax%
							\else%
								\ifdim\pgfmath@ya<1638.4pt\relax%
									\pgfmathmultiply@dimenbyten\pgfmath@ya%
									\ifdim\pgfmath@ya>\pgfmath@x%
										\pgfmathdivide@dimenbyten\pgfmath@ya%
										\c@pgfmath@counta2\relax%
									\else%
										\ifdim\pgfmath@ya<1638.4pt\relax%
											\pgfmathmultiply@dimenbyten\pgfmath@ya%
											\ifdim\pgfmath@ya>\pgfmath@x%
												\pgfmathdivide@dimenbyten\pgfmath@ya%
												\c@pgfmath@counta3\relax%
											\else%
												\pgfmath@error{The result of `#1/#2' is too big for me}{}%
											\fi%
										\fi%
									\fi%
								\fi%
							\fi%
						\fi%
					\fi%
				\fi%
			\fi%
			%
			% If y < 1pt use reciprocal operation.
			%
			\ifdim\pgfmath@y<1pt\relax%
				\ifdim\pgfmath@y<.00007pt\relax%
					\pgfmath@error{The result of `#1/#2' is too big for me}{}%
				\fi%
				\pgfmathreciprocal@{\pgfmath@tonumber{\pgfmath@y}}%
				\pgfmath@x\pgfmathresult\pgfmath@x%
				\edef\pgfmathresult{\pgfmath@tonumber{\pgfmath@x}}%
				\let\pgfmath@next\pgfmathdivide@@@%
			\else%
				\pgfmath@y\pgfmath@ya%
				\def\pgfmathresult{}%
				\let\pgfmath@next\pgfmathdivide@@%
			\fi%
		\fi%
		\pgfmath@next%
}

\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@tempcnta10\relax%
	\pgfmath@tempcntb\pgfmath@tempcnta%
	\multiply\pgfmath@tempcnta-10\relax%
	\advance\pgfmath@tempcnta#1\relax%
	#3\the\pgfmath@tempcntb.\the\pgfmath@tempcnta#2pt\relax%
}

\def\pgfmathmultiply@dimenbyten#1{%
	\edef\pgfmath@temp{\pgfmath@tonumber{#1}}%
	\expandafter\pgfmathmultiply@@dimenbyten\pgfmath@temp\@@#1\@@}
\def\pgfmathmultiply@@dimenbyten#1.#2#3\@@#4\@@{#4#1#2.#3pt\relax}

\def\pgfmathdivide@@{%
		\ifdim\pgfmath@x>\pgfmath@y%
			\pgfmath@yb6.0\pgfmath@y%
			\ifdim\pgfmath@yb>\pgfmath@x% 6*y
				\advance\pgfmath@yb-\pgfmath@y%
				\advance\pgfmath@yb-\pgfmath@y%
				\ifdim\pgfmath@yb>\pgfmath@x% 4*y
					\advance\pgfmath@yb-\pgfmath@y%
					\advance\pgfmath@yb-\pgfmath@y%
					\ifdim\pgfmath@yb>\pgfmath@x% 2*y
						\advance\pgfmath@yb-\pgfmath@y%
						\ifdim\pgfmath@yb>\pgfmath@x% 1*y
							\c@pgfmath@countb0\relax%
						\else%
							\c@pgfmath@countb1\relax%
						\fi%
					\else%
						\advance\pgfmath@yb\pgfmath@y% 3*y
						\ifdim\pgfmath@yb>\pgfmath@x%
							\c@pgfmath@countb2\relax%
						\else%
							\c@pgfmath@countb3\relax%
						\fi%
					\fi%
				\else%
					\advance\pgfmath@yb\pgfmath@y% 5*y
					\ifdim\pgfmath@yb>\pgfmath@x%
						\c@pgfmath@countb4\relax%
					\else%
						\c@pgfmath@countb5\relax%
					\fi%
				\fi%
			\else%
				\advance\pgfmath@yb\pgfmath@y%
				\advance\pgfmath@yb\pgfmath@y%
				\ifdim\pgfmath@yb>\pgfmath@x% 8*y
					\advance\pgfmath@yb-\pgfmath@y%
					\ifdim\pgfmath@yb>\pgfmath@x% 7*y
						\c@pgfmath@countb6\relax%
					\else%
						\c@pgfmath@countb7\relax%
					\fi%
				\else%
					\advance\pgfmath@yb\pgfmath@y%
					\ifdim\pgfmath@yb>\pgfmath@x% 9*y
						\c@pgfmath@countb8\relax%
					\else%
						\c@pgfmath@countb9\relax%
					\fi%
				\fi%
			\fi%	
		\else%
			\c@pgfmath@countb=0\relax%
		\fi%
		\ifnum\c@pgfmath@countb=0\relax%
			\edef\pgfmathresult{\pgfmathresult0}%
		\else%
			\pgfmath@yb\c@pgfmath@countb\pgfmath@y%
			\advance\pgfmath@x-\pgfmath@yb%	
			\edef\pgfmathresult{\pgfmathresult\the\c@pgfmath@countb}%	
		\fi%
		\pgfmathdivide@dimenbyten\pgfmath@y%
		\ifnum\c@pgfmath@counta=0\relax%
			\edef\pgfmathresult{\pgfmathresult.}%
		\fi%
		\advance\c@pgfmath@counta-1\relax%
		\ifdim\pgfmath@y=0pt\relax%
			\let\pgfmath@next\pgfmathdivide@@@%
		\else%
			\let\pgfmath@next\pgfmathdivide@@%
		\fi%
		\pgfmath@next%
}
		
\def\pgfmathdivide@@@{%
		\pgfmath@x\pgfmath@sign\pgfmathresult pt\relax%
		\pgfmath@returnone\pgfmath@x%
	\endgroup%
}


% \pgfmathgreaterthan
%
% 1.0 if #1 > #2. Otherwise 0.0
%
\def\pgfmathgreaterthan#1#2{%
	\edef\pgfmath@marshal{%
		\noexpand\pgfmathparse{#2}
		\noexpand\let\noexpand\pgfmath@arg\noexpand\pgfmathresult%
		\noexpand\pgfmathparse{#1}%
	}%
	\pgfmath@marshal%
	\pgfmathgreaterthan@{\pgfmathresult}{\pgfmath@arg}}
\def\pgfmathgreaterthan@#1#2{%
  \begingroup%
    \pgfmath@x#1pt%
    \pgfmath@y#2pt%
    \advance\pgfmath@x-\pgfmath@y%
    \ifdim\pgfmath@x>0pt\relax%
      \pgfmath@x1pt\relax%
    \else%
      \pgfmath@x0pt\relax%
    \fi%
    \pgfmath@returnone\pgfmath@x%
  \endgroup%
}

% \pgfmathlessthan
%
% 1.0 if #1< #2. Otherwise 0.0
%
\def\pgfmathlessthan#1#2{%
	\edef\pgfmath@marshal{%
		\noexpand\pgfmathparse{#2}
		\noexpand\let\noexpand\pgfmath@arg\noexpand\pgfmathresult%
		\noexpand\pgfmathparse{#1}%
	}%
	\pgfmath@marshal%
	\pgfmathlessthan@{\pgfmathresult}{\pgfmath@arg}}
\def\pgfmathlessthan@#1#2{%
  \begingroup%
    \pgfmath@x#1pt%
    \pgfmath@y#2pt%
    \advance\pgfmath@x-\pgfmath@y\relax%
    \ifdim\pgfmath@x<0pt\relax%
      \pgfmath@x1pt\relax%
    \else%
      \pgfmath@x0pt\relax%
    \fi%
    \pgfmath@returnone\pgfmath@x%
  \endgroup%
}

% \pgfmathequalto
%
% 1.0 if #1 = #2. Otherwise 0.0
%
\def\pgfmathequalto#1#2{%
	\edef\pgfmath@marshal{%
		\noexpand\pgfmathparse{#2}
		\noexpand\let\noexpand\pgfmath@arg\noexpand\pgfmathresult%
		\noexpand\pgfmathparse{#1}%
	}%
	\pgfmath@marshal%
	\pgfmathequalto@{\pgfmathresult}{\pgfmath@arg}}
\def\pgfmathequalto@#1#2{%
  \begingroup%
    \pgfmath@x#1pt%
    \pgfmath@y#2pt%
    \advance\pgfmath@x-\pgfmath@y%
    \ifdim\pgfmath@x=0pt\relax%
      \pgfmath@x1pt\relax%
    \else%
      \pgfmath@x0pt\relax%
    \fi%
    \pgfmath@returnone\pgfmath@x%
  \endgroup%
}

% \pgfmathreciprocal
%
% 1 / #1
%
\def\pgfmathreciprocal#1{%
	\pgfmathparse{#1}%
	\pgfmathreciprocal@{\pgfmathresult}}
\def\pgfmathreciprocal@#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%
}

% \pgfmathln
%
% Calculate ln(#1}
%
% This is based on an algorithm due to Rouben Rostamian and
% uses coefficients contributed by Alain Matthes.
%
\def\pgfmathln#1{\pgfmathparse{#1}\pgfmathln@{\pgfmathresult}}
\def\pgfmathln@#1{%
	\begingroup%
		\expandafter\pgfmath@x#1pt\relax%
		\ifdim\pgfmath@x>0pt\else%
			\pgfmath@error{I cannot calculate the logarithm of `#1'}{}%
		\fi%		
		\c@pgfmath@counta0\relax%
		\ifdim\pgfmath@x>2pt\relax%
			\ifdim\pgfmath@x<128pt\relax%
				\ifdim\pgfmath@x<8pt\relax%
					\ifdim\pgfmath@x<4pt\relax%
						\divide\pgfmath@x2\relax%
						\c@pgfmath@counta1\relax%
					\else%
						\divide\pgfmath@x4\relax%
						\c@pgfmath@counta2\relax%
					\fi%
				\else%
					\ifdim\pgfmath@x<32pt\relax%
						\ifdim\pgfmath@x<16pt\relax%
							\divide\pgfmath@x8\relax%
							\c@pgfmath@counta3\relax%
						\else%
							\divide\pgfmath@x16\relax%
							\c@pgfmath@counta4\relax%
						\fi%
					\else%
						\ifdim\pgfmath@x<64pt\relax%
							\divide\pgfmath@x32\relax%
							\c@pgfmath@counta5\relax%
						\else%
							\divide\pgfmath@x64\relax%
							\c@pgfmath@counta6\relax%
						\fi%
					\fi%
				\fi%
			\else%
				\ifdim\pgfmath@x<2048pt\relax%
					\ifdim\pgfmath@x<512pt\relax%
						\ifdim\pgfmath@x<256pt\relax%
							\divide\pgfmath@x128\relax%
							\c@pgfmath@counta7\relax%
						\else%
							\divide\pgfmath@x256\relax%
							\c@pgfmath@counta8\relax%
						\fi%
					\else%
						\ifdim\pgfmath@x<1024pt\relax%
							\divide\pgfmath@x512\relax%
							\c@pgfmath@counta9\relax%
						\else%
							\divide\pgfmath@x1024\relax%
							\c@pgfmath@counta10\relax%
						\fi%
					\fi%
				\else%
					\ifdim\pgfmath@x<8192pt\relax%
						\ifdim\pgfmath@x<4096pt\relax%
							\divide\pgfmath@x2048\relax%
							\c@pgfmath@counta11\relax%
						\else%
							\divide\pgfmath@x4096\relax%
							\c@pgfmath@counta12\relax%
						\fi%
					\else%
						\divide\pgfmath@x8192\relax%
						\c@pgfmath@counta13\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@x2\relax%
							\c@pgfmath@counta-1\relax%
						\else%
							\multiply\pgfmath@x4\relax%
							\c@pgfmath@counta-2\relax%
						\fi%
					\else%
						\ifdim\pgfmath@x>0.03125pt\relax% 2^-5
							\ifdim\pgfmath@x>0.0625pt\relax%
								\multiply\pgfmath@x8\relax%
								\c@pgfmath@counta-3\relax%
							\else%
								\multiply\pgfmath@x16\relax%
								\c@pgfmath@counta-4\relax%
							\fi%
						\else%
							\ifdim\pgfmath@x>0.015625pt\relax% 2^-6
								\multiply\pgfmath@x32\relax%
								\c@pgfmath@counta-5\relax%
							\else%
								\multiply\pgfmath@x64\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@x128\relax%
								\c@pgfmath@counta-7\relax%
							\else%
								\multiply\pgfmath@x256\relax%
								\c@pgfmath@counta-8\relax%
							\fi%
						\else%
							\ifdim\pgfmath@x>0.0009765625pt\relax% 2^-10
								\multiply\pgfmath@x512\relax%
								\c@pgfmath@counta-9\relax%
							\else%
								\multiply\pgfmath@x1024\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@x2048\relax%
								\c@pgfmath@counta-11\relax%
							\else%
								\multiply\pgfmath@x4096\relax%
								\c@pgfmath@counta-12\relax%
							\fi%
						\else%
							\multiply\pgfmath@x8192\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@x0.04300521312\pgfmath@x%
		\advance\pgfmath@x-.4025153233pt\relax%
		\pgfmath@x\pgfmath@temp\pgfmath@x%
		\advance\pgfmath@x1.589480044pt\relax%
		\pgfmath@x\pgfmath@temp\pgfmath@x%
		\advance\pgfmath@x-3.489886985pt\relax%
		\pgfmath@x\pgfmath@temp\pgfmath@x%
		\advance\pgfmath@x5.047861502pt\relax%
		\pgfmath@x\pgfmath@temp\pgfmath@x%
		\advance\pgfmath@x-2.787927935pt\relax%
		\advance\pgfmath@x\c@pgfmath@counta pt\relax%
		\pgfmath@x0.6931471806\pgfmath@x%	
		\pgfmath@returnone\pgfmath@x%
	\endgroup%
}
	
% \pgfmathabs
%
% Calculate |#1|
%
\def\pgfmathabs#1{%
	\pgfmathparse{#1}%
	\pgfmathabsolute@{\pgfmathresult}}
\def\pgfmathabs@#1{%
	\begingroup%
		\expandafter\pgfmath@x#1pt\relax%
		\ifdim\pgfmath@x<0pt\relax%
			\pgfmath@x-\pgfmath@x%
		\fi%
	\pgfmath@returnone\pgfmath@x%
	\endgroup%
}

% \pgfmathmod
%
% Calculate #1 mod #2.
%
\def\pgfmathmod#1#2{%
	\edef\pgfmath@marshal{%
		\noexpand\pgfmathparse{#2}
		\noexpand\let\noexpand\pgfmath@arg\noexpand\pgfmathresult%
		\noexpand\pgfmathparse{#1}%
	}%
	\pgfmath@marshal%
	\pgfmathmod@{\pgfmathresult}{\pgfmath@arg}%
}
\def\pgfmathmod@#1#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%
}

% \pgfmathsqrt
%
% Square-root of #1.
%
%
\def\pgfmathsqrt#1{\pgfmathparse{#1}\pgfmathsqrt@{\pgfmathresult}}
\def\pgfmathsqrt@#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@counta9\relax%
				\else%
					\c@pgfmath@counta8\relax%
				\fi%
			\else%
				\ifnum\c@pgfmath@countb>48\relax%
					\c@pgfmath@counta7\relax%
				\else%
					\c@pgfmath@counta6\relax%
				\fi%
			\fi%
		\else%
			\ifnum\c@pgfmath@countb>15\relax%
				\ifnum\c@pgfmath@countb>24\relax%
					\c@pgfmath@counta5\relax%
				\else%
					\c@pgfmath@counta4\relax%
				\fi%
			\else%
				\ifnum\c@pgfmath@countb>3\relax%
					\ifnum\c@pgfmath@countb>8\relax%
						\c@pgfmath@counta3\relax%
					\else%
						\c@pgfmath@counta2\relax%
					\fi%
				\else%
					\ifnum\c@pgfmath@countb>0\relax%
						\c@pgfmath@counta1\relax%
					\else%
						\c@pgfmath@counta0\relax%
					\fi%
				\fi%
			\fi%
		\fi%
		\edef\pgfmath@root{\the\c@pgfmath@counta}%
		\edef\pgfmath@rootspecial{\the\c@pgfmath@counta}%
		\multiply\c@pgfmath@counta-\c@pgfmath@counta\relax%
		\advance\c@pgfmath@counta#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@counta20\relax%
		\c@pgfmath@countb\c@pgfmath@counta%
		\multiply\c@pgfmath@countb6\relax%
		\advance\c@pgfmath@countb36\relax%
		\c@pgfmath@countc\c@pgfmath@counta\relax%
		\multiply\c@pgfmath@countc2\relax%
		\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax% 
			\advance\c@pgfmath@countb-\c@pgfmath@countc%
			\advance\c@pgfmath@countb-20\relax%
			\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
				\advance\c@pgfmath@countb-\c@pgfmath@countc%
				\advance\c@pgfmath@countb-12\relax%
				\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
					\advance\c@pgfmath@countb-\c@pgfmath@counta%
					\advance\c@pgfmath@countb-3\relax%
					\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
						\def\pgfmath@digit{0}%
					\else%
						\def\pgfmath@digit{1}%
					\fi%
				\else%
					\advance\c@pgfmath@countb\c@pgfmath@counta%
					\advance\c@pgfmath@countb5\relax%
					\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
						\def\pgfmath@digit{2}%
					\else%
						\def\pgfmath@digit{3}%
					\fi%
				\fi%
			\else%
				\advance\c@pgfmath@countb\c@pgfmath@counta%
				\advance\c@pgfmath@countb9\relax%
				\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
					\def\pgfmath@digit{4}%
				\else%
					\def\pgfmath@digit{5}%
				\fi%
			\fi%
		\else%
			\advance\c@pgfmath@countb\c@pgfmath@countc%
			\advance\c@pgfmath@countb28\relax%
			\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
				\advance\c@pgfmath@countb-\c@pgfmath@counta%
				\advance\c@pgfmath@countb-15\relax%
				\ifnum\c@pgfmath@countb>\pgfmath@remainder\relax%
					\def\pgfmath@digit{6}%
				\else%
					\def\pgfmath@digit{7}%
				\fi%
			\else%
				\advance\c@pgfmath@countb\c@pgfmath@counta%
				\advance\c@pgfmath@countb17\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\pgfmath@digit\relax%
		\multiply\c@pgfmath@counta-\pgfmath@digit\relax%
		\advance\c@pgfmath@counta\pgfmath@remainder\relax%
		\edef\pgfmath@remainder{\the\c@pgfmath@counta}%
		\c@pgfmath@counta\pgfmath@targetiterations\relax%
		\advance\c@pgfmath@counta-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
%
\def\pgfmathpow#1#2{%
	\edef\pgfmath@marshal{%
		\noexpand\pgfmathparse{#2}
		\noexpand\let\noexpand\pgfmath@arg\noexpand\pgfmathresult%
		\noexpand\pgfmathparse{#1}%
	}%
	\pgfmath@marshal%
	\pgfmathpow@{\pgfmathresult}{\pgfmath@arg}}
\def\pgfmathpow@#1#2{%
  \begingroup%
    \pgfmath@xa#1pt%
    \pgfmath@xb#2pt%
    \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@x1pt\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@counta2\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%
		\pgfmath@returnone\pgf@x%
	\endgroup%
}

% \pgfmathround
% 
% Asymmetric Half-up rounding.
%
\def\pgfmathround#1{\pgfmathparse{#1}\pgfmathround@{\pgfmathresult}}
\def\pgfmathround@#1{%
	\begingroup%
		\expandafter\pgfmath@x#1pt\relax%
		\afterassignment\pgfmath@xa%
		\expandafter\c@pgfmath@counta\the\pgfmath@x\relax%
		\pgfmath@xb\pgfmath@x%
		\ifdim\pgfmath@xb<0pt\relax%
			\ifdim\pgfmath@xa<0.5pt\relax%
			\else%
				\advance\c@pgfmath@counta-1\relax%
			\fi%
		\else%
			\ifdim\pgfmath@xa<0.5pt\relax%
			\else%
				\advance\c@pgfmath@counta1\relax%
			\fi%
		\fi%
		\pgfmath@returnone\the\c@pgfmath@counta pt%
	\endgroup%
}%

% \pgfmathfloor
% 
% Floor function.
%
\def\pgfmathfloor#1{\pgfmathparse{#1}\pgfmathfloor@{\pgfmathresult}}
\def\pgfmathfloor@#1{%
	\begingroup%
		\expandafter\pgfmath@x#1pt\relax%
		\afterassignment\pgfmath@gobbletilpgfmath@%
		\expandafter\c@pgfmath@counta\the\pgfmath@x\relax\pgfmath@%
		\pgfmath@x\c@pgfmath@counta pt\relax%
		\pgfmath@returnone\pgfmath@x%
	\endgroup%
}%

% \pgfmathceil
% 
% Ceiling function.
%
\def\pgfmathceil#1{\pgfmathparse{#1}\pgfmathceil@{\pgfmathresult}}
\def\pgfmathceil@#1{%
	\begingroup%
		\expandafter\pgfmath@x#1pt\relax%
		\afterassignment\pgfmath@gobbletilpgfmath@%
		\expandafter\c@pgfmath@counta\the\pgfmath@x\relax\pgfmath@%
		\pgfmath@y\pgfmath@x%
		\advance\pgfmath@y-\c@pgfmath@counta pt\relax%
		\pgfmath@x\c@pgfmath@counta pt\relax%
		\ifdim\pgfmath@y>0pt\relax%
			\advance\pgfmath@x1pt\relax%
		\fi%
	\pgfmath@returnone\pgfmath@x%
	\endgroup%
}%

% \pgfmathexp
%
% A Maclaurens expansion for e^#1.
% 0 <= #1 < ln(16384).
%
\def\pgfmathexp#1{\pgfmathparse{#1}\pgfmathexp@{\pgfmathresult}}
\def\pgfmathexp@#1{%
	\begingroup%
		\edef\pgfmath@arg{#1}%
		\pgfmath@x1pt\relax%
		\pgfmath@xa1pt\relax%
		\pgfmath@xb\pgfmath@x%
		\pgfmathloop%
			\pgfmath@xc\pgfmathcounter pt\relax%
			\c@pgfmath@counta\pgfmath@xc%
			\divide\c@pgfmath@counta65536\relax%
			\pgfmath@xc1pt\relax%
			\divide\pgfmath@xc\c@pgfmath@counta%
			\pgfmath@xa\pgfmath@tonumber{\pgfmath@xc}\pgfmath@xa%
			\expandafter\pgfmath@xa\pgfmath@arg\pgfmath@xa%
			\advance\pgfmath@x\pgfmath@xa%
			\ifdim\pgfmath@x=\pgfmath@xb%
			\else%
				\pgfmath@xb\pgfmath@x%
		\repeatpgfmathloop%
	\pgfmath@returnone\pgfmath@x%
	\endgroup%
}



% \pgfmathvectorlength
%
% Calcluate the Eulidean length of a 2D vector.
%
% This based on polynomial approximation co-efficents
% contributed by Rouben Rostamian.
%
% #1 - the x component of the vector.
% #2 - the y component of the vector.
%
\def\pgfmathveclen#1#2{%
	\edef\pgfmath@marshal{%
		\noexpand\pgfmathparse{#2}
		\noexpand\let\noexpand\pgfmath@arg\noexpand\pgfmathresult%
		\noexpand\pgfmathparse{#1}%
	}%
	\pgfmath@marshal%
	\pgfmathveclen@{\pgfmathresult}{\pgfmath@arg}%
}
\def\pgfmathveclen@#1#2{%
  \begingroup%
    \pgfmath@x#1pt\relax%
    \pgfmath@y#2pt\relax%
		\ifdim\pgfmath@x<0pt\relax%
			\pgfmath@x-\pgfmath@x%
		\fi%
		\ifdim\pgfmath@y<0pt\relax%
			\pgfmath@y-\pgfmath@y%
		\fi%
		\ifdim\pgfmath@x=0pt\relax%
			\pgfmath@x\pgfmath@y%
		\else%
			\ifdim\pgfmath@y=0pt\relax%
			\else%
				\ifdim\pgfmath@x>\pgfmath@y%
					\pgfmath@xa\pgfmath@x%
					\pgfmath@x\pgfmath@y%
					\pgfmath@y\pgfmath@xa%
				\fi%
				% We use a scaling factor to reduce errors.
				\ifdim\pgfmath@y>10000pt\relax%
					\c@pgfmath@counta1500\relax%
				\else%
					\ifdim\pgfmath@y>1000pt\relax%
						\c@pgfmath@counta150\relax%
					\else%
						\ifdim\pgfmath@y>100pt\relax%
							\c@pgfmath@counta50\relax%
						\else%
							\c@pgfmath@counta1\relax%
						\fi%
					\fi%
				\fi%
				\divide\pgfmath@x\c@pgfmath@counta\relax%
				\divide\pgfmath@y\c@pgfmath@counta\relax%
				\pgfmathreciprocal@{\pgfmath@tonumber{\pgfmath@y}}%
				\pgfmath@x\pgfmathresult\pgfmath@x%
				\pgfmath@xa\pgfmath@tonumber{\pgfmath@x}\pgfmath@x%
				\edef\pgfmath@temp{\pgfmath@tonumber{\pgfmath@xa}}%
				%
				% Use A+x^2*(B+x^2*(C+x^2*(D+E*x^2))) 
				% where
				% A = +1.000012594
				% B = +0.4993615349 
				% C = -0.1195159052
				% D = +0.04453994279
				% E = -0.01019210944
				%
				\pgfmath@x-0.01019210944\pgfmath@xa%
				\advance\pgfmath@x0.04453994279pt\relax%
				\pgfmath@x\pgfmath@temp\pgfmath@x%
				\advance\pgfmath@x-0.1195159052pt\relax%
				\pgfmath@x\pgfmath@temp\pgfmath@x%
				\advance\pgfmath@x0.4993615349pt\relax%
				\pgfmath@x\pgfmath@temp\pgfmath@x%
				\advance\pgfmath@x1.000012594pt\relax%
				\ifdim\pgfmath@y<0pt\relax%
					\pgfmath@y-\pgfmath@y%
				\fi%
				\pgfmath@x\pgfmath@tonumber{\pgfmath@y}\pgfmath@x%
				% Invert the scaling factor.
				\multiply\pgfmath@x\c@pgfmath@counta\relax%
			\fi%
		\fi%
		\pgfmath@returnone\pgfmath@x%
	\endgroup%
}

% \pgfmathmax
%
% Return the maximum of #1 or #2
%
\def\pgfmathmax#1#2{%
	\edef\pgfmath@marshal{%
		\noexpand\pgfmathparse{#2}
		\noexpand\let\noexpand\pgfmath@arg\noexpand\pgfmathresult%
		\noexpand\pgfmathparse{#1}%
	}%
	\pgfmath@marshal%
	\pgfmathmax@{\pgfmathresult}{\pgfmath@arg}}%
\def\pgfmathmax@#1#2{%
  \begingroup
    \pgfmath@x#1pt%
    \pgfmath@y#2pt\relax%
    \ifdim\pgfmath@x>\pgfmath@y%
      \pgfmath@returnone\pgfmath@x%
    \else%
      \pgfmath@returnone\pgfmath@y%
    \fi%
  \endgroup
}

% \pgfmathmax
%
% Return the minimim of #1 or #2
%
\def\pgfmathmin#1#2{%
	\edef\pgfmath@marshal{%
		\noexpand\pgfmathparse{#2}
		\noexpand\let\noexpand\pgfmath@arg\noexpand\pgfmathresult%
		\noexpand\pgfmathparse{#1}%
	}%
	\pgfmath@marshal%
	\pgfmathmin@{\pgfmathresult}{\pgfmath@arg}}%
\def\pgfmathmin@#1#2{%
  \begingroup
    \pgfmath@x#1pt%
    \pgfmath@y#2pt%
    \relax%
    \ifdim\pgfmath@x<\pgfmath@y%
      \pgfmath@returnone\pgfmath@x%
    \else%
      \pgfmath@returnone\pgfmath@y%
    \fi%
  \endgroup%
}

% \pgfmathscientific
%
% Return the value of #1e#2
%
% e.g. \pgfmathscientific{1.23456789123}{4}
%
% defines \pgfmathresult as 12345.67891
%
% NB These arguments *are not parsed*, as the long mantissa would be lost.
%
\def\pgfmathscientific#1#2{%
	\begingroup%
		\edef\pgfmath@sci@exponent{#2}%
		\pgfmath@x#1pt\relax%
		\ifdim\pgfmath@x<0pt\relax%
			\pgfmath@x-\pgfmath@x%
			\edef\pgfmath@sci@sign{-}%
			\edef\pgfmath@temp{\pgfmath@gobbleone#1}%
		\else%
			\edef\pgfmath@sci@sign{+}%
			\edef\pgfmath@temp{#1}%
		\fi%
		\expandafter\pgfmath@scientific@@\pgfmath@temp00000000000\pgfmath@}

\def\pgfmath@scientific@@#1.#2#3#4#5#6{%
		\edef\pgfmath@sci@int{#1}%
		\edef\pgfmath@sci@mantissaA{#2#3#4#5#6}%
		\pgfmath@scientific@@@}
	
\def\pgfmath@scientific@@@#1#2#3#4#5#6\pgfmath@{%
		\edef\pgfmath@sci@mantissaB{#1#2#3#4#5}%
		\c@pgfmath@counta\pgfmath@sci@exponent\relax%
		\c@pgfmath@countb\c@pgfmath@counta%
		\ifnum\c@pgfmath@counta<0\relax%
			\c@pgfmath@counta-\c@pgfmath@counta%
		\fi%
		\pgfmathpow@{10}{\the\c@pgfmath@counta}%
		\afterassignment\pgfmath@gobbletilpgfmath@
		\c@pgfmath@countc\pgfmathresult\relax\pgfmath@
		\edef\pgfmath@sci@factor{\the\c@pgfmath@countc}%
		\ifnum\c@pgfmath@countb<0\relax%
			% xE-y: easy...
			\pgfmath@x\pgfmath@sci@int.\pgfmath@sci@mantissaA pt\relax%
			\divide\pgfmath@x\pgfmath@sci@factor\relax%
		\else%
			% xE+y: 
			% Must do this way so as not lose digits in a long mantissa. Sigh...
			\c@pgfmath@counta\pgfmath@sci@int%
			\c@pgfmath@countb\pgfmath@sci@mantissaA%
			\multiply\c@pgfmath@counta\pgfmath@sci@factor\relax%
			\multiply\c@pgfmath@countb\pgfmath@sci@factor\relax%
			\c@pgfmath@countc\c@pgfmath@countb%
			\divide\c@pgfmath@countb100000\relax%
			\advance\c@pgfmath@counta\c@pgfmath@countb%
			\multiply\c@pgfmath@countb100000\relax%
			\advance\c@pgfmath@countc-\c@pgfmath@countb%
			\c@pgfmath@countb\pgfmath@sci@mantissaB\relax%
			\multiply\c@pgfmath@countb\pgfmath@sci@factor\relax%
			\divide\c@pgfmath@countb100000\relax%
			\advance\c@pgfmath@countc\c@pgfmath@countb%
			\advance\c@pgfmath@countc100000\relax%
			\edef\pgfmath@sci@result{\pgfmath@sci@sign\the\c@pgfmath@counta.\expandafter\pgfmath@gobbleone\the\c@pgfmath@countc pt}%
			\pgfmath@x\pgfmath@sci@result\relax%
		\fi%
		\pgfmath@returnone\pgfmath@x%
	\endgroup}





% *** The following commands DO NOT WORK without the rest of PGF ***
%
% (a dumping ground for stuff that doesn't really belong anywhere else)
	
% \pgfmathanglebetweenpoints
%
% Define \pgfmathresult as the angle between points #1 and #2
% Should get the quadrants right as well.
%
\def\pgfmathanglebetweenpoints#1#2{%
	\begingroup%
		\pgf@process{\pgfpointdiff{#1}{#2}}%
		%
		% First approximate the angle of the external point...
		%
		\pgf@xa\pgf@x%
		\pgf@ya\pgf@y%
		\pgf@xb\pgf@x%
		\pgf@yb\pgf@y%
		\ifdim\pgf@xa<0pt\relax%
			\pgf@xa-\pgf@xa%
		\fi%
		\ifdim\pgf@ya<0pt\relax%
			\pgf@ya-\pgf@ya%
		\fi%
		\ifdim\pgf@ya>\pgf@xa%
			\pgf@x\pgf@xa%
			\pgf@y\pgf@ya%
		\else%
			\pgf@x\pgf@ya%
			\pgf@y\pgf@xa%
		\fi%
		\ifdim\pgf@y=0pt\relax%
			\pgf@x0pt%
		\else%
			\pgfmathreciprocal@{\pgfmath@tonumber{\pgf@y}}%
			\pgf@x\pgfmathresult\pgf@x%
		\fi%
		\multiply\pgf@x1000\relax%
		\afterassignment\pgfmath@gobbletilpgfmath@%
		\expandafter\c@pgf@counta\the\pgf@x\relax\pgfmath@%
		\expandafter\pgf@x\csname pgfmath@atan@\the\c@pgf@counta\endcsname pt\relax%
		\ifdim\pgfmath@ya>\pgfmath@xa\relax%
			\pgf@x-\pgf@x%
			\advance\pgf@x90pt%
		\fi%
		\ifdim\pgf@xb<0pt%
			\ifdim\pgf@yb>0pt%
				\pgf@x-\pgf@x%
			\fi%
			\advance\pgf@x180pt\relax%
		\else%
			\ifdim\pgf@yb<0pt%
			\pgf@x-\pgf@x%
			\advance\pgf@x360pt\relax%
		\fi\fi%
		\pgfmath@returnone\pgf@x%
	\endgroup%
}

%
% \pgfmathrotatepointaround
%
% Rotate point #1 about point #2 by #3 degrees.
%
\def\pgfmathrotatepointaround#1#2#3{%
	\pgf@process{%
		\pgf@process{#2}%
		\pgf@xa\pgf@x%
		\pgf@ya\pgf@y%
		\pgf@xb\pgf@x%
		\pgf@yb\pgf@y%
		\pgf@process{#1}%
		\advance\pgf@x-\pgf@xa%
		\advance\pgf@y-\pgf@ya%
		\pgfmathsetmacro\angle{#3}%
		\pgfmathsin@{\angle}%
		\let\sineangle\pgfmathresult%
		\pgfmathcos@{\angle}%
		\let\cosineangle\pgfmathresult%
		\pgf@xa\cosineangle\pgf@x%
		\advance\pgf@xa-\sineangle\pgf@y%
		\pgf@ya\sineangle\pgf@x%
		\advance\pgf@ya\cosineangle\pgf@y%
		\pgf@x\pgf@xb%
		\pgf@y\pgf@yb%
		\advance\pgf@x\pgf@xa%
		\advance\pgf@y\pgf@ya%
	}%
}

% \pgfmathanglebetweenlines
%
% Calculate the clockwise angle between a line from point #1
% to point #2 and a line from #3 to point #4.
%
\def\pgfmathanglebetweenlines#1#2#3#4{%
	\begingroup%
		\pgfmathanglebetweenpoints{#1}{#2}%
		\let\firstangle\pgfmathresult%
		\pgfmathanglebetweenpoints{#3}{#4}%
		\let\secondangle\pgfmathresult%
		\ifdim\firstangle pt>\secondangle pt\relax%
			\pgfmathadd@{\secondangle}{360}%
			\let\secondangle\pgfmathresult%
		\fi%
		\pgfmathsubtract@{\secondangle}{\firstangle}%
		\pgfmath@smuggleone\pgfmathresult%
	\endgroup%
}

% \pgfmathpointreflectalongaxis
%
% Reflects point #2 around an axis centered on #2 at an angle #3.
%
\def\pgfmathreflectpointalongaxis#1#2#3{%
	\pgf@process{%
		\pgfmathanglebetweenpoints{#2}{#1}%
		\pgfmath@tempdima\pgfmathresult pt\relax%
		\pgfmathparse{#3}%
		\advance\pgfmath@tempdima-\pgfmathresult pt\relax%
		\pgfmath@tempdima-2.0\pgfmath@tempdima%
		\pgfmathrotatepointaround{#1}{#2}{\pgfmath@tonumber{\pgfmath@tempdima}}%
	}%
}


% \pgfmathpointintersectionoflineandarc 
%
% A bit experimental at the moment:
%
% Locates the point where a line crosses an eliptical arc. If the line
% does not cross the arc, a meaningless point will result.
%
% #1 the point of the line on the "convex" side of the arc.
% #2 the point of the line on the "concave" side of the arc.
% #3 the center of the eliptical arc.
% #4 start angle of the arc.
% #5 end angle of the arc.
% #6 radii of the arc.
%
\def\pgfmathpointintersectionoflineandarc#1#2#3#4#5#6{%
	\pgf@process{%
		%
		% Get the required angle.
		%
		\pgfmathanglebetweenpoints{#2}{#1}%
		\let\x\pgfmathresult%
		%
		% Get the radii of the arc.
		%
		\pgfmath@in@{and }{#6}%
	  \ifpgfmath@in@%
	    \pgf@polar@#6\@@%
	  \else%
	    \pgf@polar@#6 and #6\@@%
	  \fi%
	  \edef\xarcradius{\the\pgf@x}%
	  \edef\yarcradius{\the\pgf@y}% 
	   %
	  % Get the start and end angles of the arc...
	  % 
	  \pgfmathsetmacro\s{#4}%
	  \pgfmathsetmacro\e{#5}%
	  %
	  % ...and also with rounding.
	  %
	  \pgfmathmod@{\s}{360}%
	  \ifdim\pgfmathresult pt<0pt\relax%
	  	\pgfmathadd@{\pgfmathresult}{360}%
	  \fi%
	  \let\ss\pgfmathresult%
	  \pgfmathmod@{\e}{360}%
	  \ifdim\pgfmathresult pt<0pt\relax%
	  	\pgfmathadd@{\pgfmathresult}{360}%
	  \fi%
	  \let\ee\pgfmathresult%
	  %
	  % Hackery for when arc straddles zero.
	  %
	  \ifdim\ee pt<\ss pt\relax%
	  	\pgfmathadd@{\x}{180}%
	  	\pgfmathmod@{\pgfmathresult}{360}%
	  	\let\x\pgfmathresult%
	  \fi%
	  \def\m{360}% Measure of nearness.
		\pgfmathadd@{\s}{\e}%
		\pgfmathdivide@{\pgfmathresult}{2}%
		\let\n\pgfmathresult% The best estimate (default to middle of arc).
		\pgfmathloop%
			\pgfmathadd@{\s}{\e}%
			\pgfmathdivide@{\pgfmathresult}{2}%
			\let\p\pgfmathresult%
			\ifdim\p pt=\s pt\relax% 
			\else%
				\pgfmathanglebetweenpoints{#2}{%
					\pgfpointadd{#3}{%
						\pgf@x\xarcradius\relax%
						\pgfmathcos@{\p}%
						\pgf@x\pgfmathresult\pgf@x%
						\pgf@y\yarcradius\relax%
						\pgfmathsin@{\p}%
						\pgf@y\pgfmathresult\pgf@y%
					}%
				}%
				%
			  % Hackery for when arc straddles zero.
			  %
				\ifdim\ee pt<\ss pt\relax%
			  	\pgfmathadd@{\pgfmathresult}{180}%
			  	\pgfmathmod@{\pgfmathresult}{360}%
			  \fi%
				\let\q\pgfmathresult%
				%
				% More hackery...
				%
				\ifdim\x pt>335pt\relax%
					\ifdim\q pt<45pt\relax%
						\pgfmathadd@{\q}{360}%
						\let\q\pgfmathresult%
					\fi%
				\fi%
				\ifdim\x pt=\q pt% Found it!
						\pgfmathbreakloop% Breaks after current iteration is complete.
					\else
						\ifdim\x pt<\q pt\relax%
							\let\e\p%
						\else%
							\let\s\p%
						\fi%
					\fi%
					\pgfmathsubtract@{\x}{\q}%
					\pgfmathabs@{\pgfmathresult}%
					%
					% Save the estimate if it is better than any previous estimate.
					%
					\ifdim\pgfmathresult pt<\m pt\relax%
						\let\m\pgfmathresult%
						\let\n\p%
					\fi%				
		\repeatpgfmathloop%
		\pgfpointadd{#3}{\pgfpointpolar{\n}{\xarcradius and \yarcradius}}%
	}%
}

% \pgfmathangleonellipse
% 
% Find the angle corresponding to a point on the border of an ellispe.
%
% #1 - the point on the border.
% #2 - the radii of the ellipse.
%
\def\pgfmathangleonellipse#1#2{%
	\begingroup%
		\pgfmath@in@{and }{#2}%
	  \ifpgfmath@in@%
	    \pgf@polar@#2\@@%
	  \else%
	    \pgf@polar@#2 and #2\@@%
	  \fi%
	  \pgf@xa\pgf@x%
	  \pgf@ya\pgf@y%
	  \pgf@process{#1}% 
	  \ifdim\pgf@x=0pt\relax%
			\pgfutil@tempdima1pt\relax%
		\else%
			\pgfutil@tempdima\pgf@x%
			\pgfmathdivide@{\pgfmath@tonumber{\pgf@xa}}{\pgfmath@tonumber{\pgfutil@tempdima}}%
			\pgfutil@tempdima\pgfmathresult pt\relax%
		\fi%
		\ifdim\pgf@y=0pt\relax%
			\pgfutil@tempdima1pt\relax%
		\else%
			\pgfmathdivide@{\pgfmath@tonumber{\pgf@y}}{\pgfmath@tonumber{\pgf@ya}}%
			\pgfutil@tempdima\pgfmathresult\pgfutil@tempdima%
			\pgfmathatan@{\pgfmath@tonumber{\pgfutil@tempdima}}%
		\fi%
		%
		\pgfutil@tempdima\pgfmathresult pt\relax%
		\ifdim\pgfutil@tempdima<0pt\relax%
  		\advance\pgfutil@tempdima360pt\relax%
  	\fi%
		\ifdim\pgf@x<0pt\relax%
			\ifdim\pgf@y=0pt\relax%
				\pgfutil@tempdima180pt\relax%
			\else%
				\ifdim\pgf@y<0pt\relax%
					\advance\pgfutil@tempdima180pt\relax%
				\else%
					\advance\pgfutil@tempdima-180pt\relax%
				\fi%
			\fi%
		\else%
			\ifdim\pgf@x=0pt\relax%
				\ifdim\pgf@y<0pt\relax%
					\pgfutil@tempdima270pt\relax%
				\else%
					\pgfutil@tempdima90pt\relax%
				\fi%
			\else%
				\ifdim\pgf@y=0pt\relax%
					\pgfutil@tempdima0pt\relax%
				\fi%
			\fi%				
		\fi%
		\pgfmath@returnone\pgfutil@tempdima%
	\endgroup%
}