2 %% File: trig.dtx Copyright (C) 1993 1994 1995 1996 1997 1999 David Carlisle
3 %% Copyright (C) 2000--2015 David Carlisle, LaTeX3 Project
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.
12 \ProvidesFile{trig.dtx}
15 %<package&!plain>\NeedsTeXFormat{LaTeX2e}
16 %<package&!plain>\ProvidesPackage{trig}
17 %<driver> \ProvidesFile{trig.drv}
19 % \ProvidesFile{trig.dtx}
20 [2016/01/03 v1.10 sin cos tan (DPC)]
25 \documentclass{ltxdoc}
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}
39 % \MaintainedByLaTeXTeam{graphics}
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}}|,
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}.
81 % \section{The Macros}
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}$$
95 \mathchardef\@mmmmlxviii=4068
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.
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}
108 % &=&\frac{((((1/72x^2-1)x^2+42)x^2 +840)x^2+5040)x}
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.
123 \mathchardef\@coefc=840
124 \mathchardef\@coefd=5040
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
134 {\catcode`t=12\catcode`p=12\gdef\noPT#1pt{#1}}
135 \def\TG@rem@pt#1{\expandafter\noPT\the#1\space}
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|.
144 \dimen@\@tempb\dimen@
145 \advance\dimen@ #1\p@}
149 % \begin{macro}{\TG@series}
150 % Compute the above series. the value in degrees will be in |\dimen@|
151 % before this is called.
155 \divide \dimen@ \@mmmmlxviii
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}
161 \edef\@tempa{\TG@rem@pt\dimen@}%
163 % Now put $x^2$ in |\dimen@| and |\@tempb|.
165 \dimen@\@tempa\dimen@
166 \edef\@tempb{\TG@rem@pt\dimen@}%
168 % The first coefficient is $1/72$.
170 \divide\dimen@\@coeffz
171 \advance\dimen@\m@ne\p@
176 % Now the cubic in $x^2$ is completed, so we need to multiply by $x$ and
179 \dimen@\@tempa\dimen@
180 \divide\dimen@ \@coefd}
184 % \begin{macro}{\CalculateSin}
185 % If this angle has already been computed, do nothing, else store the
186 % angle, and call |\TG@@sin|.
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
197 % \begin{macro}{\CalculateCos}
198 % As above, but use the relation $\cos(x) = \sin(90-x)$.
200 \def\CalculateCos#1{{%
201 \expandafter\ifx\csname cos(\number#1)\endcsname\relax
203 \advance\dimen@-#1\p@
205 \expandafter\xdef\csname cos(\number#1)\endcsname
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|.
217 \dimen@#1#2\nin@ty\p@
218 \advance\dimen@#2-\@clxx\p@
224 % \begin{macro}{\TG@@sin}
225 % Slightly cryptic, but it seems to work\ldots
229 \else\ifdim\TG@reduce<-%
230 \else\TG@series\fi\fi}%
234 % \begin{macro}{\UseSin}
235 % \begin{macro}{\UseCos}
236 % Use a pre-computed value.
238 \def\UseSin#1{\csname sin(\number#1)\endcsname}
239 \def\UseCos#1{\csname cos(\number#1)\endcsname}
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}
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
263 % A few more added in 1.10 (previously in pdftex.def)
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
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$.
284 \def\CalculateTan#1{{%
285 \expandafter\ifx\csname tan(\number#1)\endcsname\relax
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}%
299 % \begin{macro}{\UseTan}
300 % Just like |\UseSin|.
302 \def\UseTan#1{\csname tan(\number#1)\endcsname}
306 % \begin{macro}{\two@fourteen}
307 % \begin{macro}{\@iv}
308 % two constants needed to keep the division within \TeX's range.
310 \mathchardef\two@fourteen=16384
316 % Predefine $\tan(\pm90)$ to be an error.
318 \expandafter\def\csname tan(90)\endcsname{\errmessage{Infinite tan !}}
319 \expandafter\let\csname tan(-90)\expandafter\endcsname
320 \csname tan(90)\endcsname