%% $Id: pst-fp.tex 112 2009-04-17 19:21:56Z herbert $
%%
%%
%% This is file `pst-fp.tex',
%%
%% IMPORTANT NOTICE:
%%
%% Package `pst-fp.tex'
%%
%% Herbert Voss <Herbert.Voss _at_ pstricks.de>
%% stolen from the fp package by Michael Mehlich
%%
%% This program can be redistributed and/or modified under the terms
%% of the LaTeX Project Public License Distributed from CTAN archives
%% in directory macros/latex/base/lppl.txt.
%%
%% DESCRIPTION:
%%   `pst-fp' is a PSTricks related package for a division
%%
\csname PSTFPloaded\endcsname
\let\PSTFPloaded\endinput
%
% Requires some packages
\ifx\PSTricksLoaded\endinput\else\input pstricks \fi
%
\def\fileversion{0.02}
\def\filedate{2009/04/02}
\message{`pst-fp' v\fileversion, \filedate\space (hv)}
%
\edef\PstAtCode{\the\catcode`\@} \catcode`\@=11\relax

%fixed point arithmetic with values between (including)
%      -999999999999999999.999999999999999999
% and  +999999999999999999.999999999999999999

\def\pstFPdiv#1#2#3{\pstFP@callc\pstFP@div#1{#2}{#3}}        % #1 := #2/#3

\countdef\pstFP@actcounter=10 % register 0 for counter
\ifnum\pstFP@actcounter<60\relax \pstFP@actcounter=60\fi

\newcount\pstFP@xs %sign of 1st value
\newcount\pstFP@xia%integer part of 1st value
\newcount\pstFP@xib%integer part of 1st value
\newcount\pstFP@xfa%fractional part of 1st value
\newcount\pstFP@xfb%fractional part of 1st value

\countdef\pstFP@ys=5 %sign of 2nd value
\countdef\pstFP@yia=6%integer part of 2nd value
\countdef\pstFP@yib=7%integer part of 2nd value
\countdef\pstFP@yfa=8%fractional part of 2nd value
\countdef\pstFP@yfb=9%fractional part of 2nd value

\countdef\pstFP@xk=10  %registers for splitting 1st value
\countdef\pstFP@xl=11
\countdef\pstFP@xm=12
\countdef\pstFP@xn=13
\countdef\pstFP@xo=14
\countdef\pstFP@xp=15
\countdef\pstFP@xq=16
\countdef\pstFP@xr=17
\countdef\pstFP@xz=18
\countdef\pstFP@xt=19
\countdef\pstFP@xu=20
\countdef\pstFP@xv=21

\countdef\pstFP@yk=22 %registers for splitting 2nd value
\countdef\pstFP@yl=23
\countdef\pstFP@ym=24
\countdef\pstFP@yn=25
\countdef\pstFP@yo=26
\countdef\pstFP@yp=27
\countdef\pstFP@yq=28
\countdef\pstFP@yr=29
\countdef\pstFP@yz=30
\countdef\pstFP@yt=31
\countdef\pstFP@yu=32
\countdef\pstFP@yv=33

\newcount\pstFP@rega   %auxiliary registers
\newcount\pstFP@regb
\countdef\pstFP@regc=36
\countdef\pstFP@regd=37
\countdef\pstFP@rege=38

\countdef\pstFP@rs=39 %result registers
\countdef\pstFP@ria=40
\countdef\pstFP@rib=41
\countdef\pstFP@rfa=42
\countdef\pstFP@rfb=43

\newcount\pstFP@regs    %local auxiliary registers
\countdef\pstFP@count=45
\countdef\pstFP@res=46
\countdef\pstFP@shift=47
\newcount\pstFP@times

%auxiliary macros which may be used in all of the following macros
\newif\ifpstFP@test

\def\pstFP@ignorenext#1{}
\def\pstFP@first#1#2\relax{#1}
\def\pstFP@swallow#1\relax{}
%
\def\ifpstFP@zero#1{%
  \ifnum%
    \expandafter\ifnum\csname pstFP@#1ia\endcsname=0 0\else1\fi%
    \expandafter\ifnum\csname pstFP@#1ib\endcsname=0 0\else1\fi%
    \expandafter\ifnum\csname pstFP@#1fa\endcsname=0 0\else1\fi%
    \expandafter\ifnum\csname pstFP@#1fb\endcsname=0 0\else1\fi%
    =0\relax%
}
%
%read value
%
\def\pstFP@correctintcounter#1\relax{%
  {\edef\pstFP@tmp{#1}%
   \pstFP@count=0\relax%
   \loop%
     \edef\pstFP@tmpa{\expandafter\pstFP@first\pstFP@tmp\noexpand\relax}%
     \expandafter\ifx\pstFP@tmpa0\relax%
        \advance\pstFP@count1\relax%
        \edef\pstFP@tmp{\expandafter\pstFP@ignorenext\pstFP@tmp}%
     \repeat%
   \ifnum\pstFP@count>18\relax%
     \typeout{>>>> Overflow}%
   \fi%
   \expandafter\if!\pstFP@tmp!%
     \advance\pstFP@count-18\relax%
     \pstFP@count=-\pstFP@count%
     \loop%
       \ifnum\pstFP@count>0\relax%
          \pstFP@regc=\pstFP@rega%
	  \divide\pstFP@rega10\relax\multiply\pstFP@rega10\relax%
	  \advance\pstFP@regc-\pstFP@rega\multiply\pstFP@regc100000000\relax%
	  \divide\pstFP@rega10\relax%
	  \divide\pstFP@regb10\relax\advance\pstFP@regb\pstFP@regc%
	  \advance\pstFP@count-1\relax%
       \repeat%
     \global\pstFP@rega=\pstFP@rega%
     \global\pstFP@regb=\pstFP@regb%
   \else%
     \typeout{>>>>Number too big}%
   \fi%
  }%
}
\def\pstFP@@setintcounter#1#2#3#4#5#6#7#8#9{%
  \pstFP@regb=#1#2#3#4#5#6#7#8#9\relax%
  \pstFP@correctintcounter%
}
\def\pstFP@setintcounter#1#2#3#4#5#6#7#8#9{%
  \pstFP@rega=#1#2#3#4#5#6#7#8#9\relax%
  \pstFP@@setintcounter%
}

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

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

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

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

\def\pstFP@readvalue#1#2#3{%
  % #1    macro family to catch the value
  % #2.#3 value
  %
  % regular expression [+|-]*[d]_0^18.[d]*
  %
  \pstFP@strip{#2}%
  %sign
  \expandafter\pstFP@getsign\pstFP@tmp\relax%
  \csname pstFP@#1s\endcsname=\pstFP@regs%
  %
  %integer part
  \pstFP@removeleadingzeros\pstFP@tmp\relax%
  \expandafter\pstFP@setintcounter\pstFP@tmp000000000000000000\relax%
  \csname pstFP@#1ia\endcsname=\pstFP@rega%
  \csname pstFP@#1ib\endcsname=\pstFP@regb%
  %
  %fractional part
  \pstFP@strip{#3}%
  \expandafter\pstFP@setfractcounter\pstFP@tmp000000000000000000\relax%
  \csname pstFP@#1fa\endcsname=\pstFP@rega%
  \csname pstFP@#1fb\endcsname=\pstFP@regb%
  %
  %correct sign
  \ifnum\pstFP@rega=0\relax%
    \ifnum\pstFP@regb=0\relax%
      \expandafter\ifnum\csname pstFP@#1ib\endcsname=0\relax%
        \expandafter\ifnum\csname pstFP@#1ia\endcsname=0\relax%
	  \csname pstFP@#1s\endcsname=1\relax%
	\fi%
      \fi%
    \fi%
  \fi%
}
%
%store value in macro
%
\def\pstFP@store#1#2{%
  % #1 macro
  % #2 macro family (value) to store
  %
  \ifpstFP@zero{#2}%
    \csname pstFP@#2s\endcsname=1\relax%
  \fi%
  \expandafter\ifnum\csname pstFP@#2s\endcsname<0\relax%
    \edef#1{-}%
  \else%
    \edef#1{}%
  \fi%
  \expandafter\ifnum\csname pstFP@#2ia\endcsname=0\relax%
     \expandafter\ifnum\csname pstFP@#2ib\endcsname=0\relax%
       \edef#1{#10}%
     \else%
       \edef#1{#1\expandafter\the\csname pstFP@#2ib\endcsname}%
     \fi%
  \else%
    \expandafter\advance\csname pstFP@#2ib\endcsname1000000000\relax%
    \edef#1{#1\expandafter\the\csname pstFP@#2ia\endcsname\expandafter\pstFP@ignorenext\the\csname pstFP@#2ib\endcsname}%
  \fi%
  \expandafter\advance\csname pstFP@#2fa\endcsname1000000000\relax%
  \expandafter\advance\csname pstFP@#2fb\endcsname1000000000\relax%
  \edef#1{#1\noexpand.\expandafter\pstFP@ignorenext\the\csname pstFP@#2fa\endcsname\expandafter\pstFP@ignorenext\the\csname pstFP@#2fb\endcsname}%
}
%macros to expand some arguments
%
\def\pstFP@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%
}
%
\def\pstFP@divten#1{%
  \expandafter\pstFP@regc\csname pstFP@#1ia\endcsname%
  \expandafter\divide\csname pstFP@#1ia\endcsname10\relax%
  \expandafter\pstFP@regb\csname pstFP@#1ia\endcsname%
  \multiply\pstFP@regb10\relax%
  \advance\pstFP@regc-\pstFP@regb%
  \multiply\pstFP@regc100000000\relax%
  %
  \expandafter\pstFP@rega\csname pstFP@#1ib\endcsname%
  \expandafter\divide\csname pstFP@#1ib\endcsname10\relax%
  \expandafter\pstFP@regb\csname pstFP@#1ib\endcsname%
  \multiply\pstFP@regb10\relax%
  \advance\pstFP@rega-\pstFP@regb%
  \multiply\pstFP@rega100000000\relax%
  \expandafter\advance\csname pstFP@#1ib\endcsname\pstFP@regc%
  %
  \expandafter\pstFP@regc\csname pstFP@#1fa\endcsname%
  \expandafter\divide\csname pstFP@#1fa\endcsname10\relax%
  \expandafter\pstFP@regb\csname pstFP@#1fa\endcsname%
  \multiply\pstFP@regb10\relax%
  \advance\pstFP@regc-\pstFP@regb%
  \multiply\pstFP@regc100000000\relax%
  \expandafter\advance\csname pstFP@#1fa\endcsname\pstFP@rega%
  %
  \expandafter\divide\csname pstFP@#1fb\endcsname10\relax%
  \expandafter\advance\csname pstFP@#1fb\endcsname\pstFP@regc%
}
%
\def\pstFP@multen#1{%
  \expandafter\multiply\csname pstFP@#1ia\endcsname10\relax%
  \expandafter\ifnum\csname pstFP@#1ib\endcsname<100000000\relax%
  \else%
    \expandafter\pstFP@regc\csname pstFP@#1ib\endcsname%
    \divide\pstFP@regc100000000%
    \expandafter\advance\csname pstFP@#1ia\endcsname\pstFP@regc%
    \multiply\pstFP@regc100000000%
    \expandafter\advance\csname pstFP@#1ib\endcsname-\pstFP@regc%
  \fi%
  \expandafter\multiply\csname pstFP@#1ib\endcsname10\relax%
  \expandafter\ifnum\csname pstFP@#1fa\endcsname<100000000\relax%
  \else%
    \expandafter\pstFP@regc\csname pstFP@#1fa\endcsname%
    \divide\pstFP@regc100000000%
    \expandafter\advance\csname pstFP@#1ib\endcsname\pstFP@regc%
    \multiply\pstFP@regc100000000%
    \expandafter\advance\csname pstFP@#1fa\endcsname-\pstFP@regc%
  \fi%
  \expandafter\multiply\csname pstFP@#1fa\endcsname10\relax%
  \expandafter\ifnum\csname pstFP@#1fb\endcsname<100000000\relax%
  \else%
    \expandafter\pstFP@regc\csname pstFP@#1fb\endcsname%
    \divide\pstFP@regc100000000%
    \expandafter\advance\csname pstFP@#1fa\endcsname\pstFP@regc%
    \multiply\pstFP@regc100000000%
    \expandafter\advance\csname pstFP@#1fb\endcsname-\pstFP@regc%
  \fi%
  \expandafter\multiply\csname pstFP@#1fb\endcsname10\relax%
}
%
\def\pstFP@counttimes{%
  {\global\pstFP@times=0\relax%
   \loop%
     \ifnum%
        \ifnum\pstFP@xia>\pstFP@yia1%
	\else\ifnum\pstFP@xia<\pstFP@yia0%
	\else%
	  \ifnum\pstFP@xib>\pstFP@yib1%
	  \else\ifnum\pstFP@xib<\pstFP@yib0%
	  \else%
	    \ifnum\pstFP@xfa>\pstFP@yfa1%
	    \else\ifnum\pstFP@xfa<\pstFP@yfa0%
	    \else%
	      \ifnum\pstFP@xfb>\pstFP@yfb1%
	      \else\ifnum\pstFP@xfb<\pstFP@yfb0%
	      \else%
	         1%
	      \fi\fi%
	    \fi\fi%
	  \fi\fi%
	\fi\fi%
	=1\relax%
       \global\advance\pstFP@times1\relax%
       \global\advance\pstFP@xfb-\pstFP@yfb%
       \ifnum\pstFP@xfb<0\relax%
         \global\advance\pstFP@xfb1000000000\relax%
	 \global\advance\pstFP@xfa-1\relax%
       \fi%
       \global\advance\pstFP@xfa-\pstFP@yfa%
       \ifnum\pstFP@xfa<0\relax%
         \global\advance\pstFP@xfa1000000000\relax%
	 \global\advance\pstFP@xib-1\relax%
       \fi%
       \global\advance\pstFP@xib-\pstFP@yib%
       \ifnum\pstFP@xib<0\relax%
         \global\advance\pstFP@xib1000000000\relax%
	 \global\advance\pstFP@xia-1\relax%
       \fi%
       \global\advance\pstFP@xia-\pstFP@yia%
     \repeat%
  }%
}
%
\def\pstFP@div#1#2.#3.#4\relax#5.#6.#7\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 '.'
  %
  % algorithmic idea (for x>0, y>0)
  %   - %determine \pstFP@shift  such that y*10^\pstFP@shift <100000000<=y*10^(\pstFP@shift+1)
  %   - %determine \pstFP@shift' such that x*10^\pstFP@shift'<100000000<=x*10^(\pstFP@shift+1)
  %   - x=x*\pstFP@shift'
  %   - y=y*\pstFP@shift
  %   - \pstFP@shift=\pstFP@shift-\pstFP@shift'
  %   - res=0
  %   - while y>0 %fixed-point representation!
  %   -   \pstFP@times=0
  %   -	  while x>y
  %   -     \pstFP@times=\pstFP@times+1
  %   -     x=x-y
  %   -   end
  %   -   y=y/10
  %   -   res=10*res+\pstFP@times/1000000000
  %   - end
  %   - %shift the result according to \pstFP@shift
  %
  {\pstFP@readvalue{x}{#2}{#3}%
   \pstFP@readvalue{y}{#5}{#6}%
   %
   %sign
   \multiply\pstFP@xs\pstFP@ys%
   \pstFP@rs=\pstFP@xs%
   %
   %compute division
   \ifpstFP@zero{y}%
     \typeout{>>>>Division by zero}%
   \else%
     \ifpstFP@zero{x}\def\next##1{\edef\pstFP@tmp{0}}\else\def\next##1{##1}\fi%
     \next%
       {\pstFP@shift=0\relax%
        \loop%
          \ifnum\pstFP@yia<100000000\relax%
	     \pstFP@multen{y}%
	     \advance\pstFP@shift1\relax%
	   \repeat%
        \loop%
          \ifnum\pstFP@xia<100000000\relax%
	     \pstFP@multen{x}%
	     \advance\pstFP@shift-1\relax%
	  \repeat%
        \pstFP@ria=0\pstFP@rib=0\pstFP@rfa=0\pstFP@rfb=0\relax%
        \loop%
          \ifpstFP@zero{y}\else%
	     \pstFP@counttimes%divides x by \pstFP@times*y
	     \pstFP@divten{y}%
	     \pstFP@multen{r}%
	     \advance\pstFP@rfb\pstFP@times%
	     \ifnum\pstFP@rfb<1000000000\relax\else%
	       \advance\pstFP@rfa1\advance\pstFP@rfb-1000000000\relax%
	       \ifnum\pstFP@rfa<1000000000\relax\else%
	         \advance\pstFP@rib1\advance\pstFP@rfa-1000000000\relax%
		 \ifnum\pstFP@rib<1000000000\relax\else%
		   \advance\pstFP@ria1\advance\pstFP@rib-1000000000\relax%
		 \fi%
	       \fi%
	     \fi%
	  \repeat%
        \loop%
          \ifnum\pstFP@shift>17%
	    \advance\pstFP@shift-1\relax%
	    \ifnum\pstFP@ria<100000000\else\pstFP@ria=-1\fi%
	    \ifnum\pstFP@ria<0\pstFP@ria=-1\fi%
	    \pstFP@multen{r}%
	  \repeat%
        \ifnum\pstFP@ria<1000000000\else\pstFP@ria=-1\fi%
        \loop%
          \ifnum\pstFP@shift<17%
	    \advance\pstFP@shift1\relax%
	    \pstFP@divten{r}%
	  \repeat%
        \ifnum\pstFP@ria<0\relax%
          \typeout{>>>>Overflow}%
        \else%
          \pstFP@store\pstFP@tmp{r}%
        \fi%
       }%
    \fi%
   %
   \global\let\pstFP@tmp=\pstFP@tmp%
   %
  }%
  \let#1\pstFP@tmp%
}
%
\catcode`\@=\PstAtCode\relax
%
%% END: pst-fp.tex
\endinput
