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