summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/fp/fp-pas.sty
blob: 8cef34d97a4be4e0052e70c60d99750fbe081962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{fp-pas}[1994/08/29]

%version information
\def\FP@pasversion{1.0}
\message{%
  `Fixed Point Pascal Triangle',%
  \space\space\space%
  Version \FP@pasversion\space%
  \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)%%%

\def\FPpascal#1#2{\FP@pascal{#1}{#2}}%pascal triangle for lines 0,...,63

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

%allocation of registers
\countdef\FP@pas=50
\countdef\FP@pasc=51

%addition of two natural numbers
\def\FP@pasadd#1#2#3{%
  % #1 macro, which gets the result
  % #2 1st value
  % #3 2nd value
  {\FP@removeleadingzeros#2\relax%
   \expandafter\FP@setintcounter\FP@tmp000000000000000000\relax%
   \FP@xia=\FP@rega%
   \FP@xib=\FP@regb%
   %
   \FP@removeleadingzeros#3\relax%
   \expandafter\FP@setintcounter\FP@tmp000000000000000000\relax%
   \FP@yia=\FP@rega%
   \FP@yib=\FP@regb%
   %
   \advance\FP@xia\FP@yia%
   \advance\FP@xib\FP@yib%
   \ifnum\FP@xib<1000000000\relax\else%
     \advance\FP@xia1\relax%
     \advance\FP@xib-1000000000\relax%
   \fi%
   \ifnum\FP@xia<1000000000\relax\else%
     \FP@errmessage{Overflow}%
   \fi%
   %
   \ifnum\FP@xia=0\relax%
     \edef\FP@tmpa{\the\FP@xib}%
   \else%
     \advance\FP@xib1000000000%
     \edef\FP@tmpa{\the\FP@xia\expandafter\FP@ignorenext\the\FP@xib}%
   \fi%
   %
   \global\let\FP@tmpa\FP@tmpa%
  }%
  %
  \let#1\FP@tmpa%
}

%the first two entries of the pascal-triangle
\expandafter\edef\csname FP@pas0\endcsname{[1]}
\expandafter\edef\csname FP@pas1\endcsname{[1,1]}

%which is the highest line of the pascal-triangle we have already computed?
\expandafter\edef\csname FP@pascount\endcsname{1}

%compute next line of pascal-triangle
\def\FP@pasdouble[#1]{%
  % #1 previous line of pascal-triangle
  \edef\FP@old{}%
  \edef\FP@tmpb{}%
  \@for\FP@new:=#1\do{%
    \expandafter\if!\FP@old!\relax%
      \edef\FP@old{\FP@new}%
    \else%
      \FP@pasadd{\FP@tmpa}{\FP@old}{\FP@new}%
      \edef\FP@tmpb{\FP@tmpb,\FP@tmpa}%
      \edef\FP@old{\FP@new}%
    \fi%
  }%
  \global\edef\FP@tmpb{[1\FP@tmpb,1]}%
}

%compute n-th line of pascal-triangle
\def\FP@pascomp#1{%
  % #1 which line to compute
  \expandafter\FP@pasc\FP@pascount\relax%
  \ifnum\FP@pasc<#1\relax%
    \loop%
      \ifnum\FP@pasc<#1\relax%
        \expandafter\expandafter\expandafter\FP@pasdouble\csname FP@pas\the\FP@pasc\endcsname%
        \advance\FP@pasc1\relax%
	\edef\next{\noexpand\global\noexpand\let\csname FP@pas\the\FP@pasc\endcsname\noexpand\FP@tmpb}%
	\next%
      \repeat%
  \fi%
  \global\edef\FP@pascount{\the\FP@pasc}%
}

\def\FP@pascal#1#2{%
  % #1 macro, which gets the result
  % #2 which line of the pascal-triangle to compute
  {\FP@beginmessage{PASCAL}%
   %
   \FP@pas#2\relax%
   \FP@pascomp\FP@pas%
   \global\edef\FP@tmp{\csname FP@pas\the\FP@pas\endcsname}%
   %
   \FP@endmessage{}%
  }%
  \let#1\FP@tmp%
}