summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/l3kernel/l3fp-assign.dtx
blob: ece21287941d870bc584c527ca475d14cba06b15 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
% \iffalse meta-comment
%
%% File: l3fp-assign.dtx
%
% Copyright (C) 2011-2020 The LaTeX3 Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
% This file is part of the "l3kernel bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/latex3/latex3
%
% for those people who are interested.
%
%<*driver>
\documentclass[full,kernel]{l3doc}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{The \textsf{l3fp-assign} package\\
%   Floating point expressions}
% \author{^^A
%  The \LaTeX3 Project\thanks
%    {^^A
%      E-mail:
%        \href{mailto:latex-team@latex-project.org}
%          {latex-team@latex-project.org}^^A
%    }^^A
% }
% \date{Released 2020-04-06}
% \maketitle
%
% \begin{documentation}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3fp-assign} implementation}
%
%    \begin{macrocode}
%<*initex|package>
%    \end{macrocode}
%
%    \begin{macrocode}
%<@@=fp>
%    \end{macrocode}
%
% \subsection{Assigning values}
%
% \begin{macro}{\fp_new:N}
%   Floating point variables are initialized to be $+0$.
%    \begin{macrocode}
\cs_new_protected:Npn \fp_new:N #1
  { \cs_new_eq:NN #1 \c_zero_fp }
\cs_generate_variant:Nn \fp_new:N {c}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \fp_set:Nn, \fp_set:cn,
%     \fp_gset:Nn, \fp_gset:cn,
%     \fp_const:Nn, \fp_const:cn
%   }
%   Simply use \cs{@@_parse:n} within various \texttt{f}-expanding
%   assignments.
%    \begin{macrocode}
\cs_new_protected:Npn \fp_set:Nn   #1#2
  { \tl_set:Nx #1 { \exp_not:f { \@@_parse:n {#2} } } }
\cs_new_protected:Npn \fp_gset:Nn  #1#2
  { \tl_gset:Nx #1 { \exp_not:f { \@@_parse:n {#2} } } }
\cs_new_protected:Npn \fp_const:Nn #1#2
  { \tl_const:Nx #1 { \exp_not:f { \@@_parse:n {#2} } } }
\cs_generate_variant:Nn \fp_set:Nn {c}
\cs_generate_variant:Nn \fp_gset:Nn {c}
\cs_generate_variant:Nn \fp_const:Nn {c}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {
%     \fp_set_eq:NN , \fp_set_eq:cN , \fp_set_eq:Nc , \fp_set_eq:cc,
%     \fp_gset_eq:NN, \fp_gset_eq:cN, \fp_gset_eq:Nc, \fp_gset_eq:cc
%   }
%   Copying a floating point is the same as copying the underlying token
%   list.
%    \begin{macrocode}
\cs_new_eq:NN \fp_set_eq:NN  \tl_set_eq:NN
\cs_new_eq:NN \fp_gset_eq:NN \tl_gset_eq:NN
\cs_generate_variant:Nn \fp_set_eq:NN  { c , Nc , cc }
\cs_generate_variant:Nn \fp_gset_eq:NN { c , Nc , cc }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fp_zero:N, \fp_zero:c, \fp_gzero:N, \fp_gzero:c}
%   Setting a floating point to zero: copy \cs{c_zero_fp}.
%    \begin{macrocode}
\cs_new_protected:Npn \fp_zero:N #1 { \fp_set_eq:NN #1 \c_zero_fp }
\cs_new_protected:Npn \fp_gzero:N #1 { \fp_gset_eq:NN #1 \c_zero_fp }
\cs_generate_variant:Nn \fp_zero:N  { c }
\cs_generate_variant:Nn \fp_gzero:N { c }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {\fp_zero_new:N, \fp_zero_new:c, \fp_gzero_new:N, \fp_gzero_new:c}
%   Set the floating point to zero, or define it if needed.
%    \begin{macrocode}
\cs_new_protected:Npn \fp_zero_new:N #1
  { \fp_if_exist:NTF #1 { \fp_zero:N #1 } { \fp_new:N #1 } }
\cs_new_protected:Npn \fp_gzero_new:N #1
  { \fp_if_exist:NTF #1 { \fp_gzero:N #1 } { \fp_new:N #1 } }
\cs_generate_variant:Nn \fp_zero_new:N  { c }
\cs_generate_variant:Nn \fp_gzero_new:N { c }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Updating values}
%
% These match the equivalent functions in \pkg{l3int} and \pkg{l3skip}.
%
% \begin{macro}
%   {
%     \fp_add:Nn, \fp_add:cn, \fp_gadd:Nn, \fp_gadd:cn,
%     \fp_sub:Nn, \fp_sub:cn, \fp_gsub:Nn, \fp_gsub:cn,
%   }
% \begin{macro}{\@@_add:NNNn}
%   For the sake of error recovery we should not simply set |#1| to
%   $|#1| \pm (|#2|)$: for instance, if |#2| is % ^^A(
%   |0)+2|, the parsing error would be raised at the last closing
%   parenthesis rather than at the closing parenthesis in the user
%   argument.  Thus we evaluate |#2| instead of just putting
%   parentheses.  As an optimization we use \cs{@@_parse:n} rather than
%   \cs{fp_eval:n}, which would convert the result away from the
%   internal representation and back.
%    \begin{macrocode}
\cs_new_protected:Npn \fp_add:Nn  { \@@_add:NNNn \fp_set:Nn  + }
\cs_new_protected:Npn \fp_gadd:Nn { \@@_add:NNNn \fp_gset:Nn + }
\cs_new_protected:Npn \fp_sub:Nn  { \@@_add:NNNn \fp_set:Nn  - }
\cs_new_protected:Npn \fp_gsub:Nn { \@@_add:NNNn \fp_gset:Nn - }
\cs_new_protected:Npn \@@_add:NNNn #1#2#3#4
  { #1 #3 { #3 #2 \@@_parse:n {#4} } }
\cs_generate_variant:Nn \fp_add:Nn  { c }
\cs_generate_variant:Nn \fp_gadd:Nn { c }
\cs_generate_variant:Nn \fp_sub:Nn  { c }
\cs_generate_variant:Nn \fp_gsub:Nn { c }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Showing values}
%
% \begin{macro}{\fp_show:N, \fp_show:c, \fp_log:N, \fp_log:c, \@@_show:NN}
%   This shows the result of computing its argument by
%   passing the right data to \cs{tl_show:n} or \cs{tl_log:n}.
%    \begin{macrocode}
\cs_new_protected:Npn \fp_show:N { \@@_show:NN \tl_show:n }
\cs_generate_variant:Nn \fp_show:N { c }
\cs_new_protected:Npn \fp_log:N { \@@_show:NN \tl_log:n }
\cs_generate_variant:Nn \fp_log:N { c }
\cs_new_protected:Npn \@@_show:NN #1#2
  {
    \__kernel_chk_defined:NT #2
      { \exp_args:Nx #1 { \token_to_str:N #2 = \fp_to_tl:N #2 } }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fp_show:n, \fp_log:n}
%   Use general tools.
%    \begin{macrocode}
\cs_new_protected:Npn \fp_show:n
  { \msg_show_eval:Nn \fp_to_tl:n }
\cs_new_protected:Npn \fp_log:n
  { \msg_log_eval:Nn \fp_to_tl:n }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Some useful constants and scratch variables}
%
% \begin{variable}{\c_one_fp, \c_e_fp}
%   Some constants.
%    \begin{macrocode}
\fp_const:Nn \c_e_fp          { 2.718 2818 2845 9045 }
\fp_const:Nn \c_one_fp        { 1 }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\c_pi_fp, \c_one_degree_fp}
%   We simply round $\pi$ to and $\pi/180$ to $16$ significant digits.
%    \begin{macrocode}
\fp_const:Nn \c_pi_fp         { 3.141 5926 5358 9793 }
\fp_const:Nn \c_one_degree_fp { 0.0 1745 3292 5199 4330 }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_tmpa_fp, \l_tmpb_fp, \g_tmpa_fp, \g_tmpb_fp}
%   Scratch variables are simply initialized there.
%    \begin{macrocode}
\fp_new:N \l_tmpa_fp
\fp_new:N \l_tmpb_fp
\fp_new:N \g_tmpa_fp
\fp_new:N \g_tmpb_fp
%    \end{macrocode}
% \end{variable}
%
%    \begin{macrocode}
%</initex|package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintChanges
%
% \PrintIndex