| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3fp-round.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-round} module\\ Rounding floating points^^A |
| 35 |
% } |
| 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 |
% |
| 46 |
% \date{Released 2026-05-15} |
| 47 |
% |
| 48 |
% \maketitle |
| 49 |
% |
| 50 |
% \begin{documentation} |
| 51 |
% |
| 52 |
% \end{documentation} |
| 53 |
% |
| 54 |
% \begin{implementation} |
| 55 |
% |
| 56 |
% \section{\pkg{l3fp-round} implementation} |
| 57 |
% |
| 58 |
% \begin{macrocode} |
| 59 |
%<*code> |
| 60 |
% \end{macrocode} |
| 61 |
% |
| 62 |
% \begin{macrocode} |
| 63 |
%<@@=fp> |
| 64 |
% \end{macrocode} |
| 65 |
% |
| 66 |
% ^^A todo: provide an interface for rounding modes. |
| 67 |
% ^^A todo: provide a \l_@@_rounding_mode_int giving the current mode. |
| 68 |
% ^^A todo: make transcendental function obey the correct rounding mode. |
| 69 |
% ^^A todo: optimize all rounding functions for various rounding modes. |
| 70 |
% ^^A todo: reduce the number of almost identical functions. |
| 71 |
% |
| 72 |
% \begin{macro}[EXP] |
| 73 |
% { |
| 74 |
% \@@_parse_word_trunc:N, |
| 75 |
% \@@_parse_word_floor:N, |
| 76 |
% \@@_parse_word_ceil:N |
| 77 |
% } |
| 78 |
% \begin{macrocode} |
| 79 |
\cs_new:Npn \@@_parse_word_trunc:N |
| 80 |
{ \@@_parse_function:NNN \@@_round_o:Nw \@@_round_to_zero:NNN } |
| 81 |
\cs_new:Npn \@@_parse_word_floor:N |
| 82 |
{ \@@_parse_function:NNN \@@_round_o:Nw \@@_round_to_ninf:NNN } |
| 83 |
\cs_new:Npn \@@_parse_word_ceil:N |
| 84 |
{ \@@_parse_function:NNN \@@_round_o:Nw \@@_round_to_pinf:NNN } |
| 85 |
% \end{macrocode} |
| 86 |
% \end{macro} |
| 87 |
% |
| 88 |
% \begin{macro}[EXP] |
| 89 |
% { |
| 90 |
% \@@_parse_word_round:N, \@@_parse_round:Nw, |
| 91 |
% } |
| 92 |
% \begin{macrocode} |
| 93 |
\cs_new:Npn \@@_parse_word_round:N #1#2 |
| 94 |
{ |
| 95 |
\@@_parse_function:NNN |
| 96 |
\@@_round_o:Nw \@@_round_to_nearest:NNN #1 |
| 97 |
#2 |
| 98 |
} |
| 99 |
\cs_new:Npn \@@_parse_round:Nw #1 #2 \@@_round_to_nearest:NNN #3#4 |
| 100 |
{ #2 #1 #3 } |
| 101 |
% \end{macrocode} |
| 102 |
% \end{macro} |
| 103 |
% |
| 104 |
% \subsection{Rounding tools} |
| 105 |
% |
| 106 |
% \begin{variable}{\c_@@_five_int} |
| 107 |
% This is used as the half-point for which numbers are rounded |
| 108 |
% up/down. |
| 109 |
% \begin{macrocode} |
| 110 |
\int_const:Nn \c_@@_five_int { 5 } |
| 111 |
% \end{macrocode} |
| 112 |
% \end{variable} |
| 113 |
% |
| 114 |
% Floating point operations often yield a result that cannot be exactly |
| 115 |
% represented in a significand with $16$ digits. In that case, we need to |
| 116 |
% round the exact result to a representable number. The \textsc{ieee} |
| 117 |
% standard defines four rounding modes: |
| 118 |
% \begin{itemize} |
| 119 |
% \item Round to nearest: round to the representable floating point |
| 120 |
% number whose absolute difference with the exact result is the |
| 121 |
% smallest. If the exact result lies exactly at the mid-point |
| 122 |
% between two consecutive representable floating point numbers, |
| 123 |
% round to the floating point number whose last digit is even. |
| 124 |
% \item Round towards negative infinity: round to the greatest |
| 125 |
% floating point number not larger than the exact result. |
| 126 |
% \item Round towards zero: round to a floating point number with the |
| 127 |
% same sign as the exact result, with the largest absolute value not |
| 128 |
% larger than the absolute value of the exact result. |
| 129 |
% \item Round towards positive infinity: round to the least floating |
| 130 |
% point number not smaller than the exact result. |
| 131 |
% \end{itemize} |
| 132 |
% This is not fully implemented in \pkg{l3fp} yet, and transcendental |
| 133 |
% functions fall back on the \enquote{round to nearest} mode. All |
| 134 |
% rounding for basic algebra is done through the functions defined in |
| 135 |
% this module, which can be redefined to change their rounding behavior |
| 136 |
% (but there is not interface for that yet). |
| 137 |
% |
| 138 |
% The rounding tools available in this module are many variations on a |
| 139 |
% base function \cs{@@_round:NNN}, which expands to |0\exp_stop_f:| or |
| 140 |
% |1\exp_stop_f:| depending on whether the final result should be rounded up |
| 141 |
% or down. |
| 142 |
% \begin{itemize} |
| 143 |
% \item \cs{@@_round:NNN} \meta{sign} \meta{digit_1} \meta{digit_2} |
| 144 |
% can expand to |0\exp_stop_f:| or |1\exp_stop_f:|. |
| 145 |
% \item \cs{@@_round_s:NNNw} \meta{sign} \meta{digit_1} \meta{digit_2} |
| 146 |
% \meta{more digits}\cs{@@_sep:} can expand to |
| 147 |
% |0\exp_stop_f:|\cs{@@_sep:} or |1\exp_stop_f:|\cs{@@_sep:}. |
| 148 |
% \item \cs{@@_round_neg:NNN} \meta{sign} \meta{digit_1} \meta{digit_2} |
| 149 |
% can expand to |0\exp_stop_f:| or |1\exp_stop_f:|. |
| 150 |
% \end{itemize} |
| 151 |
% See implementation comments for details on the syntax. |
| 152 |
% |
| 153 |
% \begin{macro}[rEXP]{\@@_round:NNN} |
| 154 |
% \begin{macro}[rEXP] |
| 155 |
% { |
| 156 |
% \@@_round_to_nearest:NNN, |
| 157 |
% \@@_round_to_nearest_ninf:NNN, |
| 158 |
% \@@_round_to_nearest_zero:NNN, |
| 159 |
% \@@_round_to_nearest_pinf:NNN, |
| 160 |
% \@@_round_to_ninf:NNN, |
| 161 |
% \@@_round_to_zero:NNN, |
| 162 |
% \@@_round_to_pinf:NNN |
| 163 |
% } |
| 164 |
% \begin{syntax} |
| 165 |
% \cs{@@_round:NNN} \meta{final sign} \meta{digit_1} \meta{digit_2} |
| 166 |
% \end{syntax} |
| 167 |
% If rounding the number $\meta{final sign} |
| 168 |
% \meta{digit_1}.\meta{digit_2}$ to an integer rounds it towards zero |
| 169 |
% (truncates it), this function expands to |0\exp_stop_f:|, and otherwise |
| 170 |
% to |1\exp_stop_f:|. Typically used within the scope of an |
| 171 |
% \cs{@@_int_eval:w}, to add~$1$ if needed, and thereby round |
| 172 |
% correctly. The result depends on the rounding mode. |
| 173 |
% |
| 174 |
% It is very important that \meta{final sign} be the final sign of the |
| 175 |
% result. Otherwise, the result would be incorrect in the case of |
| 176 |
% rounding towards~$-\infty$ or towards~$+\infty$. Also recall that |
| 177 |
% \meta{final sign} is~$0$ for positive, and~$2$ for negative. |
| 178 |
% |
| 179 |
% By default, the functions below return |0\exp_stop_f:|, but this is |
| 180 |
% superseded by \cs{@@_round_return_one:}, which instead returns |
| 181 |
% |1\exp_stop_f:|, expanding everything and removing |0\exp_stop_f:| in the |
| 182 |
% process. In the case of rounding towards~$\pm\infty$ or |
| 183 |
% towards~$0$, this is not really useful, but it prepares us for the |
| 184 |
% \enquote{round to nearest, ties to even} mode. |
| 185 |
% |
| 186 |
% The \enquote{round to nearest} mode is the default. If the |
| 187 |
% \meta{digit_2} is larger than~$5$, then round up. If it is less |
| 188 |
% than~$5$, round down. If it is exactly $5$, then round such that |
| 189 |
% \meta{digit_1} plus the result is even. In other words, round up if |
| 190 |
% \meta{digit_1} is odd. |
| 191 |
% |
| 192 |
% The \enquote{round to nearest} mode has three variants, which differ |
| 193 |
% in how ties are rounded: down towards $-\infty$, truncated towards $0$, |
| 194 |
% or up towards $+\infty$. |
| 195 |
% \begin{macrocode} |
| 196 |
\cs_new:Npn \@@_round_return_one: |
| 197 |
{ \exp_after:wN 1 \exp_after:wN \exp_stop_f: \exp:w } |
| 198 |
\cs_new:Npn \@@_round_to_ninf:NNN #1 #2 #3 |
| 199 |
{ |
| 200 |
\if_meaning:w 2 #1 |
| 201 |
\if_int_compare:w #3 > \c_zero_int |
| 202 |
\@@_round_return_one: |
| 203 |
\fi: |
| 204 |
\fi: |
| 205 |
\c_zero_int |
| 206 |
} |
| 207 |
\cs_new:Npn \@@_round_to_zero:NNN #1 #2 #3 { \c_zero_int } |
| 208 |
\cs_new:Npn \@@_round_to_pinf:NNN #1 #2 #3 |
| 209 |
{ |
| 210 |
\if_meaning:w 0 #1 |
| 211 |
\if_int_compare:w #3 > \c_zero_int |
| 212 |
\@@_round_return_one: |
| 213 |
\fi: |
| 214 |
\fi: |
| 215 |
\c_zero_int |
| 216 |
} |
| 217 |
\cs_new:Npn \@@_round_to_nearest:NNN #1 #2 #3 |
| 218 |
{ |
| 219 |
\if_int_compare:w #3 > \c_@@_five_int |
| 220 |
\@@_round_return_one: |
| 221 |
\else: |
| 222 |
\if_meaning:w 5 #3 |
| 223 |
\if_int_odd:w #2 \exp_stop_f: |
| 224 |
\@@_round_return_one: |
| 225 |
\fi: |
| 226 |
\fi: |
| 227 |
\fi: |
| 228 |
\c_zero_int |
| 229 |
} |
| 230 |
\cs_new:Npn \@@_round_to_nearest_ninf:NNN #1 #2 #3 |
| 231 |
{ |
| 232 |
\if_int_compare:w #3 > \c_@@_five_int |
| 233 |
\@@_round_return_one: |
| 234 |
\else: |
| 235 |
\if_meaning:w 5 #3 |
| 236 |
\if_meaning:w 2 #1 |
| 237 |
\@@_round_return_one: |
| 238 |
\fi: |
| 239 |
\fi: |
| 240 |
\fi: |
| 241 |
\c_zero_int |
| 242 |
} |
| 243 |
\cs_new:Npn \@@_round_to_nearest_zero:NNN #1 #2 #3 |
| 244 |
{ |
| 245 |
\if_int_compare:w #3 > \c_@@_five_int |
| 246 |
\@@_round_return_one: |
| 247 |
\fi: |
| 248 |
\c_zero_int |
| 249 |
} |
| 250 |
\cs_new:Npn \@@_round_to_nearest_pinf:NNN #1 #2 #3 |
| 251 |
{ |
| 252 |
\if_int_compare:w #3 > \c_@@_five_int |
| 253 |
\@@_round_return_one: |
| 254 |
\else: |
| 255 |
\if_meaning:w 5 #3 |
| 256 |
\if_meaning:w 0 #1 |
| 257 |
\@@_round_return_one: |
| 258 |
\fi: |
| 259 |
\fi: |
| 260 |
\fi: |
| 261 |
\c_zero_int |
| 262 |
} |
| 263 |
\cs_new_eq:NN \@@_round:NNN \@@_round_to_nearest:NNN |
| 264 |
% \end{macrocode} |
| 265 |
% \end{macro} |
| 266 |
% \end{macro} |
| 267 |
% |
| 268 |
% \begin{macro}[EXP]{\@@_round_s:NNNw} |
| 269 |
% \begin{syntax} |
| 270 |
% \cs{@@_round_s:NNNw} \meta{final sign} \meta{digit} \meta{more digits} \cs{@@_sep:} |
| 271 |
% \end{syntax} |
| 272 |
% Similar to \cs{@@_round:NNN}, but with an extra \cs{@@_sep:}, this |
| 273 |
% function expands to |0\exp_stop_f:|\cs{@@_sep:} if rounding |
| 274 |
% $\meta{final sign}\meta{digit}.\meta{more digits}$ to an integer |
| 275 |
% truncates, and to |1\exp_stop_f:|\cs{@@_sep:} otherwise. |
| 276 |
% The \meta{more digits} part must be a digit, |
| 277 |
% followed by something that does not overflow a \cs{int_use:N} |
| 278 |
% \cs{@@_int_eval:w} construction. The only relevant information about |
| 279 |
% this piece is whether it is zero or not. |
| 280 |
% \begin{macrocode} |
| 281 |
\cs_new:Npn \@@_round_s:NNNw #1 #2 #3 #4\@@_sep: |
| 282 |
{ |
| 283 |
\exp_after:wN \@@_round:NNN |
| 284 |
\exp_after:wN #1 |
| 285 |
\exp_after:wN #2 |
| 286 |
\int_value:w \@@_int_eval:w |
| 287 |
\if_int_odd:w 0 \if_meaning:w 0 #3 1 \fi: |
| 288 |
\if_meaning:w 5 #3 1 \fi: |
| 289 |
\exp_stop_f: |
| 290 |
\if_int_compare:w \@@_int_eval:w #4 > \c_zero_int |
| 291 |
1 + |
| 292 |
\fi: |
| 293 |
\fi: |
| 294 |
#3 |
| 295 |
\@@_sep: |
| 296 |
} |
| 297 |
% \end{macrocode} |
| 298 |
% \end{macro} |
| 299 |
% |
| 300 |
% \begin{macro}[EXP]{\@@_round_digit:Nw} |
| 301 |
% \begin{syntax} |
| 302 |
% \cs{int_value:w} \cs{@@_round_digit:Nw} \meta{digit} \meta{int expr} \cs{@@_sep:} |
| 303 |
% \end{syntax} |
| 304 |
% This function should always be called within an \cs{int_value:w} |
| 305 |
% or \cs{@@_int_eval:w} expansion; it may add an extra |
| 306 |
% \cs{@@_int_eval:w}, which means that the integer or integer |
| 307 |
% expression should not be ended with a synonym of \tn{relax}, but |
| 308 |
% with a \cs{@@_sep:} for instance. |
| 309 |
% \begin{macrocode} |
| 310 |
\cs_new:Npn \@@_round_digit:Nw #1 #2\@@_sep: |
| 311 |
{ |
| 312 |
\if_int_odd:w \if_meaning:w 0 #1 1 \else: |
| 313 |
\if_meaning:w 5 #1 1 \else: |
| 314 |
0 \fi: \fi: \exp_stop_f: |
| 315 |
\if_int_compare:w \@@_int_eval:w #2 > \c_zero_int |
| 316 |
\@@_int_eval:w 1 + |
| 317 |
\fi: |
| 318 |
\fi: |
| 319 |
#1 |
| 320 |
} |
| 321 |
% \end{macrocode} |
| 322 |
% \end{macro} |
| 323 |
% |
| 324 |
% \begin{macro}[EXP]{\@@_round_neg:NNN} |
| 325 |
% \begin{macro}[EXP] |
| 326 |
% { |
| 327 |
% \@@_round_to_nearest_neg:NNN, |
| 328 |
% \@@_round_to_nearest_ninf_neg:NNN, |
| 329 |
% \@@_round_to_nearest_zero_neg:NNN, |
| 330 |
% \@@_round_to_nearest_pinf_neg:NNN, |
| 331 |
% \@@_round_to_ninf_neg:NNN, |
| 332 |
% \@@_round_to_zero_neg:NNN, |
| 333 |
% \@@_round_to_pinf_neg:NNN |
| 334 |
% } |
| 335 |
% \begin{syntax} |
| 336 |
% \cs{@@_round_neg:NNN} \meta{final sign} \meta{digit_1} \meta{digit_2} |
| 337 |
% \end{syntax} |
| 338 |
% This expands to |0\exp_stop_f:| or |1\exp_stop_f:| after doing the following |
| 339 |
% test. Starting from a number of |
| 340 |
% the form $ \meta{final sign}0.\meta{15 digits}\meta{digit_1} $ with exactly |
| 341 |
% $15$ (non-all-zero) digits before \meta{digit_1}, subtract from it |
| 342 |
% $\meta{final sign}0.0\ldots{}0\meta{digit_2}$, where there are $16$~zeros. |
| 343 |
% If in the current rounding mode the result should be rounded down, |
| 344 |
% then this function returns |1\exp_stop_f:|. Otherwise, i.e., |
| 345 |
% if the result is rounded back to the first operand, then this function |
| 346 |
% returns |0\exp_stop_f:|. |
| 347 |
% |
| 348 |
% It turns out that this negative \enquote{round to nearest} |
| 349 |
% is identical to the positive one. And this is the default mode. |
| 350 |
% \begin{macrocode} |
| 351 |
\cs_new_eq:NN \@@_round_to_ninf_neg:NNN \@@_round_to_pinf:NNN |
| 352 |
\cs_new:Npn \@@_round_to_zero_neg:NNN #1 #2 #3 |
| 353 |
{ |
| 354 |
\if_int_compare:w #3 > \c_zero_int |
| 355 |
\@@_round_return_one: |
| 356 |
\fi: |
| 357 |
\c_zero_int |
| 358 |
} |
| 359 |
\cs_new_eq:NN \@@_round_to_pinf_neg:NNN \@@_round_to_ninf:NNN |
| 360 |
\cs_new_eq:NN \@@_round_to_nearest_neg:NNN \@@_round_to_nearest:NNN |
| 361 |
\cs_new_eq:NN \@@_round_to_nearest_ninf_neg:NNN |
| 362 |
\@@_round_to_nearest_pinf:NNN |
| 363 |
\cs_new:Npn \@@_round_to_nearest_zero_neg:NNN #1 #2 #3 |
| 364 |
{ |
| 365 |
\if_int_compare:w #3 < \c_@@_five_int \else: |
| 366 |
\@@_round_return_one: |
| 367 |
\fi: |
| 368 |
\c_zero_int |
| 369 |
} |
| 370 |
\cs_new_eq:NN \@@_round_to_nearest_pinf_neg:NNN |
| 371 |
\@@_round_to_nearest_ninf:NNN |
| 372 |
\cs_new_eq:NN \@@_round_neg:NNN \@@_round_to_nearest_neg:NNN |
| 373 |
% \end{macrocode} |
| 374 |
% \end{macro} |
| 375 |
% \end{macro} |
| 376 |
% |
| 377 |
% \subsection{The \texttt{round} function} |
| 378 |
% |
| 379 |
% ^^A todo: This macro is intermingled with l3fp-parse. |
| 380 |
% ^^A todo: Add explanations. |
| 381 |
% \begin{macro}[EXP]{\@@_round_o:Nw, \@@_round_aux_o:Nw} |
| 382 |
% First check that all arguments are floating point numbers. |
| 383 |
% The |trunc|, |ceil| and |floor| functions expect one or two |
| 384 |
% arguments (the second is $0$ by default), and the |round| function |
| 385 |
% also accepts a third argument (\texttt{nan} by default), which |
| 386 |
% changes |#1| from \cs{@@_round_to_nearest:NNN} to one of its |
| 387 |
% analogues. |
| 388 |
% \begin{macrocode} |
| 389 |
\cs_new:Npn \@@_round_o:Nw #1 |
| 390 |
{ |
| 391 |
\@@_parse_function_all_fp_o:fnw |
| 392 |
{ \@@_round_name_from_cs:N #1 } |
| 393 |
{ \@@_round_aux_o:Nw #1 } |
| 394 |
} |
| 395 |
\cs_new:Npn \@@_round_aux_o:Nw #1#2 @ |
| 396 |
{ |
| 397 |
\if_case:w |
| 398 |
\@@_int_eval:w \@@_array_count:n {#2} \@@_int_eval_end: |
| 399 |
\@@_round_no_arg_o:Nw #1 \exp:w |
| 400 |
\or: \@@_round:Nwn #1 #2 {0} \exp:w |
| 401 |
\or: \@@_round:Nww #1 #2 \exp:w |
| 402 |
\else: \@@_round:Nwww #1 #2 @ \exp:w |
| 403 |
\fi: |
| 404 |
\exp_after:wN \exp_end: |
| 405 |
} |
| 406 |
% \end{macrocode} |
| 407 |
% \end{macro} |
| 408 |
% |
| 409 |
% \begin{macro}[EXP]{\@@_round_no_arg_o:Nw} |
| 410 |
% \begin{macrocode} |
| 411 |
\cs_new:Npn \@@_round_no_arg_o:Nw #1 |
| 412 |
{ |
| 413 |
\cs_if_eq:NNTF #1 \@@_round_to_nearest:NNN |
| 414 |
{ \@@_error_num_args:nnnn { round } { 1 } { 3 } { 0 } } |
| 415 |
{ |
| 416 |
\@@_error_num_args:ffff |
| 417 |
{ \@@_round_name_from_cs:N #1 } { 1 } { 2 } { 0 } |
| 418 |
} |
| 419 |
\exp_after:wN \c_nan_fp |
| 420 |
} |
| 421 |
% \end{macrocode} |
| 422 |
% \end{macro} |
| 423 |
% |
| 424 |
% \begin{macro}[EXP]{\@@_round:Nwww} |
| 425 |
% Having three arguments is only allowed for |round|, not |trunc|, |
| 426 |
% |ceil|, |floor|, so check for that case. If all is well, construct |
| 427 |
% one of \cs{@@_round_to_nearest:NNN}, \cs{@@_round_to_nearest_zero:NNN}, |
| 428 |
% \cs{@@_round_to_nearest_ninf:NNN}, \cs{@@_round_to_nearest_pinf:NNN} |
| 429 |
% and act accordingly. |
| 430 |
% \begin{macrocode} |
| 431 |
\cs_new:Npn \@@_round:Nwww |
| 432 |
#1#2 \@@_sep: #3 \@@_sep: \s_@@ \@@_chk:w #4#5#6 \@@_sep: #7 @ |
| 433 |
{ |
| 434 |
\cs_if_eq:NNTF #1 \@@_round_to_nearest:NNN |
| 435 |
{ |
| 436 |
\tl_if_empty:nTF {#7} |
| 437 |
{ |
| 438 |
\exp_args:Nc \@@_round:Nww |
| 439 |
{ |
| 440 |
@@_round_to_nearest |
| 441 |
\if_meaning:w 0 #4 _zero \else: |
| 442 |
\if_case:w #5 \exp_stop_f: _pinf \or: \else: _ninf \fi: \fi: |
| 443 |
:NNN |
| 444 |
} |
| 445 |
#2 \@@_sep: #3 \@@_sep: |
| 446 |
} |
| 447 |
{ |
| 448 |
\@@_error_num_args:ffff { round } { 1 } { 3 } |
| 449 |
{ \int_eval:n { 3 + \@@_array_count:n {#7} } } |
| 450 |
\exp_after:wN \c_nan_fp |
| 451 |
} |
| 452 |
} |
| 453 |
{ |
| 454 |
\@@_error_num_args:ffff |
| 455 |
{ \@@_round_name_from_cs:N #1 } { 1 } { 2 } |
| 456 |
{ \int_eval:n { 3 + \@@_array_count:n {#7} } } |
| 457 |
\exp_after:wN \c_nan_fp |
| 458 |
} |
| 459 |
} |
| 460 |
% \end{macrocode} |
| 461 |
% \end{macro} |
| 462 |
% |
| 463 |
% \begin{macro}[EXP]{\@@_round_name_from_cs:N} |
| 464 |
% \begin{macrocode} |
| 465 |
\cs_new:Npn \@@_round_name_from_cs:N #1 |
| 466 |
{ |
| 467 |
\cs_if_eq:NNTF #1 \@@_round_to_zero:NNN { trunc } |
| 468 |
{ |
| 469 |
\cs_if_eq:NNTF #1 \@@_round_to_ninf:NNN { floor } |
| 470 |
{ |
| 471 |
\cs_if_eq:NNTF #1 \@@_round_to_pinf:NNN { ceil } |
| 472 |
{ round } |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
% \end{macrocode} |
| 477 |
% \end{macro} |
| 478 |
% |
| 479 |
% \begin{macro}[EXP]{\@@_round:Nww, \@@_round:Nwn} |
| 480 |
% \begin{macro}[EXP] |
| 481 |
% { |
| 482 |
% \@@_round_normal:NwNNnw , |
| 483 |
% \@@_round_normal:NnnwNNnn , |
| 484 |
% \@@_round_pack:Nw , |
| 485 |
% \@@_round_normal:NNwNnn , |
| 486 |
% \@@_round_normal_end:wwNnn , |
| 487 |
% \@@_round_special:NwwNnn , |
| 488 |
% \@@_round_special_aux:Nw |
| 489 |
% } |
| 490 |
% If the number of digits to round to is an integer or infinity all is |
| 491 |
% good; if it is \texttt{nan} then just produce a \texttt{nan}; |
| 492 |
% otherwise invalid as we have something like |round(1,3.14)| where |
| 493 |
% the number of digits is not an integer. |
| 494 |
% \begin{macrocode} |
| 495 |
\cs_new:Npn \@@_round:Nww #1#2 \@@_sep: #3 \@@_sep: |
| 496 |
{ |
| 497 |
\@@_small_int:wTF #3\@@_sep: { \@@_round:Nwn #1#2\@@_sep: } |
| 498 |
{ |
| 499 |
\if:w 3 \@@_kind:w #3 \@@_sep: |
| 500 |
\exp_after:wN \use_i:nn |
| 501 |
\else: |
| 502 |
\exp_after:wN \use_ii:nn |
| 503 |
\fi: |
| 504 |
{ \exp_after:wN \c_nan_fp } |
| 505 |
{ |
| 506 |
\@@_invalid_operation_tl_o:ff |
| 507 |
{ \@@_round_name_from_cs:N #1 } |
| 508 |
{ \@@_array_to_clist:n { #2\@@_sep: #3\@@_sep: } } |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
\cs_new:Npn \@@_round:Nwn #1 \s_@@ \@@_chk:w #2#3#4\@@_sep: #5 |
| 513 |
{ |
| 514 |
\if_meaning:w 1 #2 |
| 515 |
\exp_after:wN \@@_round_normal:NwNNnw |
| 516 |
\exp_after:wN #1 |
| 517 |
\int_value:w #5 |
| 518 |
\else: |
| 519 |
\exp_after:wN \@@_exp_after_o:w |
| 520 |
\fi: |
| 521 |
\s_@@ \@@_chk:w #2#3#4\@@_sep: |
| 522 |
} |
| 523 |
\cs_new:Npn \@@_round_normal:NwNNnw #1#2 \s_@@ \@@_chk:w 1#3#4#5\@@_sep: |
| 524 |
{ |
| 525 |
\@@_decimate:nNnnnn { \c_@@_prec_int - #4 - #2 } |
| 526 |
\@@_round_normal:NnnwNNnn #5 #1 #3 {#4} {#2} |
| 527 |
} |
| 528 |
\cs_new:Npn \@@_round_normal:NnnwNNnn #1#2#3#4\@@_sep: #5#6 |
| 529 |
{ |
| 530 |
\exp_after:wN \@@_round_normal:NNwNnn |
| 531 |
\int_value:w \@@_int_eval:w |
| 532 |
\if_int_compare:w #2 > \c_zero_int |
| 533 |
1 \int_value:w #2 |
| 534 |
\exp_after:wN \@@_round_pack:Nw |
| 535 |
\int_value:w \@@_int_eval:w 1#3 + |
| 536 |
\else: |
| 537 |
\if_int_compare:w #3 > \c_zero_int |
| 538 |
1 \int_value:w #3 + |
| 539 |
\fi: |
| 540 |
\fi: |
| 541 |
\exp_after:wN #5 |
| 542 |
\exp_after:wN #6 |
| 543 |
\use_none:nnnnnnn #3 |
| 544 |
#1 |
| 545 |
\@@_int_eval_end: |
| 546 |
0000 0000 0000 0000 \@@_sep: #6 |
| 547 |
} |
| 548 |
\cs_new:Npn \@@_round_pack:Nw #1 |
| 549 |
{ \if_meaning:w 2 #1 + 1 \fi: \@@_int_eval_end: } |
| 550 |
\cs_new:Npn \@@_round_normal:NNwNnn #1 #2 |
| 551 |
{ |
| 552 |
\if_meaning:w 0 #2 |
| 553 |
\exp_after:wN \@@_round_special:NwwNnn |
| 554 |
\exp_after:wN #1 |
| 555 |
\fi: |
| 556 |
\@@_pack_twice_four:wNNNNNNNN |
| 557 |
\@@_pack_twice_four:wNNNNNNNN |
| 558 |
\@@_round_normal_end:wwNnn |
| 559 |
\@@_sep: #2 |
| 560 |
} |
| 561 |
\cs_new:Npn \@@_round_normal_end:wwNnn #1\@@_sep:#2\@@_sep:#3#4#5 |
| 562 |
{ |
| 563 |
\exp_after:wN \@@_exp_after_o:w \exp:w \exp_end_continue_f:w |
| 564 |
\@@_sanitize:Nw #3 #4 \@@_sep: #1 \@@_sep: |
| 565 |
} |
| 566 |
\cs_new:Npn \@@_round_special:NwwNnn #1#2\@@_sep:#3\@@_sep:#4#5#6 |
| 567 |
{ |
| 568 |
\if_meaning:w 0 #1 |
| 569 |
\@@_case_return:nw |
| 570 |
{ \exp_after:wN \@@_zero_fp:N \exp_after:wN #4 } |
| 571 |
\else: |
| 572 |
\exp_after:wN \@@_round_special_aux:Nw |
| 573 |
\exp_after:wN #4 |
| 574 |
\int_value:w \@@_int_eval:w 1 |
| 575 |
\if_meaning:w 1 #1 -#6 \else: +#5 \fi: |
| 576 |
\fi: |
| 577 |
\@@_sep: |
| 578 |
} |
| 579 |
\cs_new:Npn \@@_round_special_aux:Nw #1#2\@@_sep: |
| 580 |
{ |
| 581 |
\exp_after:wN \@@_exp_after_o:w \exp:w \exp_end_continue_f:w |
| 582 |
\@@_sanitize:Nw #1#2\@@_sep: {1000}{0000}{0000}{0000}\@@_sep: |
| 583 |
} |
| 584 |
% \end{macrocode} |
| 585 |
% \end{macro} |
| 586 |
% \end{macro} |
| 587 |
% |
| 588 |
% \begin{macrocode} |
| 589 |
%</code> |
| 590 |
% \end{macrocode} |
| 591 |
% |
| 592 |
% \end{implementation} |
| 593 |
% |
| 594 |
% \PrintChanges |
| 595 |
% |
| 596 |
% \PrintIndex |