summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/l3kernel/l3fp-logic.dtx
blob: 26f18c3c1e296e16aeb64b124dec8abeb12d0f56 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
% \iffalse meta-comment
%
%% File: l3fp-logic.dtx Copyright (C) 2011-2014,2016,2017 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
%
%    http://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]{l3doc}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{The \textsf{l3fp-logic} package\\
%   Floating point conditionals}
% \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 2017/07/19}
%
% \maketitle
%
% \begin{documentation}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3fp-logic} Implementation}
%
%    \begin{macrocode}
%<*initex|package>
%    \end{macrocode}
%
%    \begin{macrocode}
%<@@=fp>
%    \end{macrocode}
%
% \begin{macro}[aux, EXP]{\@@_parse_word_max:N  , \@@_parse_word_min:N}
%   Those functions may receive a variable number of arguments.
%    \begin{macrocode}
\cs_new:Npn \@@_parse_word_max:N
  { \@@_parse_function:NNN \@@_minmax_o:Nw 2 }
\cs_new:Npn \@@_parse_word_min:N
  { \@@_parse_function:NNN \@@_minmax_o:Nw 0 }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Syntax of internal functions}
%
% \begin{itemize}
%   \item \cs{@@_compare_npos:nwnw} \Arg{expo_1} \meta{body_1} |;|
%     \Arg{expo_2} \meta{body_2} |;|
%   \item \cs{@@_minmax_o:Nw} \meta{sign} \meta{floating point array}
%   \item \cs{@@_not_o:w} |?| \meta{floating point array} (with one floating point number only)
%   \item \cs{@@_&_o:ww} \meta{floating point} \meta{floating point}
%   \item \cs{@@_|_o:ww} \meta{floating point} \meta{floating point}
%   \item \cs{@@_ternary:NwwN}, \cs{@@_ternary_auxi:NwwN},
%     \cs{@@_ternary_auxii:NwwN} have to be understood.
% \end{itemize}
%
% \subsection{Existence test}
%
% \begin{macro}[pTF]{\fp_if_exist:N, \fp_if_exist:c}
%   Copies of the \texttt{cs} functions defined in \pkg{l3basics}.
%    \begin{macrocode}
\prg_new_eq_conditional:NNn \fp_if_exist:N \cs_if_exist:N { TF , T , F , p }
\prg_new_eq_conditional:NNn \fp_if_exist:c \cs_if_exist:c { TF , T , F , p }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Comparison}
%
% \begin{macro}[pTF, EXP]{\fp_compare:n}
% \begin{macro}[aux, EXP]{\@@_compare_return:w}
%   Within floating point expressions, comparison operators are treated
%   as operations, so we evaluate |#1|, then compare with $0$.
%    \begin{macrocode}
\prg_new_conditional:Npnn \fp_compare:n #1 { p , T , F , TF }
  {
    \exp_after:wN \@@_compare_return:w
    \exp:w \exp_end_continue_f:w \@@_parse:n {#1}
  }
\cs_new:Npn \@@_compare_return:w \s_@@ \@@_chk:w #1#2;
  {
    \if_meaning:w 0 #1
      \prg_return_false:
    \else:
      \prg_return_true:
    \fi:
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[pTF, EXP]{\fp_compare:nNn}
% \begin{macro}[aux, EXP]{\@@_compare_aux:wn}
%   Evaluate |#1| and |#3|, using an auxiliary to expand both, and feed
%   the two floating point numbers swapped to \cs{@@_compare_back:ww},
%   defined below.  Compare the result with |`#2-`=|, which is $-1$ for
%   |<|, $0$ for |=|, $1$ for |>| and $2$ for |?|.
%    \begin{macrocode}
\prg_new_conditional:Npnn \fp_compare:nNn #1#2#3 { p , T , F , TF }
  {
    \if_int_compare:w
        \exp_after:wN \@@_compare_aux:wn
          \exp:w \exp_end_continue_f:w \@@_parse:n {#1} {#3}
        = \__int_eval:w `#2 - `= \__int_eval_end:
      \prg_return_true:
    \else:
      \prg_return_false:
    \fi:
  }
\cs_new:Npn \@@_compare_aux:wn #1; #2
  {
    \exp_after:wN \@@_compare_back:ww
      \exp:w \exp_end_continue_f:w \@@_parse:n {#2} #1;
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_compare_back:ww, \@@_compare_nan:w}
%   \begin{quote}
%     \cs{@@_compare_back:ww} \meta{y} |;| \meta{x} |;|
%   \end{quote}
%   Expands (in the same way as \cs{int_eval:n}) to $-1$ if $x<y$, $0$
%   if $x=y$, $1$ if $x>y$, and $2$ otherwise (denoted as $x?y$).  If
%   either operand is \texttt{nan}, stop the comparison with
%   \cs{@@_compare_nan:w} returning $2$.  If $x$ is negative, swap the
%   outputs $1$ and $-1$ (\emph{i.e.}, $>$ and $<$); we can henceforth
%   assume that $x\geq 0$.  If $y\geq 0$, and they have the same type,
%   either they are normal and we compare them with
%   \cs{@@_compare_npos:nwnw}, or they are equal.  If $y\geq 0$, but of
%   a different type, the highest type is a larger number.  Finally, if
%   $y\leq 0$, then $x>y$, unless both are zero.
%    \begin{macrocode}
\cs_new:Npn \@@_compare_back:ww
    \s_@@ \@@_chk:w #1 #2 #3;
    \s_@@ \@@_chk:w #4 #5 #6;
  {
    \__int_value:w
      \if_meaning:w 3 #1 \exp_after:wN \@@_compare_nan:w \fi:
      \if_meaning:w 3 #4 \exp_after:wN \@@_compare_nan:w \fi:
      \if_meaning:w 2 #5 - \fi:
      \if_meaning:w #2 #5
        \if_meaning:w #1 #4
          \if_meaning:w 1 #1
            \@@_compare_npos:nwnw #6; #3;
          \else:
            0
          \fi:
        \else:
          \if_int_compare:w #4 < #1 - \fi: 1
        \fi:
      \else:
        \if_int_compare:w #1#4 = 0 \exp_stop_f:
          0
        \else:
          1
        \fi:
      \fi:
    \exp_stop_f:
  }
\cs_new:Npn \@@_compare_nan:w #1 \fi: \exp_stop_f: { 2 \exp_stop_f: }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int, EXP]{\@@_compare_npos:nwnw}
% \begin{macro}[aux, EXP]{\@@_compare_significand:nnnnnnnn}
%   \begin{quote}
%     \cs{@@_compare_npos:nwnw}
%       \Arg{expo_1} \meta{body_1} |;|
%       \Arg{expo_2} \meta{body_2} |;|
%   \end{quote}
%   Within an \cs{__int_value:w} \ldots{} \cs{exp_stop_f:} construction,
%   this expands to $0$ if the two numbers are equal, $-1$ if the first
%   is smaller, and $1$ if the first is bigger.  First compare the
%   exponents: the larger one denotes the larger number.  If they are
%   equal, we must compare significands.  If both the first $8$ digits and
%   the next $8$ digits coincide, the numbers are equal.  If only the
%   first $8$ digits coincide, the next $8$ decide.  Otherwise, the
%   first $8$ digits are compared.
%    \begin{macrocode}
\cs_new:Npn \@@_compare_npos:nwnw #1#2; #3#4;
  {
    \if_int_compare:w #1 = #3 \exp_stop_f:
      \@@_compare_significand:nnnnnnnn #2 #4
    \else:
      \if_int_compare:w #1 < #3 - \fi: 1
    \fi:
  }
\cs_new:Npn \@@_compare_significand:nnnnnnnn #1#2#3#4#5#6#7#8
  {
    \if_int_compare:w #1#2 = #5#6 \exp_stop_f:
      \if_int_compare:w #3#4 = #7#8 \exp_stop_f:
        0
      \else:
        \if_int_compare:w #3#4 < #7#8 - \fi: 1
      \fi:
    \else:
      \if_int_compare:w #1#2 < #5#6 - \fi: 1
    \fi:
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Floating point expression loops}
%
% \begin{macro}[rEXP]
%   {
%     \fp_do_until:nn, \fp_do_while:nn,
%     \fp_until_do:nn, \fp_while_do:nn
%   }
%   These are quite easy given the above functions. The |do_until| and
%   |do_while| versions execute the body, then test.  The |until_do| and
%   |while_do| do it the other way round.
%    \begin{macrocode}
\cs_new:Npn \fp_do_until:nn #1#2
  {
    #2
    \fp_compare:nF {#1}
      { \fp_do_until:nn {#1} {#2} }
  }
\cs_new:Npn \fp_do_while:nn #1#2
  {
    #2
    \fp_compare:nT {#1}
      { \fp_do_while:nn {#1} {#2} }
  }
\cs_new:Npn \fp_until_do:nn #1#2
  {
    \fp_compare:nF {#1}
      {
        #2
        \fp_until_do:nn {#1} {#2}
      }
  }
\cs_new:Npn \fp_while_do:nn #1#2
  {
    \fp_compare:nT {#1}
      {
        #2
        \fp_while_do:nn {#1} {#2}
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[rEXP]
%   {
%     \fp_do_until:nNnn, \fp_do_while:nNnn,
%     \fp_until_do:nNnn, \fp_while_do:nNnn
%   }
%   As above but not using the |nNn| syntax.
%    \begin{macrocode}
\cs_new:Npn \fp_do_until:nNnn #1#2#3#4
  {
    #4
    \fp_compare:nNnF {#1} #2 {#3}
      { \fp_do_until:nNnn {#1} #2 {#3} {#4} }
  }
\cs_new:Npn \fp_do_while:nNnn #1#2#3#4
  {
    #4
    \fp_compare:nNnT {#1} #2 {#3}
      { \fp_do_while:nNnn {#1} #2 {#3} {#4} }
  }
\cs_new:Npn \fp_until_do:nNnn #1#2#3#4
  {
    \fp_compare:nNnF {#1} #2 {#3}
      {
        #4
        \fp_until_do:nNnn {#1} #2 {#3} {#4}
      }
  }
\cs_new:Npn \fp_while_do:nNnn #1#2#3#4
  {
    \fp_compare:nNnT {#1} #2 {#3}
      {
        #4
        \fp_while_do:nNnn {#1} #2 {#3} {#4}
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\fp_step_function:nnnN, \fp_step_function:nnnc}
% \begin{macro}[EXP, aux]{\@@_step:wwwN}
% \begin{macro}[EXP, aux]{\@@_step:NnnnnN, \@@_step:NfnnnN}
%   The approach here is somewhat similar to
%   \cs{int_step_function:nnnN}.  There are two subtleties: we use the
%   internal parser \cs{@@_parse:n} to avoid converting back and forth
%   from the internal representation; and (due to rounding) even a
%   non-zero step does not guarantee that the loop counter increases.
%    \begin{macrocode}
\cs_new:Npn \fp_step_function:nnnN #1#2#3
  {
    \exp_after:wN \@@_step:wwwN
      \exp:w \exp_end_continue_f:w \@@_parse_o:n {#1}
      \exp:w \exp_end_continue_f:w \@@_parse_o:n {#2}
      \exp:w \exp_end_continue_f:w \@@_parse:n {#3}
  }
\cs_generate_variant:Nn \fp_step_function:nnnN { nnnc }
%      \end{macrocode}
%   Only \enquote{normal} floating points (not $\pm 0$,
%   $\pm\texttt{inf}$, \texttt{nan}) can be used as step; if positive,
%   call \cs{@@_step:NnnnnN} with argument |>| otherwise~|<|.  This
%   function has one more argument than its integer counterpart, namely
%   the previous value, to catch the case where the loop has made no
%   progress.  Conversion to decimal is done just before calling the
%   user's function.
%    \begin{macrocode}
\cs_new:Npn \@@_step:wwwN #1 ; \s_@@ \@@_chk:w #2#3#4 ; #5; #6
  {
    \token_if_eq_meaning:NNTF #2 1
      {
        \token_if_eq_meaning:NNTF #3 0
          { \@@_step:NnnnnN > }
          { \@@_step:NnnnnN < }
      }
      {
        \token_if_eq_meaning:NNTF #2 0
          { \__msg_kernel_expandable_error:nnn { kernel } { zero-step } {#6} }
          {
            \@@_error:nnfn { fp-bad-step } { }
              { \fp_to_tl:n { \s_@@ \@@_chk:w #2#3#4 ; } } {#6}
          }
        \use_none:nnnnn
      }
      { #1 ; } { \c_nan_fp } { \s_@@ \@@_chk:w #2#3#4 ; } { #5 ; } #6
  }
\cs_new:Npn \@@_step:NnnnnN #1#2#3#4#5#6
  {
    \fp_compare:nNnTF {#2} = {#3}
      {
        \@@_error:nffn { fp-tiny-step }
          { \fp_to_tl:n {#3} } { \fp_to_tl:n {#4} } {#6}
      }
      {
        \fp_compare:nNnF {#2} #1 {#5}
          {
            \exp_args:Nf #6 { \@@_to_decimal_dispatch:w #2 }
            \@@_step:NfnnnN
              #1 { \@@_parse:n { #2 + #4 } } {#2} {#4} {#5} #6
          }
      }
  }
\cs_generate_variant:Nn \@@_step:NnnnnN { Nf }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\fp_step_inline:nnnn, \fp_step_variable:nnnNn}
% \begin{macro}[aux]{\@@_step:NNnnnn}
%   As for \cs{int_step_inline:nnnn}, create a global function and apply it,
%   following up with a break point.
%    \begin{macrocode}
\cs_new_protected:Npn \fp_step_inline:nnnn
  {
    \int_gincr:N \g__prg_map_int
    \exp_args:NNc \@@_step:NNnnnn
      \cs_gset_protected:Npn
      { __prg_map_ \int_use:N \g__prg_map_int :w }
  }
\cs_new_protected:Npn \fp_step_variable:nnnNn #1#2#3#4#5
  {
    \int_gincr:N \g__prg_map_int
    \exp_args:NNc \@@_step:NNnnnn
      \cs_gset_protected:Npx
      { __prg_map_ \int_use:N \g__prg_map_int :w }
      {#1} {#2} {#3}
      {
        \tl_set:Nn \exp_not:N #4 {##1}
        \exp_not:n {#5}
      }
  }
\cs_new_protected:Npn \@@_step:NNnnnn #1#2#3#4#5#6
  {
    #1 #2 ##1 {#6}
    \fp_step_function:nnnN {#3} {#4} {#5} #2
    \__prg_break_point:Nn \scan_stop: { \int_gdecr:N \g__prg_map_int }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
\__msg_kernel_new:nnn { kernel } { fp-bad-step }
  { Invalid~step~size~#2~in~step~function~#3. }
\__msg_kernel_new:nnn { kernel } { fp-tiny-step }
  { Tiny~step~size~(#1+#2=#1)~in~step~function~#3. }
%    \end{macrocode}
%
% \subsection{Extrema}
%
% \begin{macro}[int, EXP]{\@@_minmax_o:Nw}
%   The argument~|#1| is $2$~to find the maximum of an array~|#2| of
%   floating point numbers, and $0$~to find the minimum.  We read
%   numbers sequentially, keeping track of the largest (smallest) number
%   found so far.  If numbers are equal (for instance~$\pm0$), the first
%   is kept.  We append $-\infty$ ($\infty$), for the case of an empty
%   array.  Since no number is smaller (larger) than that, this
%   additional item only affects the maximum (minimum) in the case of
%   |max()| and |min()| with no argument.  The weird
%   fp-like trailing marker breaks the loop correctly: see the precise
%   definition of \cs{@@_minmax_loop:Nww}.
%    \begin{macrocode}
\cs_new:Npn \@@_minmax_o:Nw #1#2 @
  {
    \if_meaning:w 0 #1
      \exp_after:wN \@@_minmax_loop:Nww \exp_after:wN +
    \else:
      \exp_after:wN \@@_minmax_loop:Nww \exp_after:wN -
    \fi:
    #2
    \s_@@ \@@_chk:w 2 #1 \s_@@_exact ;
    \s_@@ \@@_chk:w { 3 \@@_minmax_break_o:w } ;
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_minmax_loop:Nww}
%   The first argument is $-$ or $+$ to denote the case where the
%   currently largest (smallest) number found (first floating point
%   argument) should be replaced by the new number (second floating
%   point argument).  If the new number is \texttt{nan}, keep that as
%   the extremum, unless that extremum is already a \texttt{nan}.
%   Otherwise, compare the two numbers.  If the new number is larger (in
%   the case of |max|) or smaller (in the case of |min|), the test
%   yields \texttt{true}, and we keep the second number as a new
%   maximum; otherwise we keep the first number.  Then loop.
%    \begin{macrocode}
\cs_new:Npn \@@_minmax_loop:Nww
    #1 \s_@@ \@@_chk:w #2#3; \s_@@ \@@_chk:w #4#5;
  {
    \if_meaning:w 3 #4
      \if_meaning:w 3 #2
        \@@_minmax_auxi:ww
      \else:
        \@@_minmax_auxii:ww
      \fi:
    \else:
      \if_int_compare:w
          \@@_compare_back:ww
            \s_@@ \@@_chk:w #4#5;
            \s_@@ \@@_chk:w #2#3;
          = #1 1 \exp_stop_f:
        \@@_minmax_auxii:ww
      \else:
        \@@_minmax_auxi:ww
      \fi:
    \fi:
    \@@_minmax_loop:Nww #1
      \s_@@ \@@_chk:w #2#3;
      \s_@@ \@@_chk:w #4#5;
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_minmax_auxi:ww, \@@_minmax_auxii:ww}
%   Keep the first/second number, and remove the other.
%    \begin{macrocode}
\cs_new:Npn \@@_minmax_auxi:ww  #1 \fi: \fi: #2 \s_@@ #3 ; \s_@@ #4;
  { \fi: \fi: #2 \s_@@ #3 ; }
\cs_new:Npn \@@_minmax_auxii:ww #1 \fi: \fi: #2 \s_@@ #3 ;
  { \fi: \fi: #2 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[aux, EXP]{\@@_minmax_break_o:w}
%   This function is called from within an \cs{if_meaning:w} test.  Skip
%   to the end of the tests, close the current test with \cs{fi:}, clean
%   up, and return the appropriate number with one post-expansion.
%    \begin{macrocode}
\cs_new:Npn \@@_minmax_break_o:w #1 \fi: \fi: #2 \s_@@ #3; #4;
  { \fi: \@@_exp_after_o:w \s_@@ #3; }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Boolean operations}
%
% \begin{macro}[int, EXP]{\@@_not_o:w}
%   Return \texttt{true} or \texttt{false}, with two expansions, one to
%   exit the conditional, and one to please \pkg{l3fp-parse}.  The first
%   argument is provided by \pkg{l3fp-parse} and is ignored.
%    \begin{macrocode}
\cs_new:cpn { @@_not_o:w } #1 \s_@@ \@@_chk:w #2#3; @
  {
    \if_meaning:w 0 #2
      \exp_after:wN \exp_after:wN \exp_after:wN \c_one_fp
    \else:
      \exp_after:wN \exp_after:wN \exp_after:wN \c_zero_fp
    \fi:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int, EXP]+\@@_&_o:ww+
% \begin{macro}[int, EXP]+\@@_|_o:ww+
% \begin{macro}[aux, EXP]{\@@_and_return:wNw}
%   For \texttt{and}, if the first number is zero, return it (with the
%   same sign).  Otherwise, return the second one.  For \texttt{or}, the
%   logic is reversed: if the first number is non-zero, return it,
%   otherwise return the second number: we achieve that by hi-jacking
%   \cs{@@_&_o:ww}, inserting an extra argument, \cs{else:}, before
%   \cs{s_@@}.  In all cases, expand after the floating point number.
%    \begin{macrocode}
\group_begin:
  \char_set_catcode_letter:N &
  \char_set_catcode_letter:N |
  \cs_new:Npn \@@_&_o:ww #1 \s_@@ \@@_chk:w #2#3;
    {
      \if_meaning:w 0 #2 #1
        \@@_and_return:wNw \s_@@ \@@_chk:w #2#3;
      \fi:
      \@@_exp_after_o:w
    }
  \cs_new:Npn \@@_|_o:ww { \@@_&_o:ww \else: }
\group_end:
\cs_new:Npn \@@_and_return:wNw #1; \fi: #2#3; { \fi: #2 #1; }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Ternary operator}
%
%^^A todo: understand and optimize.
% \begin{macro}[int, EXP]
%   {\@@_ternary:NwwN, \@@_ternary_auxi:NwwN, \@@_ternary_auxii:NwwN}
% \begin{macro}[aux, EXP]
%   {
%     \@@_ternary_loop_break:w, \@@_ternary_loop:Nw,
%     \@@_ternary_map_break:, \@@_ternary_break_point:n
%   }
%   The first function receives the test and the true branch of the |?:|
%   ternary operator.  It returns the true branch, unless the test
%   branch is zero.  In that case, the function returns a very specific
%   \texttt{nan}.  The second function receives the output of the first
%   function, and the false branch.  It returns the previous input,
%   unless that is the special \texttt{nan}, in which case we return the
%   false branch.
%    \begin{macrocode}
\cs_new:Npn \@@_ternary:NwwN #1 #2@ #3@ #4
  {
    \if_meaning:w \@@_parse_infix_::N #4
      \@@_ternary_loop:Nw
        #2
        \s_@@ \@@_chk:w { \@@_ternary_loop_break:w } ;
      \@@_ternary_break_point:n { \exp_after:wN \@@_ternary_auxi:NwwN }
      \exp_after:wN #1
      \exp:w \exp_end_continue_f:w
      \@@_exp_after_array_f:w #3 \s_@@_stop
      \exp_after:wN @
      \exp:w
        \@@_parse_operand:Nw \c_@@_prec_colon_int
        \@@_parse_expand:w
    \else:
      \__msg_kernel_expandable_error:nnnn
        { kernel } { fp-missing } { : } { ~for~?: }
      \exp_after:wN \@@_parse_continue:NwN
      \exp_after:wN #1
      \exp:w \exp_end_continue_f:w
      \@@_exp_after_array_f:w #3 \s_@@_stop
      \exp_after:wN #4
      \exp_after:wN #1
    \fi:
  }
\cs_new:Npn \@@_ternary_loop_break:w
    #1 \fi: #2 \@@_ternary_break_point:n #3
  {
    0 = 0 \exp_stop_f: \fi:
    \exp_after:wN \@@_ternary_auxii:NwwN
  }
\cs_new:Npn \@@_ternary_loop:Nw \s_@@ \@@_chk:w #1#2;
  {
    \if_int_compare:w #1 > 0 \exp_stop_f:
      \exp_after:wN \@@_ternary_map_break:
    \fi:
    \@@_ternary_loop:Nw
  }
\cs_new:Npn \@@_ternary_map_break: #1 \@@_ternary_break_point:n #2 {#2}
\cs_new:Npn \@@_ternary_auxi:NwwN #1#2@#3@#4
  {
    \exp_after:wN \@@_parse_continue:NwN
    \exp_after:wN #1
    \exp:w \exp_end_continue_f:w
    \@@_exp_after_array_f:w #2 \s_@@_stop
    #4 #1
  }
\cs_new:Npn \@@_ternary_auxii:NwwN #1#2@#3@#4
  {
    \exp_after:wN \@@_parse_continue:NwN
    \exp_after:wN #1
    \exp:w \exp_end_continue_f:w
    \@@_exp_after_array_f:w #3 \s_@@_stop
    #4 #1
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
%</initex|package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintChanges
%
% \PrintIndex