\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{fp-exp}[1995/04/03]

%version information
\def\FP@expversion{0.7e}
\message{%
  `Fixed Point Exponentiation',%
  \space\space\space\space%
  Version \FP@expversion%
  \space (C) Michael Mehlich%
  \space\space\space\space\space\space\space%
  \space\space\space\space\space\space%
}

%resolve dependencies
\RequirePackage{fp-basic}

%%%public area (macros which may be used)%%%

%constants
\edef\FPe{2.718281828459045235}
%unary functions
\def\FPexp#1#2{\FP@calld\FP@exp#1{#2}}
\def\FPln#1#2{\FP@calld\FP@ln#1{#2}}
%binary functions
\def\FPpow#1#2#3{\FP@pow#1{#2}{#3}}
\def\FProot#1#2#3{\FP@root#1{#2}{#3}}

%%%private fp-area (don't use these macros)%%%

%allocation of registers

\countdef\FP@ls=50
\countdef\FP@lfa=51
\countdef\FP@lfb=52

\countdef\FP@regd=53
\countdef\FP@rege=54

%compute e pow x

\expandafter\edef\csname FP@e0\endcsname{1.000000000000000000}
\expandafter\edef\csname FP@e1\endcsname{2.718281828459045235}
\expandafter\edef\csname FP@e2\endcsname{7.389056098930650227}
\expandafter\edef\csname FP@e3\endcsname{20.085536923187667741}
\expandafter\edef\csname FP@e4\endcsname{54.598150033144239078}
\expandafter\edef\csname FP@e5\endcsname{148.413159102576603421}
\expandafter\edef\csname FP@e6\endcsname{403.428793492735122608}
\expandafter\edef\csname FP@e7\endcsname{1096.633158428458599264}
\expandafter\edef\csname FP@e8\endcsname{2980.957987041728274744}
\expandafter\edef\csname FP@e9\endcsname{8103.083927575384007710}
\expandafter\edef\csname FP@e10\endcsname{22026.465794806716516958}
\expandafter\edef\csname FP@e11\endcsname{59874.141715197818455326}
\expandafter\edef\csname FP@e12\endcsname{162754.791419003920808005}
\expandafter\edef\csname FP@e13\endcsname{442413.392008920503326103}
\expandafter\edef\csname FP@e14\endcsname{1202604.284164776777749237}
\expandafter\edef\csname FP@e15\endcsname{3269017.372472110639301855}
\expandafter\edef\csname FP@e16\endcsname{8886110.520507872636763024}
\expandafter\edef\csname FP@e17\endcsname{24154952.753575298214775435}
\expandafter\edef\csname FP@e18\endcsname{65659969.137330511138786503}
\expandafter\edef\csname FP@e19\endcsname{178482300.963187260844910034}
\expandafter\edef\csname FP@e20\endcsname{485165195.409790277969106831}
\expandafter\edef\csname FP@e21\endcsname{1318815734.483214697209998884}
\expandafter\edef\csname FP@e22\endcsname{3584912846.131591561681159946}
\expandafter\edef\csname FP@e23\endcsname{9744803446.248902600034632685}
\expandafter\edef\csname FP@e24\endcsname{26489122129.843472294139162153}
\expandafter\edef\csname FP@e25\endcsname{72004899337.385872524161351466}
\expandafter\edef\csname FP@e26\endcsname{195729609428.838764269776397876}
\expandafter\edef\csname FP@e27\endcsname{532048240601.798616683747304341}
\expandafter\edef\csname FP@e28\endcsname{1446257064291.475173677047422997}
\expandafter\edef\csname FP@e29\endcsname{3931334297144.042074388620580844}
\expandafter\edef\csname FP@e30\endcsname{10686474581524.462146990468650741}
\expandafter\edef\csname FP@e31\endcsname{29048849665247.425231085682111680}
\expandafter\edef\csname FP@e32\endcsname{78962960182680.695160978022635108}
\expandafter\edef\csname FP@e33\endcsname{214643579785916.064624297761531261}
\expandafter\edef\csname FP@e34\endcsname{583461742527454.881402902734610391}
\expandafter\edef\csname FP@e35\endcsname{1586013452313430.728129644625774660}
\expandafter\edef\csname FP@e36\endcsname{4311231547115195.227113422292856925}
\expandafter\edef\csname FP@e37\endcsname{11719142372802611.308772939791190195}
\expandafter\edef\csname FP@e38\endcsname{31855931757113756.220328671701298646}
\expandafter\edef\csname FP@e39\endcsname{86593400423993746.953606932719264934}
\expandafter\edef\csname FP@e40\endcsname{235385266837019985.407899910749034805}
\expandafter\edef\csname FP@e41\endcsname{639843493530054949.222663403515570819}

\expandafter\edef\csname FP@e-0\endcsname{1.000000000000000000}
\expandafter\edef\csname FP@e-1\endcsname{0.367879441171442322}
\expandafter\edef\csname FP@e-2\endcsname{0.135335283236612692}
\expandafter\edef\csname FP@e-3\endcsname{0.049787068367863943}
\expandafter\edef\csname FP@e-4\endcsname{0.018315638888734180}
\expandafter\edef\csname FP@e-5\endcsname{0.006737946999085467}
\expandafter\edef\csname FP@e-6\endcsname{0.002478752176666358}
\expandafter\edef\csname FP@e-7\endcsname{0.000911881965554516}
\expandafter\edef\csname FP@e-8\endcsname{0.000335462627902512}
\expandafter\edef\csname FP@e-9\endcsname{0.000123409804086680}
\expandafter\edef\csname FP@e-10\endcsname{0.000045399929762485}
\expandafter\edef\csname FP@e-11\endcsname{0.000016701700790246}
\expandafter\edef\csname FP@e-12\endcsname{0.000006144212353328}
\expandafter\edef\csname FP@e-13\endcsname{0.000002260329406981}
\expandafter\edef\csname FP@e-14\endcsname{0.000000831528719104}
\expandafter\edef\csname FP@e-15\endcsname{0.000000305902320502}
\expandafter\edef\csname FP@e-16\endcsname{0.000000112535174719}
\expandafter\edef\csname FP@e-17\endcsname{0.000000041399377188}
\expandafter\edef\csname FP@e-18\endcsname{0.000000015229979745}
\expandafter\edef\csname FP@e-19\endcsname{0.000000005602796438}
\expandafter\edef\csname FP@e-20\endcsname{0.000000002061153622}
\expandafter\edef\csname FP@e-21\endcsname{0.000000000758256043}
\expandafter\edef\csname FP@e-22\endcsname{0.000000000278946809}
\expandafter\edef\csname FP@e-23\endcsname{0.000000000102618796}
\expandafter\edef\csname FP@e-24\endcsname{0.000000000037751345}
\expandafter\edef\csname FP@e-25\endcsname{0.000000000013887944}
\expandafter\edef\csname FP@e-26\endcsname{0.000000000005109089}
\expandafter\edef\csname FP@e-27\endcsname{0.000000000001879529}
\expandafter\edef\csname FP@e-28\endcsname{0.000000000000691440}
\expandafter\edef\csname FP@e-29\endcsname{0.000000000000254367}
\expandafter\edef\csname FP@e-30\endcsname{0.000000000000093576}
\expandafter\edef\csname FP@e-31\endcsname{0.000000000000034425}
\expandafter\edef\csname FP@e-32\endcsname{0.000000000000012664}
\expandafter\edef\csname FP@e-33\endcsname{0.000000000000004659}
\expandafter\edef\csname FP@e-34\endcsname{0.000000000000001714}
\expandafter\edef\csname FP@e-35\endcsname{0.000000000000000631}
\expandafter\edef\csname FP@e-36\endcsname{0.000000000000000232}
\expandafter\edef\csname FP@e-37\endcsname{0.000000000000000085}
\expandafter\edef\csname FP@e-38\endcsname{0.000000000000000031}
\expandafter\edef\csname FP@e-39\endcsname{0.000000000000000012}
\expandafter\edef\csname FP@e-40\endcsname{0.000000000000000004}
\expandafter\edef\csname FP@e-41\endcsname{0.000000000000000002}
\expandafter\edef\csname FP@e-42\endcsname{0.000000000000000001}

\def\FP@exp@mul#1#2#3#4#5#6{%
  % #1 macro which gets fractional part 1
  % #2 macro which fractional part 1
  % #3 first value part 1
  % #4 first value part 2
  % #5 second value part 1
  % #6 second value part 2
  \FP@split\FP@xq\FP@xr\FP@xz{#3}\FP@split\FP@xt\FP@xu\FP@xv{#4}%
  \FP@split\FP@yq\FP@yr\FP@yz{#5}\FP@split\FP@yt\FP@yu\FP@yv{#6}%
  \FP@prod=0\relax%
  \edef\FP@rd{}%
  \FP@@mul vv 			\relax\FP@saveshift%
  \FP@@mul vu uv 		\relax\FP@saveshift%
  \FP@@mul uu vt tv 		\relax\FP@saveshift%
  \FP@@mul ut tu vz zv 		\relax\FP@saveshift%
  \FP@@mul tt uz zu vr rv 	\relax\FP@saveshift%
  \FP@@mul zt tz ur ru qv vq 	\relax\FP@saveshift%
  \FP@@mul zz rt tr qu uq 	\relax\FP@saveshift%
  \FP@@mul rz zr qt tq 		\relax\FP@saveshift%
  \FP@@mul rr qz zq 		\relax\FP@saveshift%
  \FP@@mul qr rq 		\relax\FP@saveshift%
  \FP@@mul qq 			\relax\FP@saveshift\FP@saveshift%
  \FP@ninesplit\FP@rd#1=\FP@res%
  \FP@ninesplit\FP@rd#2=\FP@res%
}

\def\FP@exp@divn#1#2#3#4#5{%
  % #1 macro which gets fractional part 1
  % #2 macro which fractional part 1
  % #3 value part 1
  % #4 value part 2
  % #5 integer (0,..,1000)
  \FP@rega=#3\divide\FP@rega#5%
  \FP@regd=\FP@rega%
  \multiply\FP@rega#5\relax%
  \advance\FP@rega-#3\relax%
  \multiply\FP@rega-1000\relax%
  \FP@regb=#4\divide\FP@regb1000000\relax%
  \advance\FP@rega\FP@regb%
  \FP@regc=\FP@rega\divide\FP@regc#5\relax%
  \FP@rege=\FP@regc\multiply\FP@rege1000000\relax%
  \multiply\FP@regc#5\advance\FP@rega-\FP@regc\multiply\FP@rega1000000\relax%
  \multiply\FP@regb1000000\relax\advance\FP@regb-#4\relax%
  \advance\FP@rega-\FP@regb%
  \divide\FP@rega#5\relax%
  \advance\FP@rege\FP@rega%
  #1=\FP@regd%
  #2=\FP@rege%
}

%exp (for values between -1 and 1)
\def\FP@@exp#1#2#3#4{%
  % #1 macro which gets the result
  % #2 sign
  % #3 fractional part 1
  % #4 fractional part 2
  \ifnum\ifnum#3=0 0\else1\fi\ifnum#4=0 0\else1\fi=0\relax%
    \edef#1{1}%
  \else%
    \FP@rs=1\relax%
    \FP@ria=0\relax%
    \ifnum#2>0\relax%
      \FP@rib=1\relax%
      \FP@rfa=#3\relax%
      \FP@rfb=#4\relax%
    \else%
      \FP@rib=0\relax%
      \ifnum#4=0\relax%
        \FP@rfa=1000000000\relax\FP@rfb=0\relax%
      \else%
        \FP@rfa=999999999\relax\FP@rfb=1000000000\relax%
      \fi%
      \advance\FP@rfa-#3\relax%
      \advance\FP@rfb-#4\relax%
    \fi%
    \FP@ls=#2\relax%
    \FP@lfa=#3\relax%
    \FP@lfb=#4\relax%
    \FP@count=1\relax%
    \loop%
      \advance\FP@count1\relax%
      \multiply\FP@ls#2\relax%
      \FP@exp@mul\FP@lfa\FP@lfb\FP@lfa\FP@lfb#3#4\relax%
      \FP@exp@divn\FP@lfa\FP@lfb\FP@lfa\FP@lfb\FP@count%
      \ifnum\ifnum\FP@lfa=0 0\else1\fi\ifnum\FP@lfb=0 0\else1\fi>0\relax%
        \ifnum\FP@ls>0\relax%
          \advance\FP@rfa\FP@lfa%
	  \advance\FP@rfb\FP@lfb%
	  \ifnum\FP@rfb<1000000000\relax\else%
	    \advance\FP@rfa1\relax%
	    \advance\FP@rfb-1000000000\relax%
	  \fi%
	  \ifnum\FP@rfa<1000000000\relax\else%
	    \advance\FP@rib1\relax%
	    \advance\FP@rfa-1000000000\relax%
	  \fi%
        \else%
          \advance\FP@rfa-\FP@lfa%
	  \advance\FP@rfb-\FP@lfb%
	  \ifnum\FP@rfb<0\relax%
	    \advance\FP@rfa-1\relax%
	    \advance\FP@rfb1000000000\relax%
	  \fi%
	  \ifnum\FP@rfa<0\relax%
	    \advance\FP@rib-1\relax%
	    \advance\FP@rfa1000000000\relax%
	  \fi%
        \fi%
      \repeat%
    \FP@store#1{r}%
  \fi%
}

\def\FP@exp#1#2.#3.#4\relax{%
  % #1 macro which gets the result
  % #2 integer part of value
  % #3 fractional part of value
  % #4 dummy to swallow everthing after the 2nd '.'
  %
  \FP@beginmessage{EXP}%
  %
  {\def\FP@beginmessage##1{}%
   \def\FP@endmessage##1{}%
   \FP@readvalue{x}{#2}{#3}%
   \ifnum\FP@xia=0\relax%
      \ifnum\FP@xs<0\relax%
        \ifnum\FP@xib>42\relax%
          \edef\FP@tmp{0}%
        \else%
          \edef\FP@tmp{\csname FP@e-\the\FP@xib\endcsname}%
        \fi%
      \else%
        \ifnum\FP@xib>41\relax%
          \FP@errmessage{Overflow}%
        \else%
          \edef\FP@tmp{\csname FP@e\the\FP@xib\endcsname}%
        \fi%
      \fi%
      \FP@@exp\FP@tmpa\FP@xs\FP@xfa\FP@xfb%
      \FPmul\FP@tmp\FP@tmpa\FP@tmp%
      \global\let\FP@tmp=\FP@tmp%
   \else%
     \FP@errmessage{Overflow}%
   \fi%
  }%
  %
  \FP@endmessage{}%
  %
  \let#1\FP@tmp%
}

%compute ln x

\expandafter\edef\csname FP@ln2-0\endcsname{0.000000000000000000}
\expandafter\edef\csname FP@ln2-1\endcsname{0.693147180559945309}
\expandafter\edef\csname FP@ln2-2\endcsname{1.386294361119890618}
\expandafter\edef\csname FP@ln2-3\endcsname{2.079441541679835928}

\expandafter\edef\csname FP@ln10-0\endcsname{0.000000000000000000}
\expandafter\edef\csname FP@ln10-1\endcsname{2.302585092994045684}
\expandafter\edef\csname FP@ln10-2\endcsname{4.605170185988091368}
\expandafter\edef\csname FP@ln10-3\endcsname{6.907755278982137052}
\expandafter\edef\csname FP@ln10-4\endcsname{9.210340371976182736}
\expandafter\edef\csname FP@ln10-5\endcsname{11.512925464970228420}
\expandafter\edef\csname FP@ln10-6\endcsname{13.815510557964274104}
\expandafter\edef\csname FP@ln10-7\endcsname{16.118095650958319788}
\expandafter\edef\csname FP@ln10-8\endcsname{18.420680743952365472}
\expandafter\edef\csname FP@ln10-9\endcsname{20.723265836946411156}
\expandafter\edef\csname FP@ln10-10\endcsname{23.025850929940456840}
\expandafter\edef\csname FP@ln10-11\endcsname{25.328436022934502524}
\expandafter\edef\csname FP@ln10-12\endcsname{27.631021115928548208}
\expandafter\edef\csname FP@ln10-13\endcsname{29.933606208922593892}
\expandafter\edef\csname FP@ln10-14\endcsname{32.236191301916639576}
\expandafter\edef\csname FP@ln10-15\endcsname{34.538776394910685260}
\expandafter\edef\csname FP@ln10-16\endcsname{36.841361487904730944}
\expandafter\edef\csname FP@ln10-17\endcsname{39.143946580898776628}
\expandafter\edef\csname FP@ln10-18\endcsname{41.446531673892822312}

\def\FP@ln@divtwo#1#2#3#4#5#6{%
  % #1 macro which gets integer part
  % #2 macro which gets fractional part 1
  % #3 macro which fractional part 2
  % #4 integer part 1
  % #5 fractional part 1
  % #6 fractional part 2
  #1=#4\relax%
  #2=#5\relax\ifodd#4\advance#21000000000\relax\fi%
  #3=#6\relax\ifodd#5\advance#31000000000\relax\fi%
  \divide#32\relax%
  \divide#22\relax%
  \divide#12\relax%
}

%ln for values between (1 and 2)
\def\FP@@ln#1#2#3#4{%
  % #1 macro which gets the result
  % #2 integer part (1 or 2)
  % #3 fractional part 1
  % #4 fractional part 2
  {\FP@xs=1\FP@xia=0\FP@xib=#2\FP@xfa=#3\FP@xfb=#4%
   {\advance\FP@xib-1\relax\FP@store\FP@tmpa{x}\global\let\FP@tmpa\FP@tmpa}%
   {\advance\FP@xib+1\relax\FP@store\FP@tmpb{x}\global\let\FP@tmpb\FP@tmpb}%
   \FPdiv\FP@tmpa\FP@tmpa\FP@tmpb%
   %
   \def\next##1.##2.##3\relax{\FP@readvalue{x}{##1}{##2}}%
   \expandafter\next\FP@tmpa..\relax%
   %
   \FP@lfa=\FP@xfa%
   \FP@lfb=\FP@xfb%
   %
   \FP@rs=1\relax%
   \FP@ria=0\relax%
   \FP@rib=0\relax%
   \FP@rfa=\FP@xfa%
   \FP@rfb=\FP@xfb%
   %
   \FP@count=1\relax%
   \FP@exp@mul\FP@xfa\FP@xfb\FP@xfa\FP@xfb\FP@xfa\FP@xfb%
   %
   \loop%
     \advance\FP@count2\relax%
     \FP@exp@mul\FP@lfa\FP@lfb\FP@lfa\FP@lfb\FP@xfa\FP@xfb%
     \ifnum\ifnum\FP@lfa=0 0\else1\fi\ifnum\FP@lfb=0 0\else1\fi=0\relax\else%
       \FP@exp@divn\FP@yfa\FP@yfb\FP@lfa\FP@lfb\FP@count%
       \advance\FP@rfb\FP@yfb%
       \advance\FP@rfa\FP@yfa%
       \ifnum\FP@rfb<1000000000\relax\else%
         \advance\FP@rfa1\relax%
	 \advance\FP@rfb-1000000000\relax%
       \fi%
       \ifnum\FP@rfa<1000000000\relax\else%
         \advance\FP@rib1\relax%
	 \advance\FP@rfa-1000000000\relax%
       \fi%
     \repeat%
   %
   \advance\FP@rfa\FP@rfa%
   \ifnum\FP@rfb<500000000\relax\else%
     \advance\FP@rfa1\relax%
     \advance\FP@rfb-500000000\relax%
   \fi%
   \advance\FP@rfb\FP@rfb%
   %
   \FP@store\FP@tmpa{r}%
   %
   \global\let\FP@tmpa\FP@tmpa%
  }%
  %
  \let#1\FP@tmpa%
}

\def\FP@ln#1#2.#3.#4\relax{%
  % #1 macro which gets the result
  % #2 integer part of value
  % #3 fractional part of value
  % #4 dummy to swallow everthing after the 2nd '.'
  %
  \FP@beginmessage{LN}%
  %
  {\def\FP@beginmessage##1{}%
   \def\FP@endmessage##1{}%
   %
   \FP@readvalue{x}{#2}{#3}%
   %
   \ifFP@zero{x}%
     \FP@errmessage{Logarithm of zero}%
   \else\ifnum\FP@xs<0\relax%
     \FP@errmessage{Logarithm of negative value}%
   \else%
     \FP@count=0\relax%
     \loop%
        \ifnum\ifnum\FP@xia>0 1\else0\fi\ifnum\FP@xib>9 1\else0\fi>0\relax%
          \advance\FP@count1\relax%
          \FP@divten{x}%
        \repeat%
     \loop%
       \ifnum\ifnum\FP@xia=0 0\else1\fi\ifnum\FP@xib=0 0\else1\fi=0\relax%
         \advance\FP@count-1\relax%
	 \FP@multen{x}%
       \repeat%
     \ifnum\FP@count<0\relax%
       \FP@count=-\FP@count%
       \edef\FP@tmpc{-\csname FP@ln10-\the\FP@count\endcsname}%
     \else%
       \edef\FP@tmpc{\csname FP@ln10-\the\FP@count\endcsname}%
     \fi%
     \FP@count=0\relax%
     \loop%
       \ifnum\FP@xib<2\relax\else%
         \advance\FP@count1\relax%
         \FP@ln@divtwo\FP@xib\FP@xfa\FP@xfb\FP@xib\FP@xfa\FP@xfb%
       \repeat%
     \FPadd\FP@tmpc\FP@tmpc{\csname FP@ln2-\the\FP@count\endcsname}%
     \FP@@ln\FP@tmpb\FP@xib\FP@xfa\FP@xfb%
     \FPadd\FP@tmp\FP@tmpc\FP@tmpb%
   \fi\fi%
   %
   \global\let\FP@tmp\FP@tmp%
  }%
  %
  \FP@endmessage{}%
  %
  \let#1\FP@tmp%
}

%compute x pow y

\def\FP@pow@zero#1{%
  \FPifzero{#1}%
    \FP@errmessage{0-th pow of 0 not defined}%
  \else%
    \edef\FP@tmp{0}%
  \fi%
}

\def\FP@pow#1#2#3{%
  % #1 macro, which gets the result
  % #2 base
  % #3 exponent
  %
  \FP@beginmessage{POW}%
  %
  {\def\FP@beginmessage##1{}%
   \def\FP@endmessage##1{}%
   %
   \FPifzero{#2}%
     \FP@pow@zero{#3}%
   \else%
     \FPln\FP@tmpd{#2}%
     \FPmul\FP@tmpd\FP@tmpd{#3}%
     \FPexp\FP@tmp\FP@tmpd%
   \fi%
   %
   \global\let\FP@tmp\FP@tmp%
  }%
  %
  \FP@endmessage{}%
  %
  \let#1\FP@tmp%
}

%compute y-th root of x

\def\FP@root#1#2#3{%
  % #1 macro, which gets the result
  % #2 base
  % #3 which root
  %
  \FP@beginmessage{ROOT}%
  %
  {\def\FP@beginmessage##1{}%
   \def\FP@endmessage##1{}%
   %
   \FPifzero{#3}%
     \FP@errmessage{0-th root not defined}%
   \fi%
   %
   \FPifzero{#2}%
     \edef\FP@tmp{0}%
   \else%
     \FPln\FP@tmpd{#2}%
     \FPdiv\FP@tmpd\FP@tmpd{#3}%
     \FPexp\FP@tmp\FP@tmpd%
   \fi%
   %
   \global\let\FP@tmp\FP@tmp%
  }%
  %
  \FP@endmessage{}%
  %
  \let#1\FP@tmp%
}
