% This material is subject to the LaTeX Project Public License. See
%   http://www.ctan.org/tex-archive/help/Catalogue/licenses.lppl.html
% for the details of that license.

% This package is loaded by animate.sty. It defines \@anim@add for fixed point
% addition

% Copyright notice:
% The code relavant to fixed point addition was taken with virtually no
% modification from Michael Mehlich's fp-basic[1996/05/13] package

% Copyright 2008 Alexander Grahn

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{animfp}[2009/07/23 fixed point addition for animate.sty]

%adding two values
\def\@anim@add#1#2#3{\afp@callc\afp@add#1{#2}{#3}+\relax} % #1 := #2+#3

%allocation of registers
\newcount\afp@xs %sign of 1st value
\newcount\afp@xia%integer part of 1st value
\newcount\afp@xib%integer part of 1st value
\newcount\afp@xfa%fractional part of 1st value
\newcount\afp@xfb%fractional part of 1st value
\countdef\afp@ys=5 %sign of 2nd value
\countdef\afp@yia=6%integer part of 2nd value
\countdef\afp@yib=7%integer part of 2nd value
\countdef\afp@yfa=8%fractional part of 2nd value
\countdef\afp@yfb=9%fractional part of 2nd value

\newcount\afp@rega   %auxiliary registers
\newcount\afp@regb
\countdef\afp@regc=36

\newcount\afp@regs    %local auxiliary registers
\countdef\afp@count=45

%auxiliary macros which may be used in all of the following macros
\def\afp@ignorenext#1{}
\def\afp@first#1#2\relax{#1}
\def\afp@swallow#1\relax{}

\def\ifafp@zero#1{%
  \ifnum%
    \expandafter\ifnum\csname afp@#1ia\endcsname=0 0\else1\fi%
    \expandafter\ifnum\csname afp@#1ib\endcsname=0 0\else1\fi%
    \expandafter\ifnum\csname afp@#1fa\endcsname=0 0\else1\fi%
    \expandafter\ifnum\csname afp@#1fb\endcsname=0 0\else1\fi%
    =0\relax%
}

%read value
\def\afp@correctintcounter#1\relax{%
  {\edef\afp@tmp{#1}%
   \afp@count=0\relax%
   \loop%
     \edef\afp@tmpa{\expandafter\afp@first\afp@tmp\noexpand\relax}%
     \expandafter\ifx\afp@tmpa0\relax%
        \advance\afp@count1\relax%
        \edef\afp@tmp{\expandafter\afp@ignorenext\afp@tmp}%
     \repeat%
   \ifnum\afp@count>18\relax%
     \PackageError{animfp}{Overflow}{}%
   \fi%
   \expandafter\if!\afp@tmp!%
     \advance\afp@count-18\relax%
     \afp@count=-\afp@count%
     \loop%
       \ifnum\afp@count>0\relax%
          \afp@regc=\afp@rega%
      \divide\afp@rega10\relax\multiply\afp@rega10\relax%
      \advance\afp@regc-\afp@rega\multiply\afp@regc100000000\relax%
      \divide\afp@rega10\relax%
      \divide\afp@regb10\relax\advance\afp@regb\afp@regc%
      \advance\afp@count-1\relax%
       \repeat%
     \global\afp@rega=\afp@rega%
     \global\afp@regb=\afp@regb%
   \else%
     \PackageError{animfp}{Number too big}{}%
   \fi%
  }%
}
\def\afp@@setintcounter#1#2#3#4#5#6#7#8#9{%
  \afp@regb=#1#2#3#4#5#6#7#8#9\relax%
  \afp@correctintcounter%
}
\def\afp@setintcounter#1#2#3#4#5#6#7#8#9{%
  \afp@rega=#1#2#3#4#5#6#7#8#9\relax%
  \afp@@setintcounter%
}

\def\afp@@setfractcounter#1#2#3#4#5#6#7#8#9{%
  \afp@regb=#1#2#3#4#5#6#7#8#9\relax%
  \afp@swallow%
}
\def\afp@setfractcounter#1#2#3#4#5#6#7#8#9{%
  \afp@rega=#1#2#3#4#5#6#7#8#9\relax%
  \afp@@setfractcounter%
}

\def\afp@getsign#1\relax{%
  {\afp@regs=1\relax%
   \edef\afp@tmp{#1}%
   \loop%
     \edef\afp@tmpa{\expandafter\afp@first\afp@tmp\noexpand\relax}%
     \expandafter\ifx\afp@tmpa-\relax%
        \multiply\afp@regs-1\relax%
     \fi%
     \ifnum\expandafter\ifx\afp@tmpa-1\else0\fi\expandafter\ifx\afp@tmpa+1\else0\fi>0%
        \edef\afp@tmp{\expandafter\afp@ignorenext\afp@tmp}%
     \repeat%
   \global\let\afp@tmp\afp@tmp%
   \global\afp@regs=\afp@regs%
  }%
}

\def\afp@removeleadingzeros#1\relax{%
  {\edef\afp@tmp{#1}%
   \loop%
     \edef\afp@tmpa{\expandafter\afp@first\afp@tmp\noexpand\relax}%
     \expandafter\ifx\afp@tmpa0\relax%
       \edef\afp@tmp{\expandafter\afp@ignorenext\afp@tmp}%
     \repeat%
   \global\let\afp@tmp\afp@tmp%
  }%
}

\newif\ifafp@nonstop
\def\afp@strip#1{%
  {\edef\afp@tmp{#1}%
   \edef\afp@tmpb{}%
   \ifx\afp@tmp\@empty\else%
     \afp@nonstoptrue%
     \loop%
       \edef\afp@tmpa{\expandafter\afp@first\afp@tmp\noexpand\relax}%
       \expandafter\ifx\afp@tmpa-\relax\edef\afp@tmpb{\afp@tmpb\afp@tmpa}\else%
       \expandafter\ifx\afp@tmpa+\relax\edef\afp@tmpb{\afp@tmpb\afp@tmpa}\else%
       \expandafter\ifx\afp@tmpa0\relax\edef\afp@tmpb{\afp@tmpb\afp@tmpa}\else%
       \expandafter\ifx\afp@tmpa1\relax\edef\afp@tmpb{\afp@tmpb\afp@tmpa}\else%
       \expandafter\ifx\afp@tmpa2\relax\edef\afp@tmpb{\afp@tmpb\afp@tmpa}\else%
       \expandafter\ifx\afp@tmpa3\relax\edef\afp@tmpb{\afp@tmpb\afp@tmpa}\else%
       \expandafter\ifx\afp@tmpa4\relax\edef\afp@tmpb{\afp@tmpb\afp@tmpa}\else%
       \expandafter\ifx\afp@tmpa5\relax\edef\afp@tmpb{\afp@tmpb\afp@tmpa}\else%
       \expandafter\ifx\afp@tmpa6\relax\edef\afp@tmpb{\afp@tmpb\afp@tmpa}\else%
       \expandafter\ifx\afp@tmpa7\relax\edef\afp@tmpb{\afp@tmpb\afp@tmpa}\else%
       \expandafter\ifx\afp@tmpa8\relax\edef\afp@tmpb{\afp@tmpb\afp@tmpa}\else%
       \expandafter\ifx\afp@tmpa9\relax\edef\afp@tmpb{\afp@tmpb\afp@tmpa}\else%
       \ifx\afp@tmpa\@empty\afp@nonstopfalse\else%
       \ifx\afp@tmpa\space\afp@nonstopfalse\else%
         \PackageError{animfp}{Illegal character `\afp@tmpa'\space found in float number}{}%
       \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi%
       \edef\afp@tmp{\expandafter\afp@ignorenext\afp@tmp}%
       \ifx\afp@tmp\@empty\afp@nonstopfalse\fi%
       \ifafp@nonstop%
       \repeat%
   \fi%
   \global\let\afp@tmp\afp@tmpb%
  }%
}

\def\afp@readvalue#1#2#3{%
  % #1    macro family to catch the value
  % #2.#3 value
  %
  % regular expression [+|-]*[d]_0^18.[d]*
  %
  \afp@strip{#2}%
  %sign
  \expandafter\afp@getsign\afp@tmp\relax%
  \csname afp@#1s\endcsname=\afp@regs%
  %
  %integer part
  \afp@removeleadingzeros\afp@tmp\relax%
  \expandafter\afp@setintcounter\afp@tmp000000000000000000\relax%
  \csname afp@#1ia\endcsname=\afp@rega%
  \csname afp@#1ib\endcsname=\afp@regb%
  %
  %fractional part
  \afp@strip{#3}%
  \expandafter\afp@setfractcounter\afp@tmp000000000000000000\relax%
  \csname afp@#1fa\endcsname=\afp@rega%
  \csname afp@#1fb\endcsname=\afp@regb%
  %
  %correct sign
  \ifnum\afp@rega=0\relax%
    \ifnum\afp@regb=0\relax%
      \expandafter\ifnum\csname afp@#1ib\endcsname=0\relax%
        \expandafter\ifnum\csname afp@#1ia\endcsname=0\relax%
      \csname afp@#1s\endcsname=1\relax%
    \fi%
      \fi%
    \fi%
  \fi%
}

%store value in macro
\def\afp@store#1#2{%
  % #1 macro
  % #2 macro family (value) to store
  %
  \ifafp@zero{#2}%
    \csname afp@#2s\endcsname=1\relax%
  \fi%
  \expandafter\ifnum\csname afp@#2s\endcsname<0\relax%
    \edef#1{-}%
  \else%
    \edef#1{}%
  \fi%
  \expandafter\ifnum\csname afp@#2ia\endcsname=0\relax%
     \expandafter\ifnum\csname afp@#2ib\endcsname=0\relax%
       \edef#1{#10}%
     \else%
       \edef#1{#1\expandafter\the\csname afp@#2ib\endcsname}%
     \fi%
  \else%
    \expandafter\advance\csname afp@#2ib\endcsname1000000000\relax%
    \edef#1{#1\expandafter\the\csname afp@#2ia\endcsname\expandafter\afp@ignorenext\the\csname afp@#2ib\endcsname}%
  \fi%
  \expandafter\advance\csname afp@#2fa\endcsname1000000000\relax%
  \expandafter\advance\csname afp@#2fb\endcsname1000000000\relax%
  \edef#1{#1\noexpand.\expandafter\afp@ignorenext\the\csname afp@#2fa\endcsname\expandafter\afp@ignorenext\the\csname afp@#2fb\endcsname}%
}

%macros to expand some arguments
\def\afp@callc#1#2#3#4{%
  % #1 macro to call
  % #2 macro, which gets the result
  % #3 1st value
  % #4 2nd value
  % expand the values and split them into the integer and the fractional parts
  \edef\next{\noexpand#1\noexpand#2#3..\noexpand\relax#4..\noexpand\relax}%
  \next%
}

%remove trailing zeros of argument (A. Grahn 2009/06/29)
\def\afp@removetrailingzeros#1\relax{%
  {%
    \edef\afp@tmp{#1}%
    \gdef\afp@reversed{}%
    \expandafter\afp@reverse\afp@tmp,\@nil%
    \afp@removeleadingzeros\afp@reversed\relax%
    \gdef\afp@reversed{}%
    \expandafter\afp@reverse\afp@tmp,\@nil%
    \global\let\afp@tmp\afp@reversed%
  }%
}
\def\afp@reverse#1#2\@nil{% reverse string, used in \afp@removetrailingzeros
  \if#1,\else%
    \xdef\afp@reversed{#1\afp@reversed}%
    \afp@reverse#2\@nil%
  \fi%
}%

%add two values
\def\afp@add#1#2.#3.#4\relax#5.#6.#7\relax#8\relax{%
  % #1 macro, which gets the result
  % #2 integer part of 1st value
  % #3 fractional part of 1st value
  % #4 dummy to swallow everthing after the 2nd '.'
  % #5 integer part of 2nd value
  % #6 fractional part of 2nd value
  % #7 dummy to swallow everthing after the 2nd '.'
  %
  {%
   \afp@readvalue{x}{#2}{#3}%
   \afp@readvalue{y}{#5}{#6}%
   %
   \ifnum\afp@xs=\afp@ys%
     \advance\afp@xfb\afp@yfb%
     \advance\afp@xfa\afp@yfa%
     \ifnum\afp@xfb<1000000000\relax\else%
       \advance\afp@xfb-1000000000\relax%
       \advance\afp@xfa1\relax%
     \fi%
     \advance\afp@xib\afp@yib%
     \ifnum\afp@xfa<1000000000\relax\else%
       \advance\afp@xfa-1000000000\relax%
       \advance\afp@xib1\relax%
     \fi%
     \advance\afp@xia\afp@yia%
     \ifnum\afp@xib<1000000000\relax\else%
       \advance\afp@xib-1000000000\relax%
       \advance\afp@xia1\relax%
     \fi%
     \ifnum\afp@xia<1000000000\relax\else%
       \PackageError{animfp}{Overflow}{}%
     \fi%
     \afp@store\afp@tmp{x}%
   \else%
     \advance\afp@xfb-\afp@yfb%
     \ifnum\afp@xfb<0\relax%
       \advance\afp@yfa1\relax%
       \advance\afp@xfb1000000000\relax%
     \fi%
     \advance\afp@xfa-\afp@yfa%
     \ifnum\afp@xfa<0\relax%
       \advance\afp@yib1\relax%
       \advance\afp@xfa1000000000\relax%
     \fi%
     \advance\afp@xib-\afp@yib%
     \ifnum\afp@xib<0\relax%
       \advance\afp@yia1\relax%
       \advance\afp@xib1000000000\relax%
     \fi%
     \advance\afp@xia-\afp@yia%
     \ifnum\afp@xia<0\relax%
       \afp@xs=-\afp@xs%
       \ifnum\afp@xfb=0\relax\else%
         \advance\afp@xfb-1000000000\relax\afp@xfb=-\afp@xfb%
     \advance\afp@xfa1\relax%
       \fi%
       \ifnum\afp@xfa=0\relax\else%
         \advance\afp@xfa-1000000000\relax\afp@xfa=-\afp@xfa%
     \advance\afp@xib1\relax%
       \fi%
       \ifnum\afp@xib=0\relax\else%
         \advance\afp@xib-1000000000\relax\afp@xib=-\afp@xib%
     \advance\afp@xia1\relax%
       \fi%
       \relax\afp@xia=-\afp@xia%
     \fi%
     \afp@store\afp@tmp{x}%
   \fi%
   %
   \global\let\afp@tmp\afp@tmp%
  }%
  \afp@removetrailingzeros\afp@tmp\relax% (A. Grahn 2009/06/29)
  \let#1\afp@tmp%
}
