| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3fp-convert.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-convert} module\\ Floating point conversion^^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{\texttt{l3fp-convert} implementation} |
| 57 |
% |
| 58 |
% \begin{macrocode} |
| 59 |
%<*code> |
| 60 |
% \end{macrocode} |
| 61 |
% |
| 62 |
% \begin{macrocode} |
| 63 |
%<@@=fp> |
| 64 |
% \end{macrocode} |
| 65 |
% |
| 66 |
% \subsection{Dealing with tuples} |
| 67 |
% |
| 68 |
% \begin{macro}[EXP] |
| 69 |
% {\@@_tuple_convert:Nw, \@@_tuple_convert_loop:nNw, \@@_tuple_convert_end:w} |
| 70 |
% The first argument is for instance \cs{@@_to_tl_dispatch:w}, which |
| 71 |
% converts any floating point object to the appropriate |
| 72 |
% representation. We loop through all items, putting |,~| between all |
| 73 |
% of them and making sure to remove the leading |,~|. |
| 74 |
% \begin{macrocode} |
| 75 |
\cs_new:Npn \@@_tuple_convert:Nw #1 \s_@@_tuple \@@_tuple_chk:w #2 \@@_sep: |
| 76 |
{ |
| 77 |
\int_case:nnF { \@@_array_count:n {#2} } |
| 78 |
{ |
| 79 |
{ 0 } { ( ) } |
| 80 |
{ 1 } { \@@_tuple_convert_end:w @ { #1 #2 , } } |
| 81 |
} |
| 82 |
{ |
| 83 |
\@@_tuple_convert_loop:nNw { } #1 |
| 84 |
#2 { ? \@@_tuple_convert_end:w } \@@_sep: |
| 85 |
@ { \use_none:nn } |
| 86 |
} |
| 87 |
} |
| 88 |
\cs_new:Npn \@@_tuple_convert_loop:nNw #1#2#3#4\@@_sep: #5 @ #6 |
| 89 |
{ |
| 90 |
\use_none:n #3 |
| 91 |
\exp_args:Nf \@@_tuple_convert_loop:nNw { #2 #3#4 \@@_sep: } #2 #5 |
| 92 |
@ { #6 , ~ #1 } |
| 93 |
} |
| 94 |
\cs_new:Npn \@@_tuple_convert_end:w #1 @ #2 |
| 95 |
{ \exp_after:wN ( \exp:w \exp_end_continue_f:w #2 ) } |
| 96 |
% \end{macrocode} |
| 97 |
% \end{macro} |
| 98 |
% |
| 99 |
% \subsection{Trimming trailing zeros} |
| 100 |
% |
| 101 |
% \begin{macro}[EXP]{\@@_trim_zeros:w} |
| 102 |
% \begin{macro}[EXP] |
| 103 |
% {\@@_trim_zeros_loop:w, \@@_trim_zeros_dot:w, \@@_trim_zeros_end:w} |
| 104 |
% If |#1| ends with a $0$, the \texttt{loop} auxiliary takes that zero |
| 105 |
% as an end-delimiter for its first argument, and the second argument |
| 106 |
% is the same \texttt{loop} auxiliary. Once the last trailing zero is |
| 107 |
% reached, the second argument is the \texttt{dot} auxiliary, |
| 108 |
% which removes a trailing dot if any. We then clean-up with the |
| 109 |
% \texttt{end} auxiliary, keeping only the number. |
| 110 |
% \begin{macrocode} |
| 111 |
\cs_new:Npn \@@_trim_zeros:w #1 \@@_sep: |
| 112 |
{ |
| 113 |
\@@_trim_zeros_loop:w #1 \@@_sep: |
| 114 |
\@@_trim_zeros_loop:w 0\@@_sep: |
| 115 |
\@@_trim_zeros_dot:w .\@@_sep: |
| 116 |
\s_@@_stop |
| 117 |
} |
| 118 |
\cs_new:Npn \@@_trim_zeros_loop:w #1 0\@@_sep: #2 { #2 #1 \@@_sep: #2 } |
| 119 |
\cs_new:Npn \@@_trim_zeros_dot:w #1 .\@@_sep: |
| 120 |
{ \@@_trim_zeros_end:w #1 \@@_sep: } |
| 121 |
\cs_new:Npn \@@_trim_zeros_end:w #1 \@@_sep: #2 \s_@@_stop { #1 } |
| 122 |
% \end{macrocode} |
| 123 |
% \end{macro} |
| 124 |
% \end{macro} |
| 125 |
% |
| 126 |
% \subsection{Scientific notation} |
| 127 |
% |
| 128 |
% \begin{macro}[EXP] |
| 129 |
% {\fp_to_scientific:N, \fp_to_scientific:c, \fp_to_scientific:n} |
| 130 |
% The three public functions evaluate their argument, then pass it to |
| 131 |
% \cs{@@_to_scientific_dispatch:w}. |
| 132 |
% \begin{macrocode} |
| 133 |
\cs_new:Npn \fp_to_scientific:N #1 |
| 134 |
{ \exp_after:wN \@@_to_scientific_dispatch:w #1 } |
| 135 |
\cs_generate_variant:Nn \fp_to_scientific:N { c } |
| 136 |
\cs_new:Npn \fp_to_scientific:n |
| 137 |
{ |
| 138 |
\exp_after:wN \@@_to_scientific_dispatch:w |
| 139 |
\exp:w \exp_end_continue_f:w \@@_parse:n |
| 140 |
} |
| 141 |
% \end{macrocode} |
| 142 |
% \end{macro} |
| 143 |
% |
| 144 |
% \begin{macro}[EXP] |
| 145 |
% {\@@_to_scientific_dispatch:w, \@@_to_scientific_recover:w, \@@_tuple_to_scientific:w} |
| 146 |
% We allow tuples. |
| 147 |
% \begin{macrocode} |
| 148 |
\cs_new:Npn \@@_to_scientific_dispatch:w #1 |
| 149 |
{ |
| 150 |
\@@_change_func_type:NNN |
| 151 |
#1 \@@_to_scientific:w \@@_to_scientific_recover:w |
| 152 |
#1 |
| 153 |
} |
| 154 |
\cs_new:Npn \@@_to_scientific_recover:w #1 #2 \@@_sep: |
| 155 |
{ |
| 156 |
\@@_error:nffn { unknown-type } { \tl_to_str:n { #2 \@@_sep: } } { } { } |
| 157 |
nan |
| 158 |
} |
| 159 |
\cs_new:Npn \@@_tuple_to_scientific:w |
| 160 |
{ \@@_tuple_convert:Nw \@@_to_scientific_dispatch:w } |
| 161 |
% \end{macrocode} |
| 162 |
% \end{macro} |
| 163 |
% |
| 164 |
% \begin{macro}[EXP] |
| 165 |
% { |
| 166 |
% \@@_to_scientific:w, |
| 167 |
% \@@_to_scientific_normal:wnnnnn, |
| 168 |
% \@@_to_scientific_normal:wNw |
| 169 |
% } |
| 170 |
% Expressing an internal floating point number in scientific notation |
| 171 |
% is quite easy: no rounding, and the format is very well defined. |
| 172 |
% First cater for the sign: negative numbers ($|#2|=2$) start |
| 173 |
% with~|-|; we then only need to care about positive numbers and |
| 174 |
% \texttt{nan}. Then filter the special cases: $\pm0$~are represented |
| 175 |
% as~|0|; infinities are converted to a number slightly larger than |
| 176 |
% the largest after an \enquote{invalid_operation} exception; |
| 177 |
% \texttt{nan} is represented as~|0| after an |
| 178 |
% \enquote{invalid_operation} exception. In the normal case, |
| 179 |
% decrement the exponent and unbrace the $4$ brace groups, then in a |
| 180 |
% second step grab the first digit (previously hidden in braces) to |
| 181 |
% order the various parts correctly. |
| 182 |
% \begin{macrocode} |
| 183 |
\cs_new:Npn \@@_to_scientific:w \s_@@ \@@_chk:w #1#2 |
| 184 |
{ |
| 185 |
\if_meaning:w 2 #2 \exp_after:wN - \exp:w \exp_end_continue_f:w \fi: |
| 186 |
\if_case:w #1 \exp_stop_f: |
| 187 |
\@@_case_return:nw { 0.000000000000000e0 } |
| 188 |
\or: \exp_after:wN \@@_to_scientific_normal:wnnnnn |
| 189 |
\or: |
| 190 |
\@@_case_use:nw |
| 191 |
{ |
| 192 |
\@@_invalid_operation:nnw |
| 193 |
{ \fp_to_scientific:N \c_@@_overflowing_fp } |
| 194 |
{ fp_to_scientific } |
| 195 |
} |
| 196 |
\or: |
| 197 |
\@@_case_use:nw |
| 198 |
{ |
| 199 |
\@@_invalid_operation:nnw |
| 200 |
{ \fp_to_scientific:N \c_zero_fp } |
| 201 |
{ fp_to_scientific } |
| 202 |
} |
| 203 |
\fi: |
| 204 |
\s_@@ \@@_chk:w #1 #2 |
| 205 |
} |
| 206 |
\cs_new:Npn \@@_to_scientific_normal:wnnnnn |
| 207 |
\s_@@ \@@_chk:w 1 #1 #2 #3#4#5#6 \@@_sep: |
| 208 |
{ |
| 209 |
\exp_after:wN \@@_to_scientific_normal:wNw |
| 210 |
\exp_after:wN e |
| 211 |
\int_value:w \@@_int_eval:w #2 - 1 |
| 212 |
\@@_sep: #3 #4 #5 #6 \@@_sep: |
| 213 |
} |
| 214 |
\cs_new:Npn \@@_to_scientific_normal:wNw #1 \@@_sep: #2#3\@@_sep: |
| 215 |
{ #2.#3 #1 } |
| 216 |
% \end{macrocode} |
| 217 |
% \end{macro} |
| 218 |
% |
| 219 |
% \subsection{Decimal representation} |
| 220 |
% |
| 221 |
% \begin{macro}[EXP] |
| 222 |
% {\fp_to_decimal:N, \fp_to_decimal:c, \fp_to_decimal:n} |
| 223 |
% All three public variants are based on the same |
| 224 |
% \cs{@@_to_decimal_dispatch:w} |
| 225 |
% after evaluating their argument to an internal floating point. |
| 226 |
% \begin{macrocode} |
| 227 |
\cs_new:Npn \fp_to_decimal:N #1 |
| 228 |
{ \exp_after:wN \@@_to_decimal_dispatch:w #1 } |
| 229 |
\cs_generate_variant:Nn \fp_to_decimal:N { c } |
| 230 |
\cs_new:Npn \fp_to_decimal:n |
| 231 |
{ |
| 232 |
\exp_after:wN \@@_to_decimal_dispatch:w |
| 233 |
\exp:w \exp_end_continue_f:w \@@_parse:n |
| 234 |
} |
| 235 |
% \end{macrocode} |
| 236 |
% \end{macro} |
| 237 |
% |
| 238 |
% \begin{macro}[EXP] |
| 239 |
% {\@@_to_decimal_dispatch:w, \@@_to_decimal_recover:w, \@@_tuple_to_decimal:w} |
| 240 |
% We allow tuples. |
| 241 |
% \begin{macrocode} |
| 242 |
\cs_new:Npn \@@_to_decimal_dispatch:w #1 |
| 243 |
{ |
| 244 |
\@@_change_func_type:NNN |
| 245 |
#1 \@@_to_decimal:w \@@_to_decimal_recover:w |
| 246 |
#1 |
| 247 |
} |
| 248 |
\cs_new:Npn \@@_to_decimal_recover:w #1 #2 \@@_sep: |
| 249 |
{ |
| 250 |
\@@_error:nffn { unknown-type } { \tl_to_str:n { #2 \@@_sep: } } { } { } |
| 251 |
nan |
| 252 |
} |
| 253 |
\cs_new:Npn \@@_tuple_to_decimal:w |
| 254 |
{ \@@_tuple_convert:Nw \@@_to_decimal_dispatch:w } |
| 255 |
% \end{macrocode} |
| 256 |
% \end{macro} |
| 257 |
% |
| 258 |
% \begin{macro}[EXP] |
| 259 |
% { |
| 260 |
% \@@_to_decimal:w, |
| 261 |
% \@@_to_decimal_normal:wnnnnn, |
| 262 |
% \@@_to_decimal_large:Nnnw, |
| 263 |
% \@@_to_decimal_huge:wnnnn, |
| 264 |
% } |
| 265 |
% The structure is similar to \cs{@@_to_scientific:w}. |
| 266 |
% Insert |-| for |
| 267 |
% negative numbers. Zero gives $0$, $\pm\infty$ and \nan{} yield an |
| 268 |
% \enquote{invalid operation} exception; note that $\pm\infty$ |
| 269 |
% produces a very large output, which we don't expand now since it |
| 270 |
% most likely won't be needed. Normal numbers with an exponent in the |
| 271 |
% range $[1,15]$ have that number of digits before the decimal |
| 272 |
% separator: \enquote{decimate} them, and remove leading zeros with |
| 273 |
% \cs{int_value:w}, then trim trailing zeros and dot. Normal |
| 274 |
% numbers with an exponent $16$ or larger have no decimal separator, |
| 275 |
% we only need to add trailing zeros. When the exponent is |
| 276 |
% non-positive, the result should be $0.\meta{zeros}\meta{digits}$, |
| 277 |
% trimmed. |
| 278 |
% \begin{macrocode} |
| 279 |
\cs_new:Npn \@@_to_decimal:w \s_@@ \@@_chk:w #1#2 |
| 280 |
{ |
| 281 |
\if_meaning:w 2 #2 \exp_after:wN - \exp:w \exp_end_continue_f:w \fi: |
| 282 |
\if_case:w #1 \exp_stop_f: |
| 283 |
\@@_case_return:nw { 0 } |
| 284 |
\or: \exp_after:wN \@@_to_decimal_normal:wnnnnn |
| 285 |
\or: |
| 286 |
\@@_case_use:nw |
| 287 |
{ |
| 288 |
\@@_invalid_operation:nnw |
| 289 |
{ \fp_to_decimal:N \c_@@_overflowing_fp } |
| 290 |
{ fp_to_decimal } |
| 291 |
} |
| 292 |
\or: |
| 293 |
\@@_case_use:nw |
| 294 |
{ |
| 295 |
\@@_invalid_operation:nnw |
| 296 |
{ 0 } |
| 297 |
{ fp_to_decimal } |
| 298 |
} |
| 299 |
\fi: |
| 300 |
\s_@@ \@@_chk:w #1 #2 |
| 301 |
} |
| 302 |
\cs_new:Npn \@@_to_decimal_normal:wnnnnn |
| 303 |
\s_@@ \@@_chk:w 1 #1 #2 #3#4#5#6 \@@_sep: |
| 304 |
{ |
| 305 |
\int_compare:nNnTF {#2} > 0 |
| 306 |
{ |
| 307 |
\int_compare:nNnTF {#2} < \c_@@_prec_int |
| 308 |
{ |
| 309 |
\@@_decimate:nNnnnn { \c_@@_prec_int - #2 } |
| 310 |
\@@_to_decimal_large:Nnnw |
| 311 |
} |
| 312 |
{ |
| 313 |
\exp_after:wN \exp_after:wN |
| 314 |
\exp_after:wN \@@_to_decimal_huge:wnnnn |
| 315 |
\prg_replicate:nn { #2 - \c_@@_prec_int } { 0 } \@@_sep: |
| 316 |
} |
| 317 |
{#3} {#4} {#5} {#6} |
| 318 |
} |
| 319 |
{ |
| 320 |
\exp_after:wN \@@_trim_zeros:w |
| 321 |
\exp_after:wN 0 |
| 322 |
\exp_after:wN . |
| 323 |
\exp:w \exp_end_continue_f:w \prg_replicate:nn { - #2 } { 0 } |
| 324 |
#3#4#5#6 \@@_sep: |
| 325 |
} |
| 326 |
} |
| 327 |
\cs_new:Npn \@@_to_decimal_large:Nnnw #1#2#3#4\@@_sep: |
| 328 |
{ |
| 329 |
\exp_after:wN \@@_trim_zeros:w \int_value:w |
| 330 |
\if_int_compare:w #2 > \c_zero_int |
| 331 |
#2 |
| 332 |
\fi: |
| 333 |
\exp_stop_f: |
| 334 |
#3.#4 \@@_sep: |
| 335 |
} |
| 336 |
\cs_new:Npn \@@_to_decimal_huge:wnnnn #1\@@_sep: #2#3#4#5 { #2#3#4#5 #1 } |
| 337 |
% \end{macrocode} |
| 338 |
% \end{macro} |
| 339 |
% |
| 340 |
% \subsection{Token list representation} |
| 341 |
% |
| 342 |
% \begin{macro}[EXP]{\fp_to_tl:N, \fp_to_tl:c, \fp_to_tl:n} |
| 343 |
% These three public functions evaluate their argument, then pass it |
| 344 |
% to \cs{@@_to_tl_dispatch:w}. |
| 345 |
% \begin{macrocode} |
| 346 |
\cs_new:Npn \fp_to_tl:N #1 { \exp_after:wN \@@_to_tl_dispatch:w #1 } |
| 347 |
\cs_generate_variant:Nn \fp_to_tl:N { c } |
| 348 |
\cs_new:Npn \fp_to_tl:n |
| 349 |
{ |
| 350 |
\exp_after:wN \@@_to_tl_dispatch:w |
| 351 |
\exp:w \exp_end_continue_f:w \@@_parse:n |
| 352 |
} |
| 353 |
% \end{macrocode} |
| 354 |
% \end{macro} |
| 355 |
% |
| 356 |
% \begin{macro}[EXP]{\@@_to_tl_dispatch:w, \@@_to_tl_recover:w, \@@_tuple_to_tl:w} |
| 357 |
% We allow tuples. |
| 358 |
% \begin{macrocode} |
| 359 |
\cs_new:Npn \@@_to_tl_dispatch:w #1 |
| 360 |
{ \@@_change_func_type:NNN #1 \@@_to_tl:w \@@_to_tl_recover:w #1 } |
| 361 |
\cs_new:Npn \@@_to_tl_recover:w #1 #2 \@@_sep: |
| 362 |
{ |
| 363 |
\@@_error:nffn { unknown-type } { \tl_to_str:n { #2 \@@_sep: } } { } { } |
| 364 |
nan |
| 365 |
} |
| 366 |
\cs_new:Npn \@@_tuple_to_tl:w |
| 367 |
{ \@@_tuple_convert:Nw \@@_to_tl_dispatch:w } |
| 368 |
% \end{macrocode} |
| 369 |
% \end{macro} |
| 370 |
% |
| 371 |
% \begin{macro}[EXP] |
| 372 |
% { |
| 373 |
% \@@_to_tl:w, \@@_to_tl_normal:nnnnn, |
| 374 |
% \@@_to_tl_scientific:wnnnnn, \@@_to_tl_scientific:wNw |
| 375 |
% } |
| 376 |
% A structure similar to \cs{@@_to_scientific_dispatch:w} and |
| 377 |
% \cs{@@_to_decimal_dispatch:w}, but without the \enquote{invalid operation} |
| 378 |
% exception. First filter special cases. We express normal numbers |
| 379 |
% in decimal notation if the exponent is in the range $[-2,16]$, and |
| 380 |
% otherwise use scientific notation. |
| 381 |
% \begin{macrocode} |
| 382 |
\cs_new:Npn \@@_to_tl:w \s_@@ \@@_chk:w #1#2 |
| 383 |
{ |
| 384 |
\if_meaning:w 2 #2 \exp_after:wN - \exp:w \exp_end_continue_f:w \fi: |
| 385 |
\if_case:w #1 \exp_stop_f: |
| 386 |
\@@_case_return:nw { 0 } |
| 387 |
\or: \exp_after:wN \@@_to_tl_normal:nnnnn |
| 388 |
\or: \@@_case_return:nw { inf } |
| 389 |
\else: \@@_case_return:nw { nan } |
| 390 |
\fi: |
| 391 |
} |
| 392 |
\cs_new:Npn \@@_to_tl_normal:nnnnn #1 |
| 393 |
{ |
| 394 |
\int_compare:nTF |
| 395 |
{ -2 <= #1 <= \c_@@_prec_int } |
| 396 |
{ \@@_to_decimal_normal:wnnnnn } |
| 397 |
{ \@@_to_tl_scientific:wnnnnn } |
| 398 |
\s_@@ \@@_chk:w 1 0 {#1} |
| 399 |
} |
| 400 |
\cs_new:Npn \@@_to_tl_scientific:wnnnnn |
| 401 |
\s_@@ \@@_chk:w 1 #1 #2 #3#4#5#6 \@@_sep: |
| 402 |
{ |
| 403 |
\exp_after:wN \@@_to_tl_scientific:wNw |
| 404 |
\exp_after:wN e |
| 405 |
\int_value:w \@@_int_eval:w #2 - 1 |
| 406 |
\@@_sep: #3 #4 #5 #6 \@@_sep: |
| 407 |
} |
| 408 |
\cs_new:Npn \@@_to_tl_scientific:wNw #1 \@@_sep: #2#3\@@_sep: |
| 409 |
{ \@@_trim_zeros:w #2.#3 \@@_sep: #1 } |
| 410 |
% \end{macrocode} |
| 411 |
% \end{macro} |
| 412 |
% |
| 413 |
% \subsection{Formatting} |
| 414 |
% |
| 415 |
% This is not implemented yet, as it is not yet clear what a correct |
| 416 |
% interface would be, for this kind of structured conversion from a |
| 417 |
% floating point (or other types of variables) to a string. Ideas |
| 418 |
% welcome. |
| 419 |
% |
| 420 |
% \subsection{Convert to dimension or integer} |
| 421 |
% |
| 422 |
% \begin{macro}[EXP]{\fp_to_dim:N, \fp_to_dim:c, \fp_to_dim:n} |
| 423 |
% \begin{macro}[EXP]{\@@_to_dim_dispatch:w, \@@_to_dim_recover:w, \@@_to_dim:w} |
| 424 |
% All three public variants are based on the same |
| 425 |
% \cs{@@_to_dim_dispatch:w} after evaluating their argument to an |
| 426 |
% internal floating point. |
| 427 |
% We only allow floating point numbers, not tuples. |
| 428 |
% \begin{macrocode} |
| 429 |
\cs_new:Npn \fp_to_dim:N #1 |
| 430 |
{ \exp_after:wN \@@_to_dim_dispatch:w #1 } |
| 431 |
\cs_generate_variant:Nn \fp_to_dim:N { c } |
| 432 |
\cs_new:Npn \fp_to_dim:n |
| 433 |
{ |
| 434 |
\exp_after:wN \@@_to_dim_dispatch:w |
| 435 |
\exp:w \exp_end_continue_f:w \@@_parse:n |
| 436 |
} |
| 437 |
\cs_new:Npn \@@_to_dim_dispatch:w #1#2 \@@_sep: |
| 438 |
{ |
| 439 |
\@@_change_func_type:NNN #1 \@@_to_dim:w \@@_to_dim_recover:w |
| 440 |
#1 #2 \@@_sep: |
| 441 |
} |
| 442 |
\cs_new:Npn \@@_to_dim_recover:w #1 |
| 443 |
{ \@@_invalid_operation:nnw { 0pt } { fp_to_dim } } |
| 444 |
\cs_new:Npn \@@_to_dim:w #1 \@@_sep: { \@@_to_decimal:w #1 \@@_sep: pt } |
| 445 |
% \end{macrocode} |
| 446 |
% \end{macro} |
| 447 |
% \end{macro} |
| 448 |
% |
| 449 |
% \begin{macro}[EXP]{\fp_to_int:N, \fp_to_int:c, \fp_to_int:n} |
| 450 |
% \begin{macro}[EXP]{\@@_to_int_dispatch:w, \@@_to_int_recover:w} |
| 451 |
% For the most part identical to \cs{fp_to_dim:N} but without |pt|, |
| 452 |
% and where \cs{@@_to_int:w} does more work. |
| 453 |
% To convert to an integer, first round to $0$ places (to the nearest |
| 454 |
% integer), then express the result as a decimal number: the |
| 455 |
% definition of \cs{@@_to_decimal_dispatch:w} is such that there are no |
| 456 |
% trailing dot nor zero. |
| 457 |
% \begin{macrocode} |
| 458 |
\cs_new:Npn \fp_to_int:N #1 { \exp_after:wN \@@_to_int_dispatch:w #1 } |
| 459 |
\cs_generate_variant:Nn \fp_to_int:N { c } |
| 460 |
\cs_new:Npn \fp_to_int:n |
| 461 |
{ |
| 462 |
\exp_after:wN \@@_to_int_dispatch:w |
| 463 |
\exp:w \exp_end_continue_f:w \@@_parse:n |
| 464 |
} |
| 465 |
\cs_new:Npn \@@_to_int_dispatch:w #1#2 \@@_sep: |
| 466 |
{ |
| 467 |
\@@_change_func_type:NNN #1 \@@_to_int:w \@@_to_int_recover:w |
| 468 |
#1 #2 \@@_sep: |
| 469 |
} |
| 470 |
\cs_new:Npn \@@_to_int_recover:w #1 |
| 471 |
{ \@@_invalid_operation:nnw { 0 } { fp_to_int } } |
| 472 |
\cs_new:Npn \@@_to_int:w #1\@@_sep: |
| 473 |
{ |
| 474 |
\exp_after:wN \@@_to_decimal:w \exp:w \exp_end_continue_f:w |
| 475 |
\@@_round:Nwn \@@_round_to_nearest:NNN #1\@@_sep: { 0 } |
| 476 |
} |
| 477 |
% \end{macrocode} |
| 478 |
% \end{macro} |
| 479 |
% \end{macro} |
| 480 |
% |
| 481 |
% \subsection{Convert from a dimension} |
| 482 |
% |
| 483 |
% \begin{macro}[EXP]{\dim_to_fp:n} |
| 484 |
% \begin{macro}[EXP] |
| 485 |
% { |
| 486 |
% \@@_from_dim_test:ww, |
| 487 |
% \@@_from_dim:wNw, |
| 488 |
% \@@_from_dim:wNNnnnnnn, |
| 489 |
% \@@_from_dim:wnnnnwNw, |
| 490 |
% } |
| 491 |
% The dimension expression (which can in fact be a glue expression) is |
| 492 |
% evaluated, converted to a number (i.e., expressed in scaled |
| 493 |
% points), then multiplied by $2^{-16} = 0.0000152587890625$ to give a |
| 494 |
% value expressed in points. The auxiliary \cs{@@_mul_npos_o:Nww} |
| 495 |
% expects the desired \meta{final sign} and two floating point |
| 496 |
% operands (of the form \cs{s_@@} \ldots{} \cs{@@_sep:}) as arguments. |
| 497 |
% This set of functions is also used to convert dimension registers to |
| 498 |
% floating points while parsing expressions: in this context there is |
| 499 |
% an additional exponent, which is the first argument of |
| 500 |
% \cs{@@_from_dim_test:ww}, and is combined with the exponent $-4$ |
| 501 |
% of $2^{-16}$. There is also a need to expand afterwards: this is |
| 502 |
% performed by \cs{@@_mul_npos_o:Nww}, and cancelled by |
| 503 |
% \cs{prg_do_nothing:} here. |
| 504 |
% \begin{macrocode} |
| 505 |
\cs_new:Npn \dim_to_fp:n #1 |
| 506 |
{ |
| 507 |
\exp_after:wN \@@_from_dim_test:ww |
| 508 |
\exp_after:wN 0 |
| 509 |
\exp_after:wN , |
| 510 |
\int_value:w \tex_glueexpr:D #1 \@@_sep: |
| 511 |
} |
| 512 |
\cs_new:Npn \@@_from_dim_test:ww #1, #2 |
| 513 |
{ |
| 514 |
\if_meaning:w 0 #2 |
| 515 |
\@@_case_return:nw { \exp_after:wN \c_zero_fp } |
| 516 |
\else: |
| 517 |
\exp_after:wN \@@_from_dim:wNw |
| 518 |
\int_value:w \@@_int_eval:w #1 - 4 |
| 519 |
\if_meaning:w - #2 |
| 520 |
\exp_after:wN , \exp_after:wN 2 \int_value:w |
| 521 |
\else: |
| 522 |
\exp_after:wN , \exp_after:wN 0 \int_value:w #2 |
| 523 |
\fi: |
| 524 |
\fi: |
| 525 |
} |
| 526 |
\cs_new:Npn \@@_from_dim:wNw #1,#2#3\@@_sep: |
| 527 |
{ |
| 528 |
\@@_pack_twice_four:wNNNNNNNN \@@_from_dim:wNNnnnnnn \@@_sep: |
| 529 |
#3 000 0000 00 {10}987654321\@@_sep: #2 {#1} |
| 530 |
} |
| 531 |
\cs_new:Npn \@@_from_dim:wNNnnnnnn #1\@@_sep: #2#3#4#5#6#7#8#9 |
| 532 |
{ \@@_from_dim:wnnnnwNn #1 {#2#300} {0000} \@@_sep: } |
| 533 |
\cs_new:Npn \@@_from_dim:wnnnnwNn #1\@@_sep: #2#3#4#5#6\@@_sep: #7#8 |
| 534 |
{ |
| 535 |
\@@_mul_npos_o:Nww #7 |
| 536 |
\s_@@ \@@_chk:w 1 #7 {#5} #1 \@@_sep: |
| 537 |
\s_@@ \@@_chk:w 1 0 {#8} {1525} {8789} {0625} {0000} \@@_sep: |
| 538 |
\prg_do_nothing: |
| 539 |
} |
| 540 |
% \end{macrocode} |
| 541 |
% \end{macro} |
| 542 |
% \end{macro} |
| 543 |
% |
| 544 |
% \subsection{Use and eval} |
| 545 |
% |
| 546 |
% \begin{macro}[EXP]{\fp_use:N, \fp_use:c, \fp_eval:n} |
| 547 |
% Those public functions are simple copies of the decimal conversions. |
| 548 |
% \begin{macrocode} |
| 549 |
\cs_new_eq:NN \fp_use:N \fp_to_decimal:N |
| 550 |
\cs_generate_variant:Nn \fp_use:N { c } |
| 551 |
\cs_new_eq:NN \fp_eval:n \fp_to_decimal:n |
| 552 |
% \end{macrocode} |
| 553 |
% \end{macro} |
| 554 |
% |
| 555 |
% \begin{macro}{\fp_sign:n} |
| 556 |
% Trivial but useful. See the implementation of \cs{fp_add:Nn} for an |
| 557 |
% explanation of why to use \cs{@@_parse:n}, namely, for better error |
| 558 |
% reporting. |
| 559 |
% \begin{macrocode} |
| 560 |
\cs_new:Npn \fp_sign:n #1 |
| 561 |
{ \fp_to_decimal:n { sign \@@_parse:n {#1} } } |
| 562 |
% \end{macrocode} |
| 563 |
% \end{macro} |
| 564 |
% |
| 565 |
% \begin{macro}[EXP]{\fp_abs:n} |
| 566 |
% Trivial but useful. See the implementation of \cs{fp_add:Nn} for an |
| 567 |
% explanation of why to use \cs{@@_parse:n}, namely, for better error |
| 568 |
% reporting. |
| 569 |
% \begin{macrocode} |
| 570 |
\cs_new:Npn \fp_abs:n #1 |
| 571 |
{ \fp_to_decimal:n { abs \@@_parse:n {#1} } } |
| 572 |
% \end{macrocode} |
| 573 |
% \end{macro} |
| 574 |
% |
| 575 |
% \begin{macro}[EXP]{\fp_max:nn, \fp_min:nn} |
| 576 |
% Similar to \cs{fp_abs:n}, for consistency with \cs{int_max:nn}, etc. |
| 577 |
% \begin{macrocode} |
| 578 |
\cs_new:Npn \fp_max:nn #1#2 |
| 579 |
{ \fp_to_decimal:n { max ( \@@_parse:n {#1} , \@@_parse:n {#2} ) } } |
| 580 |
\cs_new:Npn \fp_min:nn #1#2 |
| 581 |
{ \fp_to_decimal:n { min ( \@@_parse:n {#1} , \@@_parse:n {#2} ) } } |
| 582 |
% \end{macrocode} |
| 583 |
% \end{macro} |
| 584 |
% |
| 585 |
% \subsection{Convert an array of floating points to a comma list} |
| 586 |
% |
| 587 |
% \begin{macro}[EXP]{\@@_array_to_clist:n} |
| 588 |
% \begin{macro}[EXP]{\@@_array_to_clist_loop:Nw} |
| 589 |
% Converts an array of floating point numbers to a comma-list. If |
| 590 |
% speed here ends up irrelevant, we can simplify the code for the |
| 591 |
% auxiliary to become |
| 592 |
% \begin{verbatim} |
| 593 |
% \cs_new:Npn \__fp_array_to_clist_loop:Nw #1#2; |
| 594 |
% { |
| 595 |
% \use_none:n #1 |
| 596 |
% { , ~ } \fp_to_tl:n { #1 #2 ; } |
| 597 |
% \__fp_array_to_clist_loop:Nw |
| 598 |
% } |
| 599 |
% \end{verbatim} |
| 600 |
% The \cs{use_ii:nn} function is expanded after \cs{@@_expand:n} is |
| 601 |
% done, and it removes |,~| from the start of the representation. |
| 602 |
% \begin{macrocode} |
| 603 |
\cs_new:Npn \@@_array_to_clist:n #1 |
| 604 |
{ |
| 605 |
\tl_if_empty:nF {#1} |
| 606 |
{ |
| 607 |
\exp_last_unbraced:Ne \use_ii:nn |
| 608 |
{ |
| 609 |
\@@_array_to_clist_loop:Nw #1 { ? \prg_break: } \@@_sep: |
| 610 |
\prg_break_point: |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
\cs_new:Npn \@@_array_to_clist_loop:Nw #1#2\@@_sep: |
| 615 |
{ |
| 616 |
\use_none:n #1 |
| 617 |
, ~ |
| 618 |
\exp_not:f { \@@_to_tl_dispatch:w #1 #2 \@@_sep: } |
| 619 |
\@@_array_to_clist_loop:Nw |
| 620 |
} |
| 621 |
% \end{macrocode} |
| 622 |
% \end{macro} |
| 623 |
% \end{macro} |
| 624 |
% |
| 625 |
% \begin{macrocode} |
| 626 |
%</code> |
| 627 |
% \end{macrocode} |
| 628 |
% |
| 629 |
% \end{implementation} |
| 630 |
% |
| 631 |
% \PrintChanges |
| 632 |
% |
| 633 |
% \PrintIndex |