Debian Codegraph @

texmf-dist/source/latex-dev/l3kernel/l3fp-symbolic.dtx

raw
1 % \iffalse
2 %
3 %% File l3fp-symbolic.dtx (C) Copyright 2012-2025 The LaTeX Project
4 %
5 % It may be distributed and/or modified under the conditions of the
6 % LaTeX Project Public License (LPPL), either version 1.3c of this
7 % license or (at your option) any later version. The latest version
8 % of this license is in the file
9 %
10 % http://www.latex-project.org/lppl.txt
11 %
12 % This file is part of the "l3kernel bundle" (The Work in LPPL)
13 % and all files in that bundle must be distributed together.
14 %
15 % -----------------------------------------------------------------------
16 %
17 % The development version of the bundle can be found at
18 %
19 % https://github.com/latex3/latex3
20 %
21 % for those people who are interested.
22 %
23 %<*driver>
24 \documentclass[full]{l3doc}
25 \usepackage{amsmath}
26 \begin{document}
27 \DocInput{\jobname.dtx}
28 \end{document}
29 %</driver>
30 % \fi
31 %
32 % \title{^^A
33 % The \pkg{l3fp-symbolic} module\\ Symbolic expressions^^A
34 % }
35 %
36 % \author{^^A
37 % The \LaTeX{} Project\thanks
38 % {^^A
39 % E-mail:
40 % \href{mailto:[email protected]}
41 % {[email protected]}^^A
42 % }^^A
43 % }
44 %
45 % \date{Released 2026-05-26}
46 %
47 % \maketitle
48 %
49 % \begin{documentation}
50 %
51 % \end{documentation}
52 %
53 % \begin{implementation}
54 %
55 % \section{\pkg{l3fp-symbolic} implementation}
56 %
57 % \begin{macrocode}
58 %<*code>
59 % \end{macrocode}
60 %
61 % \begin{macrocode}
62 %<@@=fp>
63 % \end{macrocode}
64 %
65 % \subsection{Misc}
66 %
67 % \begin{variable}{\l_@@_symbolic_fp}
68 % Scratch floating point.
69 % \begin{macrocode}
70 \fp_new:N \l_@@_symbolic_fp
71 % \end{macrocode}
72 % \end{variable}
73 %
74 % \subsection{Building blocks for expressions}
75 %
76 % Every symbolic expression has the form \cs{s_@@_symbolic}
77 % \cs{@@_symbolic_chk:w} \meta{operation} |,| \Arg{operands} \meta{junk}
78 % \cs{@@_sep:} where the \meta{operation} is a list of \texttt{N}-type tokens,
79 % the \meta{operands} is an array of floating point objects, and the
80 % \meta{junk} (typically empty) is to be discarded. If the outermost operator
81 % (last to be evaluated) is unary, the expression has the form
82 % \begin{quote}
83 % \cs{s_@@_symbolic} \cs{@@_symbolic_chk:w} \\
84 % \cs{@@_types_unary:NNw} |\__fp_|\meta{op}|_o:w| \meta{token} |,| \\
85 % |{| \meta{operand} |}| \meta{junk} \cs{@@_sep:}
86 % \end{quote}
87 % where the \meta{op} is a unary operation (|set_sign|, |cos|,
88 % \ldots{}), and the \meta{token} and \meta{operand} are used as
89 % arguments for \cs{@@_\meta{op}_o:w} (or the type-specific analog of
90 % this function). If the outermost operator is binary, the expression
91 % has the form
92 % \begin{quote}
93 % \cs{s_@@_symbolic} \cs{@@_symbolic_chk:w} \\
94 % \cs{@@_types_binary:Nww} |\__fp_|\meta{op}|_o:ww| |,| \\
95 % |{| \meta{operand_1} \meta{operand_2} |}| \meta{junk} \cs{@@_sep:}
96 % \end{quote}
97 % where the \meta{op} is an operation (|+|, |&|, \ldots{}), and
98 % |\__fp_|\meta{op}|_o:ww| receives the \meta{operands} as arguments.
99 % If the expression is a user-defined function applied to some arguments
100 % it is stored as
101 % \begin{quote}
102 % \cs{s_@@_symbolic} \cs{@@_symbolic_chk:w} \\
103 % \cs{@@_function_o:w} \cs[no-index]{@@_\meta{identifier}_o:w} |,| \\
104 % |{| \meta{operands} |}| \meta{junk} \cs{@@_sep:}
105 % \end{quote}
106 % If the expression consists of a single variable, it is stored as
107 % \begin{quote}
108 % \cs{s_@@_symbolic} \cs{@@_symbolic_chk:w} \\
109 % \cs{@@_variable_o:w} \meta{identifier} |,| \\
110 % |{| |}| \meta{junk} \cs{@@_sep:}
111 % \end{quote}
112 %
113 % Symbolic expressions are stored in a prefix form. When encountering a
114 % symbolic expression in a floating point computation, we attempt to
115 % evaluate the operands as much as possible, and if that yields floating
116 % point numbers rather than expressions, we apply the operator which
117 % follows (if the function is known).
118 %
119 % For instance, the expression |a + b * sin(c)| is stored as
120 % \begin{verbatim}
121 % \s__fp_symbolic \__fp_symbolic_chk:w
122 % \__fp_types_binary:Nww \__fp_+_o:ww ,
123 % {
124 % \s__fp_symbolic \__fp_symbolic_chk:w
125 % \__fp_variable_o:w a , { } \__fp_sep:
126 % \s__fp_symbolic \__fp_symbolic_chk:w
127 % \__fp_types_binary:Nww \__fp_*_o:ww ,
128 % {
129 % \s__fp_symbolic \__fp_symbolic_chk:w
130 % \__fp_variable_o:w b , { } \__fp_sep:
131 % \s__fp_symbolic \__fp_symbolic_chk:w
132 % \__fp_types_unary:NNw \__fp_sin_o:w \use_i:nn ,
133 % {
134 % \s__fp_symbolic \__fp_symbolic_chk:w
135 % \__fp_variable_o:w c , { } \__fp_sep:
136 % } \__fp_sep:
137 % } \__fp_sep:
138 % } \__fp_sep:
139 % \end{verbatim}
140 %
141 % \begin{variable}{\s_@@_symbolic}
142 % Scan mark indicating the start of a symbolic expression.
143 % \begin{macrocode}
144 \scan_new:N \s_@@_symbolic
145 % \end{macrocode}
146 % \end{variable}
147 %
148 % \begin{macro}{\@@_symbolic_chk:w}
149 % Analog of \cs{@@_chk:w} for symbolic expressions.
150 % \begin{macrocode}
151 \cs_new_protected:Npn \@@_symbolic_chk:w #1,#2#3\@@_sep:
152 {
153 \msg_error:nne { fp } { misused-fp }
154 {
155 \@@_to_tl_dispatch:w
156 \s_@@_symbolic \@@_symbolic_chk:w #1,{#2}\@@_sep:
157 }
158 }
159 % \end{macrocode}
160 % \end{macro}
161 %
162 % \subsection{Expanding after a symbolic expression}
163 %
164 % \begin{macro}[EXP]
165 % {\@@_if_has_symbolic:nTF, \@@_if_has_symbolic_aux:w}
166 % Tests if |#1| contains \cs{s_@@_symbolic} at top-level. This test
167 % should be precise enough to determine if a given array contains a
168 % symbolic expression or only consists of floating points. Used in
169 % \cs{@@_exp_after_symbolic_f:nw}.
170 % \begin{macrocode}
171 \cs_new:Npn \@@_if_has_symbolic:nTF #1
172 {
173 \@@_if_has_symbolic_aux:w
174 #1 \s_@@_mark \use_i:nn
175 \s_@@_symbolic \s_@@_mark \use_ii:nn
176 \s_@@_stop
177 }
178 \cs_new:Npn \@@_if_has_symbolic_aux:w
179 #1 \s_@@_symbolic #2 \s_@@_mark #3#4 \s_@@_stop { #3 }
180 % \end{macrocode}
181 % \end{macro}
182 %
183 % \begin{macro}[EXP]{\@@_exp_after_symbolic_f:nw}
184 % \begin{macro}[EXP]
185 % {\@@_exp_after_symbolic_aux:w, \@@_exp_after_symbolic_loop:N}
186 % This function does two things: trigger an \texttt{f}-expansion of
187 % the argument~|#1| after the following symbolic expression, and
188 % evaluate all pieces of the expression which can be evaluated.
189 % \begin{macrocode}
190 \cs_new:Npn \@@_exp_after_symbolic_f:nw
191 #1 \s_@@_symbolic \@@_symbolic_chk:w #2, #3#4\@@_sep:
192 {
193 \exp_after:wN \@@_exp_after_symbolic_aux:w
194 \exp:w
195 \@@_exp_after_symbolic_loop:N #2
196 { , \exp:w \use_none:nn }
197 \exp_after:wN \exp_end: \exp_after:wN
198 {
199 \exp:w \exp_end_continue_f:w
200 \@@_exp_after_array_f:w #3 \s_@@_expr_stop
201 \exp_after:wN
202 }
203 \exp_after:wN \@@_sep:
204 \exp:w \exp_end_continue_f:w #1
205 }
206 \cs_new:Npn \@@_exp_after_symbolic_aux:w #1, #2\@@_sep:
207 {
208 \@@_if_has_symbolic:nTF {#2}
209 { \s_@@_symbolic \@@_symbolic_chk:w #1, {#2} \@@_sep: }
210 { #1 #2 @ \prg_do_nothing: }
211 }
212 \cs_new:Npn \@@_exp_after_symbolic_loop:N #1
213 {
214 \exp_after:wN \exp_end:
215 \exp_after:wN #1
216 \exp:w
217 \@@_exp_after_symbolic_loop:N
218 }
219 % \end{macrocode}
220 % \end{macro}
221 % \end{macro}
222 %
223 % \subsection{Applying infix operators to expressions}
224 %
225 % \begin{macro}[EXP]{\@@_symbolic_binary_o:Nww}
226 % Used when applying infix operators to expressions.
227 % \begin{macrocode}
228 \cs_new:Npn \@@_symbolic_binary_o:Nww #1 #2\@@_sep: #3\@@_sep:
229 {
230 \@@_exp_after_symbolic_f:nw { \exp_after:wN \exp_stop_f: }
231 \s_@@_symbolic \@@_symbolic_chk:w
232 \@@_types_binary:Nww #1 , { #2\@@_sep: #3\@@_sep: } \@@_sep:
233 }
234 % \end{macrocode}
235 % \end{macro}
236 %
237 % \begin{@makeother}{^} %^^A Hack!
238 % \begin{@makeother}{|} %^^A Hack!
239 % \begin{@makeother}{&} %^^A Hack!
240 % \begin{macro}[EXP]
241 % {
242 % \@@_symbolic_+_symbolic_o:ww,
243 % \@@_symbolic_+_o:ww,
244 % \@@_+_symbolic_o:ww,
245 % \@@_symbolic_-_symbolic_o:ww,
246 % \@@_symbolic_-_o:ww,
247 % \@@_-_symbolic_o:ww,
248 % \@@_symbolic_*_symbolic_o:ww,
249 % \@@_symbolic_*_o:ww,
250 % \@@_*_symbolic_o:ww,
251 % \@@_symbolic_/_symbolic_o:ww,
252 % \@@_symbolic_/_o:ww,
253 % \@@_/_symbolic_o:ww,
254 % \@@_symbolic_^_symbolic_o:ww,
255 % \@@_symbolic_^_o:ww,
256 % \@@_^_symbolic_o:ww,
257 % \@@_symbolic_|_symbolic_o:ww,
258 % \@@_symbolic_|_o:ww,
259 % \@@_|_symbolic_o:ww,
260 % \@@_symbolic_&_symbolic_o:ww,
261 % \@@_symbolic_&_o:ww,
262 % \@@_&_symbolic_o:ww,
263 % }
264 % \begin{macrocode}
265 \cs_set_protected:Npn \@@_tmp:w #1#2
266 {
267 \cs_new:cpn
268 { @@_symbolic_#2_symbolic_o:ww }
269 { \@@_symbolic_binary_o:Nww #1 }
270 \cs_new_eq:cc
271 { @@_symbolic_#2 _o:ww }
272 { @@_symbolic_#2_symbolic_o:ww }
273 \cs_new_eq:cc
274 { @@ _#2_symbolic_o:ww }
275 { @@_symbolic_#2_symbolic_o:ww }
276 }
277 \tl_map_inline:nn { + - * / ^ & | } % |
278 { \exp_args:Nc \@@_tmp:w { @@_#1_o:ww } {#1} }
279 % \end{macrocode}
280 % \end{macro}
281 % \end{@makeother}
282 % \end{@makeother}
283 % \end{@makeother}
284 %
285 % \subsection{Applying prefix functions to expressions}
286 %
287 % \begin{macro}[EXP]{\@@_symbolic_unary_o:NNw}
288 % Used when applying infix operators to expressions.
289 % \begin{macrocode}
290 \cs_new:Npn \@@_symbolic_unary_o:NNw #1#2#3\@@_sep: @
291 {
292 \@@_exp_after_symbolic_f:nw { \exp_after:wN \exp_stop_f: }
293 \s_@@_symbolic \@@_symbolic_chk:w
294 \@@_types_unary:NNw #1#2 , { #3\@@_sep: } \@@_sep:
295 }
296 % \end{macrocode}
297 % \end{macro}
298 %
299 % \begin{macro}[EXP]
300 % {
301 % \@@_symbolic_acos_o:w ,
302 % \@@_symbolic_acsc_o:w ,
303 % \@@_symbolic_asec_o:w ,
304 % \@@_symbolic_asin_o:w ,
305 % \@@_symbolic_cos_o:w ,
306 % \@@_symbolic_cot_o:w ,
307 % \@@_symbolic_csc_o:w ,
308 % \@@_symbolic_exp_o:w ,
309 % \@@_symbolic_fact_o:w ,
310 % \@@_symbolic_ln_o:w ,
311 % \@@_symbolic_not_o:w ,
312 % \@@_symbolic_sec_o:w ,
313 % \@@_symbolic_set_sign_o:w ,
314 % \@@_symbolic_sign_o:w ,
315 % \@@_symbolic_sin_o:w ,
316 % \@@_symbolic_tan_o:w ,
317 % }
318 % \begin{macrocode}
319 \tl_map_inline:nn
320 {
321 {acos} {acsc} {asec} {asin} {cos} {cot} {csc} {exp} {fact} {ln}
322 {not} {sec} {set_sign} {sin} {sign} {sqrt} {tan}
323 }
324 {
325 \cs_new:cpe { @@_symbolic_#1_o:w }
326 {
327 \exp_not:N \@@_symbolic_unary_o:NNw
328 \exp_not:c { @@_#1_o:w }
329 }
330 }
331 % \end{macrocode}
332 % \end{macro}
333 %
334 % \subsection{Conversions}
335 %
336 % \begin{macro}[EXP]
337 % {
338 % \@@_symbolic_to_decimal:w,
339 % \@@_symbolic_to_int:w,
340 % \@@_symbolic_to_scientific:w
341 % }
342 % \begin{macro}[EXP]{\@@_symbolic_convert:wnnN}
343 % Symbolic expressions cannot be converted to decimal, integer, or
344 % scientific notation unless they can be evaluated all the way.
345 % \begin{macrocode}
346 \cs_set_protected:Npn \@@_tmp:w #1#2#3
347 {
348 \cs_new:cpn { @@_symbolic_to_#1:w }
349 {
350 \exp_after:wN \@@_symbolic_convert:wnnN
351 \exp:w \exp_end_continue_f:w
352 \@@_exp_after_symbolic_f:nw { { #2 } { fp_to_#1 } #3 }
353 }
354 }
355 \@@_tmp:w { decimal } { 0 } \@@_to_decimal_dispatch:w
356 \@@_tmp:w { int } { 0 } \@@_to_int_dispatch:w
357 \@@_tmp:w { scientific } { nan } \@@_to_scientific_dispatch:w
358 \cs_new:Npn \@@_symbolic_convert:wnnN #1#2\@@_sep: #3#4#5
359 {
360 \str_if_eq:nnTF {#1} { \s_@@_symbolic }
361 { \@@_invalid_operation:nnw {#3} {#4} #1#2\@@_sep: }
362 { #5 #1#2\@@_sep: }
363 }
364 % \end{macrocode}
365 % \end{macro}
366 % \end{macro}
367 %
368 % \begin{macro}[EXP]
369 % {\@@_symbolic_cs_arg_to_fn:NN, \@@_symbolic_op_arg_to_fn:nN}
370 % \begin{macrocode}
371 \cs_new:Npn \@@_symbolic_cs_arg_to_fn:NN #1
372 {
373 \exp_args:Nf \@@_symbolic_op_arg_to_fn:nN
374 { \@@_types_cs_to_op:N #1 }
375 }
376 \cs_new:Npn \@@_symbolic_op_arg_to_fn:nN #1#2
377 {
378 \str_case:nnF { #1 #2 }
379 {
380 { not ? } { ! }
381 { set_sign 0 } { abs }
382 { set_sign 2 } { - }
383 }
384 {
385 \token_if_eq_meaning:NNTF #2 \use_ii:nn
386 { #1 d } {#1}
387 }
388 }
389 % \end{macrocode}
390 % \end{macro}
391 %
392 % \begin{macro}[EXP]{\@@_symbolic_to_tl:w}
393 % \begin{macro}[rEXP]
394 % {
395 % \@@_symbolic_unary_to_tl:NNw,
396 % \@@_symbolic_binary_to_tl:Nww,
397 % \@@_symbolic_function_to_tl:Nw,
398 % \@@_symbolic_variadic_to_tl:NNw
399 % }
400 % Converting a symbolic expression to a token list is possible.
401 % \begin{macrocode}
402 \cs_new:Npn \@@_symbolic_to_tl:w
403 \s_@@_symbolic \@@_symbolic_chk:w #1#2, #3#4\@@_sep:
404 {
405 \str_case:nnTF {#1}
406 {
407 { \@@_types_unary:NNw } { \@@_symbolic_unary_to_tl:NNw }
408 { \@@_types_binary:Nww } { \@@_symbolic_binary_to_tl:Nww }
409 { \@@_function_o:w } { \@@_symbolic_function_to_tl:Nw }
410 { \@@_types_variadic:NNw } { \@@_symbolic_variadic_to_tl:NNw }
411 }
412 { #2, #3 @ }
413 { \tl_to_str:n {#2} }
414 }
415 \cs_new:Npn \@@_symbolic_unary_to_tl:NNw #1#2 , #3 @
416 {
417 \use:e
418 {
419 \@@_symbolic_cs_arg_to_fn:NN #1#2
420 ( \@@_to_tl_dispatch:w #3 )
421 }
422 }
423 \cs_new:Npn \@@_symbolic_binary_to_tl:Nww #1, #2\@@_sep: #3\@@_sep: @
424 {
425 \use:e
426 {
427 ( \@@_to_tl_dispatch:w #2\@@_sep: )
428 \@@_types_cs_to_op:N #1
429 ( \@@_to_tl_dispatch:w #3\@@_sep: )
430 }
431 }
432 \cs_new:Npn \@@_symbolic_function_to_tl:Nw #1, #2@
433 {
434 \use:e
435 {
436 \@@_types_cs_to_op:N #1
437 ( \@@_array_to_clist:n {#2} )
438 }
439 }
440 \cs_new:Npn \@@_symbolic_variadic_to_tl:NNw #1#2 , #3 @
441 {
442 \use:e
443 {
444 \cs_if_eq:NNTF #1 \@@_minmax_o:Nw
445 { \if_meaning:w 2 #2 max \else: min \fi: }
446 {
447 \cs_if_eq:NNTF #1 \@@_round_o:Nw
448 {
449 \cs_if_eq:NNTF #2 \@@_round_to_nearest:NNN { round }
450 { \cs_if_eq:NNTF #2 \@@_round_to_zero:NNN { trunc }
451 { \cs_if_eq:NNTF #2 \@@_round_to_ninf:NNN { floor } { ceil } } }
452 }
453 {
454 \cs_if_eq:NNTF #1 \@@_acot_o:Nw
455 { \if_meaning:w \use_i:nn #2 acot \else: acotd \fi: }
456 {
457 \cs_if_eq:NNTF #1 \@@_atan_o:Nw
458 { \if_meaning:w \use_i:nn #2 atan \else: atand \fi: }
459 {
460 \cs_if_eq:NNTF #1 \@@_rand_o:Nw { rand } { randint }
461 }
462 }
463 }
464 }
465 ( \@@_array_to_clist:n {#3} )
466 }
467 }
468 % \end{macrocode}
469 % \end{macro}
470 % \end{macro}
471 %
472 % \subsection{Identifiers}
473 %
474 % Functions defined here are not necessarily tied to symbolic
475 % expressions.
476 %
477 % \begin{macro}[TF]{\@@_id_if_invalid:n}
478 % \begin{macro}[EXP]{\@@_id_if_invalid_aux:N}
479 % If |#1| contains a space, it is not a valid identifier. Otherwise,
480 % loop through letters in |#1|: if it is not a letter, break the loop
481 % and return \texttt{true}. If the end of the loop is reached
482 % without finding any non-letter, return \texttt{false}.
483 % Note |#1| must be a str (i.e., resulted from \cs{tl_to_str:n}).
484 % \begin{macrocode}
485 \prg_new_protected_conditional:Npnn
486 \@@_id_if_invalid:n #1 { T , F , TF }
487 {
488 \tl_if_empty:nTF {#1}
489 { \prg_return_true: }
490 {
491 \tl_if_in:nnTF { #1 } { ~ }
492 { \prg_return_true: }
493 {
494 \@@_id_if_invalid_aux:N #1
495 { ? \prg_break:n \prg_return_false: }
496 \prg_break_point:
497 }
498 }
499 }
500 \cs_new:Npn \@@_id_if_invalid_aux:N #1
501 {
502 \use_none:n #1
503 \int_compare:nF { `a <= `#1 <= `z }
504 {
505 \int_compare:nF { `A <= `#1 <= `Z }
506 { \prg_break:n \prg_return_true: }
507 }
508 \@@_id_if_invalid_aux:N
509 }
510 % \end{macrocode}
511 % \end{macro}
512 % \end{macro}
513 %
514 % \subsection{Declaring variables and assigning values}
515 %
516 % \begin{macro}[EXP]{\@@_variable_o:w}
517 % We do not use \cs{exp_last_unbraced:Nv} to extract the value of
518 % |\l__fp_variable_#1_fp| because in \cs{fp_set_variable:nn} we define
519 % this \texttt{fp} variable to be something which \texttt{f}-expands
520 % to an actual floating point, rather than a genuine floating point.
521 % \begin{macrocode}
522 \cs_new:Npn \@@_variable_o:w #1 @ #2
523 {
524 \fp_if_exist:cTF { l_@@_variable_#1_fp }
525 {
526 \exp_last_unbraced:Nf \@@_exp_after_array_f:w
527 { \use:c { l_@@_variable_#1_fp } } \s_@@_expr_stop
528 \exp_after:wN \exp_stop_f: #2
529 }
530 {
531 \token_if_eq_meaning:NNTF #2 \prg_do_nothing:
532 {
533 \s_@@_symbolic \@@_symbolic_chk:w
534 \@@_variable_o:w #1 , { } \@@_sep:
535 }
536 {
537 \exp_after:wN \s_@@_symbolic
538 \exp_after:wN \@@_symbolic_chk:w
539 \exp_after:wN \@@_variable_o:w
540 \exp:w
541 \@@_exp_after_symbolic_loop:N #1
542 { , \exp:w \use_none:nn }
543 \exp_after:wN \exp_end:
544 \exp_after:wN { \exp_after:wN } \exp_after:wN \@@_sep:
545 #2
546 }
547 }
548 }
549 % \end{macrocode}
550 % \end{macro}
551 %
552 % \begin{macro}
553 % {\@@_variable_set_parsing:Nn, \@@_variable_set_parsing_aux:NNn}
554 % \begin{macrocode}
555 \cs_new_protected:Npn \@@_variable_set_parsing:Nn #1#2
556 {
557 \cs_set:Npn \@@_tmp:w
558 {
559 \@@_exp_after_symbolic_f:nw { \@@_parse_infix:NN }
560 \s_@@_symbolic \@@_symbolic_chk:w
561 \@@_variable_o:w #2 , { } \@@_sep:
562 }
563 \exp_args:NNc \@@_variable_set_parsing_aux:NNn #1
564 { @@_parse_word_#2:N } {#2}
565 }
566 \cs_new_protected:Npn \@@_variable_set_parsing_aux:NNn #1#2#3
567 {
568 \cs_if_eq:NNF #2 \@@_tmp:w
569 {
570 \cs_if_exist:NTF #2
571 {
572 \msg_warning:nnnn
573 { fp } { id-used-elsewhere } {#3} { variable }
574 #1 #2 \@@_tmp:w
575 }
576 {
577 \cs_new_eq:NN #2 \scan_stop: % to declare the function
578 #1 #2 \@@_tmp:w
579 }
580 }
581 }
582 % \end{macrocode}
583 % \end{macro}
584 %
585 % \begin{macro}
586 % {
587 % \fp_clear_variable:n,
588 % \@@_clear_variable:n, \@@_clear_variable_aux:n
589 % }
590 % We need local undefining, so have to do it low-level.
591 % \cs{@@_clear_variable_aux:n} is needed by \cs{@@_set_function:Nnnn}
592 % to skip \cs{@@_id_if_invalid:nTF}.
593 % \begin{macrocode}
594 \cs_new_protected:Npn \fp_clear_variable:n #1
595 {
596 \exp_args:No \@@_clear_variable:n { \tl_to_str:n {#1} }
597 }
598 \cs_new_protected:Npn \@@_clear_variable:n #1
599 {
600 \@@_id_if_invalid:nTF {#1}
601 { \msg_error:nnn { fp } { id-invalid } {#1} }
602 { \@@_clear_variable_aux:n {#1} }
603 }
604 \cs_new_protected:Npn \@@_clear_variable_aux:n #1
605 {
606 \cs_set_eq:cN { l_@@_variable_#1_fp } \tex_undefined:D
607 \@@_variable_set_parsing:Nn \cs_set_eq:NN {#1}
608 }
609 % \end{macrocode}
610 % \end{macro}
611 %
612 % \begin{macro}{\fp_new_variable:n}
613 % \begin{macro}{\@@_new_variable:n}
614 % Check that |#1| is a valid identifier. If the identifier is already
615 % in use, complain. Then set |\__fp_parse_word_#1:N| to use
616 % |\__fp_variable_o:w|.
617 % \begin{macrocode}
618 \cs_new_protected:Npn \fp_new_variable:n #1
619 {
620 \exp_args:No \@@_new_variable:n { \tl_to_str:n {#1} }
621 }
622 \cs_new_protected:Npn \@@_new_variable:n #1
623 {
624 \@@_id_if_invalid:nTF {#1}
625 { \msg_error:nnn { fp } { id-invalid } {#1} }
626 {
627 \cs_if_exist:cT { @@_parse_word_#1:N }
628 {
629 \msg_error:nnn
630 { fp } { id-already-defined } {#1}
631 \cs_undefine:c { @@_parse_word_#1:N }
632 \cs_set_eq:cN { l_@@_variable_#1_fp } \tex_undefined:D
633 }
634 \@@_variable_set_parsing:Nn \cs_gset_eq:NN {#1}
635 }
636 }
637 % \end{macrocode}
638 % \end{macro}
639 % \end{macro}
640 %
641 % \begin{variable}{\l_@@_symbolic_flag}
642 % \begin{macro}{\fp_set_variable:nn, \@@_set_variable:nn}
643 % Refuse invalid identifiers. If the variable does not exist yet,
644 % raise an error and define it just as in \cs{fp_new_variable:n}
645 % (but without unnecessary checks).
646 % Then evaluate~|#2|. If the result contains the
647 % identifier~|#1|, we would later get a loop in cases such as
648 % \begin{quote}
649 % \cs{fp_set_variable:nn} |{A}| |{A}|\\
650 % \cs{fp_show:n} |{A}|
651 % \end{quote}
652 % To detect this, define |\l__fp_variable_#1_fp| to raise an
653 % internal flag and evaluate to \texttt{nan}. Then re-evaluate
654 % \cs{l_@@_symbolic_fp}, and store the result in~|#1|. If the flag is
655 % raised, |#1|~was present in \cs{l_@@_symbolic_fp}. In all cases,
656 % the |#1|-free result ends up in |\l__fp_variable_#1_fp|.
657 % \begin{macrocode}
658 \flag_new:N \l_@@_symbolic_flag
659 \cs_new_protected:Npn \fp_set_variable:nn #1
660 {
661 \exp_args:No \@@_set_variable:nn { \tl_to_str:n {#1} }
662 }
663 \cs_new_protected:Npn \@@_set_variable:nn #1#2
664 {
665 \@@_id_if_invalid:nTF {#1}
666 { \msg_error:nnn { fp } { id-invalid } {#1} }
667 {
668 \cs_if_exist:cF { @@_parse_word_#1:N }
669 {
670 \msg_error:nnn {fp} { id-undefined } {#1}
671 \@@_variable_set_parsing:Nn \cs_set_eq:NN {#1}
672 }
673 \fp_set:Nn \l_@@_symbolic_fp {#2}
674 \cs_set_nopar:cpn { l_@@_variable_#1_fp }
675 { \flag_ensure_raised:N \l_@@_symbolic_flag \c_nan_fp }
676 \flag_clear:N \l_@@_symbolic_flag
677 \fp_set:cn { l_@@_variable_#1_fp } { \l_@@_symbolic_fp }
678 \flag_if_raised:NT \l_@@_symbolic_flag
679 {
680 \msg_error:nneee { fp } { id-loop }
681 { #1 }
682 { \tl_to_str:n {#2} }
683 { \fp_to_tl:N \l_@@_symbolic_fp }
684 }
685 }
686 }
687 % \end{macrocode}
688 % \end{macro}
689 % \end{variable}
690 %
691 % \subsection{Messages}
692 %
693 % \begin{macrocode}
694 \msg_new:nnnn { fp } { id-invalid }
695 { Floating~point~identifier~'#1'~invalid. }
696 {
697 LaTeX~has~been~asked~to~create~a~new~floating~point~identifier~'#1'~
698 but~this~may~only~contain~ASCII~letters.
699 }
700 \msg_new:nnnn { fp } { id-already-defined }
701 { Floating~point~identifier~'#1'~already~defined. }
702 {
703 LaTeX~has~been~asked~to~create~a~new~floating~point~identifier~'#1'~
704 but~this~name~has~already~been~used~elsewhere.
705 }
706 \msg_new:nnnn { fp } { id-undefined }
707 { Floating~point~identifier~'#1'~is~undefined. }
708 {
709 LaTeX~has~been~asked~to~set~a~floating~point~identifier~'#1'~
710 but~this~name~has~not~been~declared.
711 }
712 \msg_new:nnnn { fp } { id-used-elsewhere }
713 { Floating~point~identifier~'#1'~already~used~for~something~else. }
714 {
715 LaTeX~has~been~asked~to~create~a~new~floating~point~identifier~'#1'~
716 but~this~name~is~used,~and~is~not~a~user-defined~#2.
717 }
718 \msg_new:nnnn { fp } { id-loop }
719 { Variable~'#1'~used~in~the~definition~of~'#1'. }
720 {
721 LaTeX~has~been~asked~to~set~the~floating~point~identifier~'#1'~
722 to~the~expression~'#2'.~Evaluating~this~expression~yields~'#3',~
723 which~contains~'#1'~itself.
724 }
725 % \end{macrocode}
726 %
727 % \subsection{Road-map}
728 %
729 % The following functions are not implemented: |?:|, comparisons.
730 %
731 % \begin{macrocode}
732 %</code>
733 % \end{macrocode}
734 %
735 % \end{implementation}
736 %
737 % \PrintIndex