summaryrefslogtreecommitdiff
path: root/tex/context/base/mkii/thrd-trg.mkii
blob: c0106f5a3f137554146c06c4f5bfc91c31167d53 (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
%D In order to support rotation over arbitrary angles, we need a sine
%D and  cosine calculator. For this purpose we borrow a few macros by
%D David  Carlisle (his trig package). Because local variables are
%D used, I patched the macros a bit. Also, I used a few different names
%D for variabels and macros and use existing auxiliary macros.

\unprotect

% compare: \number 0.5 \number -0.5 \number 1.5 \number -1.5
%
% so we need:

\def\realnumber#1{\withoutpt\the\dimexpr#1\points\relax} % brrr

\chardef     \@iv         =     4
\chardef     \@xc         =    90 % was \nin@ty
\chardef     \@clxx       =   180
\chardef     \@lxxi       =    71
\mathchardef \@mmmmlxviii =  4068
\mathchardef \@xvi@k      = 16384

\chardef     \tr@coeffz   =    72
\chardef     \tr@coefb    =    42
\mathchardef \tr@coefc    =   840
\mathchardef \tr@coefd    =  5040

\def\tg@series
  {\!!dimena\@lxxi\!!dimena
   \divide\!!dimena\@mmmmlxviii
   \edef\!!stringa{\withoutpt\the\!!dimena}%
   \!!dimena\!!stringa\!!dimena
   \edef\!!stringb{\withoutpt\the\!!dimena}%
   \divide\!!dimena\tr@coeffz
   \advance\!!dimena\minusone\onepoint
   \!!dimena\!!stringb\!!dimena
   \advance\!!dimena \tr@coefb\onepoint
   \!!dimena\!!stringb\!!dimena
   \advance\!!dimena -\tr@coefc\onepoint
   \!!dimena\!!stringb\!!dimena
   \advance\!!dimena \tr@coefd\onepoint
   \!!dimena\!!stringa\!!dimena
   \divide\!!dimena \tr@coefd}

\def\tg@reduce#1#2%
  {\!!dimena#1#2\@xc\onepoint
   \advance\!!dimena#2-\@clxx\onepoint
   \!!dimena-\!!dimena
   \tg@@sin}

\def\tg@@sin
  {\ifdim\tg@reduce>+\else\ifdim\tg@reduce<-\else
     \tg@series
   \fi\fi}

%D Calculating a sine is a two step process: first a value is
%D calculated, and afterwards it can be used. This saves redundant
%D calculations.

\def\calculatesin#1%
  {{\expandafter\ifx\csname sin \realnumber{#1}\endcsname\relax
      \!!dimena#1\onepoint
      \tg@@sin
      \expandafter\xdef\csname sin \realnumber{#1}\endcsname{\withoutpt\the\!!dimena}%
    \fi}}

\def\calculatecos#1%
  {{\expandafter\ifx\csname cos \realnumber{#1}\endcsname\relax
      \!!dimena\@xc\onepoint
      \advance\!!dimena-#1\onepoint
      \tg@@sin
      \expandafter\xdef\csname cos \realnumber{#1}\endcsname{\withoutpt\the\!!dimena}%
    \fi}}

\def\calculatetan#1%
  {{\expandafter\ifx\csname tan \realnumber{#1}\endcsname\relax
      \calculatesin{#1}%
      \calculatecos{#1}%
      \!!dimena\calculatedcos{#1}\onepoint
      \divide\!!dimena\@iv
      \!!dimenb\calculatedsin{#1}\onepoint
      \!!dimenb\@xvi@k\!!dimenb
      \divide\!!dimenb\!!dimena
      \expandafter\xdef\csname tan \realnumber{#1}\endcsname{\withoutpt\the\!!dimenb}%
   \fi}}

%D The results are accessed with:

\def\calculatedsin#1{\csname sin \realnumber{#1}\endcsname}
\def\calculatedcos#1{\csname cos \realnumber{#1}\endcsname}
\def\calculatedtan#1{\csname tan \realnumber{#1}\endcsname}

%D A more save implementation would be:

\def\calculatedsin#1{\executeifdefined{sin \realnumber{#1}}\!!zerocount}
\def\calculatedcos#1{\executeifdefined{cos \realnumber{#1}}\!!plusone  }
\def\calculatedtan#1{\executeifdefined{tan \realnumber{#1}}\!!zerocount}

%D The following permits cleaner overloading (\MKIV\ will only have
%D these):

\def\setcalculatedsin#1#2{\calculatesin{#2}\edef#1{\calculatedsin{#2}}}
\def\setcalculatedcos#1#2{\calculatecos{#2}\edef#1{\calculatedcos{#2}}}
\def\setcalculatedtan#1#2{\calculatetan{#2}\edef#1{\calculatedtan{#2}}}

%D A few values are predefined, although, on todays systems there
%D is no real reason for that. I've added the 270 ones and changed
%D the -90 tan. Also, I prefer text (\type {\!!..} instead of
%D counters \type {\..}.

\expandafter\let\csname sin \realnumber{  0}\endcsname\!!zerocount
\expandafter\let\csname cos \realnumber{  0}\endcsname\!!plusone
\expandafter\let\csname sin \realnumber{ 90}\endcsname\!!plusone
\expandafter\let\csname cos \realnumber{ 90}\endcsname\!!zerocount
\expandafter\let\csname sin \realnumber{180}\endcsname\!!zerocount
\expandafter\let\csname cos \realnumber{180}\endcsname\!!minusone
\expandafter\let\csname sin \realnumber{270}\endcsname\!!minusone
\expandafter\let\csname cos \realnumber{270}\endcsname\!!zerocount

\expandafter\let\csname sin \realnumber{-90}\endcsname\!!minusone
\expandafter\let\csname cos \realnumber{-90}\endcsname\!!zerocount

\expandafter\def\csname tan \realnumber{ 90}\endcsname{\writestatus\m!systems{infinite tan +90}}
\expandafter\def\csname tan \realnumber{-90}\endcsname{\writestatus\m!systems{infinite tan -90}}

%D Usage: \type {\calculatesin{10}} and \type {\calculatedsin{10}}

\protect \endinput