| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3fp-traps.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-traps} module\\ |
| 35 |
% Trapping floating-point exceptions^^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-traps} implementation} |
| 55 |
% |
| 56 |
% \begin{macrocode} |
| 57 |
%<*code> |
| 58 |
% \end{macrocode} |
| 59 |
% |
| 60 |
% \begin{macrocode} |
| 61 |
%<@@=fp> |
| 62 |
% \end{macrocode} |
| 63 |
% |
| 64 |
% Exceptions should be accessed by an \texttt{n}-type argument, among |
| 65 |
% \begin{itemize} |
| 66 |
% \item \texttt{invalid_operation} |
| 67 |
% \item \texttt{division_by_zero} |
| 68 |
% \item \texttt{overflow} |
| 69 |
% \item \texttt{underflow} |
| 70 |
% \item \texttt{inexact} (actually never used). |
| 71 |
% \end{itemize} |
| 72 |
% |
| 73 |
% \subsection{Flags} |
| 74 |
% |
| 75 |
% \begin{variable}[module = fp] |
| 76 |
% { |
| 77 |
% \l_fp_invalid_operation_flag, |
| 78 |
% \l_fp_division_by_zero_flag, |
| 79 |
% \l_fp_overflow_flag, |
| 80 |
% \l_fp_underflow_flag |
| 81 |
% } |
| 82 |
% Flags to denote exceptions. |
| 83 |
% \begin{macrocode} |
| 84 |
\flag_new:N \l_fp_invalid_operation_flag |
| 85 |
\flag_new:N \l_fp_division_by_zero_flag |
| 86 |
\flag_new:N \l_fp_overflow_flag |
| 87 |
\flag_new:N \l_fp_underflow_flag |
| 88 |
% \end{macrocode} |
| 89 |
% \end{variable} |
| 90 |
% |
| 91 |
% \subsection{Traps} |
| 92 |
% |
| 93 |
% Exceptions can be trapped to obtain custom behavior. When an invalid |
| 94 |
% operation or a division by zero is trapped, the trap receives as |
| 95 |
% arguments the result as an |N|-type floating point number, the |
| 96 |
% function name (multiple letters for prefix operations, or a single |
| 97 |
% symbol for infix operations), and the operand(s). When an overflow or |
| 98 |
% underflow is trapped, the trap receives the resulting overly large or |
| 99 |
% small floating point number if it is not too big, otherwise it |
| 100 |
% receives $+\infty$. Currently, the inexact exception is entirely |
| 101 |
% ignored. |
| 102 |
% |
| 103 |
% The behavior when an exception occurs is controlled by the |
| 104 |
% definitions of the functions |
| 105 |
% \begin{itemize} |
| 106 |
% \item \cs{@@_invalid_operation:nnw}, |
| 107 |
% \item \cs{@@_invalid_operation_o:Nww}, |
| 108 |
% \item \cs{@@_invalid_operation_tl_o:ff}, |
| 109 |
% \item \cs{@@_division_by_zero_o:Nnw}, |
| 110 |
% \item \cs{@@_division_by_zero_o:NNww}, |
| 111 |
% \item \cs{@@_overflow:w}, |
| 112 |
% \item \cs{@@_underflow:w}. |
| 113 |
% \end{itemize} |
| 114 |
% Rather than changing them directly, we provide a user interface as |
| 115 |
% \cs{fp_trap:nn} \Arg{exception} \Arg{way of trapping}, where the |
| 116 |
% \meta{way of trapping} is one of \texttt{error}, \texttt{flag}, or |
| 117 |
% \texttt{none}. |
| 118 |
% |
| 119 |
% We also provide \cs{@@_invalid_operation_o:nw}, defined in terms of |
| 120 |
% \cs{@@_invalid_operation:nnw}. |
| 121 |
% |
| 122 |
% \begin{macro}{\fp_trap:nn} |
| 123 |
% \begin{macrocode} |
| 124 |
\cs_new_protected:Npn \fp_trap:nn #1#2 |
| 125 |
{ |
| 126 |
\cs_if_exist_use:cF { @@_trap_#1_set_#2: } |
| 127 |
{ |
| 128 |
\clist_if_in:nnTF |
| 129 |
{ invalid_operation , division_by_zero , overflow , underflow } |
| 130 |
{#1} |
| 131 |
{ |
| 132 |
\msg_error:nnee { fp } |
| 133 |
{ unknown-fpu-trap-type } {#1} {#2} |
| 134 |
} |
| 135 |
{ |
| 136 |
\msg_error:nne |
| 137 |
{ fp } { unknown-fpu-exception } {#1} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
% \end{macrocode} |
| 142 |
% \end{macro} |
| 143 |
% |
| 144 |
% \begin{macro} |
| 145 |
% { |
| 146 |
% \@@_trap_invalid_operation_set_error: , |
| 147 |
% \@@_trap_invalid_operation_set_flag: , |
| 148 |
% \@@_trap_invalid_operation_set_none: , |
| 149 |
% \@@_trap_invalid_operation_set:N , |
| 150 |
% } |
| 151 |
% We provide three types of trapping for invalid operations: either |
| 152 |
% produce an error and raise the relevant flag; or only raise the |
| 153 |
% flag; or don't even raise the flag. In most cases, the function |
| 154 |
% produces as a result its first argument, possibly with |
| 155 |
% post-expansion. |
| 156 |
% \begin{macrocode} |
| 157 |
\cs_new_protected:Npn \@@_trap_invalid_operation_set_error: |
| 158 |
{ \@@_trap_invalid_operation_set:N \prg_do_nothing: } |
| 159 |
\cs_new_protected:Npn \@@_trap_invalid_operation_set_flag: |
| 160 |
{ \@@_trap_invalid_operation_set:N \use_none:nnnnn } |
| 161 |
\cs_new_protected:Npn \@@_trap_invalid_operation_set_none: |
| 162 |
{ \@@_trap_invalid_operation_set:N \use_none:nnnnnnn } |
| 163 |
\cs_new_protected:Npn \@@_trap_invalid_operation_set:N #1 |
| 164 |
{ |
| 165 |
\exp_args:Nno \use:n |
| 166 |
{ \cs_set:Npn \@@_invalid_operation:nnw ##1##2##3\@@_sep: } |
| 167 |
{ |
| 168 |
#1 |
| 169 |
\@@_error:nnfn { invalid } {##2} { \fp_to_tl:n { ##3\@@_sep: } } { } |
| 170 |
\flag_ensure_raised:N \l_fp_invalid_operation_flag |
| 171 |
##1 |
| 172 |
} |
| 173 |
\exp_args:Nno \use:n |
| 174 |
{ \cs_set:Npn \@@_invalid_operation_o:Nww ##1##2\@@_sep: ##3\@@_sep: } |
| 175 |
{ |
| 176 |
#1 |
| 177 |
\@@_error:nffn { invalid-ii } |
| 178 |
{ \fp_to_tl:n { ##2\@@_sep: } } |
| 179 |
{ \fp_to_tl:n { ##3\@@_sep: } } |
| 180 |
{##1} |
| 181 |
\flag_ensure_raised:N \l_fp_invalid_operation_flag |
| 182 |
\exp_after:wN \c_nan_fp |
| 183 |
} |
| 184 |
\exp_args:Nno \use:n |
| 185 |
{ \cs_set:Npn \@@_invalid_operation_tl_o:ff ##1##2 } |
| 186 |
{ |
| 187 |
#1 |
| 188 |
\@@_error:nffn { invalid } {##1} {##2} { } |
| 189 |
\flag_ensure_raised:N \l_fp_invalid_operation_flag |
| 190 |
\exp_after:wN \c_nan_fp |
| 191 |
} |
| 192 |
} |
| 193 |
% \end{macrocode} |
| 194 |
% \end{macro} |
| 195 |
% |
| 196 |
% \begin{macro} |
| 197 |
% { |
| 198 |
% \@@_trap_division_by_zero_set_error: , |
| 199 |
% \@@_trap_division_by_zero_set_flag: , |
| 200 |
% \@@_trap_division_by_zero_set_none: , |
| 201 |
% \@@_trap_division_by_zero_set:N , |
| 202 |
% } |
| 203 |
% We provide three types of trapping for invalid operations and |
| 204 |
% division by zero: either produce an error and raise the relevant |
| 205 |
% flag; or only raise the flag; or don't even raise the flag. In all |
| 206 |
% cases, the function must produce a result, namely its first |
| 207 |
% argument, $\pm\infty$ or \nan{}. |
| 208 |
% \begin{macrocode} |
| 209 |
\cs_new_protected:Npn \@@_trap_division_by_zero_set_error: |
| 210 |
{ \@@_trap_division_by_zero_set:N \prg_do_nothing: } |
| 211 |
\cs_new_protected:Npn \@@_trap_division_by_zero_set_flag: |
| 212 |
{ \@@_trap_division_by_zero_set:N \use_none:nnnnn } |
| 213 |
\cs_new_protected:Npn \@@_trap_division_by_zero_set_none: |
| 214 |
{ \@@_trap_division_by_zero_set:N \use_none:nnnnnnn } |
| 215 |
\cs_new_protected:Npn \@@_trap_division_by_zero_set:N #1 |
| 216 |
{ |
| 217 |
\exp_args:Nno \use:n |
| 218 |
{ \cs_set:Npn \@@_division_by_zero_o:Nnw ##1##2##3\@@_sep: } |
| 219 |
{ |
| 220 |
#1 |
| 221 |
\@@_error:nnfn { zero-div } {##2} { \fp_to_tl:n { ##3\@@_sep: } } { } |
| 222 |
\flag_ensure_raised:N \l_fp_division_by_zero_flag |
| 223 |
\exp_after:wN ##1 |
| 224 |
} |
| 225 |
\exp_args:Nno \use:n |
| 226 |
{ |
| 227 |
\cs_set:Npn \@@_division_by_zero_o:NNww ##1##2##3\@@_sep: ##4\@@_sep: |
| 228 |
} |
| 229 |
{ |
| 230 |
#1 |
| 231 |
\@@_error:nffn { zero-div-ii } |
| 232 |
{ \fp_to_tl:n { ##3\@@_sep: } } |
| 233 |
{ \fp_to_tl:n { ##4\@@_sep: } } |
| 234 |
{##2} |
| 235 |
\flag_ensure_raised:N \l_fp_division_by_zero_flag |
| 236 |
\exp_after:wN ##1 |
| 237 |
} |
| 238 |
} |
| 239 |
% \end{macrocode} |
| 240 |
% \end{macro} |
| 241 |
% |
| 242 |
% \begin{macro} |
| 243 |
% { |
| 244 |
% \@@_trap_overflow_set_error: , |
| 245 |
% \@@_trap_overflow_set_flag: , |
| 246 |
% \@@_trap_overflow_set_none: , |
| 247 |
% \@@_trap_overflow_set:N , |
| 248 |
% } |
| 249 |
% \begin{macro} |
| 250 |
% { |
| 251 |
% \@@_trap_underflow_set_error: , |
| 252 |
% \@@_trap_underflow_set_flag: , |
| 253 |
% \@@_trap_underflow_set_none: , |
| 254 |
% \@@_trap_underflow_set:N , |
| 255 |
% } |
| 256 |
% \begin{macro}{\@@_trap_overflow_set:NnNn} |
| 257 |
% Just as for invalid operations and division by zero, the three |
| 258 |
% different behaviors are obtained by feeding \cs{prg_do_nothing:}, |
| 259 |
% \cs{use_none:nnnnn} or \cs{use_none:nnnnnnn} to an auxiliary, with a |
| 260 |
% further auxiliary common to overflow and underflow functions. In |
| 261 |
% most cases, the argument of the \cs{@@_overflow:w} and |
| 262 |
% \cs{@@_underflow:w} functions will be an (almost) normal number |
| 263 |
% (with an exponent outside the allowed range), and the error message |
| 264 |
% thus displays that number together with the result to which it |
| 265 |
% overflowed or underflowed. For extreme cases such as \texttt{10 ** |
| 266 |
% 1e9999}, the exponent would be too large for \TeX{}, and |
| 267 |
% \cs{@@_overflow:w} receives $\pm \infty$ (\cs{@@_underflow:w} would |
| 268 |
% receive $\pm 0$); then we cannot do better than simply say an |
| 269 |
% overflow or underflow occurred. |
| 270 |
% \begin{macrocode} |
| 271 |
\cs_new_protected:Npn \@@_trap_overflow_set_error: |
| 272 |
{ \@@_trap_overflow_set:N \prg_do_nothing: } |
| 273 |
\cs_new_protected:Npn \@@_trap_overflow_set_flag: |
| 274 |
{ \@@_trap_overflow_set:N \use_none:nnnnn } |
| 275 |
\cs_new_protected:Npn \@@_trap_overflow_set_none: |
| 276 |
{ \@@_trap_overflow_set:N \use_none:nnnnnnn } |
| 277 |
\cs_new_protected:Npn \@@_trap_overflow_set:N #1 |
| 278 |
{ \@@_trap_overflow_set:NnNn #1 { overflow } \@@_inf_fp:N { inf } } |
| 279 |
\cs_new_protected:Npn \@@_trap_underflow_set_error: |
| 280 |
{ \@@_trap_underflow_set:N \prg_do_nothing: } |
| 281 |
\cs_new_protected:Npn \@@_trap_underflow_set_flag: |
| 282 |
{ \@@_trap_underflow_set:N \use_none:nnnnn } |
| 283 |
\cs_new_protected:Npn \@@_trap_underflow_set_none: |
| 284 |
{ \@@_trap_underflow_set:N \use_none:nnnnnnn } |
| 285 |
\cs_new_protected:Npn \@@_trap_underflow_set:N #1 |
| 286 |
{ \@@_trap_overflow_set:NnNn #1 { underflow } \@@_zero_fp:N { 0 } } |
| 287 |
\cs_new_protected:Npn \@@_trap_overflow_set:NnNn #1#2#3#4 |
| 288 |
{ |
| 289 |
\exp_args:Nno \use:n |
| 290 |
{ \cs_set:cpn { @@_ #2 :w } \s_@@ \@@_chk:w ##1##2##3\@@_sep: } |
| 291 |
{ |
| 292 |
#1 |
| 293 |
\@@_error:nffn |
| 294 |
{ flow \if_meaning:w 1 ##1 -to \fi: } |
| 295 |
{ \fp_to_tl:n { \s_@@ \@@_chk:w ##1##2##3\@@_sep: } } |
| 296 |
{ \token_if_eq_meaning:NNF 0 ##2 { - } #4 } |
| 297 |
{#2} |
| 298 |
\flag_ensure_raised:c { l_fp_#2_flag } |
| 299 |
#3 ##2 |
| 300 |
} |
| 301 |
} |
| 302 |
% \end{macrocode} |
| 303 |
% \end{macro} |
| 304 |
% \end{macro} |
| 305 |
% \end{macro} |
| 306 |
% |
| 307 |
% \begin{macro}[EXP] |
| 308 |
% { |
| 309 |
% \@@_invalid_operation:nnw, \@@_invalid_operation_o:Nww, |
| 310 |
% \@@_invalid_operation_tl_o:ff, |
| 311 |
% \@@_division_by_zero_o:Nnw, \@@_division_by_zero_o:NNww, |
| 312 |
% \@@_overflow:w , \@@_underflow:w |
| 313 |
% } |
| 314 |
% Initialize the control sequences (to log properly their |
| 315 |
% existence). Then set invalid operations to trigger an error, and |
| 316 |
% division by zero, overflow, and underflow to act silently on their |
| 317 |
% flag. |
| 318 |
% \begin{macrocode} |
| 319 |
\cs_new:Npn \@@_invalid_operation:nnw #1#2#3\@@_sep: { } |
| 320 |
\cs_new:Npn \@@_invalid_operation_o:Nww #1#2\@@_sep: #3\@@_sep: { } |
| 321 |
\cs_new:Npn \@@_invalid_operation_tl_o:ff #1 #2 { } |
| 322 |
\cs_new:Npn \@@_division_by_zero_o:Nnw #1#2#3\@@_sep: { } |
| 323 |
\cs_new:Npn \@@_division_by_zero_o:NNww #1#2#3\@@_sep: #4\@@_sep: { } |
| 324 |
\cs_new:Npn \@@_overflow:w { } |
| 325 |
\cs_new:Npn \@@_underflow:w { } |
| 326 |
\fp_trap:nn { invalid_operation } { error } |
| 327 |
\fp_trap:nn { division_by_zero } { flag } |
| 328 |
\fp_trap:nn { overflow } { flag } |
| 329 |
\fp_trap:nn { underflow } { flag } |
| 330 |
% \end{macrocode} |
| 331 |
% \end{macro} |
| 332 |
% |
| 333 |
% \begin{macro}[EXP] |
| 334 |
% {\@@_invalid_operation_o:nw, \@@_invalid_operation_o:fw} |
| 335 |
% Convenient short-hands for returning \cs{c_nan_fp} for a unary or |
| 336 |
% binary operation, and expanding after. |
| 337 |
% \begin{macrocode} |
| 338 |
\cs_new:Npn \@@_invalid_operation_o:nw |
| 339 |
{ \@@_invalid_operation:nnw { \exp_after:wN \c_nan_fp } } |
| 340 |
\cs_generate_variant:Nn \@@_invalid_operation_o:nw { f } |
| 341 |
% \end{macrocode} |
| 342 |
% \end{macro} |
| 343 |
% |
| 344 |
% \subsection{Errors} |
| 345 |
% |
| 346 |
% \begin{macro}[EXP]{\@@_error:nnnn, \@@_error:nnfn, \@@_error:nffn, \@@_error:nfff} |
| 347 |
% \begin{macrocode} |
| 348 |
\cs_new:Npn \@@_error:nnnn |
| 349 |
{ \msg_expandable_error:nnnnn { fp } } |
| 350 |
\cs_generate_variant:Nn \@@_error:nnnn { nnf, nff , nfff } |
| 351 |
% \end{macrocode} |
| 352 |
% \end{macro} |
| 353 |
% |
| 354 |
% \begin{macro}[EXP]{\@@_error_num_args:nnnn, \@@_error_num_args:ffff} |
| 355 |
% \begin{macrocode} |
| 356 |
\cs_new:Npn \@@_error_num_args:nnnn #1#2#3#4 |
| 357 |
{ |
| 358 |
\int_compare:nNnTF {#2} = {#3} |
| 359 |
{ \msg_expandable_error:nnnnn { fp } { num-args-eq } {#1} {#2} {#4} } |
| 360 |
{ \msg_expandable_error:nnnnnn { fp } { num-args } {#1} {#2} {#3} {#4} } |
| 361 |
} |
| 362 |
\cs_generate_variant:Nn \@@_error_num_args:nnnn { ffff } |
| 363 |
\msg_new:nnn { fp } { num-args-eq } |
| 364 |
{ #1()~needs~#2~arguments,~got~#3. } |
| 365 |
\msg_new:nnn { fp } { num-args } |
| 366 |
{ #1()~needs~#2~to~#3~arguments,~got~#4. } |
| 367 |
% \end{macrocode} |
| 368 |
% \end{macro} |
| 369 |
% |
| 370 |
% \subsection{Messages} |
| 371 |
% |
| 372 |
% Some messages. |
| 373 |
% \begin{macrocode} |
| 374 |
\msg_new:nnnn { fp } { unknown-fpu-exception } |
| 375 |
{ |
| 376 |
The~FPU~exception~'#1'~is~not~known:~ |
| 377 |
that~trap~will~never~be~triggered. |
| 378 |
} |
| 379 |
{ |
| 380 |
The~only~exceptions~to~which~traps~can~be~attached~are \\ |
| 381 |
\iow_indent:n |
| 382 |
{ |
| 383 |
* ~ invalid_operation \\ |
| 384 |
* ~ division_by_zero \\ |
| 385 |
* ~ overflow \\ |
| 386 |
* ~ underflow |
| 387 |
} |
| 388 |
} |
| 389 |
\msg_new:nnnn { fp } { unknown-fpu-trap-type } |
| 390 |
{ The~FPU~trap~type~'#2'~is~not~known. } |
| 391 |
{ |
| 392 |
The~trap~type~must~be~one~of \\ |
| 393 |
\iow_indent:n |
| 394 |
{ |
| 395 |
* ~ error \\ |
| 396 |
* ~ flag \\ |
| 397 |
* ~ none |
| 398 |
} |
| 399 |
} |
| 400 |
\msg_new:nnn { fp } { flow } |
| 401 |
{ An ~ #3 ~ occurred. } |
| 402 |
\msg_new:nnn { fp } { flow-to } |
| 403 |
{ #1 ~ #3 ed ~ to ~ #2 . } |
| 404 |
\msg_new:nnn { fp } { zero-div } |
| 405 |
{ Division~by~zero~in~ #1 (#2) } |
| 406 |
\msg_new:nnn { fp } { zero-div-ii } |
| 407 |
{ Division~by~zero~in~ (#1) #3 (#2) } |
| 408 |
\msg_new:nnn { fp } { invalid } |
| 409 |
{ Invalid~operation~ #1 (#2) } |
| 410 |
\msg_new:nnn { fp } { invalid-ii } |
| 411 |
{ Invalid~operation~ (#1) #3 (#2) } |
| 412 |
\msg_new:nnn { fp } { unknown-type } |
| 413 |
{ Unknown~type~for~'#1' } |
| 414 |
% \end{macrocode} |
| 415 |
% |
| 416 |
% \begin{macrocode} |
| 417 |
%</code> |
| 418 |
% \end{macrocode} |
| 419 |
% |
| 420 |
% \end{implementation} |
| 421 |
% |
| 422 |
% \PrintChanges |
| 423 |
% |
| 424 |
% \PrintIndex |