| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3fp-extended.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-extended} module\\ |
| 35 |
% Manipulating numbers with extended precision, for internal use^^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-extended} implementation} |
| 56 |
% |
| 57 |
% \begin{macrocode} |
| 58 |
%<*code> |
| 59 |
% \end{macrocode} |
| 60 |
% |
| 61 |
% \begin{macrocode} |
| 62 |
%<@@=fp> |
| 63 |
% \end{macrocode} |
| 64 |
% |
| 65 |
% \subsection{Description of fixed point numbers} |
| 66 |
% |
| 67 |
% This module provides a few functions to manipulate positive floating |
| 68 |
% point numbers with extended precision ($24$ digits), but mostly |
| 69 |
% provides functions for fixed-point numbers with this precision ($24$ |
| 70 |
% digits). Those are used in the computation of |
| 71 |
% Taylor series for the logarithm, exponential, and trigonometric |
| 72 |
% functions. Since we eventually only care about the $16$ first digits |
| 73 |
% of the final result, some of the calculations are not performed with |
| 74 |
% the full $24$-digit precision. In other words, the last two blocks of |
| 75 |
% each fixed point number may be wrong as long as the error is small |
| 76 |
% enough to be rounded away when converting back to a floating point |
| 77 |
% number. The fixed point numbers are expressed as |
| 78 |
% \begin{quote} |
| 79 |
% \Arg{a_1} \Arg{a_2} \Arg{a_3} \Arg{a_4} \Arg{a_5} \Arg{a_6} \cs{@@_sep:} |
| 80 |
% \end{quote} |
| 81 |
% where each \meta{a_i} is exactly $4$ digits (ranging from |0000| to |
| 82 |
% |9999|), except \meta{a_1}, which may be any \enquote{not-too-large} |
| 83 |
% non-negative integer, with or without leading zeros. Here, |
| 84 |
% \enquote{not-too-large} depends on the specific function (see the |
| 85 |
% corresponding comments for details). Checking for overflow is the |
| 86 |
% responsibility of the code calling those functions. The fixed point |
| 87 |
% number $a$ corresponding to the representation above is $a = |
| 88 |
% \sum_{i=1}^{6} \meta{a_i} \cdot 10^{-4i}$. |
| 89 |
% |
| 90 |
% Most functions we define here have the form |
| 91 |
% \begin{syntax} |
| 92 |
% \cs{@@_fixed_\meta{calculation}:wwn} \meta{operand_1} \cs{@@_sep:} |
| 93 |
% ~~\meta{operand_2} \cs{@@_sep:} \Arg{continuation} |
| 94 |
% \end{syntax} |
| 95 |
% They perform the \meta{calculation} on the two \meta{operands}, then |
| 96 |
% feed the result ($6$ brace groups followed by a \cs{@@_sep:}) to the |
| 97 |
% \meta{continuation}, responsible for the next step of the calculation. |
| 98 |
% Some functions only accept an \texttt{N}-type \meta{continuation}. |
| 99 |
% This allows constructions such as |
| 100 |
% \begin{quote} |
| 101 |
% \cs{@@_fixed_add:wwn} \meta{X_1} \cs{@@_sep:} \meta{X_2} \cs{@@_sep:} \\ |
| 102 |
% \cs{@@_fixed_mul:wwn} \meta{X_3} \cs{@@_sep:} \\ |
| 103 |
% \cs{@@_fixed_add:wwn} \meta{X_4} \cs{@@_sep:} \\ |
| 104 |
% \end{quote} |
| 105 |
% to compute $(X_1+X_2)\cdot X_3 + X_4$. This turns out to be very |
| 106 |
% appropriate for computing continued fractions and Taylor series. |
| 107 |
% |
| 108 |
% At the end of the calculation, the result is turned back to a floating |
| 109 |
% point number using \cs{@@_fixed_to_float_o:wN}. This function has to |
| 110 |
% change the exponent of the floating point number: it must be used |
| 111 |
% after starting an integer expression for the overall exponent of the |
| 112 |
% result. |
| 113 |
% |
| 114 |
% \subsection{Helpers for numbers with extended precision} |
| 115 |
% |
| 116 |
% \begin{variable}{\c_@@_one_fixed_tl} |
| 117 |
% The fixed-point number~$1$, used in \pkg{l3fp-expo}. |
| 118 |
% \begin{macrocode} |
| 119 |
\tl_const:Nn \c_@@_one_fixed_tl |
| 120 |
{ {10000} {0000} {0000} {0000} {0000} {0000} \@@_sep: } |
| 121 |
% \end{macrocode} |
| 122 |
% \end{variable} |
| 123 |
% |
| 124 |
% \begin{macro}[EXP]{\@@_fixed_continue:wn} |
| 125 |
% This function simply calls the next function. |
| 126 |
% \begin{macrocode} |
| 127 |
\cs_new:Npn \@@_fixed_continue:wn #1\@@_sep: #2 { #2 #1\@@_sep: } |
| 128 |
% \end{macrocode} |
| 129 |
% \end{macro} |
| 130 |
% |
| 131 |
% \begin{macro}[EXP]{\@@_fixed_add_one:wN} |
| 132 |
% \begin{syntax} |
| 133 |
% \cs{@@_fixed_add_one:wN} \meta{a} \cs{@@_sep:} \meta{continuation} |
| 134 |
% \end{syntax} |
| 135 |
% This function adds $1$ to the fixed point \meta{a}, by changing |
| 136 |
% $a_1$ to $10000+a_1$, then calls the \meta{continuation}. This |
| 137 |
% requires $a_1 + 10000 < 2^{31}$. |
| 138 |
% \begin{macrocode} |
| 139 |
\cs_new:Npn \@@_fixed_add_one:wN #1#2\@@_sep: #3 |
| 140 |
{ |
| 141 |
\exp_after:wN #3 \exp_after:wN |
| 142 |
{ \int_value:w \@@_int_eval:w \c_@@_myriad_int + #1 } #2 \@@_sep: |
| 143 |
} |
| 144 |
% \end{macrocode} |
| 145 |
% \end{macro} |
| 146 |
% |
| 147 |
% \begin{macro}[EXP]{\@@_fixed_div_myriad:wn} |
| 148 |
% Divide a fixed point number by $10000$. This is a little bit more |
| 149 |
% subtle than just removing the last group and adding a leading group |
| 150 |
% of zeros: the first group~|#1| may have any number of digits, and we |
| 151 |
% must split~|#1| into the new first group and a second group of |
| 152 |
% exactly $4$~digits. The choice of shifts allows~|#1| to be in the |
| 153 |
% range $[0, 5\cdot 10^{8}-1]$. |
| 154 |
% \begin{macrocode} |
| 155 |
\cs_new:Npn \@@_fixed_div_myriad:wn #1#2#3#4#5#6\@@_sep: |
| 156 |
{ |
| 157 |
\exp_after:wN \@@_fixed_mul_after:wwn |
| 158 |
\int_value:w \@@_int_eval:w \c_@@_leading_shift_int |
| 159 |
\exp_after:wN \@@_pack:NNNNNw |
| 160 |
\int_value:w \@@_int_eval:w \c_@@_trailing_shift_int |
| 161 |
+ #1 \@@_sep: {#2}{#3}{#4}{#5}\@@_sep: |
| 162 |
} |
| 163 |
% \end{macrocode} |
| 164 |
% \end{macro} |
| 165 |
% |
| 166 |
% \begin{macro}[EXP]{\@@_fixed_mul_after:wwn} |
| 167 |
% The fixed point operations which involve multiplication end by |
| 168 |
% calling this auxiliary. It braces the last block of digits, and |
| 169 |
% places the \meta{continuation} |#3| in front. |
| 170 |
% \begin{macrocode} |
| 171 |
\cs_new:Npn \@@_fixed_mul_after:wwn #1\@@_sep: #2\@@_sep: #3 |
| 172 |
{ #3 {#1} #2\@@_sep: } |
| 173 |
% \end{macrocode} |
| 174 |
% \end{macro} |
| 175 |
% |
| 176 |
% \subsection{Multiplying a fixed point number by a short one} |
| 177 |
% |
| 178 |
% \begin{macro}[EXP]{\@@_fixed_mul_short:wwn} |
| 179 |
% \begin{syntax}\parskip=0pt\obeylines |
| 180 |
% \cs{@@_fixed_mul_short:wwn} |
| 181 |
% | |\Arg{a_1} \Arg{a_2} \Arg{a_3} \Arg{a_4} \Arg{a_5} \Arg{a_6} \cs{@@_sep:} |
| 182 |
% | |\Arg{b_0} \Arg{b_1} \Arg{b_2} \cs{@@_sep:} \Arg{continuation} |
| 183 |
% \end{syntax} |
| 184 |
% Computes the product $c=ab$ of $a=\sum_i \meta{a_i} 10^{-4i}$ and |
| 185 |
% $b=\sum_i \meta{b_i} 10^{-4i}$, rounds it to the closest multiple of |
| 186 |
% $10^{-24}$, and leaves \meta{continuation} \Arg{c_1} \ldots{} |
| 187 |
% \Arg{c_6} \cs{@@_sep:} in the input stream, where each of the \meta{c_i} are |
| 188 |
% blocks of $4$~digits, except \meta{c_1}, which is any \TeX{} |
| 189 |
% integer. Note that indices for \meta{b} start at~$0$: for instance |
| 190 |
% a second operand of |{0001}{0000}{0000}| leaves the first operand |
| 191 |
% unchanged (rather than dividing it by $10^{4}$, as |
| 192 |
% \cs{@@_fixed_mul:wwn} would). |
| 193 |
% \begin{macrocode} |
| 194 |
\cs_new:Npn \@@_fixed_mul_short:wwn #1#2#3#4#5#6\@@_sep: #7#8#9\@@_sep: |
| 195 |
{ |
| 196 |
\exp_after:wN \@@_fixed_mul_after:wwn |
| 197 |
\int_value:w \@@_int_eval:w \c_@@_leading_shift_int |
| 198 |
+ #1*#7 |
| 199 |
\exp_after:wN \@@_pack:NNNNNw |
| 200 |
\int_value:w \@@_int_eval:w \c_@@_middle_shift_int |
| 201 |
+ #1*#8 + #2*#7 |
| 202 |
\exp_after:wN \@@_pack:NNNNNw |
| 203 |
\int_value:w \@@_int_eval:w \c_@@_middle_shift_int |
| 204 |
+ #1*#9 + #2*#8 + #3*#7 |
| 205 |
\exp_after:wN \@@_pack:NNNNNw |
| 206 |
\int_value:w \@@_int_eval:w \c_@@_middle_shift_int |
| 207 |
+ #2*#9 + #3*#8 + #4*#7 |
| 208 |
\exp_after:wN \@@_pack:NNNNNw |
| 209 |
\int_value:w \@@_int_eval:w \c_@@_middle_shift_int |
| 210 |
+ #3*#9 + #4*#8 + #5*#7 |
| 211 |
\exp_after:wN \@@_pack:NNNNNw |
| 212 |
\int_value:w \@@_int_eval:w \c_@@_trailing_shift_int |
| 213 |
+ #4*#9 + #5*#8 + #6*#7 |
| 214 |
+ ( #5*#9 + #6*#8 + #6*#9 / \c_@@_myriad_int ) |
| 215 |
/ \c_@@_myriad_int \@@_sep: \@@_sep: |
| 216 |
} |
| 217 |
% \end{macrocode} |
| 218 |
% \end{macro} |
| 219 |
% |
| 220 |
% \subsection{Dividing a fixed point number by a small integer} |
| 221 |
% |
| 222 |
% \begin{macro}[EXP]{\@@_fixed_div_int:wwN} |
| 223 |
% \begin{macro}[EXP] |
| 224 |
% { |
| 225 |
% \@@_fixed_div_int:wnN, \@@_fixed_div_int_auxi:wnn, |
| 226 |
% \@@_fixed_div_int_auxii:wnn, \@@_fixed_div_int_pack:Nw, |
| 227 |
% \@@_fixed_div_int_after:Nw |
| 228 |
% } |
| 229 |
% \begin{syntax} |
| 230 |
% \cs{@@_fixed_div_int:wwN} \meta{a} \cs{@@_sep:} \meta{n} \cs{@@_sep:} \meta{continuation} |
| 231 |
% \end{syntax} |
| 232 |
% Divides the fixed point number \meta{a} by the (small) integer |
| 233 |
% $0<\meta{n}<10^4$ and feeds the result to the \meta{continuation}. |
| 234 |
% There is no bound on $a_1$. |
| 235 |
% |
| 236 |
% The arguments of the \texttt{i} auxiliary are 1: one of the $a_{i}$, |
| 237 |
% 2: $n$, 3: the \texttt{ii} or the \texttt{iii} auxiliary. It |
| 238 |
% computes a (somewhat tight) lower bound $Q_{i}$ for the ratio |
| 239 |
% $a_{i}/n$. |
| 240 |
% |
| 241 |
% The \texttt{ii} auxiliary receives $Q_{i}$, $n$, and $a_{i}$ as |
| 242 |
% arguments. It adds $Q_{i}$ to a surrounding integer expression, and |
| 243 |
% starts a new one with the initial value $9999$, which ensures that |
| 244 |
% the result of this expression has $5$ digits. The auxiliary |
| 245 |
% also computes $a_{i}-n\cdot Q_{i}$, placing the result in front of |
| 246 |
% the $4$ digits of $a_{i+1}$. The resulting $a'_{i+1} = 10^{4} |
| 247 |
% (a_{i} - n \cdot Q_{i}) + a_{i+1}$ serves as the first argument for |
| 248 |
% a new call to the \texttt{i} auxiliary. |
| 249 |
% |
| 250 |
% When the \texttt{iii} auxiliary is called, the situation looks like |
| 251 |
% this: |
| 252 |
% \begin{quote} |
| 253 |
% \cs{@@_fixed_div_int_after:Nw} \meta{continuation} \\ |
| 254 |
% $-1 + Q_{1}$ \\ |
| 255 |
% \cs{@@_fixed_div_int_pack:Nw} $9999 + Q_{2}$ \\ |
| 256 |
% \cs{@@_fixed_div_int_pack:Nw} $9999 + Q_{3}$ \\ |
| 257 |
% \cs{@@_fixed_div_int_pack:Nw} $9999 + Q_{4}$ \\ |
| 258 |
% \cs{@@_fixed_div_int_pack:Nw} $9999 + Q_{5}$ \\ |
| 259 |
% \cs{@@_fixed_div_int_pack:Nw} $9999$ \\ |
| 260 |
% \cs{@@_fixed_div_int_auxii:wnn} $Q_{6}$ \cs{@@_sep:} \Arg{n} \Arg{a_{6}} |
| 261 |
% \end{quote} |
| 262 |
% where expansion is happening from the last line up. The |
| 263 |
% \texttt{iii} auxiliary adds $Q_{6} + 2 \simeq a_{6}/n + 1$ to the |
| 264 |
% last $9999$, giving the integer closest to $10000 + a_{6}/n$. |
| 265 |
% |
| 266 |
% Each \texttt{pack} auxiliary receives $5$ digits followed by a |
| 267 |
% \cs{@@_sep:}. The first digit is added as a carry to the integer |
| 268 |
% expression above, and the $4$ other digits are braced. Each call to |
| 269 |
% the \texttt{pack} auxiliary thus produces one brace group. The last |
| 270 |
% brace group is produced by the \texttt{after} auxiliary, which |
| 271 |
% places the \meta{continuation} as appropriate. |
| 272 |
% \begin{macrocode} |
| 273 |
\cs_new:Npn \@@_fixed_div_int:wwN #1#2#3#4#5#6 \@@_sep: #7 \@@_sep: #8 |
| 274 |
{ |
| 275 |
\exp_after:wN \@@_fixed_div_int_after:Nw |
| 276 |
\exp_after:wN #8 |
| 277 |
\int_value:w \@@_int_eval:w - 1 |
| 278 |
\@@_fixed_div_int:wnN |
| 279 |
#1\@@_sep: {#7} \@@_fixed_div_int_auxi:wnn |
| 280 |
#2\@@_sep: {#7} \@@_fixed_div_int_auxi:wnn |
| 281 |
#3\@@_sep: {#7} \@@_fixed_div_int_auxi:wnn |
| 282 |
#4\@@_sep: {#7} \@@_fixed_div_int_auxi:wnn |
| 283 |
#5\@@_sep: {#7} \@@_fixed_div_int_auxi:wnn |
| 284 |
#6\@@_sep: {#7} \@@_fixed_div_int_auxii:wnn \@@_sep: |
| 285 |
} |
| 286 |
\cs_new:Npn \@@_fixed_div_int:wnN #1\@@_sep: #2 #3 |
| 287 |
{ |
| 288 |
\exp_after:wN #3 |
| 289 |
\int_value:w \@@_int_eval:w #1 / #2 - 1 \@@_sep: |
| 290 |
{#2} |
| 291 |
{#1} |
| 292 |
} |
| 293 |
\cs_new:Npn \@@_fixed_div_int_auxi:wnn #1\@@_sep: #2 #3 |
| 294 |
{ |
| 295 |
+ #1 |
| 296 |
\exp_after:wN \@@_fixed_div_int_pack:Nw |
| 297 |
\int_value:w \@@_int_eval:w 9999 |
| 298 |
\exp_after:wN \@@_fixed_div_int:wnN |
| 299 |
\int_value:w \@@_int_eval:w #3 - #1*#2 \@@_int_eval_end: |
| 300 |
} |
| 301 |
\cs_new:Npn \@@_fixed_div_int_auxii:wnn #1\@@_sep: #2 #3 { + #1 + 2 \@@_sep: } |
| 302 |
\cs_new:Npn \@@_fixed_div_int_pack:Nw #1 #2\@@_sep: { + #1\@@_sep: {#2} } |
| 303 |
\cs_new:Npn \@@_fixed_div_int_after:Nw #1 #2\@@_sep: { #1 {#2} } |
| 304 |
% \end{macrocode} |
| 305 |
% \end{macro} |
| 306 |
% \end{macro} |
| 307 |
% |
| 308 |
% \subsection{Adding and subtracting fixed points} |
| 309 |
% |
| 310 |
% \begin{macro}[EXP]{\@@_fixed_add:wwn, \@@_fixed_sub:wwn} |
| 311 |
% \begin{macro}[EXP] |
| 312 |
% { |
| 313 |
% \@@_fixed_add:Nnnnnwnn, |
| 314 |
% \@@_fixed_add:nnNnnnwn, |
| 315 |
% \@@_fixed_add_pack:NNNNNwn, |
| 316 |
% \@@_fixed_add_after:NNNNNwn |
| 317 |
% } |
| 318 |
% \begin{syntax} |
| 319 |
% \cs{@@_fixed_add:wwn} \meta{a} \cs{@@_sep:} \meta{b} \cs{@@_sep:} \Arg{continuation} |
| 320 |
% \end{syntax} |
| 321 |
% Computes $a+b$ (resp.\ $a-b$) and feeds the result to the |
| 322 |
% \meta{continuation}. This function requires $0\leq a_{1},b_{1}\leq |
| 323 |
% 114748$, its result must be positive (this happens automatically for |
| 324 |
% addition) and its first group must have at most~$5$ digits: $(a\pm |
| 325 |
% b)_{1}<100000$. The two functions only differ by |
| 326 |
% a sign, hence use a common auxiliary. It would be nice to grab the |
| 327 |
% $12$ brace groups in one go; only $9$ parameters are allowed. Start |
| 328 |
% by grabbing the sign, $a_{1}, \ldots, a_{4}$, the rest of $a$, |
| 329 |
% and $b_{1}$ and $b_{2}$. The second auxiliary receives the rest of |
| 330 |
% $a$, the sign multiplying $b$, the rest of $b$, and the |
| 331 |
% \meta{continuation} as arguments. After going down through the |
| 332 |
% various level, we go back up, packing digits and bringing the |
| 333 |
% \meta{continuation} (|#8|, then |#7|) from the end of the argument |
| 334 |
% list to its start. |
| 335 |
% \begin{macrocode} |
| 336 |
\cs_new:Npn \@@_fixed_add:wwn { \@@_fixed_add:Nnnnnwnn + } |
| 337 |
\cs_new:Npn \@@_fixed_sub:wwn { \@@_fixed_add:Nnnnnwnn - } |
| 338 |
\cs_new:Npn \@@_fixed_add:Nnnnnwnn #1 #2#3#4#5 #6\@@_sep: #7#8 |
| 339 |
{ |
| 340 |
\exp_after:wN \@@_fixed_add_after:NNNNNwn |
| 341 |
\int_value:w \@@_int_eval:w 9 9999 9998 + #2#3 #1 #7#8 |
| 342 |
\exp_after:wN \@@_fixed_add_pack:NNNNNwn |
| 343 |
\int_value:w \@@_int_eval:w 1 9999 9998 + #4#5 |
| 344 |
\@@_fixed_add:nnNnnnwn #6 #1 |
| 345 |
} |
| 346 |
\cs_new:Npn \@@_fixed_add:nnNnnnwn #1#2 #3 #4#5 #6#7 \@@_sep: #8 |
| 347 |
{ |
| 348 |
#3 #4#5 |
| 349 |
\exp_after:wN \@@_fixed_add_pack:NNNNNwn |
| 350 |
\int_value:w \@@_int_eval:w |
| 351 |
2 0000 0000 #3 #6#7 + #1#2 \@@_sep: {#8} \@@_sep: |
| 352 |
} |
| 353 |
\cs_new:Npn \@@_fixed_add_pack:NNNNNwn #1 #2#3#4#5 #6\@@_sep: #7 |
| 354 |
{ + #1 \@@_sep: {#7} {#2#3#4#5} {#6} } |
| 355 |
\cs_new:Npn \@@_fixed_add_after:NNNNNwn 1 #1 #2#3#4#5 #6\@@_sep: #7 |
| 356 |
{ #7 {#1#2#3#4#5} {#6} } |
| 357 |
% \end{macrocode} |
| 358 |
% \end{macro} |
| 359 |
% \end{macro} |
| 360 |
% |
| 361 |
% \subsection{Multiplying fixed points} |
| 362 |
% |
| 363 |
% ^^A todo: may a_1 or b_1 be = 10000? Used in ediv_epsi later. |
| 364 |
% \begin{macro}[EXP]{\@@_fixed_mul:wwn} |
| 365 |
% \begin{macro}[EXP]{\@@_fixed_mul:nnnnnnnw} |
| 366 |
% \begin{syntax} |
| 367 |
% \cs{@@_fixed_mul:wwn} \meta{a} \cs{@@_sep:} \meta{b} \cs{@@_sep:} \Arg{continuation} |
| 368 |
% \end{syntax} |
| 369 |
% Computes $a\times b$ and feeds the result to \meta{continuation}. |
| 370 |
% This function requires $0\leq a_{1}, b_{1} < 10000$. Once more, we |
| 371 |
% need to play around the limit of $9$ arguments for \TeX{} macros. |
| 372 |
% Note that we don't need to obtain an exact rounding, contrarily to |
| 373 |
% the |*| operator, so things could be harder. We wish to perform |
| 374 |
% carries in |
| 375 |
% \begin{align*} |
| 376 |
% a \times b = |
| 377 |
% & a_{1} \cdot b_{1} \cdot 10^{-8} \\ |
| 378 |
% & + (a_{1} \cdot b_{2} + a_{2} \cdot b_{1}) \cdot 10^{-12} \\ |
| 379 |
% & + (a_{1} \cdot b_{3} + a_{2} \cdot b_{2} |
| 380 |
% + a_{3} \cdot b_{1}) \cdot 10^{-16} \\ |
| 381 |
% & + (a_{1} \cdot b_{4} + a_{2} \cdot b_{3} |
| 382 |
% + a_{3} \cdot b_{2} + a_{4} \cdot b_{1}) \cdot 10^{-20} \\ |
| 383 |
% & + \Bigl(a_{2} \cdot b_{4} + a_{3} \cdot b_{3} + a_{4} \cdot b_{2} |
| 384 |
% \\ & \qquad |
| 385 |
% + \frac{a_{3} \cdot b_{4} + a_{4} \cdot b_{3} |
| 386 |
% + a_{1} \cdot b_{6} + a_{2} \cdot b_{5} |
| 387 |
% + a_{5} \cdot b_{2} + a_{6} \cdot b_{1}}{10^{4}} |
| 388 |
% \\ & \qquad |
| 389 |
% + a_{1} \cdot b_{5} + a_{5} \cdot b_{1}\Bigr) \cdot 10^{-24} |
| 390 |
% + O(10^{-24}), |
| 391 |
% \end{align*} |
| 392 |
% where the $O(10^{-24})$ stands for terms which are at most $5\cdot |
| 393 |
% 10^{-24}$; ignoring those leads to an error of at most |
| 394 |
% $5$~\texttt{ulp}. Note how the first $15$~terms only depend on |
| 395 |
% $a_{1},\ldots{},a_{4}$ and $b_{1},\ldots,b_{4}$, while the last |
| 396 |
% $6$~terms only depend on $a_{1},a_{2},a_{5},a_{6}$, and the |
| 397 |
% corresponding parts of~$b$. Hence, the first function grabs |
| 398 |
% $a_{1},\ldots,a_{4}$, the rest of $a$, and $b_{1},\ldots,b_{4}$, and |
| 399 |
% writes the $15$ first terms of the expression, including a left |
| 400 |
% parenthesis for the fraction. The \texttt{i} auxiliary receives |
| 401 |
% $a_{5}$, $a_{6}$, $b_{1}$, $b_{2}$, $a_{1}$, $a_{2}$, $b_{5}$, |
| 402 |
% $b_{6}$ and finally the \meta{continuation} as arguments. It writes |
| 403 |
% the end of the expression, including the right parenthesis and the |
| 404 |
% denominator of the fraction. The \meta{continuation} |
| 405 |
% is finally placed in front of the $6$ brace groups by |
| 406 |
% \cs{@@_fixed_mul_after:wwn}. |
| 407 |
% \begin{macrocode} |
| 408 |
\cs_new:Npn \@@_fixed_mul:wwn #1#2#3#4 #5\@@_sep: #6#7#8#9 |
| 409 |
{ |
| 410 |
\exp_after:wN \@@_fixed_mul_after:wwn |
| 411 |
\int_value:w \@@_int_eval:w \c_@@_leading_shift_int |
| 412 |
\exp_after:wN \@@_pack:NNNNNw |
| 413 |
\int_value:w \@@_int_eval:w \c_@@_middle_shift_int |
| 414 |
+ #1*#6 |
| 415 |
\exp_after:wN \@@_pack:NNNNNw |
| 416 |
\int_value:w \@@_int_eval:w \c_@@_middle_shift_int |
| 417 |
+ #1*#7 + #2*#6 |
| 418 |
\exp_after:wN \@@_pack:NNNNNw |
| 419 |
\int_value:w \@@_int_eval:w \c_@@_middle_shift_int |
| 420 |
+ #1*#8 + #2*#7 + #3*#6 |
| 421 |
\exp_after:wN \@@_pack:NNNNNw |
| 422 |
\int_value:w \@@_int_eval:w \c_@@_middle_shift_int |
| 423 |
+ #1*#9 + #2*#8 + #3*#7 + #4*#6 |
| 424 |
\exp_after:wN \@@_pack:NNNNNw |
| 425 |
\int_value:w \@@_int_eval:w \c_@@_trailing_shift_int |
| 426 |
+ #2*#9 + #3*#8 + #4*#7 |
| 427 |
+ ( #3*#9 + #4*#8 |
| 428 |
+ \@@_fixed_mul:nnnnnnnw #5 {#6}{#7} {#1}{#2} |
| 429 |
} |
| 430 |
\cs_new:Npn \@@_fixed_mul:nnnnnnnw #1#2 #3#4 #5#6 #7#8 \@@_sep: |
| 431 |
{ |
| 432 |
#1*#4 + #2*#3 + #5*#8 + #6*#7 ) / \c_@@_myriad_int |
| 433 |
+ #1*#3 + #5*#7 \@@_sep: \@@_sep: |
| 434 |
} |
| 435 |
% \end{macrocode} |
| 436 |
% \end{macro} |
| 437 |
% \end{macro} |
| 438 |
% |
| 439 |
% \subsection{Combining product and sum of fixed points} |
| 440 |
% |
| 441 |
% \begin{macro}[EXP] |
| 442 |
% { |
| 443 |
% \@@_fixed_mul_add:wwwn, |
| 444 |
% \@@_fixed_mul_sub_back:wwwn, |
| 445 |
% \@@_fixed_mul_one_minus_mul:wwn, |
| 446 |
% } |
| 447 |
% \begin{syntax} |
| 448 |
% \cs{@@_fixed_mul_add:wwwn} \meta{a} \cs{@@_sep:} \meta{b} \cs{@@_sep:} \meta{c} \cs{@@_sep:} \Arg{continuation} |
| 449 |
% \cs{@@_fixed_mul_sub_back:wwwn} \meta{a} \cs{@@_sep:} \meta{b} \cs{@@_sep:} \meta{c} \cs{@@_sep:} \Arg{continuation} |
| 450 |
% \cs{@@_fixed_one_minus_mul:wwn} \meta{a} \cs{@@_sep:} \meta{b} \cs{@@_sep:} \Arg{continuation} |
| 451 |
% \end{syntax} |
| 452 |
% Sometimes called |FMA| (fused multiply-add), these functions |
| 453 |
% compute $a\times b + c$, $c - a\times b$, and $1 - a\times b$ and |
| 454 |
% feed the result to the \meta{continuation}. Those functions require |
| 455 |
% $0\leq a_{1}, b_{1}, c_{1} \leq 10000$. Since those functions are |
| 456 |
% at the heart of the computation of Taylor expansions, we |
| 457 |
% over-optimize them a bit, and in particular we do not factor out the |
| 458 |
% common parts of the three functions. |
| 459 |
% |
| 460 |
% For definiteness, consider the task of computing $a\times b + c$. |
| 461 |
% We perform carries in |
| 462 |
% \begin{align*} |
| 463 |
% a \times b + c = |
| 464 |
% & (a_{1} \cdot b_{1} + c_{1} c_{2})\cdot 10^{-8} \\ |
| 465 |
% & + (a_{1} \cdot b_{2} + a_{2} \cdot b_{1}) \cdot 10^{-12} \\ |
| 466 |
% & + (a_{1} \cdot b_{3} + a_{2} \cdot b_{2} + a_{3} \cdot b_{1} |
| 467 |
% + c_{3} c_{4}) \cdot 10^{-16} \\ |
| 468 |
% & + (a_{1} \cdot b_{4} + a_{2} \cdot b_{3} + a_{3} \cdot b_{2} |
| 469 |
% + a_{4} \cdot b_{1}) \cdot 10^{-20} \\ |
| 470 |
% & + \Big(a_{2} \cdot b_{4} + a_{3} \cdot b_{3} + a_{4} \cdot b_{2} |
| 471 |
% \\ & \qquad |
| 472 |
% + \frac{a_{3} \cdot b_{4} + a_{4} \cdot b_{3} |
| 473 |
% + a_{1} \cdot b_{6} + a_{2} \cdot b_{5} |
| 474 |
% + a_{5} \cdot b_{2} + a_{6} \cdot b_{1}}{10^{4}} |
| 475 |
% \\ & \qquad |
| 476 |
% + a_{1} \cdot b_{5} + a_{5} \cdot b_{1} |
| 477 |
% + c_{5} c_{6} \Big) \cdot 10^{-24} |
| 478 |
% + O(10^{-24}), |
| 479 |
% \end{align*} |
| 480 |
% where $c_{1} c_{2}$, $c_{3} c_{4}$, $c_{5} c_{6}$ denote the |
| 481 |
% $8$-digit number obtained by juxtaposing the two blocks of digits of |
| 482 |
% $c$, and $\cdot$ denotes multiplication. The task is obviously |
| 483 |
% tough because we have $18$ brace groups in front of us. |
| 484 |
% |
| 485 |
% Each of the three function starts the first two levels (the first, |
| 486 |
% corresponding to $10^{-4}$, is empty), with $c_{1} c_{2}$ in the |
| 487 |
% first level, calls the \texttt{i} auxiliary with arguments described |
| 488 |
% later, and adds a trailing ${} + c_{5}c_{6}$ \cs{@@_sep:} |
| 489 |
% \Arg{continuation}~\cs{@@_sep:}. The ${} + c_{5}c_{6}$ piece, which is |
| 490 |
% omitted for \cs{@@_fixed_one_minus_mul:wwn}, is taken in the |
| 491 |
% integer expression for the $10^{-24}$ level. |
| 492 |
% \begin{macrocode} |
| 493 |
\cs_new:Npn \@@_fixed_mul_add:wwwn #1\@@_sep: #2\@@_sep: #3#4#5#6#7#8\@@_sep: |
| 494 |
{ |
| 495 |
\exp_after:wN \@@_fixed_mul_after:wwn |
| 496 |
\int_value:w \@@_int_eval:w \c_@@_big_leading_shift_int |
| 497 |
\exp_after:wN \@@_pack_big:NNNNNNw |
| 498 |
\int_value:w \@@_int_eval:w \c_@@_big_middle_shift_int + #3 #4 |
| 499 |
\@@_fixed_mul_add:Nwnnnwnnn + |
| 500 |
+ #5 #6 \@@_sep: #2 \@@_sep: #1 \@@_sep: #2 \@@_sep: + |
| 501 |
+ #7 #8 \@@_sep: \@@_sep: |
| 502 |
} |
| 503 |
\cs_new:Npn \@@_fixed_mul_sub_back:wwwn |
| 504 |
#1\@@_sep: #2\@@_sep: #3#4#5#6#7#8\@@_sep: |
| 505 |
{ |
| 506 |
\exp_after:wN \@@_fixed_mul_after:wwn |
| 507 |
\int_value:w \@@_int_eval:w \c_@@_big_leading_shift_int |
| 508 |
\exp_after:wN \@@_pack_big:NNNNNNw |
| 509 |
\int_value:w \@@_int_eval:w \c_@@_big_middle_shift_int + #3 #4 |
| 510 |
\@@_fixed_mul_add:Nwnnnwnnn - |
| 511 |
+ #5 #6 \@@_sep: #2 \@@_sep: #1 \@@_sep: #2 \@@_sep: - |
| 512 |
+ #7 #8 \@@_sep: \@@_sep: |
| 513 |
} |
| 514 |
\cs_new:Npn \@@_fixed_one_minus_mul:wwn #1\@@_sep: #2\@@_sep: |
| 515 |
{ |
| 516 |
\exp_after:wN \@@_fixed_mul_after:wwn |
| 517 |
\int_value:w \@@_int_eval:w \c_@@_big_leading_shift_int |
| 518 |
\exp_after:wN \@@_pack_big:NNNNNNw |
| 519 |
\int_value:w \@@_int_eval:w \c_@@_big_middle_shift_int + |
| 520 |
1 0000 0000 |
| 521 |
\@@_fixed_mul_add:Nwnnnwnnn - |
| 522 |
\@@_sep: #2 \@@_sep: #1 \@@_sep: #2 \@@_sep: - |
| 523 |
\@@_sep: \@@_sep: |
| 524 |
} |
| 525 |
% \end{macrocode} |
| 526 |
% \end{macro} |
| 527 |
% |
| 528 |
% \begin{macro}[EXP]{\@@_fixed_mul_add:Nwnnnwnnn} |
| 529 |
% \begin{syntax} |
| 530 |
% \cs{@@_fixed_mul_add:Nwnnnwnnn} \meta{op} |+| \meta{c_3} \meta{c_4} \cs{@@_sep:} |
| 531 |
% ~~\meta{b} \cs{@@_sep:} \meta{a} \cs{@@_sep:} \meta{b} \cs{@@_sep:} \meta{op} |
| 532 |
% ~~|+| \meta{c_5} \meta{c_6} \cs{@@_sep:} |
| 533 |
% \end{syntax} |
| 534 |
% Here, \meta{op} is either |+| or |-|. Arguments |#3|, |#4|, |#5| |
| 535 |
% are \meta{b_1}, \meta{b_2}, \meta{b_3}; arguments |#7|, |#8|, |#9| |
| 536 |
% are \meta{a_1}, \meta{a_2}, \meta{a_3}. We can build three levels: |
| 537 |
% $a_{1} \cdot b_{1}$ for $10^{-8}$, $(a_{1} \cdot b_{2} + a_{2} \cdot |
| 538 |
% b_{1})$ for $10^{-12}$, and $(a_{1} \cdot b_{3} + a_{2} \cdot b_{2} |
| 539 |
% + a_{3} \cdot b_{1} + c_{3} c_{4})$ for $10^{-16}$. The $a$--$b$ |
| 540 |
% products use the sign |#1|. Note that |#2| is empty for |
| 541 |
% \cs{@@_fixed_one_minus_mul:wwn}. We call the \texttt{ii} auxiliary |
| 542 |
% for levels $10^{-20}$ and $10^{-24}$, keeping the pieces of \meta{a} |
| 543 |
% we've read, but not \meta{b}, since there is another copy later in |
| 544 |
% the input stream. |
| 545 |
% \begin{macrocode} |
| 546 |
\cs_new:Npn \@@_fixed_mul_add:Nwnnnwnnn #1 #2\@@_sep: #3#4#5#6\@@_sep: #7#8#9 |
| 547 |
{ |
| 548 |
#1 #7*#3 |
| 549 |
\exp_after:wN \@@_pack_big:NNNNNNw |
| 550 |
\int_value:w \@@_int_eval:w \c_@@_big_middle_shift_int |
| 551 |
#1 #7*#4 #1 #8*#3 |
| 552 |
\exp_after:wN \@@_pack_big:NNNNNNw |
| 553 |
\int_value:w \@@_int_eval:w \c_@@_big_middle_shift_int |
| 554 |
#1 #7*#5 #1 #8*#4 #1 #9*#3 #2 |
| 555 |
\exp_after:wN \@@_pack_big:NNNNNNw |
| 556 |
\int_value:w \@@_int_eval:w \c_@@_big_middle_shift_int |
| 557 |
#1 \@@_fixed_mul_add:nnnnwnnnn {#7}{#8}{#9} |
| 558 |
} |
| 559 |
% \end{macrocode} |
| 560 |
% \end{macro} |
| 561 |
% |
| 562 |
% \begin{macro}[EXP]{\@@_fixed_mul_add:nnnnwnnnn} |
| 563 |
% \begin{syntax} |
| 564 |
% \cs{@@_fixed_mul_add:nnnnwnnnn} \meta{a} \cs{@@_sep:} \meta{b} \cs{@@_sep:} \meta{op} |
| 565 |
% ~~|+| \meta{c_5} \meta{c_6} \cs{@@_sep:} |
| 566 |
% \end{syntax} |
| 567 |
% Level $10^{-20}$ is $(a_{1} \cdot b_{4} + a_{2} \cdot b_{3} + a_{3} |
| 568 |
% \cdot b_{2} + a_{4} \cdot b_{1})$, multiplied by the sign, which was |
| 569 |
% inserted by the \texttt{i} auxiliary. Then we prepare level |
| 570 |
% $10^{-24}$. We don't have access to all parts of \meta{a} and |
| 571 |
% \meta{b} needed to make all products. Instead, we prepare the |
| 572 |
% partial expressions |
| 573 |
% \begin{align*} |
| 574 |
% & b_{1} + a_{4} \cdot b_{2} + a_{3} \cdot b_{3} + a_{2} \cdot b_{4} + a_{1} \\ |
| 575 |
% & b_{2} + a_{4} \cdot b_{3} + a_{3} \cdot b_{4} + a_{2} . |
| 576 |
% \end{align*} |
| 577 |
% Obviously, those expressions make no mathematical sense: we |
| 578 |
% complete them with $a_{5} \cdot {}$ and ${} \cdot b_{5}$, and with |
| 579 |
% $a_{6} \cdot b_{1} + a_{5} \cdot {}$ and ${} \cdot b_{5} + a_{1} |
| 580 |
% \cdot b_{6}$, and of course with the trailing ${} + c_{5} c_{6}$. |
| 581 |
% To do all this, we keep $a_{1}$, $a_{5}$, $a_{6}$, and the |
| 582 |
% corresponding pieces of \meta{b}. |
| 583 |
% \begin{macrocode} |
| 584 |
\cs_new:Npn \@@_fixed_mul_add:nnnnwnnnn #1#2#3#4#5\@@_sep: #6#7#8#9 |
| 585 |
{ |
| 586 |
( #1*#9 + #2*#8 + #3*#7 + #4*#6 ) |
| 587 |
\exp_after:wN \@@_pack_big:NNNNNNw |
| 588 |
\int_value:w \@@_int_eval:w \c_@@_big_trailing_shift_int |
| 589 |
\@@_fixed_mul_add:nnnnwnnwN |
| 590 |
{ #6 + #4*#7 + #3*#8 + #2*#9 + #1 } |
| 591 |
{ #7 + #4*#8 + #3*#9 + #2 } |
| 592 |
{#1} #5\@@_sep: |
| 593 |
{#6} |
| 594 |
} |
| 595 |
% \end{macrocode} |
| 596 |
% \end{macro} |
| 597 |
% |
| 598 |
% \begin{macro}[EXP]{\@@_fixed_mul_add:nnnnwnnwN} |
| 599 |
% \begin{syntax} |
| 600 |
% \cs{@@_fixed_mul_add:nnnnwnnwN} \Arg{partial_1} \Arg{partial_2} |
| 601 |
% ~~\Arg{a_1} \Arg{a_5} \Arg{a_6} \cs{@@_sep:} \Arg{b_1} \Arg{b_5} \Arg{b_6} \cs{@@_sep:} |
| 602 |
% ~~\meta{op} |+| \meta{c_5} \meta{c_6} \cs{@@_sep:} |
| 603 |
% \end{syntax} |
| 604 |
% Complete the \meta{partial_1} and \meta{partial_2} expressions as |
| 605 |
% explained for the \texttt{ii} auxiliary. The second one is divided |
| 606 |
% by $10000$: this is the carry from level $10^{-28}$. The trailing |
| 607 |
% ${} + c_{5} c_{6}$ is taken into the expression for level |
| 608 |
% $10^{-24}$. Note that the total of level $10^{-24}$ is in the |
| 609 |
% interval $[-5\cdot 10^{8}, 6\cdot 10^{8}$ (give or take a couple of |
| 610 |
% $10000$), hence adding it to the shift gives a $10$-digit number, as |
| 611 |
% expected by the packing auxiliaries. See \pkg{l3fp-aux} for the |
| 612 |
% definition of the shifts and packing auxiliaries. |
| 613 |
% \begin{macrocode} |
| 614 |
\cs_new:Npn \@@_fixed_mul_add:nnnnwnnwN #1#2 #3#4#5\@@_sep: #6#7#8\@@_sep: #9 |
| 615 |
{ |
| 616 |
#9 (#4* #1 *#7) |
| 617 |
#9 (#5*#6+#4* #2 *#7+#3*#8) / \c_@@_myriad_int |
| 618 |
} |
| 619 |
% \end{macrocode} |
| 620 |
% \end{macro} |
| 621 |
% |
| 622 |
% \subsection{Extended-precision floating point numbers} |
| 623 |
% |
| 624 |
% In this section we manipulate floating point numbers with roughly $24$ |
| 625 |
% significant figures (\enquote{extended-precision} numbers, in short, |
| 626 |
% \enquote{ep}), which take the form of an integer exponent, followed by a |
| 627 |
% comma, then six groups of digits, ending with a \cs{@@_sep:}. The first |
| 628 |
% group of digit may be any non-negative integer, while other groups of |
| 629 |
% digits have $4$~digits. In other words, an extended-precision number |
| 630 |
% is an exponent ending in a comma, then a fixed point number. The |
| 631 |
% corresponding value is $0.\meta{digits}\cdot 10^{\meta{exponent}}$. |
| 632 |
% This convention differs from floating points. |
| 633 |
% |
| 634 |
% \begin{macro}[EXP]{\@@_ep_to_fixed:wwn} |
| 635 |
% \begin{macro}[EXP] |
| 636 |
% {\@@_ep_to_fixed_auxi:www, \@@_ep_to_fixed_auxii:nnnnnnnwn} |
| 637 |
% Converts an extended-precision number with an exponent at most~$4$ |
| 638 |
% and a first block less than $10^{8}$ to a fixed point number whose |
| 639 |
% first block has $12$~digits, hopefully starting with many zeros. |
| 640 |
% \begin{macrocode} |
| 641 |
\cs_new:Npn \@@_ep_to_fixed:wwn #1,#2 |
| 642 |
{ |
| 643 |
\exp_after:wN \@@_ep_to_fixed_auxi:www |
| 644 |
\int_value:w \@@_int_eval:w 1 0000 0000 + #2 \exp_after:wN \@@_sep: |
| 645 |
\exp:w \exp_end_continue_f:w |
| 646 |
\prg_replicate:nn { 4 - \int_max:nn {#1} { -32 } } { 0 } \@@_sep: |
| 647 |
} |
| 648 |
\cs_new:Npn \@@_ep_to_fixed_auxi:www |
| 649 |
1#1\@@_sep: #2\@@_sep: #3#4#5#6#7\@@_sep: |
| 650 |
{ |
| 651 |
\@@_pack_eight:wNNNNNNNN |
| 652 |
\@@_pack_twice_four:wNNNNNNNN |
| 653 |
\@@_pack_twice_four:wNNNNNNNN |
| 654 |
\@@_pack_twice_four:wNNNNNNNN |
| 655 |
\@@_ep_to_fixed_auxii:nnnnnnnwn \@@_sep: |
| 656 |
#2 #1#3#4#5#6#7 0000 ! |
| 657 |
} |
| 658 |
\cs_new:Npn \@@_ep_to_fixed_auxii:nnnnnnnwn #1#2#3#4#5#6#7\@@_sep: #8! #9 |
| 659 |
{ #9 {#1#2}{#3}{#4}{#5}{#6}{#7}\@@_sep: } |
| 660 |
% \end{macrocode} |
| 661 |
% \end{macro} |
| 662 |
% \end{macro} |
| 663 |
% |
| 664 |
% ^^A todo: make it work when the arg is zero. |
| 665 |
% \begin{macro}[EXP]{\@@_ep_to_ep:wwN} |
| 666 |
% \begin{macro}[rEXP]{\@@_ep_to_ep_loop:N, \@@_ep_to_ep_end:www} |
| 667 |
% \begin{macro}[EXP]{\@@_ep_to_ep_zero:ww} |
| 668 |
% Normalize an extended-precision number. More precisely, leading |
| 669 |
% zeros are removed from the mantissa of the argument, decreasing its |
| 670 |
% exponent as appropriate. Then the digits are packed into $6$~groups |
| 671 |
% of~$4$ (discarding any remaining digit, not rounding). Finally, the |
| 672 |
% continuation~|#8| is placed before the resulting exponent--mantissa |
| 673 |
% pair. The input exponent may in fact be given as an integer |
| 674 |
% expression. The \texttt{loop} auxiliary grabs a digit: if it |
| 675 |
% is~$0$, decrement the exponent and continue looping, and otherwise |
| 676 |
% call the \texttt{end} auxiliary, which places all digits in the |
| 677 |
% right order (the digit that was not~$0$, and any remaining digits), |
| 678 |
% followed by some~$0$, then packs them up neatly in $3\times2=6$ |
| 679 |
% blocks of four. At the end of the day, remove with \cs{@@_use_i:ww} |
| 680 |
% any digit that did not make it in the final mantissa (typically only |
| 681 |
% zeros, unless the original first block has more than~$4$ digits). |
| 682 |
% \begin{macrocode} |
| 683 |
\cs_new:Npn \@@_ep_to_ep:wwN #1,#2#3#4#5#6#7\@@_sep: #8 |
| 684 |
{ |
| 685 |
\exp_after:wN #8 |
| 686 |
\int_value:w \@@_int_eval:w #1 + 4 |
| 687 |
\exp_after:wN \use_i:nn |
| 688 |
\exp_after:wN \@@_ep_to_ep_loop:N |
| 689 |
\int_value:w \@@_int_eval:w 1 0000 0000 + #2 \@@_int_eval_end: |
| 690 |
#3#4#5#6#7 \@@_sep: \@@_sep: ! |
| 691 |
} |
| 692 |
\cs_new:Npn \@@_ep_to_ep_loop:N #1 |
| 693 |
{ |
| 694 |
\if_meaning:w 0 #1 |
| 695 |
- 1 |
| 696 |
\else: |
| 697 |
\@@_ep_to_ep_end:www #1 |
| 698 |
\fi: |
| 699 |
\@@_ep_to_ep_loop:N |
| 700 |
} |
| 701 |
\cs_new:Npn \@@_ep_to_ep_end:www |
| 702 |
#1 \fi: \@@_ep_to_ep_loop:N #2\@@_sep: #3! |
| 703 |
{ |
| 704 |
\fi: |
| 705 |
\if_meaning:w \@@_sep: #1 |
| 706 |
- 2 * \c_@@_max_exponent_int |
| 707 |
\@@_ep_to_ep_zero:ww |
| 708 |
\fi: |
| 709 |
\@@_pack_twice_four:wNNNNNNNN |
| 710 |
\@@_pack_twice_four:wNNNNNNNN |
| 711 |
\@@_pack_twice_four:wNNNNNNNN |
| 712 |
\@@_use_i:ww , \@@_sep: |
| 713 |
#1 #2 0000 0000 0000 0000 0000 0000 \@@_sep: |
| 714 |
} |
| 715 |
\cs_new:Npn \@@_ep_to_ep_zero:ww \fi: #1\@@_sep: #2\@@_sep: #3\@@_sep: |
| 716 |
{ \fi: , {1000}{0000}{0000}{0000}{0000}{0000} \@@_sep: } |
| 717 |
% \end{macrocode} |
| 718 |
% \end{macro} |
| 719 |
% \end{macro} |
| 720 |
% \end{macro} |
| 721 |
% |
| 722 |
% \begin{macro}[EXP]{\@@_ep_compare:wwww} |
| 723 |
% \begin{macro}[EXP]{\@@_ep_compare_aux:wwww} |
| 724 |
% In \pkg{l3fp-trig} we need to compare two extended-precision |
| 725 |
% numbers. This is based on the same function for positive floating |
| 726 |
% point numbers, with an extra test if comparing only $16$ decimals is |
| 727 |
% not enough to distinguish the numbers. Note that this function only |
| 728 |
% works if the numbers are normalized so that their first block is |
| 729 |
% in~$[1000,9999]$. |
| 730 |
% \begin{macrocode} |
| 731 |
\cs_new:Npn \@@_ep_compare:wwww #1,#2#3#4#5#6#7\@@_sep: |
| 732 |
{ \@@_ep_compare_aux:wwww {#1}{#2}{#3}{#4}{#5}\@@_sep: #6#7\@@_sep: } |
| 733 |
\cs_new:Npn \@@_ep_compare_aux:wwww |
| 734 |
#1\@@_sep:#2\@@_sep:#3,#4#5#6#7#8#9\@@_sep: |
| 735 |
{ |
| 736 |
\if_case:w |
| 737 |
\@@_compare_npos:nwnw |
| 738 |
#1\@@_sep: {#3}{#4}{#5}{#6}{#7}\@@_sep: \exp_stop_f: |
| 739 |
\if_int_compare:w #2 = #8#9 \exp_stop_f: |
| 740 |
0 |
| 741 |
\else: |
| 742 |
\if_int_compare:w #2 < #8#9 - \fi: 1 |
| 743 |
\fi: |
| 744 |
\or: 1 |
| 745 |
\else: -1 |
| 746 |
\fi: |
| 747 |
} |
| 748 |
% \end{macrocode} |
| 749 |
% \end{macro} |
| 750 |
% \end{macro} |
| 751 |
% |
| 752 |
% ^^A todo: doc that neither operand may be zero (or fix ep_to_ep above) |
| 753 |
% \begin{macro}[EXP]{\@@_ep_mul:wwwwn, \@@_ep_mul_raw:wwwwN} |
| 754 |
% Multiply two extended-precision numbers: first normalize them to |
| 755 |
% avoid losing too much precision, then multiply the mantissas |#2| |
| 756 |
% and~|#4| as fixed point numbers, and sum the exponents |#1| |
| 757 |
% and~|#3|. The result's first block is in $[100,9999]$. |
| 758 |
% \begin{macrocode} |
| 759 |
\cs_new:Npn \@@_ep_mul:wwwwn #1,#2\@@_sep: #3,#4\@@_sep: |
| 760 |
{ |
| 761 |
\@@_ep_to_ep:wwN #3,#4\@@_sep: |
| 762 |
\@@_fixed_continue:wn |
| 763 |
{ |
| 764 |
\@@_ep_to_ep:wwN #1,#2\@@_sep: |
| 765 |
\@@_ep_mul_raw:wwwwN |
| 766 |
} |
| 767 |
\@@_fixed_continue:wn |
| 768 |
} |
| 769 |
\cs_new:Npn \@@_ep_mul_raw:wwwwN #1,#2\@@_sep: #3,#4\@@_sep: #5 |
| 770 |
{ |
| 771 |
\@@_fixed_mul:wwn #2\@@_sep: #4\@@_sep: |
| 772 |
{ \exp_after:wN #5 \int_value:w \@@_int_eval:w #1 + #3 , } |
| 773 |
} |
| 774 |
% \end{macrocode} |
| 775 |
% \end{macro} |
| 776 |
% |
| 777 |
% \subsection{Dividing extended-precision numbers} |
| 778 |
% |
| 779 |
% \newcommand{\eTeXfrac}[2]{\left[\frac{#1}{#2}\right]} |
| 780 |
% |
| 781 |
% Divisions of extended-precision numbers are difficult to perform with |
| 782 |
% exact rounding: the technique used in \pkg{l3fp-basics} for $16$-digit |
| 783 |
% floating point numbers does not generalize easily to $24$-digit |
| 784 |
% numbers. Thankfully, there is no need for exact rounding. |
| 785 |
% |
| 786 |
% Let us call \meta{n} the numerator and \meta{d} the denominator. |
| 787 |
% After a simple normalization step, we can assume that |
| 788 |
% $\meta{n}\in[0.1,1)$ and $\meta{d}\in[0.1,1)$, and compute |
| 789 |
% $\meta{n}/(10\meta{d})\in(0.01,1)$. In terms of the $6$~blocks of |
| 790 |
% digits $\meta{n_1}\cdots\meta{n_6}$ and the $6$~blocks |
| 791 |
% $\meta{d_1}\cdots\meta{d_6}$, the condition translates to |
| 792 |
% $\meta{n_1},\meta{d_1}\in[1000,9999]$. |
| 793 |
% |
| 794 |
% We first find an integer estimate $a \simeq 10^{8} / \meta{d}$ by |
| 795 |
% computing |
| 796 |
% \begin{align*} |
| 797 |
% \alpha &= \eTeXfrac{10^{9}}{\meta{d_1}+1} \\ |
| 798 |
% \beta &= \eTeXfrac{10^{9}}{\meta{d_1}} \\ |
| 799 |
% a &= 10^{3} \alpha + (\beta-\alpha) \cdot |
| 800 |
% \left(10^{3}-\eTeXfrac{\meta{d_2}}{10}\right) - 1250, |
| 801 |
% \end{align*} |
| 802 |
% where $\eTeXfrac{\bullet}{\bullet}$ denotes \eTeX{}'s rounding |
| 803 |
% division, which rounds ties away from zero. The idea is to |
| 804 |
% interpolate between $10^{3}\alpha$ and $10^{3}\beta$ with a parameter |
| 805 |
% $\meta{d_2}/10^{4}$, so that when $\meta{d_2}=0$ one gets $a = |
| 806 |
% 10^{3}\beta-1250 \simeq 10^{12} / \meta{d_1} \simeq 10^{8} / |
| 807 |
% \meta{d}$, while when $\meta{d_2}=9999$ one gets $a = |
| 808 |
% 10^{3}\alpha-1250 \simeq 10^{12} / (\meta{d_1} + 1) \simeq 10^{8} / |
| 809 |
% \meta{d}$. The shift by $1250$ helps to ensure that $a$ is an |
| 810 |
% underestimate of the correct value. We shall prove that |
| 811 |
% \[ |
| 812 |
% 1 - 1.755\cdot 10^{-5} < \frac{\meta{d}a}{10^{8}} < 1 . |
| 813 |
% \] |
| 814 |
% We can then compute the inverse of $\meta{d}a/10^{8} = 1 - \epsilon$ |
| 815 |
% using the relation $1/(1-\epsilon) \simeq (1+\epsilon)(1+\epsilon^{2}) |
| 816 |
% + \epsilon^{4}$, which is correct up to a relative error of |
| 817 |
% $\epsilon^5 < 1.6\cdot 10^{-24}$. This allows us to find the desired |
| 818 |
% ratio as |
| 819 |
% \[ |
| 820 |
% \frac{\meta{n}}{\meta{d}} |
| 821 |
% = \frac{\meta{n}a}{10^{8}} |
| 822 |
% \bigl( (1+\epsilon)(1+\epsilon^{2}) + \epsilon^{4}\bigr) . |
| 823 |
% \] |
| 824 |
% |
| 825 |
% Let us prove the upper bound first (multiplied by $10^{15}$). Note |
| 826 |
% that $10^{7} \meta{d} < 10^{3} \meta{d_1} + 10^{-1} (\meta{d_2} + 1)$, |
| 827 |
% and that \eTeX{}'s division $\eTeXfrac{\meta{d_2}}{10}$ underestimates |
| 828 |
% $10^{-1}(\meta{d_2} + 1)$ by $0.5$ at most, as can be checked |
| 829 |
% for each possible last digit of \meta{d_2}. Then, |
| 830 |
% \begin{align} |
| 831 |
% 10^{7} \meta{d}a |
| 832 |
% & < |
| 833 |
% \left(10^{3}\meta{d_1} |
| 834 |
% + \eTeXfrac{\meta{d_2}}{10} + \frac{1}{2}\right) |
| 835 |
% \left(\left(10^{3}-\eTeXfrac{\meta{d_2}}{10}\right) \beta |
| 836 |
% + \eTeXfrac{\meta{d_2}}{10} \alpha - 1250\right) |
| 837 |
% \\ |
| 838 |
% & < |
| 839 |
% \left(10^{3}\meta{d_1} |
| 840 |
% + \eTeXfrac{\meta{d_2}}{10} + \frac{1}{2}\right) |
| 841 |
% \\ & \qquad |
| 842 |
% \left( |
| 843 |
% \left(10^{3}-\eTeXfrac{\meta{d_2}}{10}\right) |
| 844 |
% \left(\frac{10^{9}}{\meta{d_1}} + \frac{1}{2} \right) |
| 845 |
% + \eTeXfrac{\meta{d_2}}{10} |
| 846 |
% \left(\frac{10^{9}}{\meta{d_1}+1} + \frac{1}{2} \right) |
| 847 |
% - 1250 |
| 848 |
% \right) |
| 849 |
% \\ |
| 850 |
% & < |
| 851 |
% \left(10^{3} \meta{d_1} |
| 852 |
% + \eTeXfrac{\meta{d_2}}{10} + \frac{1}{2}\right) |
| 853 |
% \left(\frac{10^{12}}{\meta{d_1}} |
| 854 |
% - \eTeXfrac{\meta{d_2}}{10} |
| 855 |
% \frac{10^{9}}{\meta{d_1}(\meta{d_1}+1)} |
| 856 |
% - 750\right) |
| 857 |
% \end{align} |
| 858 |
% We recognize a quadratic polynomial in $[\meta{d_2}/10]$ with a |
| 859 |
% negative leading coefficient: this polynomial is bounded above, |
| 860 |
% according to $([\meta{d_2}/10]+a)(b-c[\meta{d_2}/10]) \leq |
| 861 |
% (b+ca)^2/(4c)$. Hence, |
| 862 |
% \[ |
| 863 |
% 10^{7} \meta{d}a |
| 864 |
% < \frac{10^{15}}{\meta{d_1}(\meta{d_1}+1)} \left( |
| 865 |
% \meta{d_1} + \frac{1}{2} + \frac{1}{4} 10^{-3} |
| 866 |
% - \frac{3}{8} \cdot 10^{-9} \meta{d_1}(\meta{d_1}+1) \right)^2 |
| 867 |
% \] |
| 868 |
% Since \meta{d_1} takes integer values within $[1000,9999]$, it is a |
| 869 |
% simple programming exercise to check that the squared expression is |
| 870 |
% always less than $\meta{d_1}(\meta{d_1}+1)$, hence $10^{7} \meta{d} a |
| 871 |
% < 10^{15}$. The upper bound is proven. We also find that |
| 872 |
% $\frac{3}{8}$ can be replaced by slightly smaller numbers, but nothing |
| 873 |
% less than $0.374563\ldots$, and going back through the derivation of |
| 874 |
% the upper bound, we find that $1250$ is as small a shift as we can |
| 875 |
% obtain without breaking the bound. |
| 876 |
% |
| 877 |
% Now, the lower bound. The same computation as for the upper bound |
| 878 |
% implies |
| 879 |
% \[ |
| 880 |
% 10^{7} \meta{d}a |
| 881 |
% > \left(10^{3} \meta{d_1} + \eTeXfrac{\meta{d_2}}{10} |
| 882 |
% - \frac{1}{2}\right) |
| 883 |
% \left(\frac{10^{12}}{\meta{d_1}} |
| 884 |
% - \eTeXfrac{\meta{d_2}}{10} \frac{10^{9}}{\meta{d_1}(\meta{d_1}+1)} |
| 885 |
% - 1750\right) |
| 886 |
% \] |
| 887 |
% This time, we want to find the minimum of this quadratic polynomial. |
| 888 |
% Since the leading coefficient is still negative, the minimum is |
| 889 |
% reached for one of the extreme values $[y/10]=0$ or $[y/10]=100$, and |
| 890 |
% we easily check the bound for those values. |
| 891 |
% |
| 892 |
% We have proven that the algorithm gives us a precise enough |
| 893 |
% answer. Incidentally, the upper bound that we derived tells us that |
| 894 |
% $a < 10^{8}/\meta{d} \leq 10^{9}$, hence we can compute $a$ safely as |
| 895 |
% a \TeX{} integer, and even add $10^{9}$ to it to ease grabbing of all |
| 896 |
% the digits. The lower bound implies $10^{8} - 1755 < a$, which we do |
| 897 |
% not care about. |
| 898 |
% |
| 899 |
% ^^A todo: provide ep_inv, not ep_div? |
| 900 |
% ^^A todo: make extra sure that the result's first block cannot be 99 |
| 901 |
% ^^A todo: doc that neither operand may be zero (or fix ep_to_ep) |
| 902 |
% \begin{macro}[EXP]{\@@_ep_div:wwwwn} |
| 903 |
% Compute the ratio of two extended-precision numbers. The result is |
| 904 |
% an extended-precision number whose first block lies in the range |
| 905 |
% $[100,9999]$, and is placed after the \meta{continuation} once we |
| 906 |
% are done. First normalize the inputs so that both first block lie |
| 907 |
% in $[1000,9999]$, then call \cs{@@_ep_div_esti:wwwwn} |
| 908 |
% \meta{denominator} \meta{numerator}, responsible for estimating the |
| 909 |
% inverse of the denominator. |
| 910 |
% \begin{macrocode} |
| 911 |
\cs_new:Npn \@@_ep_div:wwwwn #1,#2\@@_sep: #3,#4\@@_sep: |
| 912 |
{ |
| 913 |
\@@_ep_to_ep:wwN #1,#2\@@_sep: |
| 914 |
\@@_fixed_continue:wn |
| 915 |
{ |
| 916 |
\@@_ep_to_ep:wwN #3,#4\@@_sep: |
| 917 |
\@@_ep_div_esti:wwwwn |
| 918 |
} |
| 919 |
} |
| 920 |
% \end{macrocode} |
| 921 |
% \end{macro} |
| 922 |
% |
| 923 |
% \begin{macro}[EXP] |
| 924 |
% { |
| 925 |
% \@@_ep_div_esti:wwwwn, |
| 926 |
% \@@_ep_div_estii:wwnnwwn, |
| 927 |
% \@@_ep_div_estiii:NNNNNwwwn |
| 928 |
% } |
| 929 |
% The \texttt{esti} function evaluates $\alpha=10^{9} / (\meta{d_1} + |
| 930 |
% 1)$, which is used twice in the expression for $a$, and combines the |
| 931 |
% exponents |#1| and~|#4| (with a shift by~$1$ because we later compute |
| 932 |
% $\meta{n}/(10\meta{d})$. Then the \texttt{estii} function evaluates |
| 933 |
% $10^{9} + a$, and puts the exponent~|#2| after the |
| 934 |
% continuation~|#7|: from there on we can forget exponents and focus |
| 935 |
% on the mantissa. The \texttt{estiii} function multiplies the |
| 936 |
% denominator~|#7| by $10^{-8}a$ (obtained as $a$ split into the |
| 937 |
% single digit~|#1| and two blocks of $4$~digits, |#2#3#4#5| |
| 938 |
% and~|#6|). The result $10^{-8}a\meta{d}=(1-\epsilon)$, and a |
| 939 |
% partially packed $10^{-9}a$ (as a block of four digits, and five |
| 940 |
% individual digits, not packed by lack of available macro parameters |
| 941 |
% here) are passed to \cs{@@_ep_div_epsi:wnNNNNn}, which computes |
| 942 |
% $10^{-9}a/(1-\epsilon)$, that is, $1/(10\meta{d})$ and we finally |
| 943 |
% multiply this by the numerator~|#8|. |
| 944 |
% \begin{macrocode} |
| 945 |
\cs_new:Npn \@@_ep_div_esti:wwwwn #1,#2#3\@@_sep: #4, |
| 946 |
{ |
| 947 |
\exp_after:wN \@@_ep_div_estii:wwnnwwn |
| 948 |
\int_value:w \@@_int_eval:w 10 0000 0000 / ( #2 + 1 ) |
| 949 |
\exp_after:wN \@@_sep: |
| 950 |
\int_value:w \@@_int_eval:w #4 - #1 + 1 , |
| 951 |
{#2} #3\@@_sep: |
| 952 |
} |
| 953 |
\cs_new:Npn \@@_ep_div_estii:wwnnwwn |
| 954 |
#1\@@_sep: #2,#3#4#5\@@_sep: #6\@@_sep: #7 |
| 955 |
{ |
| 956 |
\exp_after:wN \@@_ep_div_estiii:NNNNNwwwn |
| 957 |
\int_value:w \@@_int_eval:w 10 0000 0000 - 1750 |
| 958 |
+ #1 000 + (10 0000 0000 / #3 - #1) * (1000 - #4 / 10) \@@_sep: |
| 959 |
{#3}{#4}#5\@@_sep: #6\@@_sep: { #7 #2, } |
| 960 |
} |
| 961 |
\cs_new:Npn \@@_ep_div_estiii:NNNNNwwwn 1#1#2#3#4#5#6\@@_sep: #7\@@_sep: |
| 962 |
{ |
| 963 |
\@@_fixed_mul_short:wwn #7\@@_sep: {#1}{#2#3#4#5}{#6}\@@_sep: |
| 964 |
\@@_ep_div_epsi:wnNNNNNn {#1#2#3#4}#5#6 |
| 965 |
\@@_fixed_mul:wwn |
| 966 |
} |
| 967 |
% \end{macrocode} |
| 968 |
% \end{macro} |
| 969 |
% |
| 970 |
% \begin{macro}[EXP] |
| 971 |
% { |
| 972 |
% \@@_ep_div_epsi:wnNNNNNn, |
| 973 |
% \@@_ep_div_eps_pack:NNNNNw, |
| 974 |
% \@@_ep_div_epsii:wwnNNNNNn, |
| 975 |
% } |
| 976 |
% The bounds shown above imply that the \texttt{epsi} function's first |
| 977 |
% operand is $(1-\epsilon)$ with $\epsilon\in[0,1.755\cdot 10^{-5}]$. |
| 978 |
% The \texttt{epsi} function computes $\epsilon$ as $1-(1-\epsilon)$. |
| 979 |
% Since $\epsilon<10^{-4}$, its first block vanishes and there is no |
| 980 |
% need to explicitly use~|#1| (which is $9999$). Then \texttt{epsii} |
| 981 |
% evaluates $10^{-9}a/(1-\epsilon)$ as |
| 982 |
% $(1+\epsilon^2)(1+\epsilon)(10^{-9}a \epsilon) + 10^{-9}a$. |
| 983 |
% Importantly, we compute $10^{-9}a \epsilon$ before multiplying it |
| 984 |
% with the rest, rather than multiplying by $\epsilon$ and then |
| 985 |
% $10^{-9}a$, as this second option loses more precision. Also, the |
| 986 |
% combination of \texttt{short_mul} and \texttt{div_myriad} is both |
| 987 |
% faster and more precise than a simple \texttt{mul}. |
| 988 |
% \begin{macrocode} |
| 989 |
\cs_new:Npn \@@_ep_div_epsi:wnNNNNNn #1#2#3#4#5#6\@@_sep: |
| 990 |
{ |
| 991 |
\exp_after:wN \@@_ep_div_epsii:wwnNNNNNn |
| 992 |
\int_value:w \@@_int_eval:w 1 9998 - #2 |
| 993 |
\exp_after:wN \@@_ep_div_eps_pack:NNNNNw |
| 994 |
\int_value:w \@@_int_eval:w 1 9999 9998 - #3#4 |
| 995 |
\exp_after:wN \@@_ep_div_eps_pack:NNNNNw |
| 996 |
\int_value:w \@@_int_eval:w 2 0000 0000 - #5#6 \@@_sep: \@@_sep: |
| 997 |
} |
| 998 |
\cs_new:Npn \@@_ep_div_eps_pack:NNNNNw #1#2#3#4#5#6\@@_sep: |
| 999 |
{ + #1 \@@_sep: {#2#3#4#5} {#6} } |
| 1000 |
\cs_new:Npn \@@_ep_div_epsii:wwnNNNNNn 1#1\@@_sep: #2\@@_sep: #3#4#5#6#7#8 |
| 1001 |
{ |
| 1002 |
\@@_fixed_mul:wwn {0000}{#1}#2\@@_sep: {0000}{#1}#2\@@_sep: |
| 1003 |
\@@_fixed_add_one:wN |
| 1004 |
\@@_fixed_mul:wwn {10000} {#1} #2 \@@_sep: |
| 1005 |
{ |
| 1006 |
\@@_fixed_mul_short:wwn |
| 1007 |
{0000}{#1}#2\@@_sep: {#3}{#4#5#6#7}{#8000}\@@_sep: |
| 1008 |
\@@_fixed_div_myriad:wn |
| 1009 |
\@@_fixed_mul:wwn |
| 1010 |
} |
| 1011 |
\@@_fixed_add:wwn {#3}{#4#5#6#7}{#8000}{0000}{0000}{0000}\@@_sep: |
| 1012 |
} |
| 1013 |
% \end{macrocode} |
| 1014 |
% \end{macro} |
| 1015 |
% |
| 1016 |
% \subsection{Inverse square root of extended precision numbers} |
| 1017 |
% |
| 1018 |
% The idea here is similar to division. Normalize the input, |
| 1019 |
% multiplying by powers of $100$ until we have $x\in[0.01,1)$. Then |
| 1020 |
% find an integer approximation $r \in [101, 1003]$ of |
| 1021 |
% $10^{2}/\sqrt{x}$, as the fixed point of iterations of the Newton |
| 1022 |
% method: essentially $r \mapsto (r + 10^{8} / (x_{1} r)) / 2$, starting |
| 1023 |
% from a guess that optimizes the number of steps before convergence. |
| 1024 |
% In fact, just as there is a slight shift when computing divisions to |
| 1025 |
% ensure that some inequalities hold, we replace $10^{8}$ by a |
| 1026 |
% slightly larger number which ensures that $r^2 x \geq 10^{4}$. |
| 1027 |
% This also causes $r \in [101, 1003]$. Another correction to the above |
| 1028 |
% is that the input is actually normalized to $[0.1,1)$, and we use |
| 1029 |
% either $10^{8}$ or $10^{9}$ in the Newton method, depending on the |
| 1030 |
% parity of the exponent. Skipping those technical hurdles, once we |
| 1031 |
% have the approximation~$r$, we set $y = 10^{-4} r^{2} x$ (or rather, |
| 1032 |
% the correct power of~$10$ to get $y\simeq 1$) and compute $y^{-1/2}$ |
| 1033 |
% through another application of Newton's method. This time, the |
| 1034 |
% starting value is $z=1$, each step maps $z \mapsto z(1.5-0.5yz^2)$, |
| 1035 |
% and we perform a fixed number of steps. Our final result combines~$r$ |
| 1036 |
% with $y^{-1/2}$ as $x^{-1/2} = 10^{-2} r y^{-1/2}$. |
| 1037 |
% |
| 1038 |
% ^^A todo: doc that the operand may not be zero (or fix ep_to_ep above) |
| 1039 |
% \begin{macro}[EXP]{\@@_ep_isqrt:wwn} |
| 1040 |
% \begin{macro}[EXP] |
| 1041 |
% {\@@_ep_isqrt_aux:wwn, \@@_ep_isqrt_auxii:wwnnnwn} |
| 1042 |
% First normalize the input, then check the parity of the |
| 1043 |
% exponent~|#1|. If it is even, the result's exponent will be |
| 1044 |
% $-|#1|/2$, otherwise it will be $(|#1|-1)/2$ (except in the case |
| 1045 |
% where the input was an exact power of $100$). The \texttt{auxii} |
| 1046 |
% function receives as~|#1| the result's exponent just computed, as |
| 1047 |
% |#2| the starting value for the iteration giving~$r$ (the |
| 1048 |
% values~$168$ and~$535$ lead to the least number of iterations before |
| 1049 |
% convergence, on average), as |#3| and~|#4| one empty argument and |
| 1050 |
% one~|0|, depending on the parity of the original exponent, as |#5| |
| 1051 |
% and~|#6| the normalized mantissa ($|#5|\in[1000,9999]$), and as |#7| |
| 1052 |
% the continuation. It sets up the iteration giving~$r$: the |
| 1053 |
% \texttt{esti} function thus receives the initial two guesses |#2| |
| 1054 |
% and~$0$, an approximation~|#5| of~$10^{4}x$ (its first block of |
| 1055 |
% digits), and the empty/zero arguments |#3| and~|#4|, followed by the |
| 1056 |
% mantissa and an altered continuation where we have stored the |
| 1057 |
% result's exponent. |
| 1058 |
% \begin{macrocode} |
| 1059 |
\cs_new:Npn \@@_ep_isqrt:wwn #1,#2\@@_sep: |
| 1060 |
{ |
| 1061 |
\@@_ep_to_ep:wwN #1,#2\@@_sep: |
| 1062 |
\@@_ep_isqrt_auxi:wwn |
| 1063 |
} |
| 1064 |
\cs_new:Npn \@@_ep_isqrt_auxi:wwn #1, |
| 1065 |
{ |
| 1066 |
\exp_after:wN \@@_ep_isqrt_auxii:wwnnnwn |
| 1067 |
\int_value:w \@@_int_eval:w |
| 1068 |
\int_if_odd:nTF {#1} |
| 1069 |
{ (1 - #1) / 2 , 535 , { 0 } { } } |
| 1070 |
{ 1 - #1 / 2 , 168 , { } { 0 } } |
| 1071 |
} |
| 1072 |
\cs_new:Npn \@@_ep_isqrt_auxii:wwnnnwn #1, #2, #3#4 #5#6\@@_sep: #7 |
| 1073 |
{ |
| 1074 |
\@@_ep_isqrt_esti:wwwnnwn #2, 0, #5, {#3} {#4} |
| 1075 |
{#5} #6 \@@_sep: { #7 #1 , } |
| 1076 |
} |
| 1077 |
% \end{macrocode} |
| 1078 |
% \end{macro} |
| 1079 |
% \end{macro} |
| 1080 |
% |
| 1081 |
% \begin{macro}[EXP] |
| 1082 |
% { |
| 1083 |
% \@@_ep_isqrt_esti:wwwnnwn, |
| 1084 |
% \@@_ep_isqrt_estii:wwwnnwn, |
| 1085 |
% \@@_ep_isqrt_estiii:NNNNNwwwn |
| 1086 |
% } |
| 1087 |
% If the last two approximations gave the same result, we are done: |
| 1088 |
% call the \texttt{estii} function to clean up. Otherwise, evaluate |
| 1089 |
% $(\meta{prev} + 1.005 \cdot 10^{\text{$8$ or $9$}} / (\meta{prev} |
| 1090 |
% \cdot x)) / 2$, as the next approximation: omitting the $1.005$ |
| 1091 |
% factor, this would be Newton's method. We can check by brute force |
| 1092 |
% that if |#4| is empty (the original exponent was even), the process |
| 1093 |
% computes an integer slightly larger than $100 / \sqrt{x}$, while if |
| 1094 |
% |#4| is~$0$ (the original exponent was odd), the result is an |
| 1095 |
% integer slightly larger than $100 / \sqrt{x/10}$. Once we are done, |
| 1096 |
% we evaluate $100 r^2 / 2$ or $10 r^2 / 2$ (when the exponent is even |
| 1097 |
% or odd, respectively) and feed that to \texttt{estiii}. This third |
| 1098 |
% auxiliary finds $y_{\text{even}} / 2 = 10^{-4} r^2 x / 2$ or |
| 1099 |
% $y_{\text{odd}} / 2 = 10^{-5} r^2 x / 2$ (again, depending on |
| 1100 |
% earlier parity). A simple program shows that $y\in [1, 1.0201]$. |
| 1101 |
% The number $y/2$ is fed to \cs{@@_ep_isqrt_epsi:wN}, which computes |
| 1102 |
% $1/\sqrt{y}$, and we finally multiply the result by~$r$. |
| 1103 |
% \begin{macrocode} |
| 1104 |
\cs_new:Npn \@@_ep_isqrt_esti:wwwnnwn #1, #2, #3, #4 |
| 1105 |
{ |
| 1106 |
\if_int_compare:w #1 = #2 \exp_stop_f: |
| 1107 |
\exp_after:wN \@@_ep_isqrt_estii:wwwnnwn |
| 1108 |
\fi: |
| 1109 |
\exp_after:wN \@@_ep_isqrt_esti:wwwnnwn |
| 1110 |
\int_value:w \@@_int_eval:w |
| 1111 |
(#1 + 1 0050 0000 #4 / (#1 * #3)) / 2 , |
| 1112 |
#1, #3, {#4} |
| 1113 |
} |
| 1114 |
\cs_new:Npn \@@_ep_isqrt_estii:wwwnnwn #1, #2, #3, #4#5 |
| 1115 |
{ |
| 1116 |
\exp_after:wN \@@_ep_isqrt_estiii:NNNNNwwwn |
| 1117 |
\int_value:w \@@_int_eval:w 1000 0000 + #2 * #2 #5 * 5 |
| 1118 |
\exp_after:wN , \int_value:w \@@_int_eval:w 10000 + #2 \@@_sep: |
| 1119 |
} |
| 1120 |
\cs_new:Npn \@@_ep_isqrt_estiii:NNNNNwwwn |
| 1121 |
1#1#2#3#4#5#6, 1#7#8\@@_sep: #9\@@_sep: |
| 1122 |
{ |
| 1123 |
\@@_fixed_mul_short:wwn #9\@@_sep: {#1} {#2#3#4#5} {#600} \@@_sep: |
| 1124 |
\@@_ep_isqrt_epsi:wN |
| 1125 |
\@@_fixed_mul_short:wwn {#7} {#80} {0000} \@@_sep: |
| 1126 |
} |
| 1127 |
% \end{macrocode} |
| 1128 |
% \end{macro} |
| 1129 |
% |
| 1130 |
% \begin{macro}[EXP]{\@@_ep_isqrt_epsi:wN, \@@_ep_isqrt_epsii:wwN} |
| 1131 |
% Here, we receive a fixed point number $y/2$ with $y\in[1,1.0201]$. |
| 1132 |
% Starting from $z = 1$ we iterate $z \mapsto z(3/2 - z^2 y/2)$. In |
| 1133 |
% fact, we start from the first iteration $z=3/2-y/2$ to avoid useless |
| 1134 |
% multiplications. The \texttt{epsii} auxiliary receives $z$ as~|#1| |
| 1135 |
% and $y$ as~|#2|. |
| 1136 |
% \begin{macrocode} |
| 1137 |
\cs_new:Npn \@@_ep_isqrt_epsi:wN #1\@@_sep: |
| 1138 |
{ |
| 1139 |
\@@_fixed_sub:wwn {15000}{0000}{0000}{0000}{0000}{0000}\@@_sep: #1\@@_sep: |
| 1140 |
\@@_ep_isqrt_epsii:wwN #1\@@_sep: |
| 1141 |
\@@_ep_isqrt_epsii:wwN #1\@@_sep: |
| 1142 |
\@@_ep_isqrt_epsii:wwN #1\@@_sep: |
| 1143 |
} |
| 1144 |
\cs_new:Npn \@@_ep_isqrt_epsii:wwN #1\@@_sep: #2\@@_sep: |
| 1145 |
{ |
| 1146 |
\@@_fixed_mul:wwn #1\@@_sep: #1\@@_sep: |
| 1147 |
\@@_fixed_mul_sub_back:wwwn #2\@@_sep: |
| 1148 |
{15000}{0000}{0000}{0000}{0000}{0000}\@@_sep: |
| 1149 |
\@@_fixed_mul:wwn #1\@@_sep: |
| 1150 |
} |
| 1151 |
% \end{macrocode} |
| 1152 |
% \end{macro} |
| 1153 |
% |
| 1154 |
% \subsection{Converting from fixed point to floating point} |
| 1155 |
% ^^A todo: doc |
| 1156 |
% |
| 1157 |
% After computing Taylor series, we wish to convert the result from |
| 1158 |
% extended precision (with or without an exponent) to the public |
| 1159 |
% floating point format. The functions here should be called within an |
| 1160 |
% integer expression for the overall exponent of the floating point. |
| 1161 |
% |
| 1162 |
% \begin{macro}[rEXP]{\@@_ep_to_float_o:wwN, \@@_ep_inv_to_float_o:wwN} |
| 1163 |
% An extended-precision number is simply a comma-delimited exponent |
| 1164 |
% followed by a fixed point number. Leave the exponent in the current |
| 1165 |
% integer expression then convert the fixed point number. |
| 1166 |
% \begin{macrocode} |
| 1167 |
\cs_new:Npn \@@_ep_to_float_o:wwN #1, |
| 1168 |
{ + \@@_int_eval:w #1 \@@_fixed_to_float_o:wN } |
| 1169 |
\cs_new:Npn \@@_ep_inv_to_float_o:wwN #1,#2\@@_sep: |
| 1170 |
{ |
| 1171 |
\@@_ep_div:wwwwn |
| 1172 |
1,{1000}{0000}{0000}{0000}{0000}{0000}\@@_sep: #1,#2\@@_sep: |
| 1173 |
\@@_ep_to_float_o:wwN |
| 1174 |
} |
| 1175 |
% \end{macrocode} |
| 1176 |
% \end{macro} |
| 1177 |
% |
| 1178 |
% \begin{macro}[rEXP]{\@@_fixed_inv_to_float_o:wN} |
| 1179 |
% Another function which reduces to converting an extended precision |
| 1180 |
% number to a float. |
| 1181 |
% \begin{macrocode} |
| 1182 |
\cs_new:Npn \@@_fixed_inv_to_float_o:wN |
| 1183 |
{ \@@_ep_inv_to_float_o:wwN 0, } |
| 1184 |
% \end{macrocode} |
| 1185 |
% \end{macro} |
| 1186 |
% |
| 1187 |
% \begin{macro}[rEXP]{\@@_fixed_to_float_rad_o:wN} |
| 1188 |
% Converts the fixed point number~|#1| from degrees to radians then to |
| 1189 |
% a floating point number. This could perhaps remain in |
| 1190 |
% \pkg{l3fp-trig}. |
| 1191 |
% \begin{macrocode} |
| 1192 |
\cs_new:Npn \@@_fixed_to_float_rad_o:wN #1\@@_sep: |
| 1193 |
{ |
| 1194 |
\@@_fixed_mul:wwn #1\@@_sep: {5729}{5779}{5130}{8232}{0876}{7981}\@@_sep: |
| 1195 |
{ \@@_ep_to_float_o:wwN 2, } |
| 1196 |
} |
| 1197 |
% \end{macrocode} |
| 1198 |
% \end{macro} |
| 1199 |
% |
| 1200 |
% ^^A todo: make exponents end in ',' consistently throughout l3fp |
| 1201 |
% \begin{macro}[rEXP] |
| 1202 |
% {\@@_fixed_to_float_o:wN, \@@_fixed_to_float_o:Nw} |
| 1203 |
% \begin{syntax} |
| 1204 |
% \ldots{} \cs{@@_int_eval:w} \meta{exponent} \cs{@@_fixed_to_float_o:wN} \Arg{a_1} \Arg{a_2} \Arg{a_3} \Arg{a_4} \Arg{a_5} \Arg{a_6} \cs{@@_sep:} \meta{sign} |
| 1205 |
% \end{syntax} |
| 1206 |
% yields |
| 1207 |
% \begin{quote} |
| 1208 |
% \meta{exponent'} \cs{@@_sep:} \Arg{a'_1} \Arg{a'_2} \Arg{a'_3} \Arg{a'_4} \cs{@@_sep:} |
| 1209 |
% \end{quote} |
| 1210 |
% And the \texttt{to_fixed} version gives six brace groups instead of |
| 1211 |
% $4$, ensuring that $1000\leq\meta{a'_1}\leq 9999$. At this stage, we |
| 1212 |
% know that \meta{a_1} is positive (otherwise, it is sign of an error |
| 1213 |
% before), and we assume that it is less than $10^8$.\footnote{Bruno: |
| 1214 |
% I must double check this assumption.} |
| 1215 |
% |
| 1216 |
%^^A todo: round properly when rounding to infinity: I need the sign. |
| 1217 |
% \begin{macrocode} |
| 1218 |
\cs_new:Npn \@@_fixed_to_float_o:Nw #1#2\@@_sep: |
| 1219 |
{ \@@_fixed_to_float_o:wN #2\@@_sep: #1 } |
| 1220 |
\cs_new:Npn \@@_fixed_to_float_o:wN #1#2#3#4#5#6\@@_sep: #7 |
| 1221 |
{ % for the 8-digit-at-the-start thing |
| 1222 |
+ \@@_int_eval:w \c_@@_block_int |
| 1223 |
\exp_after:wN \exp_after:wN |
| 1224 |
\exp_after:wN \@@_fixed_to_loop:N |
| 1225 |
\exp_after:wN \use_none:n |
| 1226 |
\int_value:w \@@_int_eval:w |
| 1227 |
1 0000 0000 + #1 \exp_after:wN \@@_use_none_stop_f:n |
| 1228 |
\int_value:w 1#2 \exp_after:wN \@@_use_none_stop_f:n |
| 1229 |
\int_value:w 1#3#4 \exp_after:wN \@@_use_none_stop_f:n |
| 1230 |
\int_value:w 1#5#6 |
| 1231 |
\exp_after:wN \@@_sep: |
| 1232 |
\exp_after:wN \@@_sep: |
| 1233 |
} |
| 1234 |
\cs_new:Npn \@@_fixed_to_loop:N #1 |
| 1235 |
{ |
| 1236 |
\if_meaning:w 0 #1 |
| 1237 |
- 1 |
| 1238 |
\exp_after:wN \@@_fixed_to_loop:N |
| 1239 |
\else: |
| 1240 |
\exp_after:wN \@@_fixed_to_loop_end:w |
| 1241 |
\exp_after:wN #1 |
| 1242 |
\fi: |
| 1243 |
} |
| 1244 |
\cs_new:Npn \@@_fixed_to_loop_end:w #1 #2 \@@_sep: |
| 1245 |
{ |
| 1246 |
\if_meaning:w \@@_sep: #1 |
| 1247 |
\exp_after:wN \@@_fixed_to_float_zero:w |
| 1248 |
\else: |
| 1249 |
\exp_after:wN \@@_pack_twice_four:wNNNNNNNN |
| 1250 |
\exp_after:wN \@@_pack_twice_four:wNNNNNNNN |
| 1251 |
\exp_after:wN \@@_fixed_to_float_pack:ww |
| 1252 |
\exp_after:wN \@@_sep: |
| 1253 |
\fi: |
| 1254 |
#1 #2 0000 0000 0000 0000 \@@_sep: |
| 1255 |
} |
| 1256 |
\cs_new:Npn \@@_fixed_to_float_zero:w \@@_sep: 0000 0000 0000 0000 \@@_sep: |
| 1257 |
{ |
| 1258 |
- 2 * \c_@@_max_exponent_int \@@_sep: |
| 1259 |
{0000} {0000} {0000} {0000} \@@_sep: |
| 1260 |
} |
| 1261 |
\cs_new:Npn \@@_fixed_to_float_pack:ww #1 \@@_sep: #2#3 \@@_sep: \@@_sep: |
| 1262 |
{ |
| 1263 |
\if_int_compare:w #2 > 4 \exp_stop_f: |
| 1264 |
\exp_after:wN \@@_fixed_to_float_round_up:wnnnnw |
| 1265 |
\fi: |
| 1266 |
\@@_sep: #1 \@@_sep: |
| 1267 |
} |
| 1268 |
\cs_new:Npn \@@_fixed_to_float_round_up:wnnnnw \@@_sep: #1#2#3#4 \@@_sep: |
| 1269 |
{ |
| 1270 |
\exp_after:wN \@@_basics_pack_high:NNNNNw |
| 1271 |
\int_value:w \@@_int_eval:w 1 #1#2 |
| 1272 |
\exp_after:wN \@@_basics_pack_low:NNNNNw |
| 1273 |
\int_value:w \@@_int_eval:w 1 #3#4 + 1 \@@_sep: |
| 1274 |
} |
| 1275 |
% \end{macrocode} |
| 1276 |
% \end{macro} |
| 1277 |
% |
| 1278 |
% \begin{macrocode} |
| 1279 |
%</code> |
| 1280 |
% \end{macrocode} |
| 1281 |
% |
| 1282 |
% \end{implementation} |
| 1283 |
% |
| 1284 |
% \PrintChanges |
| 1285 |
% |
| 1286 |
% \PrintIndex |