summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/animate/animfp.sty
blob: 6346ff2c064c53ad9c914a11c070fba20e9308dc (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
% 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%
}