| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3fp-expo.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-expo} module\\ |
| 35 |
% Floating point exponential-related functions^^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-expo} implementation} |
| 56 |
% |
| 57 |
% \begin{macrocode} |
| 58 |
%<*code> |
| 59 |
% \end{macrocode} |
| 60 |
% |
| 61 |
% \begin{macrocode} |
| 62 |
%<@@=fp> |
| 63 |
% \end{macrocode} |
| 64 |
% |
| 65 |
% \begin{macro}[EXP] |
| 66 |
% { |
| 67 |
% \@@_parse_word_exp:N , |
| 68 |
% \@@_parse_word_ln:N , |
| 69 |
% \@@_parse_word_fact:N, |
| 70 |
% } |
| 71 |
% Unary functions. |
| 72 |
% \begin{macrocode} |
| 73 |
\cs_new:Npn \@@_parse_word_exp:N |
| 74 |
{ \@@_parse_unary_function:NNN \@@_exp_o:w ? } |
| 75 |
\cs_new:Npn \@@_parse_word_ln:N |
| 76 |
{ \@@_parse_unary_function:NNN \@@_ln_o:w ? } |
| 77 |
\cs_new:Npn \@@_parse_word_fact:N |
| 78 |
{ \@@_parse_unary_function:NNN \@@_fact_o:w ? } |
| 79 |
% \end{macrocode} |
| 80 |
% \end{macro} |
| 81 |
% |
| 82 |
% \subsection{Logarithm} |
| 83 |
% |
| 84 |
% \subsubsection{Work plan} |
| 85 |
% |
| 86 |
% As for many other functions, we filter out special cases in |
| 87 |
% \cs{@@_ln_o:w}. Then \cs{@@_ln_npos_o:w} receives a positive normal |
| 88 |
% number, which we write in the form $a\cdot 10^{b}$ with $a\in[0.1,1)$. |
| 89 |
% |
| 90 |
% \emph{The rest of this section is actually not in sync with the code. |
| 91 |
% Or is the code not in sync with the section? In the current code, |
| 92 |
% $c\in [1,10]$ is such that $0.7\leq ac < 1.4$.} |
| 93 |
% |
| 94 |
% We are given a positive normal number, of the form $a\cdot 10^{b}$ |
| 95 |
% with $a\in[0.1,1)$. To compute its logarithm, we find a small integer |
| 96 |
% $5\leq c < 50$ such that $0.91 \leq a c / 5 < 1.1$, and use the |
| 97 |
% relation |
| 98 |
% \begin{equation*} |
| 99 |
% \ln (a \cdot 10^b) = b \cdot \ln (10) - \ln (c/5) + \ln (ac/5). |
| 100 |
% \end{equation*} |
| 101 |
% The logarithms $\ln(10)$ and $\ln(c/5)$ are looked up in a table. The |
| 102 |
% last term is computed using the following Taylor series of $\ln$ near |
| 103 |
% $1$: |
| 104 |
% \begin{equation*} |
| 105 |
% \ln\left(\frac{ac}{5}\right) |
| 106 |
% = \ln\left(\frac{1+t}{1-t}\right) |
| 107 |
% = 2t\left(1 + t^2 \left(\frac{1}{3} + t^2 \left(\frac{1}{5} |
| 108 |
% + t^2 \left(\frac{1}{7} + t^2 \left( \frac{1}{9} + \cdots |
| 109 |
% \right)\right)\right)\right)\right) |
| 110 |
% \end{equation*} |
| 111 |
% where $t=1-10/(ac+5)$. We can now see one reason for the choice of |
| 112 |
% $ac\sim 5$: then $ac+5=10(1-\epsilon)$ with $-0.05<\epsilon\leq |
| 113 |
% 0.045$, hence |
| 114 |
% \begin{equation*} |
| 115 |
% t = \frac{\epsilon}{1-\epsilon} |
| 116 |
% = \epsilon (1+\epsilon)(1+\epsilon^2)(1+\epsilon^4)\ldots, |
| 117 |
% \end{equation*} |
| 118 |
% is not too difficult to compute. |
| 119 |
% |
| 120 |
% \subsubsection{Some constants} |
| 121 |
% |
| 122 |
% \begin{variable} |
| 123 |
% { |
| 124 |
% \c_@@_ln_i_fixed_tl , |
| 125 |
% \c_@@_ln_ii_fixed_tl , |
| 126 |
% \c_@@_ln_iii_fixed_tl , |
| 127 |
% \c_@@_ln_iv_fixed_tl , |
| 128 |
% \c_@@_ln_vi_fixed_tl , |
| 129 |
% \c_@@_ln_vii_fixed_tl , |
| 130 |
% \c_@@_ln_viii_fixed_tl , |
| 131 |
% \c_@@_ln_ix_fixed_tl , |
| 132 |
% \c_@@_ln_x_fixed_tl, |
| 133 |
% } |
| 134 |
% A few values of the logarithm as extended fixed point numbers. |
| 135 |
% Those are needed in the implementation. It turns out that we don't |
| 136 |
% need the value of $\ln(5)$. |
| 137 |
% \begin{macrocode} |
| 138 |
\tl_const:Nn \c_@@_ln_i_fixed_tl |
| 139 |
{ {0000}{0000}{0000}{0000}{0000}{0000}\@@_sep:} |
| 140 |
\tl_const:Nn \c_@@_ln_ii_fixed_tl |
| 141 |
{ {6931}{4718}{0559}{9453}{0941}{7232}\@@_sep:} |
| 142 |
\tl_const:Nn \c_@@_ln_iii_fixed_tl |
| 143 |
{{10986}{1228}{8668}{1096}{9139}{5245}\@@_sep:} |
| 144 |
\tl_const:Nn \c_@@_ln_iv_fixed_tl |
| 145 |
{{13862}{9436}{1119}{8906}{1883}{4464}\@@_sep:} |
| 146 |
\tl_const:Nn \c_@@_ln_vi_fixed_tl |
| 147 |
{{17917}{5946}{9228}{0550}{0081}{2477}\@@_sep:} |
| 148 |
\tl_const:Nn \c_@@_ln_vii_fixed_tl |
| 149 |
{{19459}{1014}{9055}{3133}{0510}{5353}\@@_sep:} |
| 150 |
\tl_const:Nn \c_@@_ln_viii_fixed_tl |
| 151 |
{{20794}{4154}{1679}{8359}{2825}{1696}\@@_sep:} |
| 152 |
\tl_const:Nn \c_@@_ln_ix_fixed_tl |
| 153 |
{{21972}{2457}{7336}{2193}{8279}{0490}\@@_sep:} |
| 154 |
\tl_const:Nn \c_@@_ln_x_fixed_tl |
| 155 |
{{23025}{8509}{2994}{0456}{8401}{7991}\@@_sep:} |
| 156 |
% \end{macrocode} |
| 157 |
% \end{variable} |
| 158 |
% |
| 159 |
% \subsubsection{Sign, exponent, and special numbers} |
| 160 |
% |
| 161 |
% \begin{macro}[EXP]{\@@_ln_o:w} |
| 162 |
% The logarithm of negative numbers (including $-\infty$ and $-0$) |
| 163 |
% raises the \enquote{invalid} exception. The logarithm of $+0$ is |
| 164 |
% $-\infty$, raising a division by zero exception. The logarithm of |
| 165 |
% $+\infty$ or a \texttt{nan} is itself. Positive normal numbers call |
| 166 |
% \cs{@@_ln_npos_o:w}. |
| 167 |
% \begin{macrocode} |
| 168 |
\cs_new:Npn \@@_ln_o:w #1 \s_@@ \@@_chk:w #2#3#4\@@_sep: @ |
| 169 |
{ |
| 170 |
\if_meaning:w 2 #3 |
| 171 |
\@@_case_use:nw { \@@_invalid_operation_o:nw { ln } } |
| 172 |
\fi: |
| 173 |
\if_case:w #2 \exp_stop_f: |
| 174 |
\@@_case_use:nw |
| 175 |
{ \@@_division_by_zero_o:Nnw \c_minus_inf_fp { ln } } |
| 176 |
\or: |
| 177 |
\else: |
| 178 |
\@@_case_return_same_o:w |
| 179 |
\fi: |
| 180 |
\@@_ln_npos_o:w \s_@@ \@@_chk:w #2#3#4\@@_sep: |
| 181 |
} |
| 182 |
% \end{macrocode} |
| 183 |
% \end{macro} |
| 184 |
% |
| 185 |
% \subsubsection{Absolute ln} |
| 186 |
% |
| 187 |
% \begin{macro}[EXP]{\@@_ln_npos_o:w} |
| 188 |
% We catch the case of a significand very close to $0.1$ or to $1$. |
| 189 |
% In all other cases, the final result is at least $10^{-4}$, and |
| 190 |
% then an error of $0.5\cdot 10^{-20}$ is acceptable. |
| 191 |
% \begin{macrocode} |
| 192 |
\cs_new:Npn \@@_ln_npos_o:w \s_@@ \@@_chk:w 10#1#2#3\@@_sep: |
| 193 |
{ %^^A todo: ln(1) should be "exact zero", not "underflow" |
| 194 |
\exp_after:wN \@@_sanitize:Nw |
| 195 |
\int_value:w % for the overall sign |
| 196 |
\if_int_compare:w #1 < \c_one_int |
| 197 |
2 |
| 198 |
\else: |
| 199 |
0 |
| 200 |
\fi: |
| 201 |
\exp_after:wN \exp_stop_f: |
| 202 |
\int_value:w \@@_int_eval:w % for the exponent |
| 203 |
\@@_ln_significand:NNNNnnnN #2#3 |
| 204 |
\@@_ln_exponent:wn {#1} |
| 205 |
} |
| 206 |
% \end{macrocode} |
| 207 |
% \end{macro} |
| 208 |
% |
| 209 |
% \begin{macro}[EXP]{\@@_ln_significand:NNNNnnnN} |
| 210 |
% \begin{syntax} |
| 211 |
% \cs{@@_ln_significand:NNNNnnnN} \meta{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4} \meta{continuation} |
| 212 |
% \end{syntax} |
| 213 |
% This function expands to |
| 214 |
% \begin{syntax} |
| 215 |
% \meta{continuation} \Arg{Y_1} \Arg{Y_2} \Arg{Y_3} \Arg{Y_4} \Arg{Y_5} \Arg{Y_6} \cs{@@_sep:} |
| 216 |
% \end{syntax} |
| 217 |
% where $Y = - \ln(X)$ as an extended fixed point. |
| 218 |
% \begin{macrocode} |
| 219 |
\cs_new:Npn \@@_ln_significand:NNNNnnnN #1#2#3#4 |
| 220 |
{ |
| 221 |
\exp_after:wN \@@_ln_x_ii:wnnnn |
| 222 |
\int_value:w |
| 223 |
\if_case:w #1 \exp_stop_f: |
| 224 |
\or: |
| 225 |
\if_int_compare:w #2 < 4 \exp_stop_f: |
| 226 |
\@@_int_eval:w 10 - #2 |
| 227 |
\else: |
| 228 |
6 |
| 229 |
\fi: |
| 230 |
\or: 4 |
| 231 |
\or: 3 |
| 232 |
\or: 2 |
| 233 |
\or: 2 |
| 234 |
\or: 2 |
| 235 |
\else: 1 |
| 236 |
\fi: |
| 237 |
\@@_sep: { #1 #2 #3 #4 } |
| 238 |
} |
| 239 |
% \end{macrocode} |
| 240 |
% \end{macro} |
| 241 |
% |
| 242 |
% \begin{macro}[EXP]{\@@_ln_x_ii:wnnnn} |
| 243 |
% We have thus found $c \in [1,10]$ such that $0.7\leq ac < 1.4$ |
| 244 |
% in all cases. Compute $ 1 + x = 1 + ac \in [1.7,2.4)$. |
| 245 |
% \begin{macrocode} |
| 246 |
\cs_new:Npn \@@_ln_x_ii:wnnnn #1\@@_sep: #2#3#4#5 |
| 247 |
{ |
| 248 |
\exp_after:wN \@@_ln_div_after:Nw |
| 249 |
\cs:w c_@@_ln_ \@@_int_to_roman:w #1 _fixed_tl \exp_after:wN \cs_end: |
| 250 |
\int_value:w |
| 251 |
\exp_after:wN \@@_ln_x_iv:wnnnnnnnn |
| 252 |
\int_value:w \@@_int_eval:w |
| 253 |
\exp_after:wN \@@_ln_x_iii_var:NNNNNw |
| 254 |
\int_value:w \@@_int_eval:w 9999 9990 + #1*#2#3 + |
| 255 |
\exp_after:wN \@@_ln_x_iii:NNNNNNw |
| 256 |
\int_value:w \@@_int_eval:w 10 0000 0000 + #1*#4#5 \@@_sep: |
| 257 |
{20000} {0000} {0000} {0000} |
| 258 |
} %^^A todo: reoptimize (a generalization attempt failed). |
| 259 |
\cs_new:Npn \@@_ln_x_iii:NNNNNNw #1#2 #3#4#5#6 #7\@@_sep: |
| 260 |
{ #1#2\@@_sep: {#3#4#5#6} {#7} } |
| 261 |
\cs_new:Npn \@@_ln_x_iii_var:NNNNNw #1 #2#3#4#5 #6\@@_sep: |
| 262 |
{ |
| 263 |
#1#2#3#4#5 + 1 \@@_sep: |
| 264 |
{#1#2#3#4#5} {#6} |
| 265 |
} |
| 266 |
% \end{macrocode} |
| 267 |
% The Taylor series to be used is expressed in terms of |
| 268 |
% $t = (x-1)/(x+1) = 1 - 2/(x+1)$. We now compute the |
| 269 |
% quotient with extended precision, reusing some code |
| 270 |
% from \cs{@@_/_o:ww}. Note that $1+x$ is known exactly. |
| 271 |
% |
| 272 |
% To reuse notations from \pkg{l3fp-basics}, we want to |
| 273 |
% compute $ A / Z $ with $ A = 2 $ and $ Z = x + 1 $. |
| 274 |
% In \pkg{l3fp-basics}, we considered the case where |
| 275 |
% both $A$ and $Z$ are arbitrary, in the range $[0.1,1)$, |
| 276 |
% and we had to monitor the growth of the sequence of |
| 277 |
% remainders $A$, $B$, $C$, etc. to ensure that no overflow |
| 278 |
% occurred during the computation of the next quotient. |
| 279 |
% The main source of risk was our choice to define the |
| 280 |
% quotient as roughly $10^9 \cdot A / 10^5 \cdot Z$: then |
| 281 |
% $A$ was bound to be below $2.147\cdots$, and this limit |
| 282 |
% was never far. |
| 283 |
% |
| 284 |
% In our case, we can simply work with $10^8 \cdot A$ and |
| 285 |
% $10^4 \cdot Z$, because our reason to work with higher |
| 286 |
% powers has gone: we needed the integer $y \simeq 10^5 \cdot Z$ |
| 287 |
% to be at least $10^4$, and now, the definition |
| 288 |
% $y \simeq 10^4 \cdot Z$ suffices. |
| 289 |
% |
| 290 |
% Let us thus define $y = \left\lfloor 10^4 \cdot Z \right\rfloor + 1 |
| 291 |
% \in ( 1.7 \cdot 10^4, 2.4 \cdot 10^4 ] $, and |
| 292 |
% \[ |
| 293 |
% Q_{1} |
| 294 |
% = |
| 295 |
% \left\lfloor |
| 296 |
% \frac {\left\lfloor 10^8 \cdot A\right\rfloor} {y} - \frac{1}{2} |
| 297 |
% \right\rfloor. |
| 298 |
% \] |
| 299 |
% (The $1/2$ comes from how \eTeX{} rounds.) As for division, it is |
| 300 |
% easy to see that $Q_{1} \leq 10^4 A / Z$, i.e., $Q_{1}$ |
| 301 |
% is an underestimate. |
| 302 |
% |
| 303 |
% Exactly as we did for division, we set $B = 10^4 A - Q_{1}Z$. Then |
| 304 |
% \begin{align*} |
| 305 |
% 10^4 B |
| 306 |
% & \leq |
| 307 |
% A_{1}A_{2}.A_{3}A_{4} |
| 308 |
% - \left( \frac{A_{1}A_{2}}{y} - \frac{3}{2} \right) 10^4 Z |
| 309 |
% \\ |
| 310 |
% & \leq |
| 311 |
% A_{1}A_{2} \left( 1 - \frac{10^4 Z}{y} \right) + 1 + \frac{3}{2} y |
| 312 |
% \\ |
| 313 |
% & \leq |
| 314 |
% 10^8 \frac{A}{y} + 1 + \frac{3}{2} y |
| 315 |
% \end{align*} |
| 316 |
% In the same way, and using $1.7\cdot 10^4\leq y\leq 2.4\cdot 10^4$, |
| 317 |
% and convexity, we get |
| 318 |
% \begin{align*} |
| 319 |
% 10^4 A &= 2\cdot 10^4 \\ |
| 320 |
% 10^4 B &\leq 10^8 \frac{A}{y} + 1.6 y \leq 4.7\cdot 10^4\\ |
| 321 |
% 10^4 C &\leq 10^8 \frac{B}{y} + 1.6 y \leq 5.8\cdot 10^4\\ |
| 322 |
% 10^4 D &\leq 10^8 \frac{C}{y} + 1.6 y \leq 6.3\cdot 10^4\\ |
| 323 |
% 10^4 E &\leq 10^8 \frac{D}{y} + 1.6 y \leq 6.5\cdot 10^4\\ |
| 324 |
% 10^4 F &\leq 10^8 \frac{E}{y} + 1.6 y \leq 6.6\cdot 10^4\\ |
| 325 |
% \end{align*} |
| 326 |
% Note that we compute more steps than for division: since $t$ is |
| 327 |
% not the end result, we need to know it with more accuracy |
| 328 |
% (on the other hand, the ending is much simpler, as we don't |
| 329 |
% need an exact rounding for transcendental functions, but just |
| 330 |
% a faithful rounding). |
| 331 |
% ^^A todo: doc |
| 332 |
% |
| 333 |
% \begin{syntax} |
| 334 |
% \cs{@@_ln_x_iv:wnnnnnnnn} \meta{1 or 2} \meta{8d} \cs{@@_sep:} \Arg{4d} \Arg{4d} \meta{fixed-tl} |
| 335 |
% \end{syntax} |
| 336 |
% The number is $x$. Compute $y$ by adding 1 to the five first digits. |
| 337 |
% \begin{macrocode} |
| 338 |
\cs_new:Npn \@@_ln_x_iv:wnnnnnnnn #1\@@_sep: #2#3#4#5 #6#7#8#9 |
| 339 |
{ |
| 340 |
\exp_after:wN \@@_div_significand_pack:NNN |
| 341 |
\int_value:w \@@_int_eval:w |
| 342 |
\@@_ln_div_i:w #1 \@@_sep: |
| 343 |
#6 #7 \@@_sep: {#8} {#9} |
| 344 |
{#2} {#3} {#4} {#5} |
| 345 |
{ \exp_after:wN \@@_ln_div_ii:wwn \int_value:w #1 } |
| 346 |
{ \exp_after:wN \@@_ln_div_ii:wwn \int_value:w #1 } |
| 347 |
{ \exp_after:wN \@@_ln_div_ii:wwn \int_value:w #1 } |
| 348 |
{ \exp_after:wN \@@_ln_div_ii:wwn \int_value:w #1 } |
| 349 |
{ \exp_after:wN \@@_ln_div_vi:wwn \int_value:w #1 } |
| 350 |
} |
| 351 |
\cs_new:Npn \@@_ln_div_i:w #1\@@_sep: |
| 352 |
{ |
| 353 |
\exp_after:wN \@@_div_significand_calc:wwnnnnnnn |
| 354 |
\int_value:w \@@_int_eval:w 999999 + 2 0000 0000 / #1 \@@_sep: % Q1 |
| 355 |
} |
| 356 |
\cs_new:Npn \@@_ln_div_ii:wwn |
| 357 |
#1\@@_sep: #2\@@_sep:#3 % y\@@_sep: B1\@@_sep:B2 <- for k=1 |
| 358 |
{ |
| 359 |
\exp_after:wN \@@_div_significand_pack:NNN |
| 360 |
\int_value:w \@@_int_eval:w |
| 361 |
\exp_after:wN \@@_div_significand_calc:wwnnnnnnn |
| 362 |
\int_value:w \@@_int_eval:w 999999 + #2 #3 / #1 \@@_sep: % Q2 |
| 363 |
#2 #3 \@@_sep: |
| 364 |
} |
| 365 |
\cs_new:Npn \@@_ln_div_vi:wwn |
| 366 |
#1\@@_sep: #2\@@_sep:#3#4#5 #6#7#8#9 %y\@@_sep:F1\@@_sep:F2F3F4x1x2x3x4 |
| 367 |
{ |
| 368 |
\exp_after:wN \@@_div_significand_pack:NNN |
| 369 |
\int_value:w \@@_int_eval:w 1000000 + #2 #3 / #1 \@@_sep: % Q6 |
| 370 |
} |
| 371 |
% \end{macrocode} |
| 372 |
% We now have essentially |
| 373 |
% ^^A todo: determine error on $Q_{6}$ (probably $6.7$), |
| 374 |
% ^^A todo: conclude the final result is off by $<10^{-23}$ |
| 375 |
% \begin{syntax} |
| 376 |
% \cs{@@_ln_div_after:Nw} \meta{fixed tl} |
| 377 |
% \cs{@@_div_significand_pack:NNN} $10^6 + Q_{1}$ |
| 378 |
% \cs{@@_div_significand_pack:NNN} $10^6 + Q_{2}$ |
| 379 |
% \cs{@@_div_significand_pack:NNN} $10^6 + Q_{3}$ |
| 380 |
% \cs{@@_div_significand_pack:NNN} $10^6 + Q_{4}$ |
| 381 |
% \cs{@@_div_significand_pack:NNN} $10^6 + Q_{5}$ |
| 382 |
% \cs{@@_div_significand_pack:NNN} $10^6 + Q_{6}$ \cs{@@_sep:} |
| 383 |
% \meta{exponent} \cs{@@_sep:} \meta{continuation} |
| 384 |
% \end{syntax} |
| 385 |
% where \meta{fixed tl} holds the logarithm of a number |
| 386 |
% in $[1,10]$, and \meta{exponent} is |
| 387 |
% the exponent. Also, the expansion is done backwards. Then |
| 388 |
% \cs{@@_div_significand_pack:NNN} puts things in the |
| 389 |
% correct order to add the $Q_{i}$ together and put \cs{@@_sep:}s |
| 390 |
% between each piece. Once those have been expanded, we get |
| 391 |
% \begin{syntax} |
| 392 |
% \cs{@@_ln_div_after:Nw} \meta{fixed-tl} \meta{1d} \cs{@@_sep:} \meta{4d} \cs{@@_sep:} |
| 393 |
% ~~\meta{4d} \cs{@@_sep:} \meta{4d} \cs{@@_sep:} \meta{4d} \cs{@@_sep:} \meta{4d} \cs{@@_sep:} |
| 394 |
% ~~\meta{4d} \cs{@@_sep:} \meta{exponent} \cs{@@_sep:} |
| 395 |
% \end{syntax} |
| 396 |
% ^^A todo: redoc. |
| 397 |
% Just as with division, we know that the first two digits |
| 398 |
% are |1| and |0| because of bounds on the final result of |
| 399 |
% the division $2/(x+1)$, which is between roughly $0.8$ and $1.2$. |
| 400 |
% We then compute $1-2/(x+1)$, after testing whether $2/(x+1)$ is |
| 401 |
% greater than or smaller than $1$. |
| 402 |
% \begin{macrocode} |
| 403 |
\cs_new:Npn \@@_ln_div_after:Nw #1#2\@@_sep: |
| 404 |
{ |
| 405 |
\if_meaning:w 0 #2 |
| 406 |
\exp_after:wN \@@_ln_t_small:Nw |
| 407 |
\else: |
| 408 |
\exp_after:wN \@@_ln_t_large:NNw |
| 409 |
\exp_after:wN - |
| 410 |
\fi: |
| 411 |
#1 |
| 412 |
} |
| 413 |
\cs_new:Npn \@@_ln_t_small:Nw |
| 414 |
#1 #2\@@_sep: #3\@@_sep: #4\@@_sep: #5\@@_sep: #6\@@_sep: #7\@@_sep: |
| 415 |
{ |
| 416 |
\exp_after:wN \@@_ln_t_large:NNw |
| 417 |
\exp_after:wN + % <sign> |
| 418 |
\exp_after:wN #1 |
| 419 |
\int_value:w \@@_int_eval:w 9999 - #2 \exp_after:wN \@@_sep: |
| 420 |
\int_value:w \@@_int_eval:w 9999 - #3 \exp_after:wN \@@_sep: |
| 421 |
\int_value:w \@@_int_eval:w 9999 - #4 \exp_after:wN \@@_sep: |
| 422 |
\int_value:w \@@_int_eval:w 9999 - #5 \exp_after:wN \@@_sep: |
| 423 |
\int_value:w \@@_int_eval:w 9999 - #6 \exp_after:wN \@@_sep: |
| 424 |
\int_value:w \@@_int_eval:w 1 0000 - #7 \@@_sep: |
| 425 |
} |
| 426 |
% \end{macrocode} |
| 427 |
% |
| 428 |
% \begin{syntax} |
| 429 |
% \cs{@@_ln_t_large:NNw} \meta{sign} \meta{fixed tl} |
| 430 |
% ~~\meta{t_1}\cs{@@_sep:} \meta{t_2} \cs{@@_sep:} \meta{t_3}\cs{@@_sep:} \meta{t_4} |
| 431 |
% ~~\cs{@@_sep:} \meta{t_5} \cs{@@_sep:} \meta{t_6}\cs{@@_sep:} |
| 432 |
% ~~\meta{exponent} \cs{@@_sep:} \meta{continuation} |
| 433 |
% \end{syntax} |
| 434 |
% Compute the square $|t|^2$, and keep $|t|$ at the end with its |
| 435 |
% sign. We know that $|t|<0.1765$, so every piece has at most $4$ |
| 436 |
% digits. However, since we were not careful in \cs{@@_ln_t_small:w}, |
| 437 |
% they can have less than $4$ digits. |
| 438 |
% \begin{macrocode} |
| 439 |
\cs_new:Npn \@@_ln_t_large:NNw |
| 440 |
#1 #2 |
| 441 |
#3\@@_sep: #4\@@_sep: #5\@@_sep: #6\@@_sep: #7\@@_sep: #8\@@_sep: |
| 442 |
{ |
| 443 |
\exp_after:wN \@@_ln_square_t_after:w |
| 444 |
\int_value:w \@@_int_eval:w 9999 0000 + #3*#3 |
| 445 |
\exp_after:wN \@@_ln_square_t_pack:NNNNNw |
| 446 |
\int_value:w \@@_int_eval:w 9999 0000 + 2*#3*#4 |
| 447 |
\exp_after:wN \@@_ln_square_t_pack:NNNNNw |
| 448 |
\int_value:w \@@_int_eval:w 9999 0000 + 2*#3*#5 + #4*#4 |
| 449 |
\exp_after:wN \@@_ln_square_t_pack:NNNNNw |
| 450 |
\int_value:w \@@_int_eval:w 9999 0000 + 2*#3*#6 + 2*#4*#5 |
| 451 |
\exp_after:wN \@@_ln_square_t_pack:NNNNNw |
| 452 |
\int_value:w \@@_int_eval:w |
| 453 |
1 0000 0000 + 2*#3*#7 + 2*#4*#6 + #5*#5 |
| 454 |
+ (2*#3*#8 + 2*#4*#7 + 2*#5*#6) / 1 0000 |
| 455 |
% \@@_sep: \@@_sep: \@@_sep: |
| 456 |
\exp_after:wN \@@_ln_twice_t_after:w |
| 457 |
\int_value:w \@@_int_eval:w -1 + 2*#3 |
| 458 |
\exp_after:wN \@@_ln_twice_t_pack:Nw |
| 459 |
\int_value:w \@@_int_eval:w 9999 + 2*#4 |
| 460 |
\exp_after:wN \@@_ln_twice_t_pack:Nw |
| 461 |
\int_value:w \@@_int_eval:w 9999 + 2*#5 |
| 462 |
\exp_after:wN \@@_ln_twice_t_pack:Nw |
| 463 |
\int_value:w \@@_int_eval:w 9999 + 2*#6 |
| 464 |
\exp_after:wN \@@_ln_twice_t_pack:Nw |
| 465 |
\int_value:w \@@_int_eval:w 9999 + 2*#7 |
| 466 |
\exp_after:wN \@@_ln_twice_t_pack:Nw |
| 467 |
\int_value:w \@@_int_eval:w 10000 + 2*#8 \@@_sep: \@@_sep: |
| 468 |
{ \@@_ln_c:NwNw #1 } |
| 469 |
#2 |
| 470 |
} |
| 471 |
\cs_new:Npn \@@_ln_twice_t_pack:Nw #1 #2\@@_sep: { + #1 \@@_sep: {#2} } |
| 472 |
\cs_new:Npn \@@_ln_twice_t_after:w #1\@@_sep: |
| 473 |
{ \@@_sep:\@@_sep:\@@_sep: {#1} } |
| 474 |
\cs_new:Npn \@@_ln_square_t_pack:NNNNNw #1 #2#3#4#5 #6\@@_sep: |
| 475 |
{ + #1#2#3#4#5 \@@_sep: {#6} } |
| 476 |
\cs_new:Npn \@@_ln_square_t_after:w 1 0 #1#2#3 #4\@@_sep: |
| 477 |
{ \@@_ln_Taylor:wwNw {0#1#2#3} {#4} } |
| 478 |
% \end{macrocode} |
| 479 |
% \end{macro} |
| 480 |
% |
| 481 |
% \begin{macro}{\@@_ln_Taylor:wwNw} |
| 482 |
% Denoting $T=t^2$, we get |
| 483 |
% \begin{syntax} |
| 484 |
% \cs{@@_ln_Taylor:wwNw} |
| 485 |
% ~~\Arg{T_1} \Arg{T_2} \Arg{T_3} \Arg{T_4} \Arg{T_5} \Arg{T_6} \cs{@@_sep:} \cs{@@_sep:} |
| 486 |
% ~~\Arg{(2t)_1} \Arg{(2t)_2} \Arg{(2t)_3} \Arg{(2t)_4} \Arg{(2t)_5} \Arg{(2t)_6} \cs{@@_sep:} |
| 487 |
% ~~|{| \cs{@@_ln_c:NwNw} \meta{sign} |}| |
| 488 |
% ~~\meta{fixed tl} \meta{exponent} \cs{@@_sep:} \meta{continuation} |
| 489 |
% \end{syntax} |
| 490 |
% And we want to compute |
| 491 |
% \[ |
| 492 |
% \ln\left(\frac{1+t}{1-t}\right) |
| 493 |
% = 2t\left(1 + T \left(\frac{1}{3} + T \left(\frac{1}{5} |
| 494 |
% + T \left(\frac{1}{7} + T \left( \frac{1}{9} + \cdots |
| 495 |
% \right)\right)\right)\right)\right) |
| 496 |
% \] |
| 497 |
% The process looks as follows (\cs{@@_sep:} represented by |;|) |
| 498 |
% \begin{verbatim} |
| 499 |
% \loop 5; A; |
| 500 |
% \div_int 5; 1.0; \add A; \mul T; {\loop \eval 5-2;} |
| 501 |
% \add 0.2; A; \mul T; {\loop \eval 5-2;} |
| 502 |
% \mul B; T; {\loop 3;} |
| 503 |
% \loop 3; C; |
| 504 |
% \end{verbatim} |
| 505 |
% ^^A todo: doc |
| 506 |
% |
| 507 |
% This uses the routine for dividing a number by a small integer |
| 508 |
% (${}<10^4$). |
| 509 |
% \begin{macrocode} |
| 510 |
\cs_new:Npn \@@_ln_Taylor:wwNw |
| 511 |
{ |
| 512 |
\@@_ln_Taylor_loop:www |
| 513 |
21 \@@_sep: {0000}{0000}{0000}{0000}{0000}{0000} \@@_sep: |
| 514 |
} |
| 515 |
\cs_new:Npn \@@_ln_Taylor_loop:www #1\@@_sep: #2\@@_sep: #3\@@_sep: |
| 516 |
{ |
| 517 |
\if_int_compare:w #1 = \c_one_int |
| 518 |
\@@_ln_Taylor_break:w |
| 519 |
\fi: |
| 520 |
\exp_after:wN \@@_fixed_div_int:wwN \c_@@_one_fixed_tl #1\@@_sep: |
| 521 |
\@@_fixed_add:wwn #2\@@_sep: |
| 522 |
\@@_fixed_mul:wwn #3\@@_sep: |
| 523 |
{ |
| 524 |
\exp_after:wN \@@_ln_Taylor_loop:www |
| 525 |
\int_value:w \@@_int_eval:w #1 - 2 \@@_sep: |
| 526 |
} |
| 527 |
#3\@@_sep: |
| 528 |
} |
| 529 |
\cs_new:Npn \@@_ln_Taylor_break:w |
| 530 |
\fi: #1 \@@_fixed_add:wwn #2#3\@@_sep: #4 \@@_sep:\@@_sep: |
| 531 |
{ |
| 532 |
\fi: |
| 533 |
\exp_after:wN \@@_fixed_mul:wwn |
| 534 |
\exp_after:wN { \int_value:w \@@_int_eval:w 10000 + #2 } #3\@@_sep: |
| 535 |
} |
| 536 |
% \end{macrocode} |
| 537 |
% \end{macro} |
| 538 |
% |
| 539 |
% \begin{macro}{\@@_ln_c:NwNw} |
| 540 |
% \begin{syntax} |
| 541 |
% \cs{@@_ln_c:NwNw} \meta{sign} |
| 542 |
% ~~\Arg{r_1} \Arg{r_2} \Arg{r_3} \Arg{r_4} \Arg{r_5} \Arg{r_6} \cs{@@_sep:} |
| 543 |
% ~~\meta{fixed tl} \meta{exponent} \cs{@@_sep:} \meta{continuation} |
| 544 |
% \end{syntax} |
| 545 |
% We are now reduced to finding $\ln(c)$ and $\meta{exponent}\ln(10)$ |
| 546 |
% in a table, and adding it to the mixture. The first step is to |
| 547 |
% get $\ln(c) - \ln(x) = - \ln(a)$, then we get $|b|\ln(10)$ and add |
| 548 |
% or subtract. |
| 549 |
% |
| 550 |
% For now, $\ln(x)$ is given as $\cdot 10^0$. Unless both the exponent |
| 551 |
% is $1$ and $c=1$, we shift to working in units of $\cdot 10^4$, |
| 552 |
% since the final result is at least $\ln(10/7) \simeq 0.35$. |
| 553 |
% \begin{macrocode} |
| 554 |
\cs_new:Npn \@@_ln_c:NwNw #1 #2\@@_sep: #3 |
| 555 |
{ |
| 556 |
\if_meaning:w + #1 |
| 557 |
\exp_after:wN \exp_after:wN \exp_after:wN \@@_fixed_sub:wwn |
| 558 |
\else: |
| 559 |
\exp_after:wN \exp_after:wN \exp_after:wN \@@_fixed_add:wwn |
| 560 |
\fi: |
| 561 |
#3 #2 \@@_sep: |
| 562 |
} |
| 563 |
% \end{macrocode} |
| 564 |
% \end{macro} |
| 565 |
% |
| 566 |
% \begin{macro}{\@@_ln_exponent:wn} |
| 567 |
% \begin{syntax} |
| 568 |
% \cs{@@_ln_exponent:wn} |
| 569 |
% ~~\Arg{s_1} \Arg{s_2} \Arg{s_3} \Arg{s_4} \Arg{s_5} \Arg{s_6} \cs{@@_sep:} |
| 570 |
% ~~\Arg{exponent} |
| 571 |
% \end{syntax} |
| 572 |
% Compute \meta{exponent} times $\ln(10)$. Apart from the cases where |
| 573 |
% \meta{exponent} is $0$ or $1$, the result is necessarily at |
| 574 |
% least $\ln(10) \simeq 2.3$ in magnitude. We can thus drop the least |
| 575 |
% significant $4$ digits. In the case of a very large (positive or |
| 576 |
% negative) exponent, we can (and we need to) drop $4$ additional |
| 577 |
% digits, since the result is of order $10^4$. Naively, one would |
| 578 |
% think that in both cases we can drop $4$ more digits than we do, |
| 579 |
% but that would be slightly too tight for rounding to happen correctly. |
| 580 |
% Besides, we already have addition and subtraction for $24$ digits |
| 581 |
% fixed point numbers. |
| 582 |
% \begin{macrocode} |
| 583 |
\cs_new:Npn \@@_ln_exponent:wn #1\@@_sep: #2 |
| 584 |
{ |
| 585 |
\if_case:w #2 \exp_stop_f: |
| 586 |
0 \@@_case_return:nw { \@@_fixed_to_float_o:Nw 2 } |
| 587 |
\or: |
| 588 |
\exp_after:wN \@@_ln_exponent_one:ww \int_value:w |
| 589 |
\else: |
| 590 |
\if_int_compare:w #2 > \c_zero_int |
| 591 |
\exp_after:wN \@@_ln_exponent_small:NNww |
| 592 |
\exp_after:wN 0 |
| 593 |
\exp_after:wN \@@_fixed_sub:wwn \int_value:w |
| 594 |
\else: |
| 595 |
\exp_after:wN \@@_ln_exponent_small:NNww |
| 596 |
\exp_after:wN 2 |
| 597 |
\exp_after:wN \@@_fixed_add:wwn \int_value:w - |
| 598 |
\fi: |
| 599 |
\fi: |
| 600 |
#2\@@_sep: #1\@@_sep: |
| 601 |
} |
| 602 |
% \end{macrocode} |
| 603 |
% Now we painfully write all the cases.\footnote{Bruno: do rounding.} |
| 604 |
% No overflow nor underflow can happen, except when computing \texttt{ln(1)}. |
| 605 |
% \begin{macrocode} |
| 606 |
\cs_new:Npn \@@_ln_exponent_one:ww 1\@@_sep: #1\@@_sep: |
| 607 |
{ |
| 608 |
0 |
| 609 |
\exp_after:wN \@@_fixed_sub:wwn \c_@@_ln_x_fixed_tl #1\@@_sep: |
| 610 |
\@@_fixed_to_float_o:wN 0 |
| 611 |
} |
| 612 |
% \end{macrocode} |
| 613 |
% For small exponents, we just drop one block of digits, and set the |
| 614 |
% exponent of the log to $4$ (minus any shift coming from leading zeros |
| 615 |
% in the conversion from fixed point to floating point). Note that here |
| 616 |
% the exponent has been made positive. |
| 617 |
% \begin{macrocode} |
| 618 |
\cs_new:Npn \@@_ln_exponent_small:NNww #1#2#3\@@_sep: #4#5#6#7#8#9\@@_sep: |
| 619 |
{ |
| 620 |
4 |
| 621 |
\exp_after:wN \@@_fixed_mul:wwn |
| 622 |
\c_@@_ln_x_fixed_tl |
| 623 |
{#3}{0000}{0000}{0000}{0000}{0000} \@@_sep: |
| 624 |
#2 |
| 625 |
{0000}{#4}{#5}{#6}{#7}{#8}\@@_sep: |
| 626 |
\@@_fixed_to_float_o:wN #1 |
| 627 |
} |
| 628 |
% \end{macrocode} |
| 629 |
% \end{macro} |
| 630 |
% |
| 631 |
% \subsection{Exponential} |
| 632 |
% |
| 633 |
% \subsubsection{Sign, exponent, and special numbers} |
| 634 |
% |
| 635 |
% \begin{macro}[EXP]{\@@_exp_o:w} |
| 636 |
% \begin{macrocode} |
| 637 |
\cs_new:Npn \@@_exp_o:w #1 \s_@@ \@@_chk:w #2#3#4\@@_sep: @ |
| 638 |
{ |
| 639 |
\if_case:w #2 \exp_stop_f: |
| 640 |
\@@_case_return_o:Nw \c_one_fp |
| 641 |
\or: |
| 642 |
\exp_after:wN \@@_exp_normal_o:w |
| 643 |
\or: |
| 644 |
\if_meaning:w 0 #3 |
| 645 |
\exp_after:wN \@@_case_return_o:Nw |
| 646 |
\exp_after:wN \c_inf_fp |
| 647 |
\else: |
| 648 |
\exp_after:wN \@@_case_return_o:Nw |
| 649 |
\exp_after:wN \c_zero_fp |
| 650 |
\fi: |
| 651 |
\or: |
| 652 |
\@@_case_return_same_o:w |
| 653 |
\fi: |
| 654 |
\s_@@ \@@_chk:w #2#3#4\@@_sep: |
| 655 |
} |
| 656 |
% \end{macrocode} |
| 657 |
% \end{macro} |
| 658 |
% |
| 659 |
% \begin{macro}[EXP]{\@@_exp_normal_o:w, \@@_exp_pos_o:Nnwnw, \@@_exp_overflow:NN} |
| 660 |
% \begin{macrocode} |
| 661 |
\cs_new:Npn \@@_exp_normal_o:w \s_@@ \@@_chk:w 1#1 |
| 662 |
{ |
| 663 |
\if_meaning:w 0 #1 |
| 664 |
\@@_exp_pos_o:NNwnw + \@@_fixed_to_float_o:wN |
| 665 |
\else: |
| 666 |
\@@_exp_pos_o:NNwnw - \@@_fixed_inv_to_float_o:wN |
| 667 |
\fi: |
| 668 |
} |
| 669 |
\cs_new:Npn \@@_exp_pos_o:NNwnw #1#2#3 \fi: #4#5\@@_sep: |
| 670 |
{ |
| 671 |
\fi: |
| 672 |
\if_int_compare:w #4 > \c_@@_max_exp_exponent_int |
| 673 |
\token_if_eq_charcode:NNTF + #1 |
| 674 |
{ \@@_exp_overflow:NN \@@_overflow:w \c_inf_fp } |
| 675 |
{ \@@_exp_overflow:NN \@@_underflow:w \c_zero_fp } |
| 676 |
\exp:w |
| 677 |
\else: |
| 678 |
\exp_after:wN \@@_sanitize:Nw |
| 679 |
\exp_after:wN 0 |
| 680 |
\int_value:w #1 \@@_int_eval:w |
| 681 |
\if_int_compare:w #4 < \c_zero_int |
| 682 |
\exp_after:wN \use_i:nn |
| 683 |
\else: |
| 684 |
\exp_after:wN \use_ii:nn |
| 685 |
\fi: |
| 686 |
{ |
| 687 |
0 |
| 688 |
\@@_decimate:nNnnnn { - #4 } |
| 689 |
\@@_exp_Taylor:Nnnwn |
| 690 |
} |
| 691 |
{ |
| 692 |
\@@_decimate:nNnnnn { \c_@@_prec_int - #4 } |
| 693 |
\@@_exp_pos_large:NnnNwn |
| 694 |
} |
| 695 |
#5 |
| 696 |
{#4} |
| 697 |
#1 #2 0 |
| 698 |
\exp:w |
| 699 |
\fi: |
| 700 |
\exp_after:wN \exp_end: |
| 701 |
} |
| 702 |
\cs_new:Npn \@@_exp_overflow:NN #1#2 |
| 703 |
{ |
| 704 |
\exp_after:wN \exp_after:wN |
| 705 |
\exp_after:wN #1 |
| 706 |
\exp_after:wN #2 |
| 707 |
} |
| 708 |
% \end{macrocode} |
| 709 |
% \end{macro} |
| 710 |
% |
| 711 |
% \begin{macro}[EXP]{\@@_exp_Taylor:Nnnwn} |
| 712 |
% \begin{macro}[EXP]{\@@_exp_Taylor_loop:www, \@@_exp_Taylor_break:Nww} |
| 713 |
% This function is called for numbers in the range $[10^{-9}, |
| 714 |
% 10^{-1})$. We compute $10$ terms of the Taylor series. The |
| 715 |
% first argument is irrelevant (rounding digit used by some other |
| 716 |
% functions). The next three arguments, at least $16$ digits, |
| 717 |
% delimited by a \cs{@@_sep:}, form a fixed point number, so we pack it |
| 718 |
% in blocks of $4$ digits. |
| 719 |
% \begin{macrocode} |
| 720 |
\cs_new:Npn \@@_exp_Taylor:Nnnwn #1#2#3 #4\@@_sep: #5 #6 |
| 721 |
{ |
| 722 |
#6 |
| 723 |
\@@_pack_twice_four:wNNNNNNNN |
| 724 |
\@@_pack_twice_four:wNNNNNNNN |
| 725 |
\@@_pack_twice_four:wNNNNNNNN |
| 726 |
\@@_exp_Taylor_ii:ww |
| 727 |
\@@_sep: #2#3#4 0000 0000 \@@_sep: |
| 728 |
} |
| 729 |
\cs_new:Npn \@@_exp_Taylor_ii:ww #1\@@_sep: #2\@@_sep: |
| 730 |
{ \@@_exp_Taylor_loop:www 10 \@@_sep: #1 \@@_sep: #1 \@@_sep: \s_@@_stop } |
| 731 |
\cs_new:Npn \@@_exp_Taylor_loop:www #1\@@_sep: #2\@@_sep: #3\@@_sep: |
| 732 |
{ |
| 733 |
\if_int_compare:w #1 = \c_one_int |
| 734 |
\exp_after:wN \@@_exp_Taylor_break:Nww |
| 735 |
\fi: |
| 736 |
\@@_fixed_div_int:wwN #3 \@@_sep: #1 \@@_sep: |
| 737 |
\@@_fixed_add_one:wN |
| 738 |
\@@_fixed_mul:wwn #2 \@@_sep: |
| 739 |
{ |
| 740 |
\exp_after:wN \@@_exp_Taylor_loop:www |
| 741 |
\int_value:w \@@_int_eval:w #1 - 1 \@@_sep: |
| 742 |
#2 \@@_sep: |
| 743 |
} |
| 744 |
} |
| 745 |
\cs_new:Npn \@@_exp_Taylor_break:Nww #1 #2\@@_sep: #3 \s_@@_stop |
| 746 |
{ \@@_fixed_add_one:wN #2 \@@_sep: } |
| 747 |
% \end{macrocode} |
| 748 |
% \end{macro} |
| 749 |
% \end{macro} |
| 750 |
% |
| 751 |
% \begin{variable}{\c_@@_exp_intarray} |
| 752 |
% The integer array has $6\times 9\times 4=216$ items encoding the |
| 753 |
% values of $\exp(j\times 10^i)$ for $j=1,\dots,9$ and $i=-1,\dots,4$. |
| 754 |
% Each value is expressed as $\simeq 10^p \times 0.m_1m_2m_3$ with |
| 755 |
% three $8$-digit blocks $m_1$, $m_2$, $m_3$ and an integer |
| 756 |
% exponent~$p$ (one more than the scientific exponent), and these are |
| 757 |
% stored in the integer array as four items: $p$, $10^8+m_1$, |
| 758 |
% $10^8+m_2$, $10^8+m_3$. The various exponentials are stored in |
| 759 |
% increasing order of $j\times 10^i$. |
| 760 |
% |
| 761 |
% Storing this data in an integer array makes it slightly harder to |
| 762 |
% access (slower, too), but uses $16$ bytes of memory per exponential |
| 763 |
% stored, while storing as tokens used around $40$ tokens; tokens have |
| 764 |
% an especially large footprint in Unicode-aware engines. |
| 765 |
% \begin{macrocode} |
| 766 |
\intarray_const_from_clist:Nn \c_@@_exp_intarray |
| 767 |
{ |
| 768 |
1 , 1 1105 1709 , 1 1807 5647 , 1 6248 1171 , |
| 769 |
1 , 1 1221 4027 , 1 5816 0169 , 1 8339 2107 , |
| 770 |
1 , 1 1349 8588 , 1 0757 6003 , 1 1039 8374 , |
| 771 |
1 , 1 1491 8246 , 1 9764 1270 , 1 3178 2485 , |
| 772 |
1 , 1 1648 7212 , 1 7070 0128 , 1 1468 4865 , |
| 773 |
1 , 1 1822 1188 , 1 0039 0508 , 1 9748 7537 , |
| 774 |
1 , 1 2013 7527 , 1 0747 0476 , 1 5216 2455 , |
| 775 |
1 , 1 2225 5409 , 1 2849 2467 , 1 6045 7954 , |
| 776 |
1 , 1 2459 6031 , 1 1115 6949 , 1 6638 0013 , |
| 777 |
1 , 1 2718 2818 , 1 2845 9045 , 1 2353 6029 , |
| 778 |
1 , 1 7389 0560 , 1 9893 0650 , 1 2272 3043 , |
| 779 |
2 , 1 2008 5536 , 1 9231 8766 , 1 7740 9285 , |
| 780 |
2 , 1 5459 8150 , 1 0331 4423 , 1 9078 1103 , |
| 781 |
3 , 1 1484 1315 , 1 9102 5766 , 1 0342 1116 , |
| 782 |
3 , 1 4034 2879 , 1 3492 7351 , 1 2260 8387 , |
| 783 |
4 , 1 1096 6331 , 1 5842 8458 , 1 5992 6372 , |
| 784 |
4 , 1 2980 9579 , 1 8704 1728 , 1 2747 4359 , |
| 785 |
4 , 1 8103 0839 , 1 2757 5384 , 1 0077 1000 , |
| 786 |
5 , 1 2202 6465 , 1 7948 0671 , 1 6516 9579 , |
| 787 |
9 , 1 4851 6519 , 1 5409 7902 , 1 7796 9107 , |
| 788 |
14 , 1 1068 6474 , 1 5815 2446 , 1 2146 9905 , |
| 789 |
18 , 1 2353 8526 , 1 6837 0199 , 1 8540 7900 , |
| 790 |
22 , 1 5184 7055 , 1 2858 7072 , 1 4640 8745 , |
| 791 |
27 , 1 1142 0073 , 1 8981 5684 , 1 2836 6296 , |
| 792 |
31 , 1 2515 4386 , 1 7091 9167 , 1 0062 6578 , |
| 793 |
35 , 1 5540 6223 , 1 8439 3510 , 1 0525 7117 , |
| 794 |
40 , 1 1220 4032 , 1 9431 7840 , 1 8020 0271 , |
| 795 |
44 , 1 2688 1171 , 1 4181 6135 , 1 4484 1263 , |
| 796 |
87 , 1 7225 9737 , 1 6812 5749 , 1 2581 7748 , |
| 797 |
131 , 1 1942 4263 , 1 9524 1255 , 1 9365 8421 , |
| 798 |
174 , 1 5221 4696 , 1 8976 4143 , 1 9505 8876 , |
| 799 |
218 , 1 1403 5922 , 1 1785 2837 , 1 4107 3977 , |
| 800 |
261 , 1 3773 0203 , 1 0092 9939 , 1 8234 0143 , |
| 801 |
305 , 1 1014 2320 , 1 5473 5004 , 1 5094 5533 , |
| 802 |
348 , 1 2726 3745 , 1 7211 2566 , 1 5673 6478 , |
| 803 |
391 , 1 7328 8142 , 1 2230 7421 , 1 7051 8866 , |
| 804 |
435 , 1 1970 0711 , 1 1401 7046 , 1 9938 8888 , |
| 805 |
869 , 1 3881 1801 , 1 9428 4368 , 1 5764 8232 , |
| 806 |
1303 , 1 7646 2009 , 1 8905 4704 , 1 8893 1073 , |
| 807 |
1738 , 1 1506 3559 , 1 7005 0524 , 1 9009 7592 , |
| 808 |
2172 , 1 2967 6283 , 1 8402 3667 , 1 0689 6630 , |
| 809 |
2606 , 1 5846 4389 , 1 5650 2114 , 1 7278 5046 , |
| 810 |
3041 , 1 1151 7900 , 1 5080 6878 , 1 2914 4154 , |
| 811 |
3475 , 1 2269 1083 , 1 0850 6857 , 1 8724 4002 , |
| 812 |
3909 , 1 4470 3047 , 1 3316 5442 , 1 6408 6591 , |
| 813 |
4343 , 1 8806 8182 , 1 2566 2921 , 1 5872 6150 , |
| 814 |
8686 , 1 7756 0047 , 1 2598 6861 , 1 0458 3204 , |
| 815 |
13029 , 1 6830 5723 , 1 7791 4884 , 1 1932 7351 , |
| 816 |
17372 , 1 6015 5609 , 1 3095 3052 , 1 3494 7574 , |
| 817 |
21715 , 1 5297 7951 , 1 6443 0315 , 1 3251 3576 , |
| 818 |
26058 , 1 4665 6719 , 1 0099 3379 , 1 5527 2929 , |
| 819 |
30401 , 1 4108 9724 , 1 3326 3186 , 1 5271 5665 , |
| 820 |
34744 , 1 3618 6973 , 1 3140 0875 , 1 3856 4102 , |
| 821 |
39087 , 1 3186 9209 , 1 6113 3900 , 1 6705 9685 , |
| 822 |
} |
| 823 |
% \end{macrocode} |
| 824 |
% \end{variable} |
| 825 |
% |
| 826 |
% \begin{macro}[rEXP] |
| 827 |
% { |
| 828 |
% \@@_exp_pos_large:NnnNwn , |
| 829 |
% \@@_exp_large_after:wwn , |
| 830 |
% \@@_exp_large:NwN , |
| 831 |
% \@@_exp_intarray:w , |
| 832 |
% \@@_exp_intarray_aux:w , |
| 833 |
% } |
| 834 |
% The first two arguments are irrelevant (a rounding digit, and a |
| 835 |
% brace group with $8$ zeros). The third argument is the integer part |
| 836 |
% of our number, then we have the decimal part delimited by a |
| 837 |
% \cs{@@_sep:}, and finally the exponent, in the range $[0,5]$. Remove |
| 838 |
% leading zeros from the integer part: putting |#4| in there too |
| 839 |
% ensures that an integer part of $0$ is also removed. Then read |
| 840 |
% digits one by one, looking up $\exp(\meta{digit}\cdot |
| 841 |
% 10^{\meta{exponent}})$ in a table, and multiplying that to the |
| 842 |
% current total. The loop is done by \cs{@@_exp_large:NwN}, whose |
| 843 |
% |#1| is the \meta{exponent}, |#2| is the current mantissa, and |#3| |
| 844 |
% is the \meta{digit}. At the end, \cs{@@_exp_large_after:wwn} moves |
| 845 |
% on to the Taylor series, eventually multiplied with the mantissa |
| 846 |
% that we have just computed. |
| 847 |
% \begin{macrocode} |
| 848 |
\cs_new:Npn \@@_exp_pos_large:NnnNwn #1#2#3 #4#5\@@_sep: #6 |
| 849 |
{ |
| 850 |
\exp_after:wN \exp_after:wN \exp_after:wN \@@_exp_large:NwN |
| 851 |
\exp_after:wN \exp_after:wN \exp_after:wN #6 |
| 852 |
\exp_after:wN \c_@@_one_fixed_tl |
| 853 |
\int_value:w #3 #4 \exp_stop_f: |
| 854 |
#5 00000 \@@_sep: |
| 855 |
} |
| 856 |
\cs_new:Npn \@@_exp_large:NwN #1#2\@@_sep: #3 |
| 857 |
{ |
| 858 |
\if_case:w #3 ~ |
| 859 |
\exp_after:wN \@@_fixed_continue:wn |
| 860 |
\else: |
| 861 |
\exp_after:wN \@@_exp_intarray:w |
| 862 |
\int_value:w \@@_int_eval:w 36 * #1 + 4 * #3 \exp_after:wN \@@_sep: |
| 863 |
\fi: |
| 864 |
#2\@@_sep: |
| 865 |
{ |
| 866 |
\if_meaning:w 0 #1 |
| 867 |
\exp_after:wN \@@_exp_large_after:wwn |
| 868 |
\else: |
| 869 |
\exp_after:wN \@@_exp_large:NwN |
| 870 |
\int_value:w \@@_int_eval:w #1 - 1 \exp_after:wN \scan_stop: |
| 871 |
\fi: |
| 872 |
} |
| 873 |
} |
| 874 |
\cs_new:Npn \@@_exp_intarray:w #1 \@@_sep: |
| 875 |
{ |
| 876 |
+ |
| 877 |
\__kernel_intarray_item:Nn \c_@@_exp_intarray |
| 878 |
{ \@@_int_eval:w #1 - 3 \scan_stop: } |
| 879 |
\exp_after:wN \use_i:nnn |
| 880 |
\exp_after:wN \@@_fixed_mul:wwn |
| 881 |
\int_value:w 0 |
| 882 |
\exp_after:wN \@@_exp_intarray_aux:w |
| 883 |
\int_value:w \__kernel_intarray_item:Nn |
| 884 |
\c_@@_exp_intarray { \@@_int_eval:w #1 - 2 } |
| 885 |
\exp_after:wN \@@_exp_intarray_aux:w |
| 886 |
\int_value:w \__kernel_intarray_item:Nn |
| 887 |
\c_@@_exp_intarray { \@@_int_eval:w #1 - 1 } |
| 888 |
\exp_after:wN \@@_exp_intarray_aux:w |
| 889 |
\int_value:w \__kernel_intarray_item:Nn |
| 890 |
\c_@@_exp_intarray {#1} \@@_sep: \@@_sep: |
| 891 |
} |
| 892 |
\cs_new:Npn \@@_exp_intarray_aux:w 1 #1#2#3#4#5 \@@_sep: |
| 893 |
{ \@@_sep: {#1#2#3#4} {#5} } |
| 894 |
\cs_new:Npn \@@_exp_large_after:wwn #1\@@_sep: #2\@@_sep: #3 |
| 895 |
{ |
| 896 |
\@@_exp_Taylor:Nnnwn ? { } { } 0 #2\@@_sep: {} #3 |
| 897 |
\@@_fixed_mul:wwn #1\@@_sep: |
| 898 |
} |
| 899 |
% \end{macrocode} |
| 900 |
% \end{macro} |
| 901 |
% |
| 902 |
% \subsection{Power} |
| 903 |
% |
| 904 |
% Raising a number $a$ to a power $b$ leads to many distinct situations. |
| 905 |
% \begin{center}\def\abs#1{\lvert #1\rvert} |
| 906 |
% \begin{tabular}{>{$}c<{$}|*8{>{$}l<{$}}} |
| 907 |
% a^b &-\infty &(-\infty,-0) &-\text{integer} &\pm 0 &+\text{integer} &(0,\infty) &+\infty &\nan \\ \hline |
| 908 |
% +\infty &+0 &\multicolumn{2}{c}{$+0$} &+1 &\multicolumn{2}{c}{$+\infty$} &+\infty &\nan \\ |
| 909 |
% (1,\infty) &+0 &\multicolumn{2}{c}{$+\abs{a}^{b}$} &+1 &\multicolumn{2}{c}{$+\abs{a}^{b}$} &+\infty &\nan \\ |
| 910 |
% +1 &+1 &\multicolumn{2}{c}{$+1$} &+1 &\multicolumn{2}{c}{$+1$} &+1 &+1 \\ |
| 911 |
% (0,1) &+\infty &\multicolumn{2}{c}{$+\abs{a}^{b}$} &+1 &\multicolumn{2}{c}{$+\abs{a}^{b}$} &+0 &\nan \\ |
| 912 |
% +0 &+\infty &\multicolumn{2}{c}{$+\infty$} &+1 &\multicolumn{2}{c}{$+0$} &+0 &\nan \\ |
| 913 |
% -0 &+\infty &\nan &(-1)^b\infty &+1 &(-1)^b 0 &+0 &+0 &\nan \\ |
| 914 |
% (-1,0) &+\infty &\nan &(-1)^b\abs{a}^{b} &+1 &(-1)^b\abs{a}^{b} &\nan &+0 &\nan \\ |
| 915 |
% -1 &+1 &\nan &(-1)^b &+1 &(-1)^b &\nan &+1 &\nan \\ |
| 916 |
% (-\infty,-1) &+0 &\nan &(-1)^b\abs{a}^{b} &+1 &(-1)^b\abs{a}^{b} &\nan &+\infty &\nan \\ |
| 917 |
% -\infty &+0 &+0 &(-1)^b 0 &+1 &(-1)^b\infty &\nan &+\infty &\nan \\ |
| 918 |
% \nan &\nan &\nan &\nan &+1 &\nan &\nan &\nan &\nan \\ |
| 919 |
% \end{tabular} |
| 920 |
% \end{center} |
| 921 |
% We distinguished in this table the cases of finite (positive or |
| 922 |
% negative) integer exponents, as $(-1)^b$ is defined in that case. |
| 923 |
% One peculiarity of this operation is that $\nan^0 = 1^\nan = 1$, |
| 924 |
% because this relation is obeyed for any number, even $\pm\infty$. |
| 925 |
% |
| 926 |
% \begin{macro}[EXP]+\@@_^_o:ww+ |
| 927 |
% We cram most of the tests into a single function to save csnames. |
| 928 |
% First treat the case $b=0$: $a^0=1$ for any $a$, even \texttt{nan}. |
| 929 |
% Then test the sign of $a$. |
| 930 |
% \begin{itemize} |
| 931 |
% \item If it is positive, and $a$ is a normal number, call |
| 932 |
% \cs{@@_pow_normal_o:ww} followed by the two \texttt{fp} $a$ and $b$. |
| 933 |
% For $a=+0$ or $+\inf$, call \cs{@@_pow_zero_or_inf:ww} instead, to |
| 934 |
% return either $+0$ or $+\infty$ as appropriate. |
| 935 |
% \item If $a$ is a \texttt{nan}, then skip to the next \cs{@@_sep:} |
| 936 |
% (which happens to be conveniently the end of $b$) and return |
| 937 |
% \texttt{nan}. |
| 938 |
% \item Finally, if $a$ is negative, compute $|a|^b$ |
| 939 |
% (\cs{@@_pow_normal_o:ww} which ignores the sign of its first |
| 940 |
% operand), and keep an extra copy of $a$ and $b$ (the second brace |
| 941 |
% group, containing \{~$b$~$a$~\}, is inserted between $a$ and $b$). |
| 942 |
% Then do some tests to find the final sign of the result if it |
| 943 |
% exists. |
| 944 |
% \end{itemize} |
| 945 |
% \begin{macrocode} |
| 946 |
\cs_new:cpn { @@_ \iow_char:N \^ _o:ww } |
| 947 |
\s_@@ \@@_chk:w #1#2#3\@@_sep: \s_@@ \@@_chk:w #4#5#6\@@_sep: |
| 948 |
{ |
| 949 |
\if_meaning:w 0 #4 |
| 950 |
\@@_case_return_o:Nw \c_one_fp |
| 951 |
\fi: |
| 952 |
\if_case:w #2 \exp_stop_f: |
| 953 |
\exp_after:wN \use_i:nn |
| 954 |
\or: |
| 955 |
\@@_case_return_o:Nw \c_nan_fp |
| 956 |
\else: |
| 957 |
\exp_after:wN \@@_pow_neg:www |
| 958 |
\exp:w \exp_end_continue_f:w \exp_after:wN \use:nn |
| 959 |
\fi: |
| 960 |
{ |
| 961 |
\if_meaning:w 1 #1 |
| 962 |
\exp_after:wN \@@_pow_normal_o:ww |
| 963 |
\else: |
| 964 |
\exp_after:wN \@@_pow_zero_or_inf:ww |
| 965 |
\fi: |
| 966 |
\s_@@ \@@_chk:w #1#2#3\@@_sep: |
| 967 |
} |
| 968 |
{ \s_@@ \@@_chk:w #4#5#6\@@_sep: \s_@@ \@@_chk:w #1#2#3\@@_sep: } |
| 969 |
\s_@@ \@@_chk:w #4#5#6\@@_sep: |
| 970 |
} |
| 971 |
% \end{macrocode} |
| 972 |
% \end{macro} |
| 973 |
% |
| 974 |
% \begin{macro}[EXP]{\@@_pow_zero_or_inf:ww} |
| 975 |
% Raising $-0$ or $-\infty$ to \texttt{nan} yields \texttt{nan}. For |
| 976 |
% other powers, the result is $+0$ if $0$ is raised to a positive |
| 977 |
% power or $\infty$ to a negative power, and $+\infty$ otherwise. |
| 978 |
% Thus, if the type of $a$ and the sign of $b$ coincide, the result |
| 979 |
% is~$0$, since those conveniently take the same possible values, $0$ |
| 980 |
% and~$2$. Otherwise, either $a=\pm\infty$ and $b>0$ and the result |
| 981 |
% is $+\infty$, or $a=\pm 0$ with $b<0$ and we have a division by zero |
| 982 |
% unless $b=-\infty$. |
| 983 |
% \begin{macrocode} |
| 984 |
\cs_new:Npn \@@_pow_zero_or_inf:ww |
| 985 |
\s_@@ \@@_chk:w #1#2\@@_sep: \s_@@ \@@_chk:w #3#4 |
| 986 |
{ |
| 987 |
\if_meaning:w 1 #4 |
| 988 |
\@@_case_return_same_o:w |
| 989 |
\fi: |
| 990 |
\if_meaning:w #1 #4 |
| 991 |
\@@_case_return_o:Nw \c_zero_fp |
| 992 |
\fi: |
| 993 |
\if_meaning:w 2 #1 |
| 994 |
\@@_case_return_o:Nw \c_inf_fp |
| 995 |
\fi: |
| 996 |
\if_meaning:w 2 #3 |
| 997 |
\@@_case_return_o:Nw \c_inf_fp |
| 998 |
\else: |
| 999 |
\@@_case_use:nw |
| 1000 |
{ |
| 1001 |
\@@_division_by_zero_o:NNww \c_inf_fp ^ |
| 1002 |
\s_@@ \@@_chk:w #1 #2 \@@_sep: |
| 1003 |
} |
| 1004 |
\fi: |
| 1005 |
\s_@@ \@@_chk:w #3#4 |
| 1006 |
} |
| 1007 |
% \end{macrocode} |
| 1008 |
% \end{macro} |
| 1009 |
% |
| 1010 |
% \begin{macro}[EXP]{\@@_pow_normal_o:ww} |
| 1011 |
% We have in front of us $a$, and $b\neq 0$, we know that $a$ is a |
| 1012 |
% normal number, and we wish to compute $\lvert a\rvert^{b}$. If |
| 1013 |
% $\lvert a\rvert=1$, we return $1$, unless $a=-1$ and $b$ is |
| 1014 |
% \texttt{nan}. Indeed, returning $1$ at this point would wrongly |
| 1015 |
% raise \enquote{invalid} when the sign is considered. If $\lvert |
| 1016 |
% a\rvert\neq 1$, test the type of $b$: |
| 1017 |
% \begin{itemize} |
| 1018 |
% \item[0] Impossible, we already filtered $b=\pm 0$. |
| 1019 |
% \item[1] Call \cs{@@_pow_npos_o:Nww}. |
| 1020 |
% \item[2] Return $+\infty$ or $+0$ depending on the sign of $b$ and |
| 1021 |
% whether the exponent of $a$ is positive or not. |
| 1022 |
% \item[3] Return $b$. |
| 1023 |
% \end{itemize} |
| 1024 |
% \begin{macrocode} |
| 1025 |
\cs_new:Npn \@@_pow_normal_o:ww |
| 1026 |
\s_@@ \@@_chk:w 1 #1#2#3\@@_sep: \s_@@ \@@_chk:w #4#5 |
| 1027 |
{ |
| 1028 |
\if:w 0 \@@_str_if_eq:nn { #2 #3 } { 1 {1000} {0000} {0000} {0000} } |
| 1029 |
\if_int_compare:w #4 #1 = 32 \exp_stop_f: |
| 1030 |
\exp_after:wN \@@_case_return_ii_o:ww |
| 1031 |
\fi: |
| 1032 |
\@@_case_return_o:Nww \c_one_fp |
| 1033 |
\fi: |
| 1034 |
\if_case:w #4 \exp_stop_f: |
| 1035 |
\or: |
| 1036 |
\exp_after:wN \@@_pow_npos_o:Nww |
| 1037 |
\exp_after:wN #5 |
| 1038 |
\or: |
| 1039 |
\if_meaning:w 2 #5 \exp_after:wN \reverse_if:N \fi: |
| 1040 |
\if_int_compare:w #2 > \c_zero_int |
| 1041 |
\exp_after:wN \@@_case_return_o:Nww |
| 1042 |
\exp_after:wN \c_inf_fp |
| 1043 |
\else: |
| 1044 |
\exp_after:wN \@@_case_return_o:Nww |
| 1045 |
\exp_after:wN \c_zero_fp |
| 1046 |
\fi: |
| 1047 |
\or: |
| 1048 |
\@@_case_return_ii_o:ww |
| 1049 |
\fi: |
| 1050 |
\s_@@ \@@_chk:w 1 #1 {#2} #3 \@@_sep: |
| 1051 |
\s_@@ \@@_chk:w #4 #5 |
| 1052 |
} |
| 1053 |
% \end{macrocode} |
| 1054 |
% \end{macro} |
| 1055 |
% |
| 1056 |
% ^^A todo: check that we compute ln to 21 digits! |
| 1057 |
% \begin{macro}[EXP]{\@@_pow_npos_o:Nww} |
| 1058 |
% We now know that $a\neq\pm 1$ is a normal number, and $b$ is a |
| 1059 |
% normal number too. We want to compute $\lvert a\rvert^{b} = (\lvert |
| 1060 |
% x\rvert\cdot 10^{n})^{y\cdot 10^{p}} = \exp((\ln\lvert x\rvert + n |
| 1061 |
% \ln(10))\cdot y \cdot 10^{p}) = \exp(z)$. To compute the |
| 1062 |
% exponential accurately, we need to know the digits of $z$ up to the |
| 1063 |
% $16$-th position. Since the exponential of $10^{5}$ is infinite, we |
| 1064 |
% only need at most $21$ digits, hence the fixed point result of |
| 1065 |
% \cs{@@_ln_o:w} is precise enough for our needs. Start an integer |
| 1066 |
% expression for the decimal exponent of $e^{\lvert z\rvert}$. If $z$ |
| 1067 |
% is negative, negate that decimal exponent, and prepare to take the |
| 1068 |
% inverse when converting from the fixed point to the floating point result. |
| 1069 |
% \begin{macrocode} |
| 1070 |
\cs_new:Npn \@@_pow_npos_o:Nww #1 \s_@@ \@@_chk:w 1#2#3 |
| 1071 |
{ |
| 1072 |
\exp_after:wN \@@_sanitize:Nw |
| 1073 |
\exp_after:wN 0 |
| 1074 |
\int_value:w |
| 1075 |
\if:w #1 \if_int_compare:w #3 > \c_zero_int 0 \else: 2 \fi: |
| 1076 |
\exp_after:wN \@@_pow_npos_aux:NNnww |
| 1077 |
\exp_after:wN + |
| 1078 |
\exp_after:wN \@@_fixed_to_float_o:wN |
| 1079 |
\else: |
| 1080 |
\exp_after:wN \@@_pow_npos_aux:NNnww |
| 1081 |
\exp_after:wN - |
| 1082 |
\exp_after:wN \@@_fixed_inv_to_float_o:wN |
| 1083 |
\fi: |
| 1084 |
{#3} |
| 1085 |
} |
| 1086 |
% \end{macrocode} |
| 1087 |
% \end{macro} |
| 1088 |
% |
| 1089 |
%^^A begin[todo] |
| 1090 |
% \begin{macro}[EXP]{\@@_pow_npos_aux:NNnww} |
| 1091 |
% The first argument is the conversion function from fixed point to |
| 1092 |
% float. Then comes an exponent and the $4$ brace groups of $x$, |
| 1093 |
% followed by $b$. Compute $-\ln(x)$. |
| 1094 |
% \begin{macrocode} |
| 1095 |
\cs_new:Npn \@@_pow_npos_aux:NNnww |
| 1096 |
#1#2#3#4#5\@@_sep: \s_@@ \@@_chk:w 1#6#7#8\@@_sep: |
| 1097 |
{ |
| 1098 |
#1 |
| 1099 |
\@@_int_eval:w |
| 1100 |
\@@_ln_significand:NNNNnnnN #4#5 |
| 1101 |
\@@_pow_exponent:wnN {#3} |
| 1102 |
\@@_fixed_mul:wwn #8 {0000}{0000} \@@_sep: |
| 1103 |
\@@_pow_B:wwN #7\@@_sep: |
| 1104 |
#1 #2 0 % fixed_to_float_o:wN |
| 1105 |
} |
| 1106 |
\cs_new:Npn \@@_pow_exponent:wnN #1\@@_sep: #2 |
| 1107 |
{ |
| 1108 |
\if_int_compare:w #2 > \c_zero_int |
| 1109 |
\exp_after:wN \@@_pow_exponent:Nwnnnnnw % n\ln(10) - (-\ln(x)) |
| 1110 |
\exp_after:wN + |
| 1111 |
\else: |
| 1112 |
\exp_after:wN \@@_pow_exponent:Nwnnnnnw % -(|n|\ln(10) + (-\ln(x))) |
| 1113 |
\exp_after:wN - |
| 1114 |
\fi: |
| 1115 |
#2\@@_sep: #1\@@_sep: |
| 1116 |
} |
| 1117 |
\cs_new:Npn \@@_pow_exponent:Nwnnnnnw #1#2\@@_sep: #3#4#5#6#7#8\@@_sep: |
| 1118 |
{ %^^A todo: use that in ln. |
| 1119 |
\exp_after:wN \@@_fixed_mul_after:wwn |
| 1120 |
\int_value:w \@@_int_eval:w \c_@@_leading_shift_int |
| 1121 |
\exp_after:wN \@@_pack:NNNNNw |
| 1122 |
\int_value:w \@@_int_eval:w \c_@@_middle_shift_int |
| 1123 |
#1#2*23025 - #1 #3 |
| 1124 |
\exp_after:wN \@@_pack:NNNNNw |
| 1125 |
\int_value:w \@@_int_eval:w \c_@@_middle_shift_int |
| 1126 |
#1 #2*8509 - #1 #4 |
| 1127 |
\exp_after:wN \@@_pack:NNNNNw |
| 1128 |
\int_value:w \@@_int_eval:w \c_@@_middle_shift_int |
| 1129 |
#1 #2*2994 - #1 #5 |
| 1130 |
\exp_after:wN \@@_pack:NNNNNw |
| 1131 |
\int_value:w \@@_int_eval:w \c_@@_middle_shift_int |
| 1132 |
#1 #2*0456 - #1 #6 |
| 1133 |
\exp_after:wN \@@_pack:NNNNNw |
| 1134 |
\int_value:w \@@_int_eval:w \c_@@_trailing_shift_int |
| 1135 |
#1 #2*8401 - #1 #7 |
| 1136 |
#1 ( #2*7991 - #8 ) / 1 0000 \@@_sep: \@@_sep: |
| 1137 |
} |
| 1138 |
\cs_new:Npn \@@_pow_B:wwN #1#2#3#4#5#6\@@_sep: #7\@@_sep: |
| 1139 |
{ |
| 1140 |
\if_int_compare:w #7 < \c_zero_int |
| 1141 |
\exp_after:wN \@@_pow_C_neg:w \int_value:w - |
| 1142 |
\else: |
| 1143 |
\if_int_compare:w #7 < 22 \exp_stop_f: |
| 1144 |
\exp_after:wN \@@_pow_C_pos:w \int_value:w |
| 1145 |
\else: |
| 1146 |
\exp_after:wN \@@_pow_C_overflow:w \int_value:w |
| 1147 |
\fi: |
| 1148 |
\fi: |
| 1149 |
#7 \exp_after:wN \@@_sep: |
| 1150 |
\int_value:w \@@_int_eval:w 10 0000 + #1 \@@_int_eval_end: |
| 1151 |
#2#3#4#5#6 0000 0000 0000 0000 0000 0000 \@@_sep: %^^A todo: how many 0? |
| 1152 |
} |
| 1153 |
\cs_new:Npn \@@_pow_C_overflow:w #1\@@_sep: #2\@@_sep: #3 |
| 1154 |
{ |
| 1155 |
+ 2 * \c_@@_max_exponent_int |
| 1156 |
\exp_after:wN \@@_fixed_continue:wn \c_@@_one_fixed_tl |
| 1157 |
} |
| 1158 |
\cs_new:Npn \@@_pow_C_neg:w #1 \@@_sep: 1 |
| 1159 |
{ |
| 1160 |
\exp_after:wN \exp_after:wN \exp_after:wN \@@_pow_C_pack:w |
| 1161 |
\prg_replicate:nn {#1} {0} |
| 1162 |
} |
| 1163 |
\cs_new:Npn \@@_pow_C_pos:w #1\@@_sep: 1 |
| 1164 |
{ \@@_pow_C_pos_loop:wN #1\@@_sep: } |
| 1165 |
\cs_new:Npn \@@_pow_C_pos_loop:wN #1\@@_sep: #2 |
| 1166 |
{ |
| 1167 |
\if_meaning:w 0 #1 |
| 1168 |
\exp_after:wN \@@_pow_C_pack:w |
| 1169 |
\exp_after:wN #2 |
| 1170 |
\else: |
| 1171 |
\if_meaning:w 0 #2 |
| 1172 |
\exp_after:wN \@@_pow_C_pos_loop:wN \int_value:w |
| 1173 |
\else: |
| 1174 |
\exp_after:wN \@@_pow_C_overflow:w \int_value:w |
| 1175 |
\fi: |
| 1176 |
\@@_int_eval:w #1 - 1 \exp_after:wN \@@_sep: |
| 1177 |
\fi: |
| 1178 |
} |
| 1179 |
\cs_new:Npn \@@_pow_C_pack:w |
| 1180 |
{ |
| 1181 |
\exp_after:wN \@@_exp_large:NwN |
| 1182 |
\exp_after:wN 5 |
| 1183 |
\c_@@_one_fixed_tl |
| 1184 |
} |
| 1185 |
% \end{macrocode} |
| 1186 |
% \end{macro} |
| 1187 |
%^^A end[todo] |
| 1188 |
% |
| 1189 |
% \begin{macro}[EXP]{\@@_pow_neg:www, \@@_pow_neg_aux:wNN} |
| 1190 |
% This function is followed by three floating point numbers: $|a|^b$, |
| 1191 |
% $a\in[-\infty,-0]$, and $b$. If $b$ is an even integer (case $-1$), |
| 1192 |
% $a^b=|a|^b$. If $b$ is an odd integer (case $0$), $a^b=-|a|^b$, |
| 1193 |
% obtained by a call to \cs{@@_pow_neg_aux:wNN}. Otherwise, the sign is |
| 1194 |
% undefined. This is invalid, unless $|a|^b$ turns out to be $+0$ or |
| 1195 |
% \texttt{nan}, in which case we return that as $a^b$. In particular, |
| 1196 |
% since the underflow detection occurs before \cs{@@_pow_neg:www} is |
| 1197 |
% called, |(-0.1)**(12345.67)| gives $+0$ rather than complaining |
| 1198 |
% that the sign is not defined. |
| 1199 |
% \begin{macrocode} |
| 1200 |
\cs_new:Npn \@@_pow_neg:www |
| 1201 |
\s_@@ \@@_chk:w #1#2\@@_sep: #3\@@_sep: #4\@@_sep: |
| 1202 |
{ |
| 1203 |
\if_case:w \@@_pow_neg_case:w #4 \@@_sep: |
| 1204 |
\exp_after:wN \@@_pow_neg_aux:wNN |
| 1205 |
\or: |
| 1206 |
\if_int_compare:w \@@_int_eval:w #1 / 2 = \c_one_int |
| 1207 |
\@@_invalid_operation_o:Nww ^ #3\@@_sep: #4\@@_sep: |
| 1208 |
\exp:w \exp_end_continue_f:w |
| 1209 |
\exp_after:wN \exp_after:wN |
| 1210 |
\exp_after:wN \@@_use_none_until_s:w |
| 1211 |
\fi: |
| 1212 |
\fi: |
| 1213 |
\@@_exp_after_o:w |
| 1214 |
\s_@@ \@@_chk:w #1#2\@@_sep: |
| 1215 |
} |
| 1216 |
\cs_new:Npn \@@_pow_neg_aux:wNN #1 \s_@@ \@@_chk:w #2#3 |
| 1217 |
{ |
| 1218 |
\exp_after:wN \@@_exp_after_o:w |
| 1219 |
\exp_after:wN \s_@@ |
| 1220 |
\exp_after:wN \@@_chk:w |
| 1221 |
\exp_after:wN #2 |
| 1222 |
\int_value:w \@@_int_eval:w 2 - #3 \@@_int_eval_end: |
| 1223 |
} |
| 1224 |
% \end{macrocode} |
| 1225 |
% ^^A todo: is this \@@_exp_after_o:w necessary? Appropriate? |
| 1226 |
% \end{macro} |
| 1227 |
% |
| 1228 |
% \begin{macro}[rEXP] |
| 1229 |
% { |
| 1230 |
% \@@_pow_neg_case:w, \@@_pow_neg_case_aux:nnnnn, |
| 1231 |
% \@@_pow_neg_case_aux:Nnnw |
| 1232 |
% } |
| 1233 |
% This function expects a floating point number, and determines its |
| 1234 |
% \enquote{parity}. It should be used after \cs{if_case:w} or in an |
| 1235 |
% integer expression. It gives $-1$ if the number is an even integer, |
| 1236 |
% $0$~if the number is an odd integer, and $1$~otherwise. Zeros and |
| 1237 |
% $\pm\infty$ are even (because very large finite floating points are |
| 1238 |
% even), while \texttt{nan} is a non-integer. The sign of normal |
| 1239 |
% numbers is irrelevant to parity. After \cs{@@_decimate:nNnnnn} the |
| 1240 |
% argument |#1| of \cs{@@_pow_neg_case_aux:Nnnw} is a rounding digit, |
| 1241 |
% |0|~if and only if the number was an integer, and |#3| is the $8$ |
| 1242 |
% least significant digits of that integer. |
| 1243 |
% \begin{macrocode} |
| 1244 |
\cs_new:Npn \@@_pow_neg_case:w \s_@@ \@@_chk:w #1#2#3\@@_sep: |
| 1245 |
{ |
| 1246 |
\if_case:w #1 \exp_stop_f: |
| 1247 |
-1 |
| 1248 |
\or: \@@_pow_neg_case_aux:nnnnn #3 |
| 1249 |
\or: -1 |
| 1250 |
\else: 1 |
| 1251 |
\fi: |
| 1252 |
\exp_stop_f: |
| 1253 |
} |
| 1254 |
\cs_new:Npn \@@_pow_neg_case_aux:nnnnn #1#2#3#4#5 |
| 1255 |
{ |
| 1256 |
\if_int_compare:w #1 > \c_@@_prec_int |
| 1257 |
-1 |
| 1258 |
\else: |
| 1259 |
\@@_decimate:nNnnnn { \c_@@_prec_int - #1 } |
| 1260 |
\@@_pow_neg_case_aux:Nnnw |
| 1261 |
{#2} {#3} {#4} {#5} |
| 1262 |
\fi: |
| 1263 |
} |
| 1264 |
\cs_new:Npn \@@_pow_neg_case_aux:Nnnw #1#2#3#4 \@@_sep: |
| 1265 |
{ |
| 1266 |
\if_meaning:w 0 #1 |
| 1267 |
\if_int_odd:w #3 \exp_stop_f: |
| 1268 |
0 |
| 1269 |
\else: |
| 1270 |
-1 |
| 1271 |
\fi: |
| 1272 |
\else: |
| 1273 |
1 |
| 1274 |
\fi: |
| 1275 |
} |
| 1276 |
% \end{macrocode} |
| 1277 |
% \end{macro} |
| 1278 |
% |
| 1279 |
% \subsection{Factorial} |
| 1280 |
% |
| 1281 |
% \begin{variable}{\c_@@_fact_max_arg_int} |
| 1282 |
% The maximum integer whose factorial fits in the exponent range is |
| 1283 |
% $3248$, as $3249!\sim 10^{10000.8}$ |
| 1284 |
% \begin{macrocode} |
| 1285 |
\int_const:Nn \c_@@_fact_max_arg_int { 3248 } |
| 1286 |
% \end{macrocode} |
| 1287 |
% \end{variable} |
| 1288 |
% |
| 1289 |
% \begin{macro}[EXP]{\@@_fact_o:w} |
| 1290 |
% First detect $\pm 0$ and $+\infty$ and \texttt{nan}. Then note that |
| 1291 |
% factorial of anything with a negative sign (except $-0$) is |
| 1292 |
% undefined. Then call \cs{@@_small_int:wTF} to get an integer as the |
| 1293 |
% argument, and start a loop. This is not the most efficient way of |
| 1294 |
% computing the factorial, but it works all right. Of course we work |
| 1295 |
% with $24$ digits instead of~$16$. It is easy to check that |
| 1296 |
% computing factorials with this precision is enough. |
| 1297 |
% \begin{macrocode} |
| 1298 |
\cs_new:Npn \@@_fact_o:w #1 \s_@@ \@@_chk:w #2#3#4\@@_sep: @ |
| 1299 |
{ |
| 1300 |
\if_case:w #2 \exp_stop_f: |
| 1301 |
\@@_case_return_o:Nw \c_one_fp |
| 1302 |
\or: |
| 1303 |
\or: |
| 1304 |
\if_meaning:w 0 #3 |
| 1305 |
\exp_after:wN \@@_case_return_same_o:w |
| 1306 |
\fi: |
| 1307 |
\or: |
| 1308 |
\@@_case_return_same_o:w |
| 1309 |
\fi: |
| 1310 |
\if_meaning:w 2 #3 |
| 1311 |
\@@_case_use:nw { \@@_invalid_operation_o:fw { fact } } |
| 1312 |
\fi: |
| 1313 |
\@@_fact_pos_o:w |
| 1314 |
\s_@@ \@@_chk:w #2 #3 #4 \@@_sep: |
| 1315 |
} |
| 1316 |
% \end{macrocode} |
| 1317 |
% \end{macro} |
| 1318 |
% |
| 1319 |
% \begin{macro}[EXP]{\@@_fact_pos_o:w, \@@_fact_int_o:w} |
| 1320 |
% Then check the input is an integer, and call |
| 1321 |
% \cs{@@_facorial_int_o:n} with that \texttt{int} as an argument. If |
| 1322 |
% it's too big the factorial overflows. Otherwise call |
| 1323 |
% \cs{@@_sanitize:Nw} with a positive sign marker~|0| and an integer |
| 1324 |
% expression that will mop up any exponent in the calculation. |
| 1325 |
% \begin{macrocode} |
| 1326 |
\cs_new:Npn \@@_fact_pos_o:w #1\@@_sep: |
| 1327 |
{ |
| 1328 |
\@@_small_int:wTF #1\@@_sep: |
| 1329 |
{ \@@_fact_int_o:n } |
| 1330 |
{ \@@_invalid_operation_o:fw { fact } #1\@@_sep: } |
| 1331 |
} |
| 1332 |
\cs_new:Npn \@@_fact_int_o:n #1 |
| 1333 |
{ |
| 1334 |
\if_int_compare:w #1 > \c_@@_fact_max_arg_int |
| 1335 |
\@@_case_return:nw |
| 1336 |
{ |
| 1337 |
\exp_after:wN \exp_after:wN \exp_after:wN \@@_overflow:w |
| 1338 |
\exp_after:wN \c_inf_fp |
| 1339 |
} |
| 1340 |
\fi: |
| 1341 |
\exp_after:wN \@@_sanitize:Nw |
| 1342 |
\exp_after:wN 0 |
| 1343 |
\int_value:w \@@_int_eval:w |
| 1344 |
\@@_fact_loop_o:w #1 . 4 , { 1 } { } { } { } { } { } \@@_sep: |
| 1345 |
} |
| 1346 |
% \end{macrocode} |
| 1347 |
% \end{macro} |
| 1348 |
% |
| 1349 |
% \begin{macro}[EXP]{\@@_fact_loop_o:w} |
| 1350 |
% The loop receives an integer |#1| whose factorial we want to |
| 1351 |
% compute, which we progressively decrement, and the result so far as |
| 1352 |
% an extended-precision number |#2| in the form |
| 1353 |
% \meta{exponent}|,|\meta{mantissa}\cs{@@_sep:}. The loop goes in steps of two |
| 1354 |
% because we compute |#1*#1-1| as an integer expression (it must fit |
| 1355 |
% since |#1| is at most $3248$), then multiply with the result so far. |
| 1356 |
% We don't need to fill in most of the mantissa with zeros because |
| 1357 |
% \cs{@@_ep_mul:wwwwn} first normalizes the extended precision number |
| 1358 |
% to avoid loss of precision. When reaching a small enough number |
| 1359 |
% simply use a table of factorials less than $10^8$. This limit is |
| 1360 |
% chosen because the normalization step cannot deal with larger |
| 1361 |
% integers. |
| 1362 |
% \begin{macrocode} |
| 1363 |
\cs_new:Npn \@@_fact_loop_o:w #1 . #2 \@@_sep: |
| 1364 |
{ |
| 1365 |
\if_int_compare:w #1 < 12 \exp_stop_f: |
| 1366 |
\@@_fact_small_o:w #1 |
| 1367 |
\fi: |
| 1368 |
\exp_after:wN \@@_ep_mul:wwwwn |
| 1369 |
\exp_after:wN 4 \exp_after:wN , |
| 1370 |
\exp_after:wN { \int_value:w \@@_int_eval:w #1 * (#1 - 1) } |
| 1371 |
{ } { } { } { } { } \@@_sep: |
| 1372 |
#2 \@@_sep: |
| 1373 |
{ |
| 1374 |
\exp_after:wN \@@_fact_loop_o:w |
| 1375 |
\int_value:w \@@_int_eval:w #1 - 2 . |
| 1376 |
} |
| 1377 |
} |
| 1378 |
\cs_new:Npn \@@_fact_small_o:w #1 \fi: #2 \@@_sep: #3 \@@_sep: #4 |
| 1379 |
{ |
| 1380 |
\fi: |
| 1381 |
\exp_after:wN \@@_ep_mul:wwwwn |
| 1382 |
\exp_after:wN 4 \exp_after:wN , |
| 1383 |
\exp_after:wN |
| 1384 |
{ |
| 1385 |
\int_value:w |
| 1386 |
\if_case:w #1 \exp_stop_f: |
| 1387 |
1 \or: 1 \or: 2 \or: 6 \or: 24 \or: 120 \or: 720 \or: 5040 |
| 1388 |
\or: 40320 \or: 362880 \or: 3628800 \or: 39916800 |
| 1389 |
\fi: |
| 1390 |
} { } { } { } { } { } \@@_sep: |
| 1391 |
#3 \@@_sep: |
| 1392 |
\@@_ep_to_float_o:wwN 0 |
| 1393 |
} |
| 1394 |
% \end{macrocode} |
| 1395 |
% \end{macro} |
| 1396 |
% |
| 1397 |
% \begin{macrocode} |
| 1398 |
%</code> |
| 1399 |
% \end{macrocode} |
| 1400 |
% |
| 1401 |
% \end{implementation} |
| 1402 |
% |
| 1403 |
% \PrintChanges |
| 1404 |
% |
| 1405 |
% \PrintIndex |