Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3fp-assign.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3fp-assign.dtx
4 %
5 % Copyright (C) 2011-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3kernel bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full,kernel]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3fp-assign} module\\
35 % Floating point expressions^^A
36 % }
37 % \author{^^A
38 % The \LaTeX{} Project\thanks
39 % {^^A
40 % E-mail:
41 % \href{mailto:[email protected]}
42 % {[email protected]}^^A
43 % }^^A
44 % }
45 % \date{Released 2026-05-15}
46 % \maketitle
47 %
48 % \begin{documentation}
49 %
50 % \end{documentation}
51 %
52 % \begin{implementation}
53 %
54 % \section{\pkg{l3fp-assign} implementation}
55 %
56 % \begin{macrocode}
57 %<*code>
58 % \end{macrocode}
59 %
60 % \begin{macrocode}
61 %<@@=fp>
62 % \end{macrocode}
63 %
64 % \subsection{Assigning values}
65 %
66 % \begin{macro}{\fp_new:N}
67 % Floating point variables are initialized to be $+0$.
68 % \begin{macrocode}
69 \cs_new_protected:Npn \fp_new:N #1
70 { \cs_new_eq:NN #1 \c_zero_fp }
71 \cs_generate_variant:Nn \fp_new:N {c}
72 % \end{macrocode}
73 % \end{macro}
74 %
75 % \begin{macro}
76 % {
77 % \fp_set:Nn, \fp_set:cn,
78 % \fp_set:NV, \fp_set:cV,
79 % \fp_gset:Nn, \fp_gset:cn,
80 % \fp_gset:NV, \fp_gset:cV,
81 % \fp_const:Nn, \fp_const:cn
82 % }
83 % Simply use \cs{@@_parse:n} within various \texttt{f}-expanding
84 % assignments.
85 % \begin{macrocode}
86 \cs_new_protected:Npn \fp_set:Nn #1#2
87 { \__kernel_tl_set:Nx #1 { \exp_not:f { \@@_parse:n {#2} } } }
88 \cs_new_protected:Npn \fp_gset:Nn #1#2
89 { \__kernel_tl_gset:Nx #1 { \exp_not:f { \@@_parse:n {#2} } } }
90 \cs_new_protected:Npn \fp_const:Nn #1#2
91 { \tl_const:Ne #1 { \exp_not:f { \@@_parse:n {#2} } } }
92 \cs_generate_variant:Nn \fp_set:Nn { NV , c , cV }
93 \cs_generate_variant:Nn \fp_gset:Nn { NV , c , cV }
94 \cs_generate_variant:Nn \fp_const:Nn {c}
95 % \end{macrocode}
96 % \end{macro}
97 %
98 % \begin{macro}
99 % {
100 % \fp_set_eq:NN , \fp_set_eq:cN , \fp_set_eq:Nc , \fp_set_eq:cc,
101 % \fp_gset_eq:NN, \fp_gset_eq:cN, \fp_gset_eq:Nc, \fp_gset_eq:cc
102 % }
103 % Copying a floating point is the same as copying the underlying token
104 % list.
105 % \begin{macrocode}
106 \cs_new_eq:NN \fp_set_eq:NN \tl_set_eq:NN
107 \cs_new_eq:NN \fp_gset_eq:NN \tl_gset_eq:NN
108 \cs_generate_variant:Nn \fp_set_eq:NN { c , Nc , cc }
109 \cs_generate_variant:Nn \fp_gset_eq:NN { c , Nc , cc }
110 % \end{macrocode}
111 % \end{macro}
112 %
113 % \begin{macro}{\fp_zero:N, \fp_zero:c, \fp_gzero:N, \fp_gzero:c}
114 % Setting a floating point to zero: copy \cs{c_zero_fp}.
115 % \begin{macrocode}
116 \cs_new_protected:Npn \fp_zero:N #1 { \fp_set_eq:NN #1 \c_zero_fp }
117 \cs_new_protected:Npn \fp_gzero:N #1 { \fp_gset_eq:NN #1 \c_zero_fp }
118 \cs_generate_variant:Nn \fp_zero:N { c }
119 \cs_generate_variant:Nn \fp_gzero:N { c }
120 % \end{macrocode}
121 % \end{macro}
122 %
123 % \begin{macro}
124 % {\fp_zero_new:N, \fp_zero_new:c, \fp_gzero_new:N, \fp_gzero_new:c}
125 % Set the floating point to zero, or define it if needed.
126 % \begin{macrocode}
127 \cs_new_protected:Npn \fp_zero_new:N #1
128 { \fp_if_exist:NTF #1 { \fp_zero:N #1 } { \fp_new:N #1 } }
129 \cs_new_protected:Npn \fp_gzero_new:N #1
130 { \fp_if_exist:NTF #1 { \fp_gzero:N #1 } { \fp_new:N #1 } }
131 \cs_generate_variant:Nn \fp_zero_new:N { c }
132 \cs_generate_variant:Nn \fp_gzero_new:N { c }
133 % \end{macrocode}
134 % \end{macro}
135 %
136 % \subsection{Updating values}
137 %
138 % These match the equivalent functions in \pkg{l3int} and \pkg{l3skip}.
139 %
140 % \begin{macro}
141 % {
142 % \fp_add:Nn, \fp_add:cn, \fp_gadd:Nn, \fp_gadd:cn,
143 % \fp_sub:Nn, \fp_sub:cn, \fp_gsub:Nn, \fp_gsub:cn,
144 % }
145 % \begin{macro}{\@@_add:NNNn}
146 % For the sake of error recovery we should not simply set |#1| to
147 % $|#1| \pm (|#2|)$: for instance, if |#2| is % ^^A(
148 % |0)+2|, the parsing error would be raised at the last closing
149 % parenthesis rather than at the closing parenthesis in the user
150 % argument. Thus we evaluate |#2| instead of just putting
151 % parentheses. As an optimization we use \cs{@@_parse:n} rather than
152 % \cs{fp_eval:n}, which would convert the result away from the
153 % internal representation and back.
154 % \begin{macrocode}
155 \cs_new_protected:Npn \fp_add:Nn { \@@_add:NNNn \fp_set:Nn + }
156 \cs_new_protected:Npn \fp_gadd:Nn { \@@_add:NNNn \fp_gset:Nn + }
157 \cs_new_protected:Npn \fp_sub:Nn { \@@_add:NNNn \fp_set:Nn - }
158 \cs_new_protected:Npn \fp_gsub:Nn { \@@_add:NNNn \fp_gset:Nn - }
159 \cs_new_protected:Npn \@@_add:NNNn #1#2#3#4
160 { #1 #3 { #3 #2 \@@_parse:n {#4} } }
161 \cs_generate_variant:Nn \fp_add:Nn { c }
162 \cs_generate_variant:Nn \fp_gadd:Nn { c }
163 \cs_generate_variant:Nn \fp_sub:Nn { c }
164 \cs_generate_variant:Nn \fp_gsub:Nn { c }
165 % \end{macrocode}
166 % \end{macro}
167 % \end{macro}
168 %
169 % \subsection{Showing values}
170 %
171 % \begin{macro}{\fp_show:N, \fp_show:c, \fp_log:N, \fp_log:c, \@@_show:NN}
172 % This shows the result of computing its argument by
173 % passing the right data to \cs{tl_show:n} or \cs{tl_log:n}.
174 % \begin{macrocode}
175 \cs_new_protected:Npn \fp_show:N { \@@_show:NN \tl_show:n }
176 \cs_generate_variant:Nn \fp_show:N { c }
177 \cs_new_protected:Npn \fp_log:N { \@@_show:NN \tl_log:n }
178 \cs_generate_variant:Nn \fp_log:N { c }
179 \cs_new_protected:Npn \@@_show:NN #1#2
180 {
181 \__kernel_chk_tl_type:NnnT #2 { fp }
182 { \exp_args:No \@@_show_validate:n #2 }
183 { \exp_args:Ne #1 { \token_to_str:N #2 = \fp_to_tl:N #2 } }
184 }
185 % \end{macrocode}
186 % \end{macro}
187 %
188 % \begin{macro}[EXP]
189 % {
190 % \@@_show_validate:n, \@@_show_validate_aux:n, \@@_show_validate:nn,
191 % }
192 % \begin{macro}[EXP]
193 % {
194 % \@@_show_validate:w,
195 % \@@_tuple_show_validate:w,
196 % \@@_symbolic_show_validate:w
197 % }
198 % To support symbolic expression, validation has to be done recursively.
199 % Two |\@@_show_validate:nn| wrappers are used to distinguish between
200 % initial and recursive calls, in which the former provides a demo of
201 % possible forms a |fp| variable would have.
202 % \begin{macrocode}
203 \cs_new:Npn \@@_show_validate:n #1
204 {
205 \@@_show_validate:nn { #1 }
206 {
207 \s_@@ \@@_chk:w ??? \@@_sep: or \iow_newline:
208 \s_@@_tuple \_@@_tuple_chk:w ? \@@_sep: or \iow_newline:
209 \s_@@_symbolic \@@_symbolic_chk:w ? , ? \@@_sep:
210 }
211 }
212 \cs_new:Npn \@@_show_validate_aux:n #1
213 {
214 \@@_show_validate:nn { #1 } { }
215 }
216 \cs_new:Npn \@@_show_validate:nn #1#2
217 {
218 \tl_if_empty:nF { #1 }
219 {
220 \str_case:enF { \tl_head:n { #1 } }
221 {
222 { \s_@@ }
223 {
224 \@@_show_validate:w #1 \s_@@
225 \@@_chk:w ??? \@@_sep: \s_@@_stop
226 }
227 { \s_@@_tuple }
228 {
229 \@@_tuple_show_validate:w #1
230 \s_@@_tuple \_@@_tuple_chk:w ?? \@@_sep: \s_@@_stop
231 }
232 { \s_@@_symbolic }
233 {
234 \@@_symbolic_show_validate:w #1
235 \s_@@_symbolic \@@_symbolic_chk:w ? , ?? \@@_sep: \s_@@_stop
236 }
237 }
238 { #2 }
239 }
240 }
241 \cs_new:Npn \@@_show_validate:w
242 #1 \s_@@ \@@_chk:w #2#3#4#5 \@@_sep: #6 \s_@@_stop
243 {
244 \str_if_eq:nnF { #2 } {?}
245 {
246 \token_if_eq_meaning:NNTF #2 1
247 { \s_@@ \@@_chk:w #2 #3 { #4 } #5 \@@_sep: }
248 { \s_@@ \@@_chk:w #2 #3 #4 #5 \@@_sep: }
249 \@@_show_validate_aux:n { #6 }
250 }
251 }
252 \cs_new:Npn \@@_tuple_show_validate:w
253 #1 \s_@@_tuple \_@@_tuple_chk:w #2#3 \@@_sep: #4 \s_@@_stop
254 {
255 \str_if_eq:nnF { #2 } {?}
256 {
257 \s_@@_tuple \@@_tuple_chk:w { \@@_show_validate_aux:n { #2 } }
258 \@@_sep:
259 }
260 }
261 \cs_new:Npn \@@_symbolic_show_validate:w
262 #1 \s_@@_symbolic \@@_symbolic_chk:w #2 , #3#4 \@@_sep: #5 \s_@@_stop
263 {
264 \str_if_eq:nnF { #2 } {?}
265 {
266 \s_@@_symbolic \@@_symbolic_chk:w \exp_not:n { #2 } ,
267 { \@@_show_validate_aux:n { #3 } }\@@_sep:
268 \@@_show_validate_aux:n { #5 }
269 }
270 }
271 % \end{macrocode}
272 % \end{macro}
273 % \end{macro}
274 %
275 % \begin{macro}{\fp_show:n, \fp_log:n}
276 % Use general tools.
277 % \begin{macrocode}
278 \cs_new_protected:Npn \fp_show:n
279 { \__kernel_msg_show_eval:Nn \fp_to_tl:n }
280 \cs_new_protected:Npn \fp_log:n
281 { \__kernel_msg_log_eval:Nn \fp_to_tl:n }
282 % \end{macrocode}
283 % \end{macro}
284 %
285 % \subsection{Some useful constants and scratch variables}
286 %
287 % \begin{variable}{\c_one_fp, \c_e_fp}
288 % Some constants.
289 % \begin{macrocode}
290 \fp_const:Nn \c_e_fp { 2.718 2818 2845 9045 }
291 \fp_const:Nn \c_one_fp { 1 }
292 % \end{macrocode}
293 % \end{variable}
294 %
295 % \begin{variable}{\c_pi_fp, \c_one_degree_fp}
296 % We simply round $\pi$ to and $\pi/180$ to $16$ significant digits.
297 % \begin{macrocode}
298 \fp_const:Nn \c_pi_fp { 3.141 5926 5358 9793 }
299 \fp_const:Nn \c_one_degree_fp { 0.0 1745 3292 5199 4330 }
300 % \end{macrocode}
301 % \end{variable}
302 %
303 % \begin{variable}{\l_tmpa_fp, \l_tmpb_fp, \g_tmpa_fp, \g_tmpb_fp}
304 % Scratch variables are simply initialized there.
305 % \begin{macrocode}
306 \fp_new:N \l_tmpa_fp
307 \fp_new:N \l_tmpb_fp
308 \fp_new:N \g_tmpa_fp
309 \fp_new:N \g_tmpb_fp
310 % \end{macrocode}
311 % \end{variable}
312 %
313 % \begin{macrocode}
314 %</code>
315 % \end{macrocode}
316 %
317 % \end{implementation}
318 %
319 % \PrintChanges
320 %
321 % \PrintIndex