% 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

% round function. Uses asymmetric half-up rounding.
%
\pgfmathdeclarefunction{round}{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%
}%

% floor function.
%
\pgfmathdeclarefunction{floor}{1}{%	
	\begingroup%
		\expandafter\pgfmath@x#1pt\relax%
		\afterassignment\pgfmathfloor@collect@remainder%
		\expandafter\c@pgfmath@counta\the\pgfmath@x\pgfmath@%
		\expandafter\pgfmath@x#1pt\relax%
		\ifdim\pgfmath@x<0pt\relax%
			\ifdim\pgfmathfloor@remainder>0pt
				\advance\c@pgfmath@counta by -1\relax%
			\else
				% ok, we stripped only '0'.
			\fi
		\fi%
		\pgfmath@x\c@pgfmath@counta pt\relax%
		\pgfmath@returnone\pgfmath@x%
	\endgroup%
}%

\def\pgfmathfloor@collect@remainder#1\pgfmath@{%
	\def\pgfmathfloor@remainder{#1}%
	\ifx\pgfmathfloor@remainder\pgfutil@empty
		\def\pgfmathfloor@remainder{0}%
	\fi
}%

% ceil function.
%
\pgfmathdeclarefunction{ceil}{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%
}%

% int function
%
\pgfmathdeclarefunction{int}{1}{%
	\begingroup%
		\expandafter\pgfmathint@@#1.\pgfmath@stop
		\pgfmath@smuggleone\pgfmathresult%
	\endgroup}

\def\pgfmathint@@#1.#2\pgfmath@stop{%
	\def\pgfmath@temp{#1}%
	\ifx\pgfmath@temp\pgfmath@empty%
		\def\pgfmathresult{0}%
	\else%
		\pgfmath@in@{#1}{+-+0-0}%
		\ifpgfmath@in@
			\def\pgfmathresult{0}%
		\else%
			\def\pgfmathresult{#1}%
		\fi%
	\fi}

% frac function.
%
\pgfmathdeclarefunction{frac}{1}{%	
	\begingroup%
		\expandafter\pgfmathfrac@@#1.\pgfmath@stop%
		\pgfmath@smuggleone\pgfmathresult%
	\endgroup}

\def\pgfmathfrac@@#1.{%
	\pgfmath@ifnextchar\pgfmath@stop{\def\pgfmathresult{0.0}\pgfmath@gobbleone}{\pgfmathfrac@@@}%
}
\def\pgfmathfrac@@@#1.\pgfmath@stop{\def\pgfmathresult{0.#1}}

% real function.
%
\pgfmathdeclarefunction{real}{1}{%
	\begingroup%
		\pgfmath@x=#1pt\relax%
		\edef\pgfmathresult{\pgfmath@tonumber{\pgfmath@x}}%
		\pgfmath@smuggleone\pgfmathresult%
	\endgroup%
}
