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}
44 % \changes{v1.00}{1993/00/00}{Undocumented versions}
45 % \changes{v1.05}{1993/10/07}{Documented, added tan}
46 % \changes{v1.06}{1994/02/01}{Update for LaTeX2e}
47 % \changes{v1.07}{1994/03/15}{Use ltxdoc}
48 % \changes{v1.08}{1994/10/16}{Change \cs{@xc} to \cs{nin@ty}}
50 % \section{Introduction}
52 % These macros implement the trigonometric functions, sin, cos and tan.
53 % In each case two commands are defined. For instance the command
54 % |\CalculateSin{33}| may be issued at some point, and then anywhere
55 % later in the document, the command |\UseSin{33}| will return the
56 % decimal expansion of $\sin(33^\circ)$.
58 % The arguments to these macros do not have to be whole numbers,
59 % although in the case of whole numbers, \LaTeX\ or plain \TeX\ counters
60 % may be used. In \TeX{}Book syntax, arguments must be of type:
61 % \meta{optional signs}\meta{factor}
63 % Some other examples are:\\
64 % |\CalculateSin{22.5}|, |\UseTan{\value{mycounter}}|,
67 % Note that unlike the psfig macros, these save all previously
68 % computed values. This could easily be changed, but I thought that in
69 % many applications one would want many instances of the
70 % same value. (eg rotating all the headings of a table by the
71 % \emph{same} amount).
73 % I don't really like this need to pre-calculate the values, I
74 % originally implemented |\UseSin| so that it automatically calculated
75 % the value if it was not pre-stored. This worked fine in testing, until
76 % I remembered why one needs these values. You want to be able to say
77 % |\dimen2=\UseSin{30}\dimen0|. Which means that |\UseSin| must
78 % \emph{expand} to a \meta{factor}.
82 % \section{The Macros}
88 % \begin{macro}{\nin@ty}\begin{macro}{\@clxx}
89 % \begin{macro}{\@lxxi}\begin{macro}{\@mmmmlxviii}
90 % Some useful constants for converting between degrees and radians.
91 % $$\frac{\pi}{180}\simeq\frac{355}{113\times180}=\frac{71}{4068}$$
96 \mathchardef\@mmmmlxviii=4068
98 % \end{macro}\end{macro}\end{macro}\end{macro}
100 % The approximation to $\sin$. I experimented with various
101 % approximations based on Tchebicheff polynomials, and also some
102 % approximations from a SIAM handbook `Computer Approximations' However
103 % the standard Taylor series seems sufficiently accurate, and used by
104 % far the fewest \TeX\ tokens, as the coefficients are all rational.
106 % \sin(x)& \simeq& x - (1/3!)x^3 + (1/5!)x^5 - (1/7!)x^7 + (1/9!)x^9\\
107 % &\simeq&\frac{((((7!/9!x^2-7!/7!)x^2+7!/5!)x^2 +7!/3!)x^2+7!/1!)x}
109 % &=&\frac{((((1/72x^2-1)x^2+42)x^2 +840)x^2+5040)x}
112 % The nested form used above reduces the number of operations required.
113 % In order to further reduce the number of operations, and more
114 % importantly reduce the number of tokens used, we can precompute the
115 % coefficients. Note that we can not use $9!$ as the denominator as
116 % this would cause overflow of \TeX's arithmetic.
117 % \begin{macro}{\@coeffz}\begin{macro}{\@coeffa}\begin{macro}{\@coeffb}
118 % \begin{macro}{\@coeffc}\begin{macro}{\@coeffd}
119 % Save the coefficients as |\|(|math|)|char|s.
124 \mathchardef\@coefc=840
125 \mathchardef\@coefd=5040
127 % \end{macro}\end{macro}\end{macro}\end{macro}\end{macro}
129 % \begin{macro}{\TG@rem@pt}
130 % The standard trick of getting a real number out of a \meta{dimen}.
131 % This gives a maximum accuracy of approx.\ 5 decimal places, which
132 % should be sufficient. It puts a space after the number, perhaps it
135 {\catcode`t=12\catcode`p=12\gdef\noPT#1pt{#1}}
136 \def\TG@rem@pt#1{\expandafter\noPT\the#1\space}
140 % \begin{macro}{\TG@term}
141 % Compute one term of the above nested series. Multiply the previous sum
142 % by $x^2$ (stored in |\@tempb|, then add the next coefficient, |#1|.
145 \dimen@\@tempb\dimen@
146 \advance\dimen@ #1\p@}
150 % \begin{macro}{\TG@series}
151 % Compute the above series. the value in degrees will be in |\dimen@|
152 % before this is called.
156 \divide \dimen@ \@mmmmlxviii
158 % |\dimen@| now contains the angle in radians, as a \meta{dimen}. We
159 % need to remove the units, so store the same value as a \meta{factor}
162 \edef\@tempa{\TG@rem@pt\dimen@}%
164 % Now put $x^2$ in |\dimen@| and |\@tempb|.
166 \dimen@\@tempa\dimen@
167 \edef\@tempb{\TG@rem@pt\dimen@}%
169 % The first coefficient is $1/72$.
171 \divide\dimen@\@coeffz
172 \advance\dimen@\m@ne\p@
177 % Now the cubic in $x^2$ is completed, so we need to multiply by $x$ and
180 \dimen@\@tempa\dimen@
181 \divide\dimen@ \@coefd}
185 % \begin{macro}{\CalculateSin}
186 % If this angle has already been computed, do nothing, else store the
187 % angle, and call |\TG@@sin|.
189 \def\CalculateSin#1{{%
190 \expandafter\ifx\csname sin(\number#1)\endcsname\relax
191 \dimen@=#1\p@\TG@@sin
192 \expandafter\xdef\csname sin(\number#1)\endcsname
198 % \begin{macro}{\CalculateCos}
199 % As above, but use the relation $\cos(x) = \sin(90-x)$.
201 \def\CalculateCos#1{{%
202 \expandafter\ifx\csname cos(\number#1)\endcsname\relax
204 \advance\dimen@-#1\p@
206 \expandafter\xdef\csname cos(\number#1)\endcsname
212 % \begin{macro}{\TG@reduce}
213 % Repeatedly use one of the the relations
214 % $\sin(x)=\sin(180-x)=\sin(-180-x)$ to get $x$ in the range $-90 \leq
215 % x\leq 90$. Then call |\TG@series|.
218 \dimen@#1#2\nin@ty\p@
219 \advance\dimen@#2-\@clxx\p@
225 % \begin{macro}{\TG@@sin}
226 % Slightly cryptic, but it seems to work\ldots
230 \else\ifdim\TG@reduce<-%
231 \else\TG@series\fi\fi}%
235 % \begin{macro}{\UseSin}
236 % \begin{macro}{\UseCos}
237 % Use a pre-computed value.
239 \def\UseSin#1{\csname sin(\number#1)\endcsname}
240 \def\UseCos#1{\csname cos(\number#1)\endcsname}
245 % A few shortcuts to save space.
246 % \changes{v1.10}{2016/01/03}{make these expand to digits so safe to use in ctm}
254 \expandafter\let\csname sin(0)\endcsname\z@num
255 \expandafter\let\csname cos(0)\endcsname\@tempa
256 \expandafter\let\csname sin(90)\endcsname\@tempa
257 \expandafter\let\csname cos(90)\endcsname\z@num
258 \expandafter\let\csname sin(-90)\endcsname\@tempb
259 \expandafter\let\csname cos(-90)\endcsname\z@num
260 \expandafter\let\csname sin(180)\endcsname\z@num
261 \expandafter\let\csname cos(180)\endcsname\@tempb
264 % A few more added in 1.10 (previously in pdftex.def)
266 \expandafter\let\csname sin(270)\endcsname\@tempb
267 \expandafter\let\csname cos(270)\endcsname\z@num
268 \expandafter\let\csname sin(360)\endcsname\z@num
269 \expandafter\let\csname cos(360)\endcsname\@tempa
270 \expandafter\let\csname sin(-180)\endcsname\z@num
271 \expandafter\let\csname cos(-180)\endcsname\@tempb
272 \expandafter\let\csname sin(-270)\endcsname\@tempa
273 \expandafter\let\csname cos(-270)\endcsname\z@num
274 \expandafter\let\csname sin(-360)\endcsname\z@num
275 \expandafter\let\csname cos(-360)\endcsname\@tempa
278 % \begin{macro}{\CalculateTan}
279 % Originally I coded the Taylor series for tan, but it seems to be
280 % more accurate to just take the ratio of the sine and cosine.
281 % This is accurate to 4 decimal places for angles up to
282 % $50^\circ$, after that the accuracy tails off, giving
283 % 57.47894 instead of 57.2900 for $89^\circ$.
285 \def\CalculateTan#1{{%
286 \expandafter\ifx\csname tan(\number#1)\endcsname\relax
289 \@tempdima\UseCos{#1}\p@
290 \divide\@tempdima\@iv
291 \@tempdimb\UseSin{#1}\p@
292 \@tempdimb\two@fourteen\@tempdimb
293 \divide\@tempdimb\@tempdima
294 \expandafter\xdef\csname tan(\number#1)\endcsname
295 {\TG@rem@pt\@tempdimb}%
300 % \begin{macro}{\UseTan}
301 % Just like |\UseSin|.
303 \def\UseTan#1{\csname tan(\number#1)\endcsname}
307 % \begin{macro}{\two@fourteen}
308 % \begin{macro}{\@iv}
309 % two constants needed to keep the division within \TeX's range.
311 \mathchardef\two@fourteen=16384
317 % Predefine $\tan(\pm90)$ to be an error.
319 \expandafter\def\csname tan(90)\endcsname{\errmessage{Infinite tan !}}
320 \expandafter\let\csname tan(-90)\expandafter\endcsname
321 \csname tan(90)\endcsname