Update LuaTeX testfiles for ^@ change
[latex2e.git] / latex2e-20170101 / required / graphics / trig.dtx
blob712db13ba2a1ee5981d0ff085ea2e51c1b2b9a9b
1 % \iffalse
2 %% File: trig.dtx Copyright (C) 1993 1994 1995 1996 1997 1999 David Carlisle
3 %%                Copyright (C) 2000--2015 David Carlisle, LaTeX3 Project
4 %%
5 %% This file is part of the Standard LaTeX `Graphics Bundle'.
6 %% It may be distributed under the terms of the LaTeX Project Public
7 %% License, as described in lppl.txt in the base LaTeX distribution.
8 %% Either version 1.3c or, at your option, any later version.
9 %%
11 %<*dtx>
12           \ProvidesFile{trig.dtx}
13 %</dtx>
14 %<*!plain>
15 %<package&!plain>\NeedsTeXFormat{LaTeX2e}
16 %<package&!plain>\ProvidesPackage{trig}
17 %<driver>        \ProvidesFile{trig.drv}
18 % \fi
19 %                \ProvidesFile{trig.dtx}
20                  [2016/01/03 v1.10 sin cos tan (DPC)]
22 % \iffalse
23 %</!plain>
24 %<*driver>
25 \documentclass{ltxdoc}
26 \usepackage{trig}
27 \begin{document}
28  \DocInput{trig.dtx}
29 \end{document}
30 %</driver>
31 % \fi
33 % \GetFileInfo{trig.dtx}
34 % \title{The \textsf{trig} package\thanks{This file
35 %        has version number \fileversion, last
36 %        revised \filedate.}}
37 % \author{David Carlisle}
38 % \date{\filedate}
39 % \MaintainedByLaTeXTeam{graphics}
40 % \maketitle
43 % \changes{v1.00}{1993/00/00}{Undocumented versions}
44 % \changes{v1.05}{1993/10/07}{Documented, added tan}
45 % \changes{v1.06}{1994/02/01}{Update for LaTeX2e}
46 % \changes{v1.07}{1994/03/15}{Use ltxdoc}
47 % \changes{v1.08}{1994/10/16}{Change \cs{@xc} to \cs{nin@ty}}
49 % \section{Introduction}
51 % These macros implement the trigonometric functions, sin, cos and tan.
52 % In each case two commands are defined. For instance the command
53 % |\CalculateSin{33}| may be issued at some point, and then anywhere
54 % later in the document, the command |\UseSin{33}| will return the
55 % decimal expansion of $\sin(33^\circ)$.
57 % The arguments to these macros do not have to be whole numbers,
58 % although in the case of whole numbers, \LaTeX\ or plain \TeX\ counters
59 % may be used. In \TeX{}Book syntax, arguments must be  of type:
60 % \meta{optional signs}\meta{factor}
62 % Some other examples are:\\
63 % |\CalculateSin{22.5}|, |\UseTan{\value{mycounter}}|,
64 % |\UseCos{\count@}|.
66 % Note that unlike the psfig macros, these save all previously
67 % computed values. This could easily be changed, but I thought that in
68 % many applications one would want many instances of the
69 % same value. (eg rotating all the headings of a table by the
70 % \emph{same} amount).
72 % I don't really like this need to pre-calculate the values, I
73 % originally implemented |\UseSin| so that it automatically calculated
74 % the value if it was not pre-stored. This worked fine in testing, until
75 % I remembered why one needs these values. You want to be able to say
76 % |\dimen2=\UseSin{30}\dimen0|. Which means that |\UseSin| must
77 % \emph{expand} to a \meta{factor}.
79 % \StopEventually{}
81 % \section{The Macros}
83 %    \begin{macrocode}
84 %<*package>
85 %    \end{macrocode}
87 % \begin{macro}{\nin@ty}\begin{macro}{\@clxx}
88 % \begin{macro}{\@lxxi}\begin{macro}{\@mmmmlxviii}
89 % Some useful constants for converting between degrees and radians.
90 % $$\frac{\pi}{180}\simeq\frac{355}{113\times180}=\frac{71}{4068}$$
91 %    \begin{macrocode}
92 \chardef\nin@ty=90
93 \chardef\@clxx=180
94 \chardef\@lxxi=71
95 \mathchardef\@mmmmlxviii=4068
96 %    \end{macrocode}
97 % \end{macro}\end{macro}\end{macro}\end{macro}
99 % The approximation to $\sin$. I experimented with various
100 % approximations based on Tchebicheff polynomials, and also some
101 % approximations from a SIAM handbook `Computer Approximations' However
102 % the standard Taylor series seems sufficiently accurate, and used by
103 % far the fewest \TeX\ tokens, as the coefficients are all rational.
104 % \begin{eqnarray*}
105 %  \sin(x)& \simeq& x - (1/3!)x^3 + (1/5!)x^5 - (1/7!)x^7 + (1/9!)x^9\\
106 %  &\simeq&\frac{((((7!/9!x^2-7!/7!)x^2+7!/5!)x^2 +7!/3!)x^2+7!/1!)x}
107 % {7!}\\
108 %  &=&\frac{((((1/72x^2-1)x^2+42)x^2 +840)x^2+5040)x}
109 % {5040}
110 % \end{eqnarray*}
111 % The nested form used above reduces the number of operations required.
112 % In order to further reduce the number of operations, and more
113 % importantly reduce the number of tokens used, we can precompute the
114 % coefficients. Note that we can not use $9!$ as the denominator as
115 % this would cause overflow of \TeX's arithmetic.
116 % \begin{macro}{\@coeffz}\begin{macro}{\@coeffa}\begin{macro}{\@coeffb}
117 % \begin{macro}{\@coeffc}\begin{macro}{\@coeffd}
118 % Save the coefficients as |\|(|math|)|char|s.
119 %    \begin{macrocode}
120 \chardef\@coeffz=72
121 %\chardef\@coefa=1
122 \chardef\@coefb=42
123 \mathchardef\@coefc=840
124 \mathchardef\@coefd=5040
125 %    \end{macrocode}
126 % \end{macro}\end{macro}\end{macro}\end{macro}\end{macro}
128 % \begin{macro}{\TG@rem@pt}
129 % The standard trick of getting a real number out of a \meta{dimen}.
130 % This gives a maximum accuracy of approx.\ 5 decimal places, which
131 % should be sufficient. It puts a space after the number, perhaps it
132 % shouldn't.
133 %    \begin{macrocode}
134 {\catcode`t=12\catcode`p=12\gdef\noPT#1pt{#1}}
135 \def\TG@rem@pt#1{\expandafter\noPT\the#1\space}
136 %    \end{macrocode}
137 % \end{macro}
139 % \begin{macro}{\TG@term}
140 % Compute one term of the above nested series. Multiply the previous sum
141 % by $x^2$ (stored in |\@tempb|, then add the next coefficient, |#1|.
142 %    \begin{macrocode}
143 \def\TG@term#1{%
144  \dimen@\@tempb\dimen@
145  \advance\dimen@ #1\p@}
146 %    \end{macrocode}
147 % \end{macro}
149 % \begin{macro}{\TG@series}
150 % Compute the above series. the value in degrees will be in |\dimen@|
151 % before this is called.
152 %    \begin{macrocode}
153 \def\TG@series{%
154  \dimen@\@lxxi\dimen@
155  \divide \dimen@ \@mmmmlxviii
156 %    \end{macrocode}
157 % |\dimen@| now contains the angle in radians, as a \meta{dimen}. We
158 % need to remove the units, so store the same value as a \meta{factor}
159 % in |\@tempa|.
160 %    \begin{macrocode}
161  \edef\@tempa{\TG@rem@pt\dimen@}%
162 %    \end{macrocode}
163 % Now put $x^2$ in |\dimen@| and |\@tempb|.
164 %    \begin{macrocode}
165  \dimen@\@tempa\dimen@
166  \edef\@tempb{\TG@rem@pt\dimen@}%
167 %    \end{macrocode}
168 % The first coefficient is  $1/72$.
169 %    \begin{macrocode}
170  \divide\dimen@\@coeffz
171  \advance\dimen@\m@ne\p@
172  \TG@term\@coefb
173  \TG@term{-\@coefc}%
174  \TG@term\@coefd
175 %    \end{macrocode}
176 % Now the cubic in $x^2$ is completed, so we need to multiply by $x$ and
177 % divide by $7!$.
178 %    \begin{macrocode}
179  \dimen@\@tempa\dimen@
180  \divide\dimen@ \@coefd}
181 %    \end{macrocode}
182 % \end{macro}
184 % \begin{macro}{\CalculateSin}
185 % If this angle has already been computed, do nothing, else store the
186 % angle, and call |\TG@@sin|.
187 %    \begin{macrocode}
188 \def\CalculateSin#1{{%
189   \expandafter\ifx\csname sin(\number#1)\endcsname\relax
190     \dimen@=#1\p@\TG@@sin
191     \expandafter\xdef\csname sin(\number#1)\endcsname
192                                     {\TG@rem@pt\dimen@}%
193   \fi}}
194 %    \end{macrocode}
195 % \end{macro}
197 % \begin{macro}{\CalculateCos}
198 % As above, but use the relation $\cos(x) = \sin(90-x)$.
199 %    \begin{macrocode}
200 \def\CalculateCos#1{{%
201   \expandafter\ifx\csname cos(\number#1)\endcsname\relax
202     \dimen@=\nin@ty\p@
203     \advance\dimen@-#1\p@
204     \TG@@sin
205     \expandafter\xdef\csname cos(\number#1)\endcsname
206                                      {\TG@rem@pt\dimen@}%
207   \fi}}
208 %    \end{macrocode}
209 % \end{macro}
211 % \begin{macro}{\TG@reduce}
212 % Repeatedly use one of the the relations
213 % $\sin(x)=\sin(180-x)=\sin(-180-x)$ to get $x$ in the range $-90 \leq
214 % x\leq 90$. Then call |\TG@series|.
215 %    \begin{macrocode}
216 \def\TG@reduce#1#2{%
217 \dimen@#1#2\nin@ty\p@
218   \advance\dimen@#2-\@clxx\p@
219   \dimen@-\dimen@
220   \TG@@sin}
221 %    \end{macrocode}
222 % \end{macro}
224 % \begin{macro}{\TG@@sin}
225 % Slightly cryptic, but it seems to  work\ldots
226 %    \begin{macrocode}
227 \def\TG@@sin{%
228   \ifdim\TG@reduce>+%
229   \else\ifdim\TG@reduce<-%
230   \else\TG@series\fi\fi}%
231 %    \end{macrocode}
232 % \end{macro}
234 % \begin{macro}{\UseSin}
235 % \begin{macro}{\UseCos}
236 % Use a pre-computed value.
237 %    \begin{macrocode}
238 \def\UseSin#1{\csname sin(\number#1)\endcsname}
239 \def\UseCos#1{\csname cos(\number#1)\endcsname}
240 %    \end{macrocode}
241 % \end{macro}
242 % \end{macro}
244 % A few shortcuts to save space.
245 % \changes{v1.10}{2016/01/03}{make these expand to digits so safe to use in ctm}
246 %    \begin{macrocode}
247 \def\z@num{0 }
248 \def\@tempa{1 }
249 \def\@tempb{-1 }
250 %    \end{macrocode}
252 %    \begin{macrocode}
253 \expandafter\let\csname sin(0)\endcsname\z@num
254 \expandafter\let\csname cos(0)\endcsname\@tempa
255 \expandafter\let\csname sin(90)\endcsname\@tempa
256 \expandafter\let\csname cos(90)\endcsname\z@num
257 \expandafter\let\csname sin(-90)\endcsname\@tempb
258 \expandafter\let\csname cos(-90)\endcsname\z@num
259 \expandafter\let\csname sin(180)\endcsname\z@num
260 \expandafter\let\csname cos(180)\endcsname\@tempb
261 %    \end{macrocode}
263 % A few more added in 1.10 (previously in pdftex.def)
264 %    \begin{macrocode}
265 \expandafter\let\csname sin(270)\endcsname\@tempb
266 \expandafter\let\csname cos(270)\endcsname\z@num
267 \expandafter\let\csname sin(360)\endcsname\z@num
268 \expandafter\let\csname cos(360)\endcsname\@tempa
269 \expandafter\let\csname sin(-180)\endcsname\z@num
270 \expandafter\let\csname cos(-180)\endcsname\@tempb
271 \expandafter\let\csname sin(-270)\endcsname\@tempa
272 \expandafter\let\csname cos(-270)\endcsname\z@num
273 \expandafter\let\csname sin(-360)\endcsname\z@num
274 \expandafter\let\csname cos(-360)\endcsname\@tempa
275 %    \end{macrocode}
277 % \begin{macro}{\CalculateTan}
278 % Originally I coded the Taylor series for tan, but it seems to be
279 % more accurate to just take the ratio of the sine and cosine.
280 % This is accurate to 4 decimal places for angles up to
281 % $50^\circ$, after that the accuracy tails off, giving
282 % 57.47894 instead of 57.2900 for $89^\circ$.
283 %    \begin{macrocode}
284 \def\CalculateTan#1{{%
285   \expandafter\ifx\csname tan(\number#1)\endcsname\relax
286     \CalculateSin{#1}%
287     \CalculateCos{#1}%
288     \@tempdima\UseCos{#1}\p@
289     \divide\@tempdima\@iv
290     \@tempdimb\UseSin{#1}\p@
291     \@tempdimb\two@fourteen\@tempdimb
292     \divide\@tempdimb\@tempdima
293     \expandafter\xdef\csname tan(\number#1)\endcsname
294                                         {\TG@rem@pt\@tempdimb}%
295   \fi}}
296 %    \end{macrocode}
297 % \end{macro}
299 % \begin{macro}{\UseTan}
300 % Just like |\UseSin|.
301 %    \begin{macrocode}
302 \def\UseTan#1{\csname tan(\number#1)\endcsname}
303 %    \end{macrocode}
304 % \end{macro}
306 % \begin{macro}{\two@fourteen}
307 % \begin{macro}{\@iv}
308 % two constants needed to keep the division within \TeX's range.
309 %    \begin{macrocode}
310 \mathchardef\two@fourteen=16384
311 \chardef\@iv=4
312 %    \end{macrocode}
313 % \end{macro}
314 % \end{macro}
316 % Predefine $\tan(\pm90)$ to be an error.
317 %    \begin{macrocode}
318 \expandafter\def\csname tan(90)\endcsname{\errmessage{Infinite tan !}}
319 \expandafter\let\csname tan(-90)\expandafter\endcsname
320                                        \csname tan(90)\endcsname
321 %    \end{macrocode}
323 %    \begin{macrocode}
324 %</package>
325 %    \end{macrocode}
327 % \Finale
329 \endinput