| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3fp-logic.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-logic} module\\ |
| 35 |
% Floating point conditionals^^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 |
% |
| 47 |
% \maketitle |
| 48 |
% |
| 49 |
% \begin{documentation} |
| 50 |
% |
| 51 |
% \end{documentation} |
| 52 |
% |
| 53 |
% \begin{implementation} |
| 54 |
% |
| 55 |
% \section{\pkg{l3fp-logic} implementation} |
| 56 |
% |
| 57 |
% \begin{macrocode} |
| 58 |
%<*code> |
| 59 |
% \end{macrocode} |
| 60 |
% |
| 61 |
% \begin{macrocode} |
| 62 |
%<@@=fp> |
| 63 |
% \end{macrocode} |
| 64 |
% |
| 65 |
% \begin{macro}[EXP]{\@@_parse_word_max:N , \@@_parse_word_min:N} |
| 66 |
% Those functions may receive a variable number of arguments. |
| 67 |
% \begin{macrocode} |
| 68 |
\cs_new:Npn \@@_parse_word_max:N |
| 69 |
{ \@@_parse_function:NNN \@@_minmax_o:Nw 2 } |
| 70 |
\cs_new:Npn \@@_parse_word_min:N |
| 71 |
{ \@@_parse_function:NNN \@@_minmax_o:Nw 0 } |
| 72 |
% \end{macrocode} |
| 73 |
% \end{macro} |
| 74 |
% |
| 75 |
% \subsection{Syntax of internal functions} |
| 76 |
% |
| 77 |
% \begin{itemize} |
| 78 |
% \item \cs{@@_compare_npos:nwnw} \Arg{expo_1} \meta{body_1} \cs{@@_sep:} |
| 79 |
% \Arg{expo_2} \meta{body_2} \cs{@@_sep:} |
| 80 |
% \item \cs{@@_minmax_o:Nw} \meta{sign} \meta{floating point array} |
| 81 |
% \item \cs{@@_not_o:w} |?| \meta{floating point array} (with one floating point number only) |
| 82 |
% \item \cs{@@_&_o:ww} \meta{floating point} \meta{floating point} |
| 83 |
% \item \cs{@@_|_o:ww} \meta{floating point} \meta{floating point} |
| 84 |
% \item \cs{@@_ternary:NwwN}, \cs{@@_ternary_auxi:NwwN}, |
| 85 |
% \cs{@@_ternary_auxii:NwwN} have to be understood. |
| 86 |
% \end{itemize} |
| 87 |
% |
| 88 |
% \subsection{Tests} |
| 89 |
% |
| 90 |
% \begin{macro}[pTF]{\fp_if_exist:N, \fp_if_exist:c} |
| 91 |
% Copies of the \texttt{cs} functions defined in \pkg{l3basics}. |
| 92 |
% \begin{macrocode} |
| 93 |
\prg_new_eq_conditional:NNn \fp_if_exist:N \cs_if_exist:N { TF , T , F , p } |
| 94 |
\prg_new_eq_conditional:NNn \fp_if_exist:c \cs_if_exist:c { TF , T , F , p } |
| 95 |
% \end{macrocode} |
| 96 |
% \end{macro} |
| 97 |
% |
| 98 |
% \begin{macro}[pTF]{\fp_if_nan:n} |
| 99 |
% Evaluate and check if the result is a floating point of the same |
| 100 |
% kind as \nan{}. |
| 101 |
% \begin{macrocode} |
| 102 |
\prg_new_conditional:Npnn \fp_if_nan:n #1 { TF , T , F , p } |
| 103 |
{ |
| 104 |
\if:w 3 \exp_last_unbraced:Nf \@@_kind:w { \@@_parse:n {#1} } |
| 105 |
\prg_return_true: |
| 106 |
\else: |
| 107 |
\prg_return_false: |
| 108 |
\fi: |
| 109 |
} |
| 110 |
% \end{macrocode} |
| 111 |
% \end{macro} |
| 112 |
% |
| 113 |
% \subsection{Comparison} |
| 114 |
% |
| 115 |
% \begin{macro}[pTF, EXP]{\fp_compare:n} |
| 116 |
% \begin{macro}[EXP]{\@@_compare_return:w} |
| 117 |
% Within floating point expressions, comparison operators are treated |
| 118 |
% as operations, so we evaluate |#1|, then compare with $\pm 0$. |
| 119 |
% Tuples are \texttt{true}. |
| 120 |
% \begin{macrocode} |
| 121 |
\prg_new_conditional:Npnn \fp_compare:n #1 { p , T , F , TF } |
| 122 |
{ |
| 123 |
\exp_after:wN \@@_compare_return:w |
| 124 |
\exp:w \exp_end_continue_f:w \@@_parse:n {#1} |
| 125 |
} |
| 126 |
\cs_new:Npn \@@_compare_return:w #1#2#3\@@_sep: |
| 127 |
{ |
| 128 |
\if_charcode:w 0 |
| 129 |
\@@_if_type_fp:NTwFw |
| 130 |
#1 { \@@_use_i_delimit_by_s_stop:nw #3 \s_@@_stop } |
| 131 |
\s_@@ 1 \s_@@_stop |
| 132 |
\prg_return_false: |
| 133 |
\else: |
| 134 |
\prg_return_true: |
| 135 |
\fi: |
| 136 |
} |
| 137 |
% \end{macrocode} |
| 138 |
% \end{macro} |
| 139 |
% \end{macro} |
| 140 |
% |
| 141 |
% \begin{macro}[pTF, EXP]{\fp_compare:nNn} |
| 142 |
% \begin{macro}[EXP]{\@@_compare_aux:wn} |
| 143 |
% Evaluate |#1| and |#3|, using an auxiliary to expand both, and feed |
| 144 |
% the two floating point numbers swapped to \cs{@@_compare_back_any:ww}, |
| 145 |
% defined below. Compare the result with |`#2-`=|, which is $-1$ for |
| 146 |
% |<|, $0$ for |=|, $1$ for |>| and $2$ for |?|. |
| 147 |
% \begin{macrocode} |
| 148 |
\prg_new_conditional:Npnn \fp_compare:nNn #1#2#3 { p , T , F , TF } |
| 149 |
{ |
| 150 |
\if_int_compare:w |
| 151 |
\exp_after:wN \@@_compare_aux:wn |
| 152 |
\exp:w \exp_end_continue_f:w \@@_parse:n {#1} {#3} |
| 153 |
= \@@_int_eval:w `#2 - `= \@@_int_eval_end: |
| 154 |
\prg_return_true: |
| 155 |
\else: |
| 156 |
\prg_return_false: |
| 157 |
\fi: |
| 158 |
} |
| 159 |
\cs_new:Npn \@@_compare_aux:wn #1\@@_sep: #2 |
| 160 |
{ |
| 161 |
\exp_after:wN \@@_compare_back_any:ww |
| 162 |
\exp:w \exp_end_continue_f:w \@@_parse:n {#2} #1\@@_sep: |
| 163 |
} |
| 164 |
% \end{macrocode} |
| 165 |
% \end{macro} |
| 166 |
% \end{macro} |
| 167 |
% |
| 168 |
% \begin{macro}[EXP]{\@@_compare_back:ww, \@@_bcmp:ww, \@@_compare_back_any:ww, \@@_compare_nan:w} |
| 169 |
% \begin{quote} |
| 170 |
% \cs{@@_compare_back_any:ww} \meta{y} \cs{@@_sep:} \meta{x} \cs{@@_sep:} |
| 171 |
% \end{quote} |
| 172 |
% Expands (in the same way as \cs{int_eval:n}) to $-1$ if $x<y$, $0$ |
| 173 |
% if $x=y$, $1$ if $x>y$, and $2$ otherwise (denoted as $x?y$). If |
| 174 |
% either operand is \texttt{nan}, stop the comparison with |
| 175 |
% \cs{@@_compare_nan:w} returning $2$. If $x$ is negative, swap the |
| 176 |
% outputs $1$ and $-1$ (i.e., $>$ and $<$); we can henceforth |
| 177 |
% assume that $x\geq 0$. If $y\geq 0$, and they have the same type, |
| 178 |
% either they are normal and we compare them with |
| 179 |
% \cs{@@_compare_npos:nwnw}, or they are equal. If $y\geq 0$, but of |
| 180 |
% a different type, the highest type is a larger number. Finally, if |
| 181 |
% $y\leq 0$, then $x>y$, unless both are zero. |
| 182 |
% \begin{macrocode} |
| 183 |
\cs_new:Npn \@@_compare_back:ww #1#2\@@_sep: #3#4\@@_sep: |
| 184 |
{ |
| 185 |
\cs:w |
| 186 |
@@ |
| 187 |
\@@_type_from_scan:N #1 |
| 188 |
_bcmp |
| 189 |
\@@_type_from_scan:N #3 |
| 190 |
:ww |
| 191 |
\cs_end: |
| 192 |
#1#2\@@_sep: #3#4\@@_sep: |
| 193 |
} |
| 194 |
\cs_new:Npn \@@_compare_back_any:ww #1#2\@@_sep: #3 |
| 195 |
{ |
| 196 |
\@@_if_type_fp:NTwFw |
| 197 |
#1 { \@@_if_type_fp:NTwFw #3 \use_i:nn \s_@@ \use_ii:nn \s_@@_stop } |
| 198 |
\s_@@ \use_ii:nn \s_@@_stop |
| 199 |
\@@_compare_back:ww |
| 200 |
{ |
| 201 |
\cs:w |
| 202 |
@@ |
| 203 |
\@@_type_from_scan:N #1 |
| 204 |
_compare_back |
| 205 |
\@@_type_from_scan:N #3 |
| 206 |
:ww |
| 207 |
\cs_end: |
| 208 |
} |
| 209 |
#1#2 \@@_sep: #3 |
| 210 |
} |
| 211 |
\cs_new:Npn \@@_bcmp:ww |
| 212 |
\s_@@ \@@_chk:w #1 #2 #3\@@_sep: |
| 213 |
\s_@@ \@@_chk:w #4 #5 #6\@@_sep: |
| 214 |
{ |
| 215 |
\int_value:w |
| 216 |
\if_meaning:w 3 #1 \exp_after:wN \@@_compare_nan:w \fi: |
| 217 |
\if_meaning:w 3 #4 \exp_after:wN \@@_compare_nan:w \fi: |
| 218 |
\if_meaning:w 2 #5 - \fi: |
| 219 |
\if_meaning:w #2 #5 |
| 220 |
\if_meaning:w #1 #4 |
| 221 |
\if_meaning:w 1 #1 |
| 222 |
\@@_compare_npos:nwnw #6\@@_sep: #3\@@_sep: |
| 223 |
\else: |
| 224 |
0 |
| 225 |
\fi: |
| 226 |
\else: |
| 227 |
\if_int_compare:w #4 < #1 - \fi: 1 |
| 228 |
\fi: |
| 229 |
\else: |
| 230 |
\if_int_compare:w #1#4 = \c_zero_int |
| 231 |
0 |
| 232 |
\else: |
| 233 |
1 |
| 234 |
\fi: |
| 235 |
\fi: |
| 236 |
\exp_stop_f: |
| 237 |
} |
| 238 |
\cs_new:Npn \@@_compare_nan:w #1 \fi: \exp_stop_f: { 2 \exp_stop_f: } |
| 239 |
% \end{macrocode} |
| 240 |
% \end{macro} |
| 241 |
% |
| 242 |
% \begin{macro}[EXP]{\@@_compare_back_tuple:ww, \@@_tuple_compare_back:ww, \@@_tuple_compare_back_tuple:ww} |
| 243 |
% \begin{macro}[EXP]{\@@_tuple_compare_back_loop:w} |
| 244 |
% Tuple and floating point numbers are not comparable so return $2$ in |
| 245 |
% mixed cases or when tuples have a different number of items. |
| 246 |
% Otherwise compare pairs of items with \cs{@@_compare_back_any:ww} |
| 247 |
% and if any don't match return~$2$ (as \cs{int_value:w} |02| |
| 248 |
% \cs{exp_stop_f:}). |
| 249 |
% \begin{macrocode} |
| 250 |
\cs_new:Npn \@@_compare_back_tuple:ww #1\@@_sep: #2\@@_sep: { 2 } |
| 251 |
\cs_new:Npn \@@_tuple_compare_back:ww #1\@@_sep: #2\@@_sep: { 2 } |
| 252 |
\cs_new:Npn \@@_tuple_compare_back_tuple:ww |
| 253 |
\s_@@_tuple \@@_tuple_chk:w #1\@@_sep: |
| 254 |
\s_@@_tuple \@@_tuple_chk:w #2\@@_sep: |
| 255 |
{ |
| 256 |
\int_compare:nNnTF { \@@_array_count:n {#1} } = |
| 257 |
{ \@@_array_count:n {#2} } |
| 258 |
{ |
| 259 |
\int_value:w 0 |
| 260 |
\@@_tuple_compare_back_loop:w |
| 261 |
#1 { \s_@@ \prg_break: } \@@_sep: @ |
| 262 |
#2 { \s_@@ \prg_break: } \@@_sep: |
| 263 |
\prg_break_point: |
| 264 |
\exp_stop_f: |
| 265 |
} |
| 266 |
{ 2 } |
| 267 |
} |
| 268 |
\cs_new:Npn \@@_tuple_compare_back_loop:w #1#2 \@@_sep: #3 @ #4#5 \@@_sep: |
| 269 |
{ |
| 270 |
\use_none:n #1 |
| 271 |
\use_none:n #4 |
| 272 |
\if_int_compare:w |
| 273 |
\@@_compare_back_any:ww #1 #2 \@@_sep: #4 #5 \@@_sep: = \c_zero_int |
| 274 |
\else: |
| 275 |
2 \exp_after:wN \prg_break: |
| 276 |
\fi: |
| 277 |
\@@_tuple_compare_back_loop:w #3 @ |
| 278 |
} |
| 279 |
% \end{macrocode} |
| 280 |
% \end{macro} |
| 281 |
% \end{macro} |
| 282 |
% |
| 283 |
% \begin{macro}[EXP]{\@@_compare_npos:nwnw} |
| 284 |
% \begin{macro}[EXP]{\@@_compare_significand:nnnnnnnn} |
| 285 |
% \begin{quote} |
| 286 |
% \cs{@@_compare_npos:nwnw} |
| 287 |
% \Arg{expo_1} \meta{body_1} \cs{@@_sep:} |
| 288 |
% \Arg{expo_2} \meta{body_2} \cs{@@_sep:} |
| 289 |
% \end{quote} |
| 290 |
% Within an \cs{int_value:w} \ldots{} \cs{exp_stop_f:} construction, |
| 291 |
% this expands to $0$ if the two numbers are equal, $-1$ if the first |
| 292 |
% is smaller, and $1$ if the first is bigger. First compare the |
| 293 |
% exponents: the larger one denotes the larger number. If they are |
| 294 |
% equal, we must compare significands. If both the first $8$ digits and |
| 295 |
% the next $8$ digits coincide, the numbers are equal. If only the |
| 296 |
% first $8$ digits coincide, the next $8$ decide. Otherwise, the |
| 297 |
% first $8$ digits are compared. |
| 298 |
% \begin{macrocode} |
| 299 |
\cs_new:Npn \@@_compare_npos:nwnw #1#2\@@_sep: #3#4\@@_sep: |
| 300 |
{ |
| 301 |
\if_int_compare:w #1 = #3 \exp_stop_f: |
| 302 |
\@@_compare_significand:nnnnnnnn #2 #4 |
| 303 |
\else: |
| 304 |
\if_int_compare:w #1 < #3 - \fi: 1 |
| 305 |
\fi: |
| 306 |
} |
| 307 |
\cs_new:Npn \@@_compare_significand:nnnnnnnn #1#2#3#4#5#6#7#8 |
| 308 |
{ |
| 309 |
\if_int_compare:w #1#2 = #5#6 \exp_stop_f: |
| 310 |
\if_int_compare:w #3#4 = #7#8 \exp_stop_f: |
| 311 |
0 |
| 312 |
\else: |
| 313 |
\if_int_compare:w #3#4 < #7#8 - \fi: 1 |
| 314 |
\fi: |
| 315 |
\else: |
| 316 |
\if_int_compare:w #1#2 < #5#6 - \fi: 1 |
| 317 |
\fi: |
| 318 |
} |
| 319 |
% \end{macrocode} |
| 320 |
% \end{macro} |
| 321 |
% \end{macro} |
| 322 |
% |
| 323 |
% \subsection{Floating point expression loops} |
| 324 |
% |
| 325 |
% \begin{macro}[rEXP] |
| 326 |
% { |
| 327 |
% \fp_do_until:nn, \fp_do_while:nn, |
| 328 |
% \fp_until_do:nn, \fp_while_do:nn |
| 329 |
% } |
| 330 |
% These are quite easy given the above functions. The |do_until| and |
| 331 |
% |do_while| versions execute the body, then test. The |until_do| and |
| 332 |
% |while_do| do it the other way round. |
| 333 |
% \begin{macrocode} |
| 334 |
\cs_new:Npn \fp_do_until:nn #1#2 |
| 335 |
{ |
| 336 |
#2 |
| 337 |
\fp_compare:nF {#1} |
| 338 |
{ \fp_do_until:nn {#1} {#2} } |
| 339 |
} |
| 340 |
\cs_new:Npn \fp_do_while:nn #1#2 |
| 341 |
{ |
| 342 |
#2 |
| 343 |
\fp_compare:nT {#1} |
| 344 |
{ \fp_do_while:nn {#1} {#2} } |
| 345 |
} |
| 346 |
\cs_new:Npn \fp_until_do:nn #1#2 |
| 347 |
{ |
| 348 |
\fp_compare:nF {#1} |
| 349 |
{ |
| 350 |
#2 |
| 351 |
\fp_until_do:nn {#1} {#2} |
| 352 |
} |
| 353 |
} |
| 354 |
\cs_new:Npn \fp_while_do:nn #1#2 |
| 355 |
{ |
| 356 |
\fp_compare:nT {#1} |
| 357 |
{ |
| 358 |
#2 |
| 359 |
\fp_while_do:nn {#1} {#2} |
| 360 |
} |
| 361 |
} |
| 362 |
% \end{macrocode} |
| 363 |
% \end{macro} |
| 364 |
% |
| 365 |
% \begin{macro}[rEXP] |
| 366 |
% { |
| 367 |
% \fp_do_until:nNnn, \fp_do_while:nNnn, |
| 368 |
% \fp_until_do:nNnn, \fp_while_do:nNnn |
| 369 |
% } |
| 370 |
% As above but not using the |nNn| syntax. |
| 371 |
% \begin{macrocode} |
| 372 |
\cs_new:Npn \fp_do_until:nNnn #1#2#3#4 |
| 373 |
{ |
| 374 |
#4 |
| 375 |
\fp_compare:nNnF {#1} #2 {#3} |
| 376 |
{ \fp_do_until:nNnn {#1} #2 {#3} {#4} } |
| 377 |
} |
| 378 |
\cs_new:Npn \fp_do_while:nNnn #1#2#3#4 |
| 379 |
{ |
| 380 |
#4 |
| 381 |
\fp_compare:nNnT {#1} #2 {#3} |
| 382 |
{ \fp_do_while:nNnn {#1} #2 {#3} {#4} } |
| 383 |
} |
| 384 |
\cs_new:Npn \fp_until_do:nNnn #1#2#3#4 |
| 385 |
{ |
| 386 |
\fp_compare:nNnF {#1} #2 {#3} |
| 387 |
{ |
| 388 |
#4 |
| 389 |
\fp_until_do:nNnn {#1} #2 {#3} {#4} |
| 390 |
} |
| 391 |
} |
| 392 |
\cs_new:Npn \fp_while_do:nNnn #1#2#3#4 |
| 393 |
{ |
| 394 |
\fp_compare:nNnT {#1} #2 {#3} |
| 395 |
{ |
| 396 |
#4 |
| 397 |
\fp_while_do:nNnn {#1} #2 {#3} {#4} |
| 398 |
} |
| 399 |
} |
| 400 |
% \end{macrocode} |
| 401 |
% \end{macro} |
| 402 |
% |
| 403 |
% \begin{macro}[rEXP]{\fp_step_function:nnnN, \fp_step_function:nnnc} |
| 404 |
% \begin{macro}[rEXP]{\@@_step:wwwN, \@@_step_fp:wwwN} |
| 405 |
% \begin{macro}[rEXP]{\@@_step:NnnnnN, \@@_step:NfnnnN} |
| 406 |
% The approach here is somewhat similar to |
| 407 |
% \cs{int_step_function:nnnN}. There are two subtleties: we use the |
| 408 |
% internal parser \cs{@@_parse:n} to avoid converting back and forth |
| 409 |
% from the internal representation; and (due to rounding) even a |
| 410 |
% non-zero step does not guarantee that the loop counter increases. |
| 411 |
% \begin{macrocode} |
| 412 |
\cs_new:Npn \fp_step_function:nnnN #1#2#3 |
| 413 |
{ |
| 414 |
\exp_after:wN \@@_step:wwwN |
| 415 |
\exp:w \exp_end_continue_f:w \@@_parse_o:n {#1} |
| 416 |
\exp:w \exp_end_continue_f:w \@@_parse_o:n {#2} |
| 417 |
\exp:w \exp_end_continue_f:w \@@_parse:n {#3} |
| 418 |
} |
| 419 |
\cs_generate_variant:Nn \fp_step_function:nnnN { nnnc } |
| 420 |
% \end{macrocode} |
| 421 |
% Only floating point numbers (not tuples) are allowed arguments. |
| 422 |
% Only \enquote{normal} floating points (not $\pm 0$, |
| 423 |
% $\pm\texttt{inf}$, \texttt{nan}) can be used as step; if positive, |
| 424 |
% call \cs{@@_step:NnnnnN} with argument |>| otherwise~|<|. This |
| 425 |
% function has one more argument than its integer counterpart, namely |
| 426 |
% the previous value, to catch the case where the loop has made no |
| 427 |
% progress. Conversion to decimal is done just before calling the |
| 428 |
% user's function. |
| 429 |
% \begin{macrocode} |
| 430 |
\cs_new:Npn \@@_step:wwwN #1#2\@@_sep: #3#4\@@_sep: #5#6\@@_sep: #7 |
| 431 |
{ |
| 432 |
\@@_if_type_fp:NTwFw #1 { } \s_@@ \prg_break: \s_@@_stop |
| 433 |
\@@_if_type_fp:NTwFw #3 { } \s_@@ \prg_break: \s_@@_stop |
| 434 |
\@@_if_type_fp:NTwFw #5 { } \s_@@ \prg_break: \s_@@_stop |
| 435 |
\use_i:nnnn |
| 436 |
{ \@@_step_fp:wwwN #1#2\@@_sep: #3#4\@@_sep: #5#6\@@_sep: #7 } |
| 437 |
\prg_break_point: |
| 438 |
\use:n |
| 439 |
{ |
| 440 |
\@@_error:nfff { step-tuple } { \fp_to_tl:n { #1#2 \@@_sep: } } |
| 441 |
{ \fp_to_tl:n { #3#4 \@@_sep: } } { \fp_to_tl:n { #5#6 \@@_sep: } } |
| 442 |
} |
| 443 |
} |
| 444 |
\cs_new:Npn \@@_step_fp:wwwN |
| 445 |
#1 \@@_sep: \s_@@ \@@_chk:w #2#3#4 \@@_sep: #5\@@_sep: #6 |
| 446 |
{ |
| 447 |
\token_if_eq_meaning:NNTF #2 1 |
| 448 |
{ |
| 449 |
\token_if_eq_meaning:NNTF #3 0 |
| 450 |
{ \@@_step:NnnnnN > } |
| 451 |
{ \@@_step:NnnnnN < } |
| 452 |
} |
| 453 |
{ |
| 454 |
\token_if_eq_meaning:NNTF #2 0 |
| 455 |
{ |
| 456 |
\msg_expandable_error:nnn { kernel } |
| 457 |
{ zero-step } {#6} |
| 458 |
} |
| 459 |
{ |
| 460 |
\@@_error:nnfn { bad-step } { } |
| 461 |
{ \fp_to_tl:n { \s_@@ \@@_chk:w #2#3#4 \@@_sep: } } {#6} |
| 462 |
} |
| 463 |
\use_none:nnnnn |
| 464 |
} |
| 465 |
{ #1 \@@_sep: } |
| 466 |
{ \c_nan_fp } |
| 467 |
{ \s_@@ \@@_chk:w #2#3#4 \@@_sep: } |
| 468 |
{ #5 \@@_sep: } |
| 469 |
#6 |
| 470 |
} |
| 471 |
\cs_new:Npn \@@_step:NnnnnN #1#2#3#4#5#6 |
| 472 |
{ |
| 473 |
\fp_compare:nNnTF {#2} = {#3} |
| 474 |
{ |
| 475 |
\@@_error:nffn { tiny-step } |
| 476 |
{ \fp_to_tl:n {#3} } { \fp_to_tl:n {#4} } {#6} |
| 477 |
} |
| 478 |
{ |
| 479 |
\fp_compare:nNnF {#2} #1 {#5} |
| 480 |
{ |
| 481 |
\exp_args:Nf #6 { \@@_to_decimal_dispatch:w #2 } |
| 482 |
\@@_step:NfnnnN |
| 483 |
#1 { \@@_parse:n { #2 + #4 } } {#2} {#4} {#5} #6 |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
\cs_generate_variant:Nn \@@_step:NnnnnN { Nf } |
| 488 |
% \end{macrocode} |
| 489 |
% \end{macro} |
| 490 |
% \end{macro} |
| 491 |
% \end{macro} |
| 492 |
% |
| 493 |
% \begin{macro}{\fp_step_inline:nnnn, \fp_step_variable:nnnNn} |
| 494 |
% \begin{macro}{\@@_step:NNnnnn} |
| 495 |
% As for \cs{int_step_inline:nnnn}, create a global function and apply it, |
| 496 |
% following up with a break point. |
| 497 |
% \begin{macrocode} |
| 498 |
\cs_new_protected:Npn \fp_step_inline:nnnn |
| 499 |
{ |
| 500 |
\int_gincr:N \g__kernel_prg_map_int |
| 501 |
\exp_args:NNc \@@_step:NNnnnn |
| 502 |
\cs_gset_protected:Npn |
| 503 |
{ @@_map_ \int_use:N \g__kernel_prg_map_int :w } |
| 504 |
} |
| 505 |
\cs_new_protected:Npn \fp_step_variable:nnnNn #1#2#3#4#5 |
| 506 |
{ |
| 507 |
\int_gincr:N \g__kernel_prg_map_int |
| 508 |
\exp_args:NNc \@@_step:NNnnnn |
| 509 |
\cs_gset_protected:Npe |
| 510 |
{ @@_map_ \int_use:N \g__kernel_prg_map_int :w } |
| 511 |
{#1} {#2} {#3} |
| 512 |
{ |
| 513 |
\tl_set:Nn \exp_not:N #4 {##1} |
| 514 |
\exp_not:n {#5} |
| 515 |
} |
| 516 |
} |
| 517 |
\cs_new_protected:Npn \@@_step:NNnnnn #1#2#3#4#5#6 |
| 518 |
{ |
| 519 |
#1 #2 ##1 {#6} |
| 520 |
\fp_step_function:nnnN {#3} {#4} {#5} #2 |
| 521 |
\prg_break_point:Nn \scan_stop: { \int_gdecr:N \g__kernel_prg_map_int } |
| 522 |
} |
| 523 |
% \end{macrocode} |
| 524 |
% \end{macro} |
| 525 |
% \end{macro} |
| 526 |
% |
| 527 |
% \begin{macrocode} |
| 528 |
\msg_new:nnn { fp } { step-tuple } |
| 529 |
{ Tuple~argument~in~fp_step_...~{#1}{#2}{#3}. } |
| 530 |
\msg_new:nnn { fp } { bad-step } |
| 531 |
{ Invalid~step~size~#2~for~function~#3. } |
| 532 |
\msg_new:nnn { fp } { tiny-step } |
| 533 |
{ Tiny~step~size~(#1+#2=#1)~for~function~#3. } |
| 534 |
% \end{macrocode} |
| 535 |
% |
| 536 |
% \subsection{Extrema} |
| 537 |
% |
| 538 |
% \begin{macro}[EXP]{\@@_minmax_o:Nw, \@@_minmax_aux_o:Nw} |
| 539 |
% First check all operands are floating point numbers. |
| 540 |
% The argument~|#1| is $2$~to find the maximum of an array~|#2| of |
| 541 |
% floating point numbers, and $0$~to find the minimum. We read |
| 542 |
% numbers sequentially, keeping track of the largest (smallest) number |
| 543 |
% found so far. If numbers are equal (for instance~$\pm0$), the first |
| 544 |
% is kept. We append $-\infty$ ($\infty$), for the case of an empty |
| 545 |
% array. Since no number is smaller (larger) than that, this |
| 546 |
% additional item only affects the maximum (minimum) in the case of |
| 547 |
% |max()| and |min()| with no argument. The weird |
| 548 |
% fp-like trailing marker breaks the loop correctly: see the precise |
| 549 |
% definition of \cs{@@_minmax_loop:Nww}. |
| 550 |
% \begin{macrocode} |
| 551 |
\cs_new:Npn \@@_minmax_o:Nw #1 |
| 552 |
{ |
| 553 |
\@@_parse_function_all_fp_o:fnw |
| 554 |
{ \token_if_eq_meaning:NNTF 0 #1 { min } { max } } |
| 555 |
{ \@@_minmax_aux_o:Nw #1 } |
| 556 |
} |
| 557 |
\cs_new:Npn \@@_minmax_aux_o:Nw #1#2 @ |
| 558 |
{ |
| 559 |
\if_meaning:w 0 #1 |
| 560 |
\exp_after:wN \@@_minmax_loop:Nww \exp_after:wN + |
| 561 |
\else: |
| 562 |
\exp_after:wN \@@_minmax_loop:Nww \exp_after:wN - |
| 563 |
\fi: |
| 564 |
#2 |
| 565 |
\s_@@ \@@_chk:w 2 #1 \s_@@_exact \@@_sep: |
| 566 |
\s_@@ \@@_chk:w { 3 \@@_minmax_break_o:w } \@@_sep: |
| 567 |
} |
| 568 |
% \end{macrocode} |
| 569 |
% \end{macro} |
| 570 |
% |
| 571 |
% \begin{macro}[EXP]{\@@_minmax_loop:Nww} |
| 572 |
% The first argument is $-$ or $+$ to denote the case where the |
| 573 |
% currently largest (smallest) number found (first floating point |
| 574 |
% argument) should be replaced by the new number (second floating |
| 575 |
% point argument). If the new number is \texttt{nan}, keep that as |
| 576 |
% the extremum, unless that extremum is already a \texttt{nan}. |
| 577 |
% Otherwise, compare the two numbers. If the new number is larger (in |
| 578 |
% the case of |max|) or smaller (in the case of |min|), the test |
| 579 |
% yields \texttt{true}, and we keep the second number as a new |
| 580 |
% maximum; otherwise we keep the first number. Then loop. |
| 581 |
% \begin{macrocode} |
| 582 |
\cs_new:Npn \@@_minmax_loop:Nww |
| 583 |
#1 \s_@@ \@@_chk:w #2#3\@@_sep: \s_@@ \@@_chk:w #4#5\@@_sep: |
| 584 |
{ |
| 585 |
\if_meaning:w 3 #4 |
| 586 |
\if_meaning:w 3 #2 |
| 587 |
\@@_minmax_auxi:ww |
| 588 |
\else: |
| 589 |
\@@_minmax_auxii:ww |
| 590 |
\fi: |
| 591 |
\else: |
| 592 |
\if_int_compare:w |
| 593 |
\@@_compare_back:ww |
| 594 |
\s_@@ \@@_chk:w #4#5\@@_sep: |
| 595 |
\s_@@ \@@_chk:w #2#3\@@_sep: |
| 596 |
= #1 1 \exp_stop_f: |
| 597 |
\@@_minmax_auxii:ww |
| 598 |
\else: |
| 599 |
\@@_minmax_auxi:ww |
| 600 |
\fi: |
| 601 |
\fi: |
| 602 |
\@@_minmax_loop:Nww #1 |
| 603 |
\s_@@ \@@_chk:w #2#3\@@_sep: |
| 604 |
\s_@@ \@@_chk:w #4#5\@@_sep: |
| 605 |
} |
| 606 |
% \end{macrocode} |
| 607 |
% \end{macro} |
| 608 |
% |
| 609 |
% \begin{macro}[EXP]{\@@_minmax_auxi:ww, \@@_minmax_auxii:ww} |
| 610 |
% Keep the first/second number, and remove the other. |
| 611 |
% \begin{macrocode} |
| 612 |
\cs_new:Npn \@@_minmax_auxi:ww |
| 613 |
#1 \fi: \fi: #2 \s_@@ #3 \@@_sep: \s_@@ #4\@@_sep: |
| 614 |
{ \fi: \fi: #2 \s_@@ #3 \@@_sep: } |
| 615 |
\cs_new:Npn \@@_minmax_auxii:ww #1 \fi: \fi: #2 \s_@@ #3 \@@_sep: |
| 616 |
{ \fi: \fi: #2 } |
| 617 |
% \end{macrocode} |
| 618 |
% \end{macro} |
| 619 |
% |
| 620 |
% \begin{macro}[EXP]{\@@_minmax_break_o:w} |
| 621 |
% This function is called from within an \cs{if_meaning:w} test. Skip |
| 622 |
% to the end of the tests, close the current test with \cs{fi:}, clean |
| 623 |
% up, and return the appropriate number with one post-expansion. |
| 624 |
% \begin{macrocode} |
| 625 |
\cs_new:Npn \@@_minmax_break_o:w #1 \fi: \fi: #2 \s_@@ #3\@@_sep: #4\@@_sep: |
| 626 |
{ \fi: \@@_exp_after_o:w \s_@@ #3\@@_sep: } |
| 627 |
% \end{macrocode} |
| 628 |
% \end{macro} |
| 629 |
% |
| 630 |
% \subsection{Boolean operations} |
| 631 |
% |
| 632 |
% \begin{macro}[EXP]{\@@_not_o:w, \@@_tuple_not_o:w} |
| 633 |
% Return \texttt{true} or \texttt{false}, with two expansions, one to |
| 634 |
% exit the conditional, and one to please \pkg{l3fp-parse}. The first |
| 635 |
% argument is provided by \pkg{l3fp-parse} and is ignored. |
| 636 |
% \begin{macrocode} |
| 637 |
\cs_new:Npn \@@_not_o:w #1 \s_@@ \@@_chk:w #2#3\@@_sep: @ |
| 638 |
{ |
| 639 |
\if_meaning:w 0 #2 |
| 640 |
\exp_after:wN \exp_after:wN \exp_after:wN \c_one_fp |
| 641 |
\else: |
| 642 |
\exp_after:wN \exp_after:wN \exp_after:wN \c_zero_fp |
| 643 |
\fi: |
| 644 |
} |
| 645 |
\cs_new:Npn \@@_tuple_not_o:w #1 @ { \exp_after:wN \c_zero_fp } |
| 646 |
% \end{macrocode} |
| 647 |
% \end{macro} |
| 648 |
% |
| 649 |
% \begin{macro}[EXP]{\@@_&_o:ww, \@@_tuple_&_o:ww, \@@_&_tuple_o:ww, \@@_tuple_&_tuple_o:ww} |
| 650 |
% \begin{macro}[EXP]{\@@_|_o:ww, \@@_tuple_|_o:ww, \@@_|_tuple_o:ww, \@@_tuple_|_tuple_o:ww} |
| 651 |
% \begin{macro}[EXP]{\@@_and_return:wNw} |
| 652 |
% For \texttt{and}, if the first number is zero, return it (with the |
| 653 |
% same sign). Otherwise, return the second one. For \texttt{or}, the |
| 654 |
% logic is reversed: if the first number is non-zero, return it, |
| 655 |
% otherwise return the second number: we achieve that by hi-jacking |
| 656 |
% \cs{@@_&_o:ww}, inserting an extra argument, \cs{else:}, before |
| 657 |
% \cs{s_@@}. In all cases, expand after the floating point number. |
| 658 |
% \begin{macrocode} |
| 659 |
\group_begin: |
| 660 |
\char_set_catcode_letter:N & |
| 661 |
\char_set_catcode_letter:N | |
| 662 |
\cs_new:Npn \@@_&_o:ww #1 \s_@@ \@@_chk:w #2#3\@@_sep: |
| 663 |
{ |
| 664 |
\if_meaning:w 0 #2 #1 |
| 665 |
\@@_and_return:wNw \s_@@ \@@_chk:w #2#3\@@_sep: |
| 666 |
\fi: |
| 667 |
\@@_exp_after_o:w |
| 668 |
} |
| 669 |
\cs_new:Npn \@@_&_tuple_o:ww #1 \s_@@ \@@_chk:w #2#3\@@_sep: |
| 670 |
{ |
| 671 |
\if_meaning:w 0 #2 #1 |
| 672 |
\@@_and_return:wNw \s_@@ \@@_chk:w #2#3\@@_sep: |
| 673 |
\fi: |
| 674 |
\@@_exp_after_tuple_o:w |
| 675 |
} |
| 676 |
\cs_new:Npn \@@_tuple_&_o:ww #1\@@_sep: { \@@_exp_after_o:w } |
| 677 |
\cs_new:Npn \@@_tuple_&_tuple_o:ww #1\@@_sep: { \@@_exp_after_tuple_o:w } |
| 678 |
\cs_new:Npn \@@_|_o:ww { \@@_&_o:ww \else: } |
| 679 |
\cs_new:Npn \@@_|_tuple_o:ww { \@@_&_tuple_o:ww \else: } |
| 680 |
\cs_new:Npn \@@_tuple_|_o:ww #1\@@_sep: #2\@@_sep: |
| 681 |
{ \@@_exp_after_tuple_o:w #1\@@_sep: } |
| 682 |
\cs_new:Npn \@@_tuple_|_tuple_o:ww #1\@@_sep: #2\@@_sep: |
| 683 |
{ \@@_exp_after_tuple_o:w #1\@@_sep: } |
| 684 |
\group_end: |
| 685 |
\cs_new:Npn \@@_and_return:wNw #1\@@_sep: \fi: #2\@@_sep: |
| 686 |
{ \fi: \@@_exp_after_o:w #1\@@_sep: } |
| 687 |
% \end{macrocode} |
| 688 |
% \end{macro} |
| 689 |
% \end{macro} |
| 690 |
% \end{macro} |
| 691 |
% |
| 692 |
% \subsection{Ternary operator} |
| 693 |
% |
| 694 |
% \begin{macro}[EXP] |
| 695 |
% {\@@_ternary:NwwN, \@@_ternary_auxi:NwwN, \@@_ternary_auxii:NwwN} |
| 696 |
% The first function receives the test and the true branch of the |?:| |
| 697 |
% ternary operator. It calls \cs{@@_ternary_auxii:NwwN} if the test |
| 698 |
% branch is a floating point number $\pm 0$, and otherwise calls |
| 699 |
% \cs{@@_ternary_auxi:NwwN}. These functions select one of their two |
| 700 |
% arguments. |
| 701 |
% \begin{macrocode} |
| 702 |
\cs_new:Npn \@@_ternary:NwwN #1 #2#3@ #4@ #5 |
| 703 |
{ |
| 704 |
\if_meaning:w \@@_parse_infix_::N #5 |
| 705 |
\if_charcode:w 0 |
| 706 |
\@@_if_type_fp:NTwFw |
| 707 |
#2 { \use_i:nn \@@_use_i_delimit_by_s_stop:nw #3 \s_@@_stop } |
| 708 |
\s_@@ 1 \s_@@_stop |
| 709 |
\exp_after:wN \exp_after:wN \exp_after:wN \@@_ternary_auxii:NwwN |
| 710 |
\else: |
| 711 |
\exp_after:wN \exp_after:wN \exp_after:wN \@@_ternary_auxi:NwwN |
| 712 |
\fi: |
| 713 |
\exp_after:wN #1 |
| 714 |
\exp:w \exp_end_continue_f:w |
| 715 |
\@@_exp_after_array_f:w #4 \s_@@_expr_stop |
| 716 |
\exp_after:wN @ |
| 717 |
\exp:w |
| 718 |
\@@_parse_operand:Nw \c_@@_prec_colon_int |
| 719 |
\@@_parse_expand:w |
| 720 |
\else: |
| 721 |
\msg_expandable_error:nnnn |
| 722 |
{ fp } { missing } { : } { ~for~?: } |
| 723 |
\exp_after:wN \@@_parse_continue:NwN |
| 724 |
\exp_after:wN #1 |
| 725 |
\exp:w \exp_end_continue_f:w |
| 726 |
\@@_exp_after_array_f:w #4 \s_@@_expr_stop |
| 727 |
\exp_after:wN #5 |
| 728 |
\exp_after:wN #1 |
| 729 |
\fi: |
| 730 |
} |
| 731 |
\cs_new:Npn \@@_ternary_auxi:NwwN #1#2@#3@#4 |
| 732 |
{ |
| 733 |
\exp_after:wN \@@_parse_continue:NwN |
| 734 |
\exp_after:wN #1 |
| 735 |
\exp:w \exp_end_continue_f:w |
| 736 |
\@@_exp_after_array_f:w #2 \s_@@_expr_stop |
| 737 |
#4 #1 |
| 738 |
} |
| 739 |
\cs_new:Npn \@@_ternary_auxii:NwwN #1#2@#3@#4 |
| 740 |
{ |
| 741 |
\exp_after:wN \@@_parse_continue:NwN |
| 742 |
\exp_after:wN #1 |
| 743 |
\exp:w \exp_end_continue_f:w |
| 744 |
\@@_exp_after_array_f:w #3 \s_@@_expr_stop |
| 745 |
#4 #1 |
| 746 |
} |
| 747 |
% \end{macrocode} |
| 748 |
% \end{macro} |
| 749 |
% |
| 750 |
% \begin{macrocode} |
| 751 |
%</code> |
| 752 |
% \end{macrocode} |
| 753 |
% |
| 754 |
% \end{implementation} |
| 755 |
% |
| 756 |
% \PrintChanges |
| 757 |
% |
| 758 |
% \PrintIndex |