| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3fp.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 |
\usepackage{amsmath} |
| 28 |
\begin{document} |
| 29 |
\DocInput{\jobname.dtx} |
| 30 |
\end{document} |
| 31 |
%</driver> |
| 32 |
% \fi |
| 33 |
% |
| 34 |
% ^^A need to provide this inside the file: |
| 35 |
% |
| 36 |
% \providecommand\nan{\texttt{NaN}} |
| 37 |
% |
| 38 |
% |
| 39 |
% \title{^^A |
| 40 |
% The \pkg{l3fp} module\\ Floating points^^A |
| 41 |
% } |
| 42 |
% |
| 43 |
% \author{^^A |
| 44 |
% The \LaTeX{} Project\thanks |
| 45 |
% {^^A |
| 46 |
% E-mail: |
| 47 |
% \href{mailto:[email protected]} |
| 48 |
% {[email protected]}^^A |
| 49 |
% }^^A |
| 50 |
% } |
| 51 |
% |
| 52 |
% \date{Released 2026-05-15} |
| 53 |
% |
| 54 |
% \maketitle |
| 55 |
% |
| 56 |
% \begin{documentation} |
| 57 |
% |
| 58 |
% A decimal floating point number is one which is stored as a significand and a |
| 59 |
% separate exponent. The module implements expandably a wide set of |
| 60 |
% arithmetic, trigonometric, and other operations on decimal floating point |
| 61 |
% numbers, to be used within floating point expressions. \emph{Floating point |
| 62 |
% expressions} (\enquote{\meta{fp expr}}) support the following operations with |
| 63 |
% their usual precedence. |
| 64 |
% \begin{itemize} |
| 65 |
% \item Basic arithmetic: addition $x+y$, subtraction $x-y$, |
| 66 |
% multiplication $x*y$, division $x/y$, square root~$\sqrt{x}$, |
| 67 |
% and parentheses. |
| 68 |
% \item Comparison operators: $x\mathop{\mathtt{<}}y$, |
| 69 |
% $x\mathop{\mathtt{<=}}y$, $x\mathop{\mathtt{>?}}y$, |
| 70 |
% $x\mathop{\mathtt{!=}}y$ etc. |
| 71 |
% \item Boolean logic: sign $\operatorname{sign} x$, |
| 72 |
% negation $\mathop{!}x$, conjunction |
| 73 |
% $x\mathop{\&\&}y$, disjunction $x\mathop{\vert\vert}y$, ternary |
| 74 |
% operator $x\mathop{?}y\mathop{:}z$. |
| 75 |
% \item Exponentials: $\exp x$, $\ln x$, $x^y$, $\operatorname{logb} x$. |
| 76 |
% \item Integer factorial: $\operatorname{fact} x$. |
| 77 |
% \item Trigonometry: $\sin x$, $\cos x$, $\tan x$, $\cot x$, $\sec |
| 78 |
% x$, $\csc x$ expecting their arguments in radians, and |
| 79 |
% $\operatorname{sind} x$, $\operatorname{cosd} x$, |
| 80 |
% $\operatorname{tand} x$, $\operatorname{cotd} x$, |
| 81 |
% $\operatorname{secd} x$, $\operatorname{cscd} x$ expecting their |
| 82 |
% arguments in degrees. |
| 83 |
% \item Inverse trigonometric functions: $\operatorname{asin} x$, |
| 84 |
% $\operatorname{acos} x$, $\operatorname{atan} x$, |
| 85 |
% $\operatorname{acot} x$, $\operatorname{asec} x$, |
| 86 |
% $\operatorname{acsc} x$ giving a result in radians, and |
| 87 |
% $\operatorname{asind} x$, $\operatorname{acosd} x$, |
| 88 |
% $\operatorname{atand} x$, $\operatorname{acotd} x$, |
| 89 |
% $\operatorname{asecd} x$, $\operatorname{acscd} x$ giving a result |
| 90 |
% in degrees. |
| 91 |
% \item [\emph{(not yet)}] Hyperbolic functions and their inverse |
| 92 |
% functions: $\sinh x$, $\cosh x$, $\tanh x$, $\coth x$, |
| 93 |
% $\operatorname{sech} x$, $\operatorname{csch}$, and |
| 94 |
% $\operatorname{asinh} x$, $\operatorname{acosh} x$, |
| 95 |
% $\operatorname{atanh} x$, $\operatorname{acoth} x$, |
| 96 |
% $\operatorname{asech} x$, $\operatorname{acsch} x$. |
| 97 |
% \item Extrema: $\max(x_{1},x_{2},\ldots)$, $\min(x_{1},x_{2},\ldots)$, |
| 98 |
% $\operatorname{abs}(x)$. |
| 99 |
% \item Rounding functions, controlled by two optional |
| 100 |
% values, $n$ (number of places, $0$ by default) and |
| 101 |
% $t$ (behavior on a tie, $\nan$ by default): |
| 102 |
% \begin{itemize} |
| 103 |
% \item $\operatorname{trunc}(x,n)$ rounds towards zero, |
| 104 |
% \item $\operatorname{floor}(x,n)$ rounds towards~$-\infty$, |
| 105 |
% \item $\operatorname{ceil}(x,n)$ rounds towards~$+\infty$, |
| 106 |
% \item $\operatorname{round}(x,n,t)$ rounds to the closest value, with |
| 107 |
% ties rounded to an even value by default, towards zero if $t=0$, |
| 108 |
% towards $+\infty$ if $t>0$ and towards $-\infty$ if $t<0$. |
| 109 |
% \end{itemize} |
| 110 |
% And \emph{(not yet)} modulo, and \enquote{quantize}. |
| 111 |
% \item Random numbers: $\operatorname{rand}()$, |
| 112 |
% $\operatorname{randint}(m,n)$. |
| 113 |
% \item Constants: \texttt{pi}, \texttt{deg} (one degree in radians). |
| 114 |
% \item Dimensions, automatically expressed in points, \emph{e.g.}, |
| 115 |
% \texttt{pc} is~$12$. |
| 116 |
% \item Automatic conversion (no need for \cs[no-index]{\meta{type}_use:N}) of |
| 117 |
% integer, dimension, and skip variables to floating point numbers, |
| 118 |
% expressing dimensions in points and ignoring the stretch and |
| 119 |
% shrink components of skips. |
| 120 |
% \item Tuples: $(x_1,\ldots{},x_n)$ that can be stored in variables, |
| 121 |
% added together, multiplied or divided by a floating point number, |
| 122 |
% and nested. |
| 123 |
% \end{itemize} |
| 124 |
% Floating point numbers can be given either explicitly (in a form such |
| 125 |
% as |1.234e-34|, or |-.0001|), or as a stored floating point variable, |
| 126 |
% which is automatically replaced by its current value. |
| 127 |
% A \enquote{floating point} is a floating point number or a tuple thereof. See |
| 128 |
% section~\ref{sec:l3fp:fp-floats} for a description of what a floating point |
| 129 |
% is, section~\ref{sec:l3fp:fp-precedence} for details about how an expression |
| 130 |
% is parsed, and section~\ref{sec:l3fp:fp-operations} to know what the various |
| 131 |
% operations do. Some operations may raise exceptions (error messages), |
| 132 |
% described in section~\ref{sec:l3fp:fp-exceptions}. |
| 133 |
% |
| 134 |
% An example of use could be the following. |
| 135 |
% \begin{verbatim} |
| 136 |
% \LaTeX{} can now compute: $ \frac{\sin (3.5)}{2} + 2\cdot 10^{-3} |
| 137 |
% = \ExplSyntaxOn \fp_to_decimal:n {sin(3.5)/2 + 2e-3} $. |
| 138 |
% \end{verbatim} |
| 139 |
% The operation \texttt{round} can be used to limit the result's |
| 140 |
% precision. Adding $+0$ avoids the possibly undesirable output |-0|, |
| 141 |
% replacing it by |+0|. However, the \pkg{l3fp} module is mostly meant |
| 142 |
% as an underlying tool for higher-level commands. For example, one |
| 143 |
% could provide a function to typeset nicely the result of floating |
| 144 |
% point computations. |
| 145 |
% \begin{verbatim} |
| 146 |
% \documentclass{article} |
| 147 |
% \usepackage{siunitx} |
| 148 |
% \ExplSyntaxOn |
| 149 |
% \NewDocumentCommand { \calcnum } { m } |
| 150 |
% { \num { \fp_to_scientific:n {#1} } } |
| 151 |
% \ExplSyntaxOff |
| 152 |
% \begin{document} |
| 153 |
% \calcnum { 2 pi * sin ( 2.3 ^ 5 ) } |
| 154 |
% \end{document} |
| 155 |
% \end{verbatim} |
| 156 |
% See the documentation of \pkg{siunitx} for various options of |
| 157 |
% \cs{num}. |
| 158 |
% |
| 159 |
% \section{Creating and initializing floating point variables} |
| 160 |
% |
| 161 |
% \begin{function}[updated = 2012-05-08, tested = m3fp001] |
| 162 |
% {\fp_new:N, \fp_new:c} |
| 163 |
% \begin{syntax} |
| 164 |
% \cs{fp_new:N} \meta{fp~var} |
| 165 |
% \end{syntax} |
| 166 |
% Creates a new \meta{fp~var} or raises an error if the name is |
| 167 |
% already taken. The declaration is global. The \meta{fp~var} is |
| 168 |
% initially~$+0$. |
| 169 |
% \end{function} |
| 170 |
% |
| 171 |
% \begin{function}[updated = 2012-05-08, tested = m3fp001] |
| 172 |
% {\fp_const:Nn, \fp_const:cn} |
| 173 |
% \begin{syntax} |
| 174 |
% \cs{fp_const:Nn} \meta{fp~var} \Arg{fp expr} |
| 175 |
% \end{syntax} |
| 176 |
% Creates a new constant \meta{fp~var} or raises an error if the name |
| 177 |
% is already taken. The \meta{fp~var} is set globally equal to |
| 178 |
% the result of evaluating the \meta{fp expr}. |
| 179 |
% \end{function} |
| 180 |
% |
| 181 |
% \begin{function}[updated = 2012-05-08, tested = m3fp001] |
| 182 |
% {\fp_zero:N, \fp_zero:c, \fp_gzero:N, \fp_gzero:c} |
| 183 |
% \begin{syntax} |
| 184 |
% \cs{fp_zero:N} \meta{fp~var} |
| 185 |
% \end{syntax} |
| 186 |
% Sets the \meta{fp~var} to~$+0$. |
| 187 |
% \end{function} |
| 188 |
% |
| 189 |
% \begin{function}[updated = 2012-05-08, tested = m3fp001] |
| 190 |
% {\fp_zero_new:N, \fp_zero_new:c, \fp_gzero_new:N, \fp_gzero_new:c} |
| 191 |
% \begin{syntax} |
| 192 |
% \cs{fp_zero_new:N} \meta{fp~var} |
| 193 |
% \end{syntax} |
| 194 |
% Ensures that the \meta{fp~var} exists globally |
| 195 |
% by applying \cs{fp_new:N} if necessary, then applies |
| 196 |
% \cs[index=fp_zero:N]{fp_(g)zero:N} to leave the \meta{fp~var} set to~$+0$. |
| 197 |
% \end{function} |
| 198 |
% |
| 199 |
% \section{Setting floating point variables} |
| 200 |
% |
| 201 |
% \begin{function}[updated = 2012-05-08, tested = m3fp002] |
| 202 |
% { |
| 203 |
% \fp_set:Nn, \fp_set:cn, \fp_set:NV, \fp_set:cV, |
| 204 |
% \fp_gset:Nn, \fp_gset:cn, \fp_gset:NV, \fp_gset:cV |
| 205 |
% } |
| 206 |
% \begin{syntax} |
| 207 |
% \cs{fp_set:Nn} \meta{fp~var} \Arg{fp expr} |
| 208 |
% \end{syntax} |
| 209 |
% Sets \meta{fp~var} equal to the result of computing the |
| 210 |
% \meta{fp expr}. |
| 211 |
% \end{function} |
| 212 |
% |
| 213 |
% \begin{function}[updated = 2012-05-08, tested = m3fp002] |
| 214 |
% { |
| 215 |
% \fp_set_eq:NN , \fp_set_eq:cN , \fp_set_eq:Nc , \fp_set_eq:cc , |
| 216 |
% \fp_gset_eq:NN, \fp_gset_eq:cN, \fp_gset_eq:Nc, \fp_gset_eq:cc |
| 217 |
% } |
| 218 |
% \begin{syntax} |
| 219 |
% \cs{fp_set_eq:NN} \meta{fp~var_1} \meta{fp~var_2} |
| 220 |
% \end{syntax} |
| 221 |
% Sets the floating point variable \meta{fp~var_1} equal to the current |
| 222 |
% value of \meta{fp~var_2}. |
| 223 |
% \end{function} |
| 224 |
% |
| 225 |
% \begin{function}[updated = 2012-05-08, tested = m3fp002] |
| 226 |
% {\fp_add:Nn, \fp_add:cn, \fp_gadd:Nn, \fp_gadd:cn} |
| 227 |
% \begin{syntax} |
| 228 |
% \cs{fp_add:Nn} \meta{fp~var} \Arg{fp expr} |
| 229 |
% \end{syntax} |
| 230 |
% Adds the result of computing the \meta{fp expr} to |
| 231 |
% the \meta{fp~var}. |
| 232 |
% This also applies if \meta{fp~var} and \meta{floating point |
| 233 |
% expression} evaluate to tuples of the same size. |
| 234 |
% \end{function} |
| 235 |
% |
| 236 |
% \begin{function}[updated = 2012-05-08, tested = m3fp002] |
| 237 |
% {\fp_sub:Nn, \fp_sub:cn, \fp_gsub:Nn, \fp_gsub:cn} |
| 238 |
% \begin{syntax} |
| 239 |
% \cs{fp_sub:Nn} \meta{fp~var} \Arg{fp expr} |
| 240 |
% \end{syntax} |
| 241 |
% Subtracts the result of computing the \meta{floating point |
| 242 |
% expression} from the \meta{fp~var}. |
| 243 |
% This also applies if \meta{fp~var} and \meta{floating point |
| 244 |
% expression} evaluate to tuples of the same size. |
| 245 |
% \end{function} |
| 246 |
% |
| 247 |
% \section{Using floating points} |
| 248 |
% |
| 249 |
% \begin{function}[EXP, added = 2012-05-08, updated = 2012-07-08, |
| 250 |
% tested = m3fp-convert003]{\fp_eval:n} |
| 251 |
% \begin{syntax} |
| 252 |
% \cs{fp_eval:n} \Arg{fp expr} |
| 253 |
% \end{syntax} |
| 254 |
% Evaluates the \meta{fp expr} and expresses the |
| 255 |
% result as a decimal number with no |
| 256 |
% exponent. Leading or trailing zeros may be inserted to compensate |
| 257 |
% for the exponent. Non-significant trailing zeros are trimmed, and |
| 258 |
% integers are expressed without a decimal separator. The values |
| 259 |
% $\pm\infty$ and \nan{} trigger an \enquote{invalid operation} |
| 260 |
% exception. |
| 261 |
% For a tuple, each item is converted using \cs{fp_eval:n} and they are |
| 262 |
% combined as |
| 263 |
% |(|\meta{fp_1}\verb*|, |\meta{fp_2}\verb*|, |\ldots{}\meta{fp_n}|)| |
| 264 |
% if $n>1$ and |(|\meta{fp_1}|,)| or |()| for fewer items. |
| 265 |
% This function is identical to \cs{fp_to_decimal:n}. |
| 266 |
% \end{function} |
| 267 |
% |
| 268 |
% \begin{function}[EXP, added = 2018-11-03]{\fp_sign:n} |
| 269 |
% \begin{syntax} |
| 270 |
% \cs{fp_sign:n} \Arg{fp expr} |
| 271 |
% \end{syntax} |
| 272 |
% Evaluates the \meta{fp expr} and leaves its sign in the input stream |
| 273 |
% using \cs{fp_eval:n} |{sign(|\meta{result}|)}|: $+1$ for positive |
| 274 |
% numbers and for $+\infty$, $-1$ for negative numbers and for |
| 275 |
% $-\infty$, $\pm 0$ for $\pm 0$. If the operand is a tuple or is |
| 276 |
% \nan{}, then \enquote{invalid operation} occurs and the result |
| 277 |
% is~$0$. |
| 278 |
% \end{function} |
| 279 |
% |
| 280 |
% \begin{function}[EXP, added = 2012-05-08, updated = 2012-07-08] |
| 281 |
% {\fp_to_decimal:N, \fp_to_decimal:c, \fp_to_decimal:n} |
| 282 |
% \begin{syntax} |
| 283 |
% \cs{fp_to_decimal:N} \meta{fp~var} |
| 284 |
% \cs{fp_to_decimal:n} \Arg{fp expr} |
| 285 |
% \end{syntax} |
| 286 |
% Evaluates the \meta{fp expr} and expresses the |
| 287 |
% result as a decimal number with no |
| 288 |
% exponent. Leading or trailing zeros may be inserted to compensate |
| 289 |
% for the exponent. Non-significant trailing zeros are trimmed, and |
| 290 |
% integers are expressed without a decimal separator. The values |
| 291 |
% $\pm\infty$ and~\nan{} trigger an \enquote{invalid operation} |
| 292 |
% exception. |
| 293 |
% For a tuple, each item is converted using \cs{fp_to_decimal:n} and they are |
| 294 |
% combined as |
| 295 |
% |(|\meta{fp_1}\verb*|, |\meta{fp_2}\verb*|, |\ldots{}\meta{fp_n}|)| |
| 296 |
% if $n>1$ and |(|\meta{fp_1}|,)| or |()| for fewer items. |
| 297 |
% \end{function} |
| 298 |
% |
| 299 |
% \begin{function}[EXP, updated = 2016-03-22] |
| 300 |
% {\fp_to_dim:N, \fp_to_dim:c, \fp_to_dim:n} |
| 301 |
% \begin{syntax} |
| 302 |
% \cs{fp_to_dim:N} \meta{fp~var} |
| 303 |
% \cs{fp_to_dim:n} \Arg{fp expr} |
| 304 |
% \end{syntax} |
| 305 |
% Evaluates the \meta{fp expr} and expresses the |
| 306 |
% result as a dimension (in~\texttt{pt}) suitable for use in dimension |
| 307 |
% expressions. The output is identical to \cs{fp_to_decimal:n}, with |
| 308 |
% an additional trailing~\texttt{pt} (both letter tokens). |
| 309 |
% In particular, the result may |
| 310 |
% be outside the range $[- 2^{14} + 2^{-17}, 2^{14} - 2^{-17}]$ of |
| 311 |
% valid \TeX{} dimensions, leading to overflow errors if used as a |
| 312 |
% dimension. Tuples, as well as the values $\pm\infty$ and~\nan{}, |
| 313 |
% trigger an \enquote{invalid operation} exception. |
| 314 |
% \end{function} |
| 315 |
% |
| 316 |
% \begin{function}[EXP, updated = 2012-07-08] |
| 317 |
% {\fp_to_int:N, \fp_to_int:c, \fp_to_int:n} |
| 318 |
% \begin{syntax} |
| 319 |
% \cs{fp_to_int:N} \meta{fp~var} |
| 320 |
% \cs{fp_to_int:n} \Arg{fp expr} |
| 321 |
% \end{syntax} |
| 322 |
% Evaluates the \meta{fp expr}, and rounds the |
| 323 |
% result to the closest integer, rounding exact ties to an even |
| 324 |
% integer. |
| 325 |
% The result may be outside the range $[- 2^{31} + 1, 2^{31} - 1]$ of |
| 326 |
% valid \TeX{}~integers, leading to overflow errors if used in an |
| 327 |
% integer expression. Tuples, as well as the values $\pm\infty$ |
| 328 |
% and~\nan{}, trigger an \enquote{invalid operation} exception. |
| 329 |
% \end{function} |
| 330 |
% |
| 331 |
% \begin{function}[EXP, added = 2012-05-08, updated = 2016-03-22] |
| 332 |
% {\fp_to_scientific:N, \fp_to_scientific:c, \fp_to_scientific:n} |
| 333 |
% \begin{syntax} |
| 334 |
% \cs{fp_to_scientific:N} \meta{fp~var} |
| 335 |
% \cs{fp_to_scientific:n} \Arg{fp expr} |
| 336 |
% \end{syntax} |
| 337 |
% Evaluates the \meta{fp expr} and expresses the |
| 338 |
% result in scientific notation: |
| 339 |
% \begin{quote} |
| 340 |
% \meta{optional \texttt{-}}\meta{digit}\texttt{.}\meta{15 digits}\texttt{e}\meta{optional sign}\meta{exponent} |
| 341 |
% \end{quote} |
| 342 |
% The leading \meta{digit} is non-zero except in the case of $\pm 0$. |
| 343 |
% The values $\pm\infty$ and~\nan{} trigger an \enquote{invalid |
| 344 |
% operation} exception. Normal category codes apply: thus the |e| is |
| 345 |
% category code~$11$ (a letter). |
| 346 |
% For a tuple, each item is converted using \cs{fp_to_scientific:n} and they |
| 347 |
% are combined as |
| 348 |
% |(|\meta{fp_1}\verb*|, |\meta{fp_2}\verb*|, |\ldots{}\meta{fp_n}|)| |
| 349 |
% if $n>1$ and |(|\meta{fp_1}|,)| or |()| for fewer items. |
| 350 |
% \end{function} |
| 351 |
% |
| 352 |
% \begin{function}[EXP, updated = 2016-03-22] |
| 353 |
% {\fp_to_tl:N, \fp_to_tl:c, \fp_to_tl:n} |
| 354 |
% \begin{syntax} |
| 355 |
% \cs{fp_to_tl:N} \meta{fp~var} |
| 356 |
% \cs{fp_to_tl:n} \Arg{fp expr} |
| 357 |
% \end{syntax} |
| 358 |
% Evaluates the \meta{fp expr} and expresses the |
| 359 |
% result in (almost) the shortest possible form. Numbers in the |
| 360 |
% ranges $(0,10^{-3})$ and $[10^{16},\infty)$ are expressed in |
| 361 |
% scientific notation with trailing zeros trimmed and no decimal |
| 362 |
% separator when there is a single significant digit (this differs from |
| 363 |
% \cs{fp_to_scientific:n}). Numbers in the range $[10^{-3},10^{16})$ |
| 364 |
% are expressed in a decimal notation without exponent, with trailing |
| 365 |
% zeros trimmed, and no decimal separator for integer values (see |
| 366 |
% \cs{fp_to_decimal:n}. Negative numbers start with~|-|. The |
| 367 |
% special values $\pm 0$, $\pm\infty$ and~\nan{} are rendered as |
| 368 |
% |0|, |-0|, \texttt{inf}, \texttt{-inf}, and~\texttt{nan} |
| 369 |
% respectively. Normal category codes apply and thus \texttt{inf} or |
| 370 |
% \texttt{nan}, if produced, are made up of letters. |
| 371 |
% For a tuple, each item is converted using \cs{fp_to_tl:n} and they are |
| 372 |
% combined as |
| 373 |
% |(|\meta{fp_1}\verb*|, |\meta{fp_2}\verb*|, |\ldots{}\meta{fp_n}|)| |
| 374 |
% if $n>1$ and |(|\meta{fp_1}|,)| or |()| for fewer items. |
| 375 |
% \end{function} |
| 376 |
% |
| 377 |
% \begin{function}[EXP, updated = 2012-07-08] |
| 378 |
% {\fp_use:N, \fp_use:c} |
| 379 |
% \begin{syntax} |
| 380 |
% \cs{fp_use:N} \meta{fp~var} |
| 381 |
% \end{syntax} |
| 382 |
% Inserts the value of the \meta{fp~var} into the input stream as a |
| 383 |
% decimal number with no exponent. |
| 384 |
% Leading or trailing zeros may be inserted to compensate for the |
| 385 |
% exponent. Non-significant trailing zeros are trimmed. Integers are |
| 386 |
% expressed without a decimal separator. The values $\pm\infty$ |
| 387 |
% and~\nan{} trigger an \enquote{invalid operation} exception. |
| 388 |
% For a tuple, each item is converted using \cs{fp_to_decimal:n} and they are |
| 389 |
% combined as |
| 390 |
% |(|\meta{fp_1}\verb*|, |\meta{fp_2}\verb*|, |\ldots{}\meta{fp_n}|)| |
| 391 |
% if $n>1$ and |(|\meta{fp_1}|,)| or |()| for fewer items. |
| 392 |
% This function is identical to \cs{fp_to_decimal:N}. |
| 393 |
% \end{function} |
| 394 |
% |
| 395 |
% \section{Formatting floating points} |
| 396 |
% |
| 397 |
% \begin{function}[EXP, added = 2025-06-09]{\fp_format:nn} |
| 398 |
% \begin{syntax} |
| 399 |
% \cs{fp_format:nn} \Arg{fp expr} \Arg{format specification} |
| 400 |
% \end{syntax} |
| 401 |
% Evaluates the \meta{fp expr} and converts the |
| 402 |
% result to a string according to the \meta{format specification}. |
| 403 |
% The \meta{style} can be |
| 404 |
% \begin{itemize} |
| 405 |
% \item |e| for scientific notation, with one digit before and |
| 406 |
% \meta{precision} digits after the decimal separator, and an |
| 407 |
% integer exponent, following |e|; |
| 408 |
% \item |f| for a fixed point notation, with \meta{precision} digits |
| 409 |
% after the decimal separator and no exponent; |
| 410 |
% \item |g| for a general format, which uses style |f| for numbers |
| 411 |
% in the range $[10^{-4}, 10^{\meta{precision}})$ and style |e| |
| 412 |
% otherwise. |
| 413 |
% \end{itemize} |
| 414 |
% When there is no \meta{style} specifier nor \meta{precision} the |
| 415 |
% number is displayed without rounding. Otherwise the |
| 416 |
% \meta{precision} defaults to~$6$. The details of the |
| 417 |
% \meta{format specification} are described in |
| 418 |
% Section~\ref{sec:l3str-format:spec}. |
| 419 |
% \end{function} |
| 420 |
% |
| 421 |
% \section{Floating point conditionals} |
| 422 |
% |
| 423 |
% \begin{function}[EXP, pTF, updated = 2012-05-08, tested = m3fp002] |
| 424 |
% {\fp_if_exist:N, \fp_if_exist:c} |
| 425 |
% \begin{syntax} |
| 426 |
% \cs{fp_if_exist_p:N} \meta{fp~var} |
| 427 |
% \cs{fp_if_exist:NTF} \meta{fp~var} \Arg{true code} \Arg{false code} |
| 428 |
% \end{syntax} |
| 429 |
% Tests whether the \meta{fp~var} is currently defined. This does not |
| 430 |
% check that the \meta{fp~var} really is a floating point variable. |
| 431 |
% \end{function} |
| 432 |
% |
| 433 |
% \begin{function}[EXP, pTF, updated = 2012-05-08, |
| 434 |
% tested = m3fp-logic001]{\fp_compare:nNn} |
| 435 |
% \begin{syntax} |
| 436 |
% \cs{fp_compare_p:nNn} \Arg{fp expr_1} \meta{relation} \Arg{fp expr_2} |
| 437 |
% \cs{fp_compare:nNnTF} \Arg{fp expr_1} \meta{relation} \Arg{fp expr_2} \Arg{true code} \Arg{false code} |
| 438 |
% \end{syntax} |
| 439 |
% Compares the \meta{fp expr_1} and the \meta{fp expr_2}, and returns |
| 440 |
% \texttt{true} if the \meta{relation} is obeyed. Two floating points |
| 441 |
% $x$ and~$y$ may obey four mutually exclusive relations: |
| 442 |
% $x<y$, $x=y$, $x>y$, or $x?y$ (\enquote{not ordered}). The last |
| 443 |
% case occurs exactly if one or both operands is~\nan{} or is a tuple, |
| 444 |
% unless they are equal tuples. Note that a~\nan{} is distinct from |
| 445 |
% any value, even another~\nan{}, hence $x=x$ is not true for |
| 446 |
% a~\nan{}. To test if a value is~\nan{}, compare it to an arbitrary |
| 447 |
% number with the \enquote{not ordered} relation. |
| 448 |
% \begin{verbatim} |
| 449 |
% \fp_compare:nNnTF { <value> } ? { 0 } |
| 450 |
% { } % <value> is nan |
| 451 |
% { } % <value> is not nan |
| 452 |
% \end{verbatim} |
| 453 |
% Tuples are equal if they have the same number of items and items |
| 454 |
% compare equal (in particular there must be no~\nan{}). |
| 455 |
% At present any other comparison with tuples yields |?| (not ordered). |
| 456 |
% This is experimental. |
| 457 |
% |
| 458 |
% This function is less flexible than \cs{fp_compare:nTF} but slightly |
| 459 |
% faster. It is provided for consistency with \cs{int_compare:nNnTF} |
| 460 |
% and \cs{dim_compare:nNnTF}. |
| 461 |
% \end{function} |
| 462 |
% |
| 463 |
% \begin{function}[EXP, pTF, updated = 2013-12-14, |
| 464 |
% tested = m3fp-logic001]{\fp_compare:n} |
| 465 |
% \begin{syntax} |
| 466 |
% \cs{fp_compare_p:n} \\ |
| 467 |
% ~~\{ \\ |
| 468 |
% ~~~~\meta{fp expr_1} \meta{relation_1} \\ |
| 469 |
% ~~~~\ldots{} \\ |
| 470 |
% ~~~~\meta{fp expr_N} \meta{relation_N} \\ |
| 471 |
% ~~~~\meta{fp expr_{N+1}} \\ |
| 472 |
% ~~\} \\ |
| 473 |
% \cs{fp_compare:nTF} |
| 474 |
% ~~\{ \\ |
| 475 |
% ~~~~\meta{fp expr_1} \meta{relation_1} \\ |
| 476 |
% ~~~~\ldots{} \\ |
| 477 |
% ~~~~\meta{fp expr_N} \meta{relation_N} \\ |
| 478 |
% ~~~~\meta{fp expr_{N+1}} \\ |
| 479 |
% ~~\} \\ |
| 480 |
% ~~\Arg{true code} \Arg{false code} |
| 481 |
% \end{syntax} |
| 482 |
% Evaluates the \meta{fp exprs} as described for |
| 483 |
% \cs{fp_eval:n} and compares consecutive result using the |
| 484 |
% corresponding \meta{relation}, namely it compares \meta{fp expr_1} |
| 485 |
% and \meta{fp expr_2} using the \meta{relation_1}, then |
| 486 |
% \meta{fp expr_2} and \meta{fp expr_3} using the \meta{relation_2}, |
| 487 |
% until finally comparing \meta{fp expr_N} and \meta{fp expr_{N+1}} |
| 488 |
% using the \meta{relation_N}. The test yields \texttt{true} if all |
| 489 |
% comparisons are \texttt{true}. Each \meta{floating point |
| 490 |
% expression} is evaluated only once. Contrarily to |
| 491 |
% \cs{int_compare:nTF}, all \meta{fp exprs} are |
| 492 |
% computed, even if one comparison is \texttt{false}. Two floating |
| 493 |
% points $x$ and~$y$ may obey four mutually exclusive |
| 494 |
% relations: $x<y$, $x=y$, $x>y$, or $x?y$ (\enquote{not ordered}). |
| 495 |
% The last case occurs exactly if one or both operands is~\nan{} or is |
| 496 |
% a tuple, unless they are equal tuples. Each \meta{relation} |
| 497 |
% can be any (non-empty) combination of |<|, |=|, |>|, and~|?|, plus |
| 498 |
% an optional leading~|!| (which negates the \meta{relation}), with |
| 499 |
% the restriction that the \meta{relation} may not start with~|?|, as |
| 500 |
% this symbol has a different meaning (in combination with~|:|) within |
| 501 |
% floating point expressions. The comparison $x$~\meta{relation}~$y$ |
| 502 |
% is then \texttt{true} if the \meta{relation} does not start with~|!| |
| 503 |
% and the actual relation (|<|, |=|, |>|, or~|?|) between $x$ and~$y$ |
| 504 |
% appears within the \meta{relation}, or on the contrary if the |
| 505 |
% \meta{relation} starts with~|!| and the relation between $x$ and~$y$ |
| 506 |
% does not appear within the \meta{relation}. Common choices of |
| 507 |
% \meta{relation} include |>=|~(greater or equal), |!=|~(not equal), |
| 508 |
% |!?|~or~|<=>| (comparable). |
| 509 |
% |
| 510 |
% This function is more flexible than \cs{fp_compare:nNnTF} and only |
| 511 |
% slightly slower. |
| 512 |
% \end{function} |
| 513 |
% |
| 514 |
% \begin{function}[pTF, added = 2019-08-25]{\fp_if_nan:n} |
| 515 |
% \begin{syntax} |
| 516 |
% \cs{fp_if_nan_p:n} \Arg{fp expr} |
| 517 |
% \cs{fp_if_nan:nTF} \Arg{fp expr} \Arg{true code} \Arg{false code} |
| 518 |
% \end{syntax} |
| 519 |
% Evaluates the \meta{fp expr} and tests whether the result is exactly |
| 520 |
% \nan{}. The test returns \texttt{false} for any other result, even |
| 521 |
% a tuple containing \nan{}. |
| 522 |
% \end{function} |
| 523 |
% |
| 524 |
% \section{Floating point expression loops} |
| 525 |
% |
| 526 |
% \begin{function}[rEXP, added = 2012-08-16, tested = m3fp-logic003] |
| 527 |
% {\fp_do_until:nNnn} |
| 528 |
% \begin{syntax} |
| 529 |
% \cs{fp_do_until:nNnn} \Arg{fp expr_1} \meta{relation} \Arg{fp expr_2} \Arg{code} |
| 530 |
% \end{syntax} |
| 531 |
% Places the \meta{code} in the input stream for \TeX{} to process, |
| 532 |
% and then evaluates the relationship between the two \meta{floating |
| 533 |
% point expressions} as described for \cs{fp_compare:nNnTF}. If the |
| 534 |
% test is \texttt{false} then the \meta{code} is inserted into |
| 535 |
% the input stream again and a loop occurs until the |
| 536 |
% \meta{relation} is \texttt{true}. |
| 537 |
% \end{function} |
| 538 |
% |
| 539 |
% \begin{function}[rEXP, added = 2012-08-16, tested = m3fp-logic003] |
| 540 |
% {\fp_do_while:nNnn} |
| 541 |
% \begin{syntax} |
| 542 |
% \cs{fp_do_while:nNnn} \Arg{fp expr_1} \meta{relation} \Arg{fp expr_2} \Arg{code} |
| 543 |
% \end{syntax} |
| 544 |
% Places the \meta{code} in the input stream for \TeX{} to process, |
| 545 |
% and then evaluates the relationship between the two \meta{floating |
| 546 |
% point expressions} as described for \cs{fp_compare:nNnTF}. If the |
| 547 |
% test is \texttt{true} then the \meta{code} is inserted into the |
| 548 |
% input stream again and a loop occurs until the \meta{relation} |
| 549 |
% is \texttt{false}. |
| 550 |
% \end{function} |
| 551 |
% |
| 552 |
% \begin{function}[rEXP, added = 2012-08-16, tested = m3fp-logic003] |
| 553 |
% {\fp_until_do:nNnn} |
| 554 |
% \begin{syntax} |
| 555 |
% \cs{fp_until_do:nNnn} \Arg{fp expr_1} \meta{relation} \Arg{fp expr_2} \Arg{code} |
| 556 |
% \end{syntax} |
| 557 |
% Evaluates the relationship between the two \meta{floating point |
| 558 |
% expressions} as described for \cs{fp_compare:nNnTF}, and then |
| 559 |
% places the \meta{code} in the input stream if the \meta{relation} is |
| 560 |
% \texttt{false}. After the \meta{code} has been processed by \TeX{} |
| 561 |
% the test is repeated, and a loop occurs until the test is |
| 562 |
% \texttt{true}. |
| 563 |
% \end{function} |
| 564 |
% |
| 565 |
% \begin{function}[rEXP, added = 2012-08-16, tested = m3fp-logic003] |
| 566 |
% {\fp_while_do:nNnn} |
| 567 |
% \begin{syntax} |
| 568 |
% \cs{fp_while_do:nNnn} \Arg{fp expr_1} \meta{relation} \Arg{fp expr_2} \Arg{code} |
| 569 |
% \end{syntax} |
| 570 |
% Evaluates the relationship between the two \meta{floating point |
| 571 |
% expressions} as described for \cs{fp_compare:nNnTF}, and then |
| 572 |
% places the \meta{code} in the input stream if the \meta{relation} is |
| 573 |
% \texttt{true}. After the \meta{code} has been processed by \TeX{} |
| 574 |
% the test is repeated, and a loop occurs until the test is |
| 575 |
% \texttt{false}. |
| 576 |
% \end{function} |
| 577 |
% |
| 578 |
% \begin{function}[rEXP, added = 2012-08-16, updated = 2013-12-14, tested = m3fp-logic003] |
| 579 |
% {\fp_do_until:nn} |
| 580 |
% \begin{syntax} |
| 581 |
% \cs{fp_do_until:nn} \{ \meta{fp expr_1} \meta{relation} \meta{fp expr_2} \} \Arg{code} |
| 582 |
% \end{syntax} |
| 583 |
% Places the \meta{code} in the input stream for \TeX{} to process, |
| 584 |
% and then evaluates the relationship between the two \meta{floating |
| 585 |
% point expressions} as described for \cs{fp_compare:nTF}. If the |
| 586 |
% test is \texttt{false} then the \meta{code} is inserted into |
| 587 |
% the input stream again and a loop occurs until the |
| 588 |
% \meta{relation} is \texttt{true}. |
| 589 |
% \end{function} |
| 590 |
% |
| 591 |
% \begin{function}[rEXP, added = 2012-08-16, updated = 2013-12-14, tested = m3fp-logic003] |
| 592 |
% {\fp_do_while:nn} |
| 593 |
% \begin{syntax} |
| 594 |
% \cs{fp_do_while:nn} \{ \meta{fp expr_1} \meta{relation} \meta{fp expr_2} \} \Arg{code} |
| 595 |
% \end{syntax} |
| 596 |
% Places the \meta{code} in the input stream for \TeX{} to process, |
| 597 |
% and then evaluates the relationship between the two \meta{floating |
| 598 |
% point expressions} as described for \cs{fp_compare:nTF}. If the |
| 599 |
% test is \texttt{true} then the \meta{code} is inserted into the |
| 600 |
% input stream again and a loop occurs until the \meta{relation} |
| 601 |
% is \texttt{false}. |
| 602 |
% \end{function} |
| 603 |
% |
| 604 |
% \begin{function}[rEXP, added = 2012-08-16, updated = 2013-12-14, tested = m3fp-logic003] |
| 605 |
% {\fp_until_do:nn} |
| 606 |
% \begin{syntax} |
| 607 |
% \cs{fp_until_do:nn} \{ \meta{fp expr_1} \meta{relation} \meta{fp expr_2} \} \Arg{code} |
| 608 |
% \end{syntax} |
| 609 |
% Evaluates the relationship between the two \meta{floating point |
| 610 |
% expressions} as described for \cs{fp_compare:nTF}, and then places |
| 611 |
% the \meta{code} in the input stream if the \meta{relation} is |
| 612 |
% \texttt{false}. After the \meta{code} has been processed by \TeX{} |
| 613 |
% the test is repeated, and a loop occurs until the test is |
| 614 |
% \texttt{true}. |
| 615 |
% \end{function} |
| 616 |
% |
| 617 |
% \begin{function}[rEXP, added = 2012-08-16, updated = 2013-12-14, tested = m3fp-logic003] |
| 618 |
% {\fp_while_do:nn} |
| 619 |
% \begin{syntax} |
| 620 |
% \cs{fp_while_do:nn} \{ \meta{fp expr_1} \meta{relation} \meta{fp expr_2} \} \Arg{code} |
| 621 |
% \end{syntax} |
| 622 |
% Evaluates the relationship between the two \meta{floating point |
| 623 |
% expressions} as described for \cs{fp_compare:nTF}, and then places |
| 624 |
% the \meta{code} in the input stream if the \meta{relation} is |
| 625 |
% \texttt{true}. After the \meta{code} has been processed by \TeX{} |
| 626 |
% the test is repeated, and a loop occurs until the test is |
| 627 |
% \texttt{false}. |
| 628 |
% \end{function} |
| 629 |
% |
| 630 |
% \begin{function}[added = 2016-11-21, updated = 2016-12-06, rEXP] |
| 631 |
% {\fp_step_function:nnnN, \fp_step_function:nnnc} |
| 632 |
% \begin{syntax} |
| 633 |
% \cs{fp_step_function:nnnN} \Arg{initial value} \Arg{step} \Arg{final value} \meta{function} |
| 634 |
% \end{syntax} |
| 635 |
% This function first evaluates the \meta{initial value}, \meta{step} |
| 636 |
% and \meta{final value}, each of which should be a floating point |
| 637 |
% expression evaluating to a floating point number, not a tuple. |
| 638 |
% The \meta{function} is then placed in front of each \meta{value} |
| 639 |
% from the \meta{initial value} to the \meta{final value} in turn |
| 640 |
% (using \meta{step} between each \meta{value}). The \meta{step} must |
| 641 |
% be non-zero. If the \meta{step} is positive, the loop stops when |
| 642 |
% the \meta{value} becomes larger than the \meta{final value}. If the |
| 643 |
% \meta{step} is negative, the loop stops when the \meta{value} |
| 644 |
% becomes smaller than the \meta{final value}. The \meta{function} |
| 645 |
% should absorb one numerical argument. For example |
| 646 |
% \begin{verbatim} |
| 647 |
% \cs_set:Npn \my_func:n #1 { [I~saw~#1] \quad } |
| 648 |
% \fp_step_function:nnnN { 1.0 } { 0.1 } { 1.5 } \my_func:n |
| 649 |
% \end{verbatim} |
| 650 |
% would print |
| 651 |
% \begin{quote} |
| 652 |
% [I saw 1.0] \quad |
| 653 |
% [I saw 1.1] \quad |
| 654 |
% [I saw 1.2] \quad |
| 655 |
% [I saw 1.3] \quad |
| 656 |
% [I saw 1.4] \quad |
| 657 |
% [I saw 1.5] \quad |
| 658 |
% \end{quote} |
| 659 |
% \begin{texnote} |
| 660 |
% Due to rounding, it may happen that adding the \meta{step} to the |
| 661 |
% \meta{value} does not change the \meta{value}; such cases give an |
| 662 |
% error, as they would otherwise lead to an infinite loop. |
| 663 |
% \end{texnote} |
| 664 |
% \end{function} |
| 665 |
% |
| 666 |
% \begin{function}[added = 2016-11-21, updated = 2016-12-06] |
| 667 |
% {\fp_step_inline:nnnn} |
| 668 |
% \begin{syntax} |
| 669 |
% \cs{fp_step_inline:nnnn} \Arg{initial value} \Arg{step} \Arg{final value} \Arg{code} |
| 670 |
% \end{syntax} |
| 671 |
% This function first evaluates the \meta{initial value}, \meta{step} |
| 672 |
% and \meta{final value}, all of which should be floating point |
| 673 |
% expressions evaluating to a floating point number, not a tuple. |
| 674 |
% Then for each \meta{value} from the \meta{initial value} to the |
| 675 |
% \meta{final value} in turn (using \meta{step} between each |
| 676 |
% \meta{value}), the \meta{code} is inserted into the input stream |
| 677 |
% with |#1| replaced by the current \meta{value}. Thus the |
| 678 |
% \meta{code} should define a function of one argument~(|#1|). |
| 679 |
% \end{function} |
| 680 |
% |
| 681 |
% \begin{function}[added = 2017-04-12]{\fp_step_variable:nnnNn} |
| 682 |
% \begin{syntax} |
| 683 |
% \cs{fp_step_variable:nnnNn} \\ |
| 684 |
% ~~\Arg{initial value} \Arg{step} \Arg{final value} \meta{tl~var} \Arg{code} |
| 685 |
% \end{syntax} |
| 686 |
% This function first evaluates the \meta{initial value}, \meta{step} |
| 687 |
% and \meta{final value}, all of which should be floating point |
| 688 |
% expressions evaluating to a floating point number, not a tuple. |
| 689 |
% Then for each \meta{value} from the \meta{initial value} to the |
| 690 |
% \meta{final value} in turn (using \meta{step} between each |
| 691 |
% \meta{value}), the \meta{code} is inserted into the input stream, |
| 692 |
% with the \meta{tl~var} defined as the current \meta{value}. Thus |
| 693 |
% the \meta{code} should make use of the \meta{tl~var}. |
| 694 |
% \end{function} |
| 695 |
% |
| 696 |
% \section{Symbolic expressions} |
| 697 |
% |
| 698 |
% Floating point expressions support variables: these can only be set locally, |
| 699 |
% so act like standard \cs[no-index]{l_\dots} variables. |
| 700 |
% \begin{quote}\let\obeyedline=\newline\obeylines^^A |
| 701 |
% \cs{fp_new_variable:n} |{ A }| |
| 702 |
% \cs{fp_set:Nn} \cs{l_tmpb_fp} |{ 1 * sin(A) + 3**2 }| |
| 703 |
% \cs{fp_show:n} |{| \cs{l_tmpb_fp} |}| |
| 704 |
% \cs{fp_show:N} \cs{l_tmpb_fp} |
| 705 |
% \cs{fp_set_variable:nn} |{ A }| |{ pi/2 }| |
| 706 |
% \cs{fp_show:n} |{| \cs{l_tmpb_fp} |}| |
| 707 |
% \cs{fp_show:N} \cs{l_tmpb_fp} |
| 708 |
% \cs{fp_set_variable:nn} |{ A }| |{ 0 }| |
| 709 |
% \cs{fp_show:n} |{| \cs{l_tmpb_fp} |}| |
| 710 |
% \cs{fp_show:N} \cs{l_tmpb_fp} |
| 711 |
% \end{quote} |
| 712 |
% defines~|A| to be a variable, then defines \cs{l_tmpb_fp} to stand for |
| 713 |
% |1*sin(A)+9| (note that |3**2| is evaluated, but the |1*|~product is |
| 714 |
% not simplified away). Until \cs{l_tmpb_fp} is changed, \cs{fp_show:N} |
| 715 |
% \cs{l_tmpb_fp} will show |((1*sin(A))+9)| regardless of the value |
| 716 |
% of~|A|. The next step defines~|A| to be equal to~|pi/2|: then |
| 717 |
% \cs{fp_show:n} |{| \cs{l_tmpb_fp} |}| will evaluate \cs{l_tmpb_fp} and |
| 718 |
% show~|10|. We then redefine~|A| to be~|0|: since \cs{l_tmpb_fp} still |
| 719 |
% stands for |1*sin(A)+9|, the value shown is then~|9|. Variables can |
| 720 |
% be set with \cs{fp_set_variable:nn} to arbitrary floating point |
| 721 |
% expressions including other variables. |
| 722 |
% |
| 723 |
% At present, the following operations and functions are \emph{not} supported |
| 724 |
% \begin{itemize} |
| 725 |
% \item infix binary comparisons like |>|, |=|, |<| |
| 726 |
% \item infix ternary operator like |?:| |
| 727 |
% \item prefix variable-ary functions like |round| and friends, |
| 728 |
% |min|, |max|, |atan|, |acot|, |atand|, |acotd| |
| 729 |
% \end{itemize} |
| 730 |
% |
| 731 |
% \begin{function}[added = 2023-10-19]{\fp_new_variable:n} |
| 732 |
% \begin{syntax} |
| 733 |
% \cs{fp_new_variable:n} \Arg{identifier} |
| 734 |
% \end{syntax} |
| 735 |
% Declares the \meta{identifier} as a variable, which allows it to be |
| 736 |
% used in floating point expressions. For instance, |
| 737 |
% \begin{quote} |
| 738 |
% \cs{fp_new_variable:n} |{ A }| \\ |
| 739 |
% \cs{fp_show:n} |{ A**2 - A + 1 }| |
| 740 |
% \end{quote} |
| 741 |
% shows |(((A^2)-A)+1)|. If the declaration was missing, the parser |
| 742 |
% would complain about an \enquote{\texttt{Unknown fp word 'A'}}. The |
| 743 |
% \meta{identifier} must consist entirely of Latin letters among |
| 744 |
% |[a-zA-Z]|. |
| 745 |
% \end{function} |
| 746 |
% |
| 747 |
% \begin{function}[added = 2023-10-19]{\fp_set_variable:nn} |
| 748 |
% \begin{syntax} |
| 749 |
% \cs{fp_set_variable:nn} \Arg{identifier} \Arg{fp expr} |
| 750 |
% \end{syntax} |
| 751 |
% Sets the \meta{identifier} to stand in any further expression for |
| 752 |
% the result of evaluating the \meta{floating point expression} as |
| 753 |
% much as possible. The \meta{identifier} must be declared by |
| 754 |
% \cs{fp_new_function:n} first. The result may contain other |
| 755 |
% variables, which are then replaced by their values if they have any. |
| 756 |
% For instance, |
| 757 |
% \begin{quote}\let\obeyedline=\newline\obeylines^^A |
| 758 |
% \cs{fp_new_variable:n} |{ A }| |
| 759 |
% \cs{fp_new_variable:n} |{ B }| |
| 760 |
% \cs{fp_new_variable:n} |{ C }| |
| 761 |
% \cs{fp_set_variable:nn} |{ A } { 3 }| |
| 762 |
% \cs{fp_set_variable:nn} |{ C } { A ** 2 + B * 1 }| |
| 763 |
% \cs{fp_show:n} |{ C + 4 }| |
| 764 |
% \cs{fp_set_variable:nn} |{ A } { 4 }| |
| 765 |
% \cs{fp_show:n} |{ C + 4 }| |
| 766 |
% \end{quote} |
| 767 |
% shows |((9+(B*1))+4)| twice: changing the value of~|A| to~|4| does |
| 768 |
% not alter~|C| because |A|~was replaced by its value~|3| when |
| 769 |
% evaluating |A**2+B*1|. |
| 770 |
% \end{function} |
| 771 |
% |
| 772 |
% \begin{function}[added = 2023-10-19]{\fp_clear_variable:n} |
| 773 |
% \begin{syntax} |
| 774 |
% \cs{fp_clear_variable:n} \Arg{identifier} |
| 775 |
% \end{syntax} |
| 776 |
% Removes any value given by \cs{fp_set_variable:nn} to the variable |
| 777 |
% with this \meta{identifier}. For instance, |
| 778 |
% \begin{quote}\let\obeyedline=\newline\obeylines^^A |
| 779 |
% \cs{fp_new_variable:n} |{ A }| |
| 780 |
% \cs{fp_set_variable:nn} |{ A } { 3 }| |
| 781 |
% \cs{fp_show:n} |{ A ^ 2 }| |
| 782 |
% \cs{fp_clear_variable:n} |{ A }| |
| 783 |
% \cs{fp_show:n} |{ A ^ 2 }| |
| 784 |
% \end{quote} |
| 785 |
% shows~|9|, then~|(A^2)|. |
| 786 |
% \end{function} |
| 787 |
% |
| 788 |
% \section{User-defined functions} |
| 789 |
% |
| 790 |
% It is possible to define new user functions which can be used inside |
| 791 |
% the argument to \cs{fp_eval:n}, etc. These functions may take one or |
| 792 |
% more named arguments, and should be implemented using expansion methods |
| 793 |
% only. |
| 794 |
% |
| 795 |
% \begin{function}[added = 2023-10-19]{\fp_new_function:n} |
| 796 |
% \begin{syntax} |
| 797 |
% \cs{fp_new_function:n} \Arg{identifier} |
| 798 |
% \end{syntax} |
| 799 |
% Declares the \meta{identifier} as a function, which allows it to be |
| 800 |
% used in floating point expressions. For instance, |
| 801 |
% \begin{quote} |
| 802 |
% \cs{fp_new_function:n} |{ foo }| \\ |
| 803 |
% \cs{fp_show:n} |{ foo ( 1 + 2 , foo(3), A ) ** 2 } }| |
| 804 |
% \end{quote} |
| 805 |
% shows |(foo(3, foo(3), A))^(2)|. If the declaration was missing, |
| 806 |
% the parser would complain about an \enquote{\texttt{Unknown fp word 'foo'}}. |
| 807 |
% The \meta{identifier} must consist entirely of Latin letters |[a-zA-Z]|. |
| 808 |
% \end{function} |
| 809 |
% |
| 810 |
% \begin{function}[added = 2023-10-19]{\fp_set_function:nnn} |
| 811 |
% \begin{syntax} |
| 812 |
% \cs{fp_set_function:nnn} \Arg{identifier} \Arg{vars} \Arg{fp expr} |
| 813 |
% \end{syntax} |
| 814 |
% Sets the \meta{identifier} to stand in any further expression for |
| 815 |
% the result of evaluating the \meta{floating point expression}, with |
| 816 |
% the \meta{identifier} accepting the \meta{vars} (a non-empty |
| 817 |
% comma-separated list). The \meta{identifier} must be declared by |
| 818 |
% \cs{fp_new_function:n} first. |
| 819 |
% The result may contain other functions, which are |
| 820 |
% then replaced by their results if they have any. For instance, |
| 821 |
% \begin{quote} |
| 822 |
% \cs{fp_new_function:n} |{ npow }| \\ |
| 823 |
% \cs{fp_set_function:nnn} |{ npow } { a,b } { a**b }| \\ |
| 824 |
% \cs{fp_show:n} |{ npow(16,0.25) }| |
| 825 |
% \end{quote} |
| 826 |
% shows |2|. The names of the \meta{vars} must |
| 827 |
% consist entirely of Latin letters |[a-zA-Z]|, but are otherwise not |
| 828 |
% restricted: in particular, they are independent of any variables |
| 829 |
% declared by \cs{fp_new_variable:n}. |
| 830 |
% \end{function} |
| 831 |
% |
| 832 |
% \begin{function}[added = 2023-10-19]{\fp_clear_function:n} |
| 833 |
% \begin{syntax} |
| 834 |
% \cs{fp_clear_function:n} \Arg{identifier} |
| 835 |
% \end{syntax} |
| 836 |
% Removes any definition given by \cs{fp_set_function:nnn} to the function |
| 837 |
% with this \meta{identifier}. |
| 838 |
% \end{function} |
| 839 |
% |
| 840 |
% \section{Some useful constants, and scratch variables} |
| 841 |
% |
| 842 |
% \begin{variable}[added = 2012-05-08, module = fp]{\c_zero_fp, \c_minus_zero_fp} |
| 843 |
% Zero, with either sign. |
| 844 |
% \end{variable} |
| 845 |
% |
| 846 |
% \begin{variable}[added = 2012-05-08, module = fp]{\c_one_fp} |
| 847 |
% One as an \texttt{fp}: useful for comparisons in some places. |
| 848 |
% \end{variable} |
| 849 |
% |
| 850 |
% \begin{variable}[added = 2012-05-08, module = fp]{\c_inf_fp, \c_minus_inf_fp} |
| 851 |
% Infinity, with either sign. These can be input directly in a |
| 852 |
% floating point expression as \texttt{inf} and \texttt{-inf}. |
| 853 |
% \end{variable} |
| 854 |
% |
| 855 |
% \begin{variable}[added = 2012-05-08, module = fp]{\c_nan_fp} |
| 856 |
% Not a number. This can be input directly in a floating point expression |
| 857 |
% as \texttt{nan}. |
| 858 |
% \end{variable} |
| 859 |
% |
| 860 |
% \begin{variable}[updated = 2012-05-08, module = fp]{\c_e_fp} |
| 861 |
% The value of the base of the natural logarithm, $\mathrm{e} = \exp(1)$. |
| 862 |
% \end{variable} |
| 863 |
% |
| 864 |
% \begin{variable}[updated = 2013-11-17, module = fp]{\c_pi_fp} |
| 865 |
% The value of~$\pi$. This can be input directly in a floating point |
| 866 |
% expression as~\texttt{pi}. |
| 867 |
% \end{variable} |
| 868 |
% |
| 869 |
% \begin{variable}[added = 2012-05-08, updated = 2013-11-17, module = fp] |
| 870 |
% {\c_one_degree_fp} |
| 871 |
% The value of $1^{\circ}$ in radians. Multiply an angle given in |
| 872 |
% degrees by this value to obtain a result in radians. Note that |
| 873 |
% trigonometric functions expecting an argument in radians or in |
| 874 |
% degrees are both available. Within floating point expressions, this |
| 875 |
% can be accessed as \texttt{deg}. |
| 876 |
% \end{variable} |
| 877 |
% |
| 878 |
% \section{Scratch variables} |
| 879 |
% |
| 880 |
% \begin{variable}[module = fp]{\l_tmpa_fp, \l_tmpb_fp} |
| 881 |
% Scratch floating points for local assignment. These are never used by |
| 882 |
% the kernel code, and so are safe for use with any \LaTeX3-defined |
| 883 |
% function. However, they may be overwritten by other non-kernel |
| 884 |
% code and so should only be used for short-term storage. |
| 885 |
% \end{variable} |
| 886 |
% |
| 887 |
% \begin{variable}[module = fp]{\g_tmpa_fp, \g_tmpb_fp} |
| 888 |
% Scratch floating points for global assignment. These are never used by |
| 889 |
% the kernel code, and so are safe for use with any \LaTeX3-defined |
| 890 |
% function. However, they may be overwritten by other non-kernel |
| 891 |
% code and so should only be used for short-term storage. |
| 892 |
% \end{variable} |
| 893 |
% |
| 894 |
% \section{Floating point exceptions} |
| 895 |
% \label{sec:l3fp:fp-exceptions} |
| 896 |
% |
| 897 |
% \emph{The functions defined in this section are experimental, and |
| 898 |
% their functionality may be altered or removed altogether.} |
| 899 |
% |
| 900 |
% \enquote{Exceptions} may occur when performing some floating point |
| 901 |
% operations, such as \texttt{0 / 0}, or \texttt{10 ** 1e9999}. The |
| 902 |
% relevant \textsc{IEEE} standard defines $5$ types of exceptions, |
| 903 |
% of which we implement~$4$. |
| 904 |
% \begin{itemize} |
| 905 |
% \item \emph{Overflow} occurs whenever the result of an operation is |
| 906 |
% too large to be represented as a normal floating point number. This |
| 907 |
% results in $\pm \infty$. |
| 908 |
% \item \emph{Underflow} occurs whenever the result of an operation is |
| 909 |
% too close to $0$ to be represented as a normal floating point |
| 910 |
% number. This results in $\pm 0$. |
| 911 |
% \item \emph{Invalid operation} occurs for operations with no defined |
| 912 |
% outcome, for instance $0/0$ or $\sin(\infty)$, and results in a \nan{}. |
| 913 |
% It also occurs for conversion functions whose target type does not |
| 914 |
% have the appropriate infinite or \nan{} value (\emph{e.g.}, |
| 915 |
% \cs{fp_to_dim:n}). |
| 916 |
% \item \emph{Division by zero} occurs when dividing a non-zero number |
| 917 |
% by $0$, or when evaluating functions at poles, \emph{e.g.}, |
| 918 |
% $\ln(0)$ or $\cot(0)$. This results in $\pm\infty$. |
| 919 |
% \item [\emph{(not yet)}] \emph{Inexact} occurs whenever the result of |
| 920 |
% a computation is not exact, in other words, almost always. At the |
| 921 |
% moment, this exception is entirely ignored in \LaTeX3. |
| 922 |
% \end{itemize} |
| 923 |
% To each exception we associate a \enquote{flag}: \cs{l_fp_overflow_flag}, |
| 924 |
% \cs{l_fp_underflow_flag}, \cs{l_fp_invalid_operation_flag} and |
| 925 |
% \cs{l_fp_division_by_zero_flag}. The state of these flags can be tested |
| 926 |
% and modified with commands from \pkg{l3flag} |
| 927 |
% |
| 928 |
% By default, the \enquote{invalid operation} exception triggers an |
| 929 |
% (expandable) error, and raises the corresponding flag. Other |
| 930 |
% exceptions raise the corresponding flag but do not trigger an error. |
| 931 |
% The behavior when an exception occurs can be modified (using |
| 932 |
% \cs{fp_trap:nn}) to either produce an error and raise the flag, or |
| 933 |
% only raise the flag, or do nothing at all. |
| 934 |
% |
| 935 |
% \begin{function}[added = 2012-07-19, updated = 2017-02-13, |
| 936 |
% tested = m3fp-traps001]{\fp_trap:nn} |
| 937 |
% \begin{syntax} |
| 938 |
% \cs{fp_trap:nn} \Arg{exception} \Arg{trap type} |
| 939 |
% \end{syntax} |
| 940 |
% All occurrences of the \meta{exception} (\texttt{overflow}, |
| 941 |
% \texttt{underflow}, \texttt{invalid_operation} or |
| 942 |
% \texttt{division_by_zero}) within the current |
| 943 |
% group are treated as \meta{trap type}, which can be |
| 944 |
% \begin{itemize} |
| 945 |
% \item \texttt{none}: the \meta{exception} will be entirely |
| 946 |
% ignored, and leave no trace; |
| 947 |
% \item \texttt{flag}: the \meta{exception} will turn the |
| 948 |
% corresponding flag on when it occurs; |
| 949 |
% \item \texttt{error}: additionally, the \meta{exception} will halt |
| 950 |
% the \TeX{} run and display some information about the current |
| 951 |
% operation in the terminal. |
| 952 |
% \end{itemize} |
| 953 |
% \end{function} |
| 954 |
% |
| 955 |
% \begin{variable} |
| 956 |
% { |
| 957 |
% \l_fp_overflow_flag, |
| 958 |
% \l_fp_underflow_flag, |
| 959 |
% \l_fp_invalid_operation_flag, |
| 960 |
% \l_fp_division_by_zero_flag |
| 961 |
% } |
| 962 |
% Flags denoting the occurrence of various floating-point exceptions. |
| 963 |
% \end{variable} |
| 964 |
% |
| 965 |
% \section{Viewing floating points} |
| 966 |
% |
| 967 |
% \begin{function}[added = 2012-05-08, updated = 2021-04-29, |
| 968 |
% tested = m3fp002]{\fp_show:N, \fp_show:c, \fp_show:n} |
| 969 |
% \begin{syntax} |
| 970 |
% \cs{fp_show:N} \meta{fp~var} |
| 971 |
% \cs{fp_show:n} \Arg{fp expr} |
| 972 |
% \end{syntax} |
| 973 |
% Evaluates the \meta{fp expr} and displays the |
| 974 |
% result in the terminal. |
| 975 |
% \end{function} |
| 976 |
% |
| 977 |
% \begin{function}[added = 2014-08-22, updated = 2021-04-29] |
| 978 |
% {\fp_log:N, \fp_log:c, \fp_log:n} |
| 979 |
% \begin{syntax} |
| 980 |
% \cs{fp_log:N} \meta{fp~var} |
| 981 |
% \cs{fp_log:n} \Arg{fp expr} |
| 982 |
% \end{syntax} |
| 983 |
% Evaluates the \meta{fp expr} and writes the |
| 984 |
% result in the log file. |
| 985 |
% \end{function} |
| 986 |
% |
| 987 |
% \section{Floating point expressions} |
| 988 |
% |
| 989 |
% \subsection{Input of floating point numbers} \label{sec:l3fp:fp-floats} |
| 990 |
% |
| 991 |
% We support four types of floating point numbers: |
| 992 |
% \begin{itemize} |
| 993 |
% \item $\pm m \cdot 10^{n}$, a floating |
| 994 |
% point number, with integer $1\leq m\leq 10^{16}$, and |
| 995 |
% $-{\ExplSyntaxOn\int_use:N\c__fp_minus_min_exponent_int}\leq |
| 996 |
% n\leq {\ExplSyntaxOn\int_use:N\c__fp_max_exponent_int}$; |
| 997 |
% \item $\pm 0$, zero, with a given sign; |
| 998 |
% \item $\pm \infty$, infinity, with a given sign; |
| 999 |
% \item \nan{}, is \enquote{not a number}, and can be either quiet |
| 1000 |
% or signaling (\emph{not yet}: this distinction is currently |
| 1001 |
% unsupported); |
| 1002 |
% \end{itemize} |
| 1003 |
% Normal floating point numbers are stored in base $10$, with up to $16$ |
| 1004 |
% significant figures. |
| 1005 |
% |
| 1006 |
% On input, a normal floating point number consists of: |
| 1007 |
% \begin{itemize} |
| 1008 |
% \item \meta{sign}: a possibly empty string of |+| and |-| characters; |
| 1009 |
% \item \meta{significand}: a non-empty string of digits together with zero |
| 1010 |
% or one dot; |
| 1011 |
% \item \meta{exponent} optionally: the character |e| or |E|, followed by a |
| 1012 |
% possibly empty string of |+|~and~|-| tokens, and a non-empty string |
| 1013 |
% of digits. |
| 1014 |
% \end{itemize} |
| 1015 |
% The sign of the resulting number is |+| if \meta{sign} contains an |
| 1016 |
% even number of |-|, and |-| otherwise, hence, an empty \meta{sign} |
| 1017 |
% denotes a non-negative input. The stored significand is obtained from |
| 1018 |
% \meta{significand} by omitting the decimal separator and leading zeros, |
| 1019 |
% and rounding to $16$ significant digits, filling with trailing zeros |
| 1020 |
% if necessary. In particular, the value stored is exact if the input |
| 1021 |
% \meta{significand} has at most $16$ digits. The stored \meta{exponent} |
| 1022 |
% is obtained by combining the input \meta{exponent} ($0$ if absent) |
| 1023 |
% with a shift depending on the position of the significand and the number |
| 1024 |
% of leading zeros. |
| 1025 |
% |
| 1026 |
% A special case arises if the resulting \meta{exponent} is either too |
| 1027 |
% large or too small for the floating point number to be |
| 1028 |
% represented. This results either in an overflow (the number is then |
| 1029 |
% replaced by $\pm\infty$), or an underflow (resulting in $\pm 0$). |
| 1030 |
% |
| 1031 |
% The result is thus $\pm 0$ if and only if \meta{significand} contains no |
| 1032 |
% non-zero digit (i.e., consists only in characters~|0|, and an |
| 1033 |
% optional period), or if there is an underflow. Note that a |
| 1034 |
% single dot is currently a valid floating point number, equal to~$+0$, |
| 1035 |
% but that is not guaranteed to remain true. |
| 1036 |
% |
| 1037 |
% The \meta{significand} must be non-empty, so |e1| and |e-1| are not |
| 1038 |
% valid floating point numbers. Note that the latter could be mistaken |
| 1039 |
% with the difference of \enquote{\texttt{e}} and $1$. To avoid |
| 1040 |
% confusions, the base of natural logarithms cannot be input as |e| and |
| 1041 |
% should be input as \texttt{exp(1)} or \cs[module = fp]{c_e_fp} (which is |
| 1042 |
% faster). |
| 1043 |
% |
| 1044 |
% Special numbers are input as follows: |
| 1045 |
% \begin{itemize} |
| 1046 |
% \item \texttt{inf} represents $+\infty$, and can be preceded by any |
| 1047 |
% \meta{sign}, yielding $\pm\infty$ as appropriate. |
| 1048 |
% \item \texttt{nan} represents a (quiet) non-number. It can be |
| 1049 |
% preceded by any sign, but that sign is ignored. |
| 1050 |
% \item Any unrecognizable string triggers an error, and produces a |
| 1051 |
% \nan{}. |
| 1052 |
% \item Note that commands such as \tn{infty}, \tn{pi}, or \tn{sin} |
| 1053 |
% \emph{do not} work in floating point expressions. They may |
| 1054 |
% silently be interpreted as completely unexpected numbers, because |
| 1055 |
% integer constants (allowed in expressions) are commonly stored as |
| 1056 |
% mathematical characters. |
| 1057 |
% \end{itemize} |
| 1058 |
% |
| 1059 |
% \subsection{Precedence of operators} |
| 1060 |
% \label{sec:l3fp:fp-precedence} |
| 1061 |
% |
| 1062 |
% We list here all the operations supported in floating point |
| 1063 |
% expressions, in order of decreasing precedence: operations listed |
| 1064 |
% earlier bind more tightly than operations listed below them. |
| 1065 |
% \begin{itemize} |
| 1066 |
% \item Function calls (\texttt{sin}, \texttt{ln}, \emph{etc}). |
| 1067 |
% \item Binary |**| and |^| (right associative). |
| 1068 |
% \item Unary |+|, |-|, |!|. |
| 1069 |
% \item Implicit multiplication by juxtaposition (\texttt{2pi}) |
| 1070 |
% when neither factor is in parentheses. |
| 1071 |
% \item Binary |*| and |/|, implicit multiplication by juxtaposition with |
| 1072 |
% parentheses (for instance \texttt{3(4+5)}). |
| 1073 |
% \item Binary |+| and |-|. |
| 1074 |
% \item Comparisons |>=|, |!=|, |<?|, \emph{etc}. |
| 1075 |
% \item Logical \texttt{and}, denoted by |&&|. |
| 1076 |
% \item Logical \texttt{or}, denoted by \verb+||+. |
| 1077 |
% \item Ternary operator |?:| (right associative). |
| 1078 |
% \item Comma (to build tuples). |
| 1079 |
% \end{itemize} |
| 1080 |
% The precedence of operations can be overridden using parentheses. |
| 1081 |
% In particular, the precedence of juxtaposition implies that |
| 1082 |
% \begin{align*} |
| 1083 |
% \mathtt{1/2pi} & = 1/(2\pi), \\ |
| 1084 |
% \mathtt{1/2pi(pi+pi)} & = (2\pi)^{-1}(\pi+\pi) \simeq 1, \\ |
| 1085 |
% \mathtt{sin 2pi} & = \sin(2)\pi \neq 0, \\ |
| 1086 |
% \mathtt{2\char`\^2max(3,5)} & = 2^2 \max(3,5) = 20, \\ |
| 1087 |
% \mathtt{1in/1cm} & = (1\mathrm{in})/(1\mathrm{cm}) = 2.54 . |
| 1088 |
% \end{align*} |
| 1089 |
% Functions are called on the value of their argument, contrarily to |
| 1090 |
% \TeX{} macros. |
| 1091 |
% |
| 1092 |
% \subsection{Operations} \label{sec:l3fp:fp-operations} |
| 1093 |
% |
| 1094 |
% We now present the various operations allowed in floating point |
| 1095 |
% expressions, from the lowest precedence to the highest. When used as |
| 1096 |
% a truth value, a floating point expression is \texttt{false} if it is |
| 1097 |
% $\pm 0$, and \texttt{true} otherwise, including when it is \nan{} or a |
| 1098 |
% tuple such as $(0,0)$. Tuples are only supported to some extent by |
| 1099 |
% operations that work with truth values (|?:|, \verb"||", |&&|, |!|), |
| 1100 |
% by comparisons (|!<=>?|), and by |+|, |-|, |*|, |/|. Unless otherwise |
| 1101 |
% specified, providing a tuple as an argument of any other operation |
| 1102 |
% yields the \enquote{invalid operation} exception and a \nan{} result. |
| 1103 |
% |
| 1104 |
% \begin{function}[tested = m3fp-logic002, module = {}]{?:} |
| 1105 |
% \begin{syntax} |
| 1106 |
% \cs{fp_eval:n} \{ \meta{operand_1} |?| \meta{operand_2} |:| \meta{operand_3} \} |
| 1107 |
% \end{syntax} |
| 1108 |
% The ternary operator |?:| results in \meta{operand_2} if |
| 1109 |
% \meta{operand_1} is true (not $\pm 0$), and \meta{operand_3} if |
| 1110 |
% \meta{operand_1} is false ($\pm 0$). All three \meta{operands} are |
| 1111 |
% evaluated in all cases; they may be tuples. The operator is right |
| 1112 |
% associative, hence |
| 1113 |
% \begin{verbatim} |
| 1114 |
% \fp_eval:n |
| 1115 |
% { |
| 1116 |
% 1 + 3 > 4 ? 1 : |
| 1117 |
% 2 + 4 > 5 ? 2 : |
| 1118 |
% 3 + 5 > 6 ? 3 : 4 |
| 1119 |
% } |
| 1120 |
% \end{verbatim} |
| 1121 |
% first tests whether $1 + 3 > 4$; since this isn't true, the branch |
| 1122 |
% following |:| is taken, and $2 + 4 > 5$ is compared; since this is |
| 1123 |
% true, the branch before |:| is taken, and everything else is |
| 1124 |
% (evaluated then) ignored. That allows testing for various cases in |
| 1125 |
% a concise manner, with the drawback that all computations are made |
| 1126 |
% in all cases. |
| 1127 |
% \end{function} |
| 1128 |
% |
| 1129 |
% \begin{function}[tested = m3fp-logic002]{||} |
| 1130 |
% \begin{syntax} |
| 1131 |
% \cs{fp_eval:n} \{ \meta{operand_1} \verb"||" \meta{operand_2} \} |
| 1132 |
% \end{syntax} |
| 1133 |
% If \meta{operand_1} is true (not $\pm 0$), use that value, otherwise the |
| 1134 |
% value of \meta{operand_2}. Both \meta{operands} are evaluated in all |
| 1135 |
% cases; they may be tuples. In \meta{operand_1} \verb"||" |
| 1136 |
% \meta{operand_2} \verb"||" \ldots{} \verb"||" \meta{operands_n}, the |
| 1137 |
% first true (nonzero) \meta{operand} is used and if all are zero the |
| 1138 |
% last one ($\pm 0$) is used. |
| 1139 |
% \end{function} |
| 1140 |
% |
| 1141 |
% \begin{function}[tested = m3fp-logic002]{&&} |
| 1142 |
% \begin{syntax} |
| 1143 |
% \cs{fp_eval:n} \{ \meta{operand_1} |&&| \meta{operand_2} \} |
| 1144 |
% \end{syntax} |
| 1145 |
% If \meta{operand_1} is false (equal to~$\pm 0$), use that value, |
| 1146 |
% otherwise the value of \meta{operand_2}. Both \meta{operands} are |
| 1147 |
% evaluated in all cases; they may be tuples. In \meta{operand_1} |
| 1148 |
% |&&| \meta{operand_2} |&&| \ldots{} |&&| \meta{operands_n}, the |
| 1149 |
% first false ($\pm 0$) \meta{operand} is used and if none is zero the |
| 1150 |
% last one is used. |
| 1151 |
% \end{function} |
| 1152 |
% |
| 1153 |
% \begin{function}[tested = m3fp-logic001, updated = 2013-12-14] |
| 1154 |
% {<, =, >, ?} |
| 1155 |
% \begin{syntax} |
| 1156 |
% \cs{fp_eval:n} \\ |
| 1157 |
% ~~\{ \\ |
| 1158 |
% ~~~~\meta{operand_1} \meta{relation_1} \\ |
| 1159 |
% ~~~~\ldots{} \\ |
| 1160 |
% ~~~~\meta{operand_N} \meta{relation_N} \\ |
| 1161 |
% ~~~~\meta{operand_{N+1}} \\ |
| 1162 |
% ~~\} |
| 1163 |
% \end{syntax} |
| 1164 |
% Each \meta{relation} consists of a non-empty string of |<|, |=|, |
| 1165 |
% |>|, and~|?|, optionally preceded by~|!|, and may not start |
| 1166 |
% with~|?|. This evaluates to $+1$ if all comparisons |
| 1167 |
% \meta{operand_i} \meta{relation_i} \meta{operand_{i+1}} are true, and |
| 1168 |
% $+0$ otherwise. All \meta{operands} are evaluated (once) in all cases. |
| 1169 |
% See \cs{fp_compare:nTF} for details. |
| 1170 |
% \end{function} |
| 1171 |
% |
| 1172 |
% \begin{function}[tested = m3fp-basics001]{+, -} |
| 1173 |
% \begin{syntax} |
| 1174 |
% \cs{fp_eval:n} \{ \meta{operand_1} |+| \meta{operand_2} \} |
| 1175 |
% \cs{fp_eval:n} \{ \meta{operand_1} |-| \meta{operand_2} \} |
| 1176 |
% \end{syntax} |
| 1177 |
% Computes the sum or the difference of its two \meta{operands}. The |
| 1178 |
% \enquote{invalid operation} exception occurs for $\infty-\infty$. |
| 1179 |
% \enquote{Underflow} and \enquote{overflow} occur when appropriate. |
| 1180 |
% These operations supports the itemwise addition or subtraction of |
| 1181 |
% two tuples, but if they have a different number of items the |
| 1182 |
% \enquote{invalid operation} exception occurs and the result is \nan{}. |
| 1183 |
% \end{function} |
| 1184 |
% |
| 1185 |
% \begin{function}[tested = {m3fp-basics002, m3fp-basics003}]{*, /} |
| 1186 |
% \begin{syntax} |
| 1187 |
% \cs{fp_eval:n} \{ \meta{operand_1} |*| \meta{operand_2} \} |
| 1188 |
% \cs{fp_eval:n} \{ \meta{operand_1} |/| \meta{operand_2} \} |
| 1189 |
% \end{syntax} |
| 1190 |
% Computes the product or the ratio of its two \meta{operands}. The |
| 1191 |
% \enquote{invalid operation} exception occurs for $\infty/\infty$, |
| 1192 |
% $0/0$, or $0*\infty$. \enquote{Division by zero} occurs when |
| 1193 |
% dividing a finite non-zero number by $\pm 0$. \enquote{Underflow} |
| 1194 |
% and \enquote{overflow} occur when appropriate. |
| 1195 |
% When \meta{operand_1} is a tuple and \meta{operand_2} is a floating |
| 1196 |
% point number, each item of \meta{operand_1} is multiplied or divided |
| 1197 |
% by \meta{operand_2}. Multiplication also supports the case where |
| 1198 |
% \meta{operand_1} is a floating point number and \meta{operand_2} a |
| 1199 |
% tuple. Other combinations yield an \enquote{invalid operation} |
| 1200 |
% exception and a \nan{} result. |
| 1201 |
% \end{function} |
| 1202 |
% |
| 1203 |
% \begin{function}[tested = m3fp-basics004, label = !]{+, -, !} |
| 1204 |
% \begin{syntax} |
| 1205 |
% \cs{fp_eval:n} \{ |+| \meta{operand} \} |
| 1206 |
% \cs{fp_eval:n} \{ |-| \meta{operand} \} |
| 1207 |
% \cs{fp_eval:n} \{ |!| \meta{operand} \} |
| 1208 |
% \end{syntax} |
| 1209 |
% The unary |+| does nothing, the unary |-| changes the sign of the |
| 1210 |
% \meta{operand} (for a tuple, of all its components), and |
| 1211 |
% |!| \meta{operand} evaluates to $1$ if \meta{operand} is false |
| 1212 |
% (is $\pm 0$) and $0$ otherwise (this is the \texttt{not} |
| 1213 |
% boolean function). Those operations never raise exceptions. |
| 1214 |
% \end{function} |
| 1215 |
% |
| 1216 |
% \begin{function}[tested = m3fp-expo001]{**, ^} |
| 1217 |
% \begin{syntax} |
| 1218 |
% \cs{fp_eval:n} \{ \meta{operand_1} |**| \meta{operand_2} \} |
| 1219 |
% \cs{fp_eval:n} \{ \meta{operand_1} |^| \meta{operand_2} \} |
| 1220 |
% \end{syntax} |
| 1221 |
% Raises \meta{operand_1} to the power \meta{operand_2}. This |
| 1222 |
% operation is right associative, hence \texttt{2 ** 2 ** 3} equals |
| 1223 |
% $2^{2^{3}} = 256$. If \meta{operand_1} is negative or $-0$ then: |
| 1224 |
% the result's sign is $+$ if the \meta{operand_2} is infinite and |
| 1225 |
% $(-1)^p$ if the \meta{operand_2} is $p/5^q$ with $p$, $q$ integers; |
| 1226 |
% the result is $+0$ if |
| 1227 |
% |abs(|\meta{operand_1}|)^|\meta{operand_2} evaluates to zero; in |
| 1228 |
% other cases the \enquote{invalid operation} exception occurs because |
| 1229 |
% the sign cannot be determined. \enquote{Division by zero} occurs |
| 1230 |
% when raising $\pm 0$ to a finite strictly negative power. |
| 1231 |
% \enquote{Underflow} and \enquote{overflow} occur when appropriate. |
| 1232 |
% If either operand is a tuple, \enquote{invalid operation} occurs. |
| 1233 |
% \end{function} |
| 1234 |
% |
| 1235 |
% \begin{function}[tested = m3fp-basics004]{abs} |
| 1236 |
% \begin{syntax} |
| 1237 |
% \cs{fp_eval:n} \{ |abs(| \meta{fp expr} |)| \} |
| 1238 |
% \end{syntax} |
| 1239 |
% Computes the absolute value of the \meta{fp expr}. If the operand is |
| 1240 |
% a tuple, \enquote{invalid operation} occurs. This operation does |
| 1241 |
% not raise exceptions in other cases. See also \cs{fp_abs:n}. |
| 1242 |
% \end{function} |
| 1243 |
% |
| 1244 |
% \begin{function}[tested = m3fp-expo001]{exp} |
| 1245 |
% \begin{syntax} |
| 1246 |
% \cs{fp_eval:n} \{ |exp(| \meta{fp expr} |)| \} |
| 1247 |
% \end{syntax} |
| 1248 |
% Computes the exponential of the \meta{fp expr}. \enquote{Underflow} |
| 1249 |
% and \enquote{overflow} occur when appropriate. |
| 1250 |
% If the operand is a tuple, \enquote{invalid operation} occurs. |
| 1251 |
% \end{function} |
| 1252 |
% |
| 1253 |
% \begin{function}[tested = m3fp-expo001]{fact} |
| 1254 |
% \begin{syntax} |
| 1255 |
% \cs{fp_eval:n} \{ |fact(| \meta{fp expr} |)| \} |
| 1256 |
% \end{syntax} |
| 1257 |
% Computes the factorial of the \meta{fp expr}. If the \meta{fp expr} |
| 1258 |
% is an integer between $-0$ and $3248$ included, the result is finite |
| 1259 |
% and correctly rounded. Larger positive integers give $+\infty$ with |
| 1260 |
% \enquote{overflow}, while $|fact(|{+\infty}|)|=+\infty$ and |
| 1261 |
% $|fact(nan)|=|nan|$ with no exception. All other inputs give \nan{} |
| 1262 |
% with the \enquote{invalid operation} exception. |
| 1263 |
% \end{function} |
| 1264 |
% |
| 1265 |
% \begin{function}[tested = m3fp-expo001]{ln} |
| 1266 |
% \begin{syntax} |
| 1267 |
% \cs{fp_eval:n} \{ |ln(| \meta{fp expr} |)| \} |
| 1268 |
% \end{syntax} |
| 1269 |
% Computes the natural logarithm of the \meta{fp expr}. Negative |
| 1270 |
% numbers have no (real) logarithm, hence the \enquote{invalid |
| 1271 |
% operation} is raised in that case, including for $\ln(-0)$. |
| 1272 |
% \enquote{Division by zero} occurs when evaluating |
| 1273 |
% $\ln(+0) = -\infty$. \enquote{Underflow} and \enquote{overflow} |
| 1274 |
% occur when appropriate. If the operand is a tuple, \enquote{invalid |
| 1275 |
% operation} occurs. |
| 1276 |
% \end{function} |
| 1277 |
% |
| 1278 |
% \begin{function}[EXP, added = 2018-11-03]{logb} |
| 1279 |
% \begin{syntax} |
| 1280 |
% \cs{fp_eval:n} \{ |logb(| \meta{fp expr} |)| \} |
| 1281 |
% \end{syntax} |
| 1282 |
% Determines the exponent of the \meta{fp expr}, namely the floor of |
| 1283 |
% the base-$10$ logarithm of its absolute value. \enquote{Division by |
| 1284 |
% zero} occurs when evaluating $\operatorname{logb}(\pm 0) = -\infty$. |
| 1285 |
% Other special values are $\operatorname{logb}(\pm\infty)=+\infty$ |
| 1286 |
% and $\operatorname{logb}(\nan{})=\nan{}$. If the operand is a tuple |
| 1287 |
% or is \nan{}, then \enquote{invalid operation} occurs and the result |
| 1288 |
% is \nan{}. |
| 1289 |
% \end{function} |
| 1290 |
% |
| 1291 |
% \begin{function}[tested = m3fp-logic002]{max, min} |
| 1292 |
% \begin{syntax} |
| 1293 |
% \cs{fp_eval:n} \{ |max(| \meta{fp expr_1} |,| \meta{fp expr_2} |,| \ldots{} |)| \} |
| 1294 |
% \cs{fp_eval:n} \{ |min(| \meta{fp expr_1} |,| \meta{fp expr_2} |,| \ldots{} |)| \} |
| 1295 |
% \end{syntax} |
| 1296 |
% Evaluates each \meta{fp expr} and computes the largest (smallest) of |
| 1297 |
% those. If any of the \meta{fp expr} is a \nan{} or tuple, the result |
| 1298 |
% is \nan{}. If any operand is a tuple, \enquote{invalid operation} |
| 1299 |
% occurs; these operations do not raise exceptions in other cases. |
| 1300 |
% \end{function} |
| 1301 |
% |
| 1302 |
% \begin{function} |
| 1303 |
% [tested = {m3fp-round001, m3fp-round002}, added = 2013-12-14, updated = 2015-08-08] |
| 1304 |
% {round, trunc, ceil, floor} |
| 1305 |
% \begin{syntax} |
| 1306 |
% \cs{fp_eval:n} \{ |round| |(| \meta{fp expr} |)| \} |
| 1307 |
% \cs{fp_eval:n} \{ |round| |(| \meta{fp expr_1} , \meta{fp expr_2} |)| \} |
| 1308 |
% \cs{fp_eval:n} \{ |round| |(| \meta{fp expr_1} , \meta{fp expr_2} , \meta{fp expr_3} |)| \} |
| 1309 |
% \end{syntax} |
| 1310 |
% Only |round| accepts a third argument. |
| 1311 |
% Evaluates $\meta{fp expr_1}=x$ and $\meta{fp expr_2}=n$ and $\meta{fp |
| 1312 |
% expr_3}=t$ then rounds $x$~to $n$~places. If $n$~is an integer, this |
| 1313 |
% rounds~$x$ to a multiple of~$10^{-n}$; if $n=+\infty$, this always |
| 1314 |
% yields~$x$; if $n=-\infty$, this yields one of $\pm 0$, $\pm\infty$, |
| 1315 |
% or~\nan{}; if $n=\nan{}$, this yields \nan{}; if |
| 1316 |
% $n$~is neither $\pm\infty$ nor an integer, then an \enquote{invalid |
| 1317 |
% operation} exception is raised. When \meta{fp expr_2} is omitted, |
| 1318 |
% $n=0$, i.e., \meta{fp expr_1} is rounded to an integer. The |
| 1319 |
% rounding direction depends on the function. |
| 1320 |
% \begin{itemize} |
| 1321 |
% \item |round| yields the multiple of~$10^{-n}$ closest to~$x$, |
| 1322 |
% with ties ($x$ half-way between two such multiples) rounded |
| 1323 |
% as follows. If $t$ is \texttt{nan} (or not given) the even |
| 1324 |
% multiple is chosen (\enquote{ties to even}), if $t=\pm 0$ the |
| 1325 |
% multiple closest to $0$ is chosen (\enquote{ties to zero}), |
| 1326 |
% if $t$ is positive/negative the multiple closest to $\infty$/$-\infty$ |
| 1327 |
% is chosen (\enquote{ties towards positive/negative infinity}). |
| 1328 |
% \item |floor| yields the largest |
| 1329 |
% multiple of~$10^{-n}$ smaller or equal to~$x$ (\enquote{round |
| 1330 |
% towards negative infinity}); |
| 1331 |
% \item |ceil| yields the smallest |
| 1332 |
% multiple of~$10^{-n}$ greater or equal to~$x$ (\enquote{round |
| 1333 |
% towards positive infinity}); |
| 1334 |
% \item |trunc| yields a multiple |
| 1335 |
% of~$10^{-n}$ with the same sign as~$x$ and with the largest |
| 1336 |
% absolute value less than that of~$x$ (\enquote{round towards |
| 1337 |
% zero}). |
| 1338 |
% \end{itemize} |
| 1339 |
% \enquote{Overflow} occurs if $x$~is finite and the result is |
| 1340 |
% infinite (this can only happen if $\meta{fp expr_2}\string<-9984$). |
| 1341 |
% If any operand is a tuple, \enquote{invalid operation} occurs. |
| 1342 |
% \end{function} |
| 1343 |
% |
| 1344 |
% \begin{function}[tested = m3fp-logic002]{sign} |
| 1345 |
% \begin{syntax} |
| 1346 |
% \cs{fp_eval:n} \{ |sign(| \meta{fp expr} |)| \} |
| 1347 |
% \end{syntax} |
| 1348 |
% Evaluates the \meta{fp expr} and determines its sign: $+1$ for |
| 1349 |
% positive numbers and for $+\infty$, $-1$ for negative numbers and |
| 1350 |
% for $-\infty$, $\pm 0$ for $\pm 0$, and \nan{} for \nan{}. |
| 1351 |
% If the operand is a tuple, \enquote{invalid operation} occurs. |
| 1352 |
% This operation does not raise exceptions in other cases. |
| 1353 |
% \end{function} |
| 1354 |
% |
| 1355 |
% \begin{function}[updated = 2013-11-17, tested = m3fp-trig001] |
| 1356 |
% {sin, cos, tan, cot, csc, sec} |
| 1357 |
% \begin{syntax} |
| 1358 |
% \cs{fp_eval:n} \{ |sin(| \meta{fp expr} |)| \} |
| 1359 |
% \cs{fp_eval:n} \{ |cos(| \meta{fp expr} |)| \} |
| 1360 |
% \cs{fp_eval:n} \{ |tan(| \meta{fp expr} |)| \} |
| 1361 |
% \cs{fp_eval:n} \{ |cot(| \meta{fp expr} |)| \} |
| 1362 |
% \cs{fp_eval:n} \{ |csc(| \meta{fp expr} |)| \} |
| 1363 |
% \cs{fp_eval:n} \{ |sec(| \meta{fp expr} |)| \} |
| 1364 |
% \end{syntax} |
| 1365 |
% Computes the sine, cosine, tangent, cotangent, cosecant, or secant |
| 1366 |
% of the \meta{fp expr} given in radians. For arguments given in |
| 1367 |
% degrees, see \texttt{sind}, \texttt{cosd}, etc. Note that |
| 1368 |
% since $\pi$~is irrational, $\operatorname{sin}(8\mathrm{pi})$ is not quite |
| 1369 |
% zero, while its analogue $\operatorname{sind}(8\times 180)$ is exactly |
| 1370 |
% zero. The trigonometric functions are undefined for |
| 1371 |
% an argument of $\pm\infty$, leading to the \enquote{invalid |
| 1372 |
% operation} exception. Additionally, evaluating tangent, |
| 1373 |
% cotangent, cosecant, or secant at one of their poles leads to a |
| 1374 |
% \enquote{division by zero} exception. \enquote{Underflow} and |
| 1375 |
% \enquote{overflow} occur when appropriate. |
| 1376 |
% If the operand is a tuple, \enquote{invalid operation} occurs. |
| 1377 |
% \end{function} |
| 1378 |
% |
| 1379 |
% \begin{function}[added = 2013-11-02, tested = m3fp-trig003] |
| 1380 |
% {sind, cosd, tand, cotd, cscd, secd} |
| 1381 |
% \begin{syntax} |
| 1382 |
% \cs{fp_eval:n} \{ |sind(| \meta{fp expr} |)| \} |
| 1383 |
% \cs{fp_eval:n} \{ |cosd(| \meta{fp expr} |)| \} |
| 1384 |
% \cs{fp_eval:n} \{ |tand(| \meta{fp expr} |)| \} |
| 1385 |
% \cs{fp_eval:n} \{ |cotd(| \meta{fp expr} |)| \} |
| 1386 |
% \cs{fp_eval:n} \{ |cscd(| \meta{fp expr} |)| \} |
| 1387 |
% \cs{fp_eval:n} \{ |secd(| \meta{fp expr} |)| \} |
| 1388 |
% \end{syntax} |
| 1389 |
% Computes the sine, cosine, tangent, cotangent, cosecant, or secant |
| 1390 |
% of the \meta{fp expr} given in degrees. For arguments given in |
| 1391 |
% radians, see \texttt{sin}, \texttt{cos}, etc. Note that |
| 1392 |
% since $\pi$~is irrational, $\operatorname{sin}(8\mathrm{pi})$ is not quite |
| 1393 |
% zero, while its analogue $\operatorname{sind}(8\times 180)$ is exactly |
| 1394 |
% zero. The trigonometric functions are undefined for |
| 1395 |
% an argument of $\pm\infty$, leading to the \enquote{invalid |
| 1396 |
% operation} exception. Additionally, evaluating tangent, |
| 1397 |
% cotangent, cosecant, or secant at one of their poles leads to a |
| 1398 |
% \enquote{division by zero} exception. \enquote{Underflow} and |
| 1399 |
% \enquote{overflow} occur when appropriate. |
| 1400 |
% If the operand is a tuple, \enquote{invalid operation} occurs. |
| 1401 |
% \end{function} |
| 1402 |
% |
| 1403 |
% \begin{function}[added = 2013-11-02, tested = m3fp-trig002] |
| 1404 |
% {asin, acos, acsc, asec} |
| 1405 |
% \begin{syntax} |
| 1406 |
% \cs{fp_eval:n} \{ |asin(| \meta{fp expr} |)| \} |
| 1407 |
% \cs{fp_eval:n} \{ |acos(| \meta{fp expr} |)| \} |
| 1408 |
% \cs{fp_eval:n} \{ |acsc(| \meta{fp expr} |)| \} |
| 1409 |
% \cs{fp_eval:n} \{ |asec(| \meta{fp expr} |)| \} |
| 1410 |
% \end{syntax} |
| 1411 |
% Computes the arcsine, arccosine, arccosecant, or arcsecant of the |
| 1412 |
% \meta{fp expr} and returns the result in radians, in the range |
| 1413 |
% $[-\pi/2,\pi/2]$ for \texttt{asin} and \texttt{acsc} and $[0,\pi]$ |
| 1414 |
% for \texttt{acos} and \texttt{asec}. For a result in degrees, use |
| 1415 |
% \texttt{asind}, etc. If the argument of |asin| or |acos| |
| 1416 |
% lies outside the range $[-1,1]$, or the argument of |acsc| or |asec| |
| 1417 |
% inside the range $(-1,1)$, an \enquote{invalid operation} exception |
| 1418 |
% is raised. \enquote{Underflow} and \enquote{overflow} occur when |
| 1419 |
% appropriate. |
| 1420 |
% If the operand is a tuple, \enquote{invalid operation} occurs. |
| 1421 |
% \end{function} |
| 1422 |
% |
| 1423 |
% \begin{function}[added = 2013-11-02, tested = m3fp-trig004] |
| 1424 |
% {asind, acosd, acscd, asecd} |
| 1425 |
% \begin{syntax} |
| 1426 |
% \cs{fp_eval:n} \{ |asind(| \meta{fp expr} |)| \} |
| 1427 |
% \cs{fp_eval:n} \{ |acosd(| \meta{fp expr} |)| \} |
| 1428 |
% \cs{fp_eval:n} \{ |acscd(| \meta{fp expr} |)| \} |
| 1429 |
% \cs{fp_eval:n} \{ |asecd(| \meta{fp expr} |)| \} |
| 1430 |
% \end{syntax} |
| 1431 |
% Computes the arcsine, arccosine, arccosecant, or arcsecant of the |
| 1432 |
% \meta{fp expr} and returns the result in degrees, in the range |
| 1433 |
% $[-90,90]$ for \texttt{asind} and \texttt{acscd} and $[0,180]$ for |
| 1434 |
% \texttt{acosd} and \texttt{asecd}. For a result in radians, use |
| 1435 |
% \texttt{asin}, etc. If the argument of |asind| or |acosd| lies |
| 1436 |
% outside the range $[-1,1]$, or the argument of |acscd| or |asecd| |
| 1437 |
% inside the range $(-1,1)$, an \enquote{invalid operation} exception |
| 1438 |
% is raised. \enquote{Underflow} and \enquote{overflow} occur when |
| 1439 |
% appropriate. |
| 1440 |
% If the operand is a tuple, \enquote{invalid operation} occurs. |
| 1441 |
% \end{function} |
| 1442 |
% |
| 1443 |
% \begin{function}[added = 2013-11-02, tested = m3fp-trig002] |
| 1444 |
% {atan, acot} |
| 1445 |
% \begin{syntax} |
| 1446 |
% \cs{fp_eval:n} \{ |atan(| \meta{fp expr} |)| \} |
| 1447 |
% \cs{fp_eval:n} \{ |atan(| \meta{fp expr_1} , \meta{fp expr_2} |)| \} |
| 1448 |
% \cs{fp_eval:n} \{ |acot(| \meta{fp expr} |)| \} |
| 1449 |
% \cs{fp_eval:n} \{ |acot(| \meta{fp expr_1} , \meta{fp expr_2} |)| \} |
| 1450 |
% \end{syntax} |
| 1451 |
% Those functions yield an angle in radians: \texttt{atand} and |
| 1452 |
% \texttt{acotd} are their analogs in degrees. The one-argument |
| 1453 |
% versions compute the arctangent or arccotangent of the |
| 1454 |
% \meta{fp expr}: arctangent takes values in the range |
| 1455 |
% $[-\pi/2,\pi/2]$, and arccotangent in the range $[0,\pi]$. The |
| 1456 |
% two-argument arctangent computes the angle in polar coordinates of |
| 1457 |
% the point with Cartesian coordinates $(\meta{fp expr_2}, |
| 1458 |
% \meta{fp expr_1})$: this is the arctangent of |
| 1459 |
% $\meta{fp expr_1}/\meta{fp expr_2}$, possibly shifted by~$\pi$ |
| 1460 |
% depending on the signs of \meta{fp expr_1} and \meta{fp expr_2}. The |
| 1461 |
% two-argument arccotangent computes the angle in polar coordinates of |
| 1462 |
% the point $(\meta{fp expr_1}, \meta{fp expr_2})$, equal to the |
| 1463 |
% arccotangent of $\meta{fp expr_1}/\meta{fp expr_2}$, possibly shifted |
| 1464 |
% by~$\pi$. Both two-argument functions take values in the wider |
| 1465 |
% range $[-\pi,\pi]$. The ratio $\meta{fp expr_1}/\meta{fp expr_2}$ |
| 1466 |
% need not be defined for the two-argument arctangent: when both |
| 1467 |
% expressions yield~$\pm 0$, or when both yield~$\pm\infty$, the |
| 1468 |
% resulting angle is one of $\{\pm\pi/4,\pm 3\pi/4\}$ depending on |
| 1469 |
% signs. The \enquote{underflow} exception can occur. |
| 1470 |
% If any operand is a tuple, \enquote{invalid operation} occurs. |
| 1471 |
% \end{function} |
| 1472 |
% |
| 1473 |
% \begin{function}[added = 2013-11-02, tested = m3fp-trig004] |
| 1474 |
% {atand, acotd} |
| 1475 |
% \begin{syntax} |
| 1476 |
% \cs{fp_eval:n} \{ |atand(| \meta{fp expr} |)| \} |
| 1477 |
% \cs{fp_eval:n} \{ |atand(| \meta{fp expr_1} , \meta{fp expr_2} |)| \} |
| 1478 |
% \cs{fp_eval:n} \{ |acotd(| \meta{fp expr} |)| \} |
| 1479 |
% \cs{fp_eval:n} \{ |acotd(| \meta{fp expr_1} , \meta{fp expr_2} |)| \} |
| 1480 |
% \end{syntax} |
| 1481 |
% Those functions yield an angle in degrees: \texttt{atan} and |
| 1482 |
% \texttt{acot} are their analogs in radians. The one-argument |
| 1483 |
% versions compute the arctangent or arccotangent of the |
| 1484 |
% \meta{fp expr}: arctangent takes values in the range $[-90,90]$, and |
| 1485 |
% arccotangent in the range $[0,180]$. The two-argument arctangent |
| 1486 |
% computes the angle in polar coordinates of the point with Cartesian |
| 1487 |
% coordinates $(\meta{fp expr_2}, \meta{fp expr_1})$: this is the |
| 1488 |
% arctangent of $\meta{fp expr_1}/\meta{fp expr_2}$, possibly shifted |
| 1489 |
% by~$180$ depending on the signs of \meta{fp expr_1} and |
| 1490 |
% \meta{fp expr_2}. The two-argument arccotangent computes the angle |
| 1491 |
% in polar coordinates of the point $(\meta{fp expr_1}, |
| 1492 |
% \meta{fp expr_2})$, equal to the arccotangent of |
| 1493 |
% $\meta{fp expr_1}/\meta{fp expr_2}$, possibly shifted by~$180$. Both |
| 1494 |
% two-argument functions take values in the wider range $[-180,180]$. |
| 1495 |
% The ratio $\meta{fp expr_1}/\meta{fp expr_2}$ need not be defined for |
| 1496 |
% the two-argument arctangent: when both expressions yield~$\pm 0$, or |
| 1497 |
% when both yield~$\pm\infty$, the resulting angle is one of $\{\pm |
| 1498 |
% 45,\pm 135\}$ depending on signs. The \enquote{underflow} |
| 1499 |
% exception can occur. |
| 1500 |
% If any operand is a tuple, \enquote{invalid operation} occurs. |
| 1501 |
% \end{function} |
| 1502 |
% |
| 1503 |
% \begin{function}[added = 2013-12-14, tested = m3fp-basics005]{sqrt} |
| 1504 |
% \begin{syntax} |
| 1505 |
% \cs{fp_eval:n} \{ |sqrt(| \meta{fp expr} |)| \} |
| 1506 |
% \end{syntax} |
| 1507 |
% Computes the square root of the \meta{fp expr}. The \enquote{invalid |
| 1508 |
% operation} is raised when the \meta{fp expr} is negative or is a tuple; no |
| 1509 |
% other exception can occur. Special values yield $\sqrt{-0} = -0$, |
| 1510 |
% $\sqrt{+0} = +0$, $\sqrt{+\infty} = +\infty$ and |
| 1511 |
% $\sqrt{\text{\nan{}}}=\text{\nan{}}$. |
| 1512 |
% \end{function} |
| 1513 |
% |
| 1514 |
% \begin{function}[added = 2016-12-05]{rand} |
| 1515 |
% \begin{syntax} |
| 1516 |
% \cs{fp_eval:n} \{ |rand()| \} |
| 1517 |
% \end{syntax} |
| 1518 |
% Produces a pseudo-random floating-point number (multiple of |
| 1519 |
% $10^{-16}$) between $0$~included and $1$~excluded. This is not available |
| 1520 |
% in older versions of \XeTeX{}. The random seed can be queried using |
| 1521 |
% \cs{sys_rand_seed:} and set using \cs{sys_gset_rand_seed:n}. |
| 1522 |
% \begin{texnote} |
| 1523 |
% This is based on pseudo-random numbers provided by the engine's |
| 1524 |
% primitive \tn{pdfuniformdeviate} in \pdfTeX{}, \pTeX{}, \upTeX{} |
| 1525 |
% and \tn{uniformdeviate} in \LuaTeX{} and \XeTeX{}. The underlying code is |
| 1526 |
% based on Metapost, which follows an additive scheme recommended in |
| 1527 |
% Section 3.6 of \enquote{The Art of Computer Programming, |
| 1528 |
% Volume~2}. |
| 1529 |
% |
| 1530 |
% While we are more careful than \tn{uniformdeviate} to preserve |
| 1531 |
% uniformity of the underlying stream of $28$-bit pseudo-random |
| 1532 |
% integers, these pseudo-random numbers should of course not be |
| 1533 |
% relied upon for serious numerical computations nor cryptography. |
| 1534 |
% \end{texnote} |
| 1535 |
% \end{function} |
| 1536 |
% |
| 1537 |
% \begin{function}[added = 2016-12-05]{randint} |
| 1538 |
% \begin{syntax} |
| 1539 |
% \cs{fp_eval:n} \{ |randint(| \meta{fp expr} |)| \} |
| 1540 |
% \cs{fp_eval:n} \{ |randint(| \meta{fp expr_1} , \meta{fp expr_2} |)| \} |
| 1541 |
% \end{syntax} |
| 1542 |
% Produces a pseudo-random integer between $1$~and \meta{fp expr} or |
| 1543 |
% between \meta{fp expr_1} and \meta{fp expr_2} inclusive. The bounds |
| 1544 |
% must be integers in the range $(-10^{16},10^{16})$ and the first |
| 1545 |
% must be smaller or equal to the second. See \texttt{rand} for |
| 1546 |
% important comments on how these pseudo-random numbers are generated. |
| 1547 |
% \end{function} |
| 1548 |
% |
| 1549 |
% \begin{variable}[tested = m3fp-parse001]{inf, nan} |
| 1550 |
% The special values $+\infty$, $-\infty$, and \nan{} are represented |
| 1551 |
% as \texttt{inf}, \texttt{-inf} and \texttt{nan} (see |
| 1552 |
% \cs[module = fp]{c_inf_fp}, \cs[module = fp]{c_minus_inf_fp} and |
| 1553 |
% \cs[module = fp]{c_nan_fp}). |
| 1554 |
% \end{variable} |
| 1555 |
% |
| 1556 |
% \begin{variable}[tested = m3fp-parse001]{pi} |
| 1557 |
% The value of $\pi$ (see \cs[module = fp]{c_pi_fp}). |
| 1558 |
% \end{variable} |
| 1559 |
% |
| 1560 |
% \begin{variable}[tested = m3fp-parse001]{deg} |
| 1561 |
% The value of $1^{\circ}$ in radians (see \cs[module = fp]{c_one_degree_fp}). |
| 1562 |
% \end{variable} |
| 1563 |
% |
| 1564 |
% \begin{variable}[tested = m3fp-parse001] |
| 1565 |
% {em, ex, in, pt, pc, cm, mm, dd, cc, nd, nc, bp, sp} |
| 1566 |
% \newcommand{\unit}[1]{\ifmmode\,\fi\text{\texttt{#1}}} |
| 1567 |
% Those units of measurement are equal to their values in \unit{pt}, |
| 1568 |
% namely |
| 1569 |
% \begin{align*} |
| 1570 |
% 1 \unit{in} & = 72.27 \unit{pt} \\ |
| 1571 |
% 1 \unit{pt} & = 1 \unit{pt} \\ |
| 1572 |
% 1 \unit{pc} & = 12 \unit{pt} \\ |
| 1573 |
% 1 \unit{cm} & = \frac{1}{2.54} \unit{in} = 28.45275590551181 \unit{pt} \\ |
| 1574 |
% 1 \unit{mm} & = \frac{1}{25.4} \unit{in} = 2.845275590551181 \unit{pt} \\ |
| 1575 |
% 1 \unit{dd} & = 0.376065 \unit{mm} = 1.07000856496063 \unit{pt} \\ |
| 1576 |
% 1 \unit{cc} & = 12 \unit{dd} = 12.84010277952756 \unit{pt} \\ |
| 1577 |
% 1 \unit{nd} & = 0.375 \unit{mm} = 1.066978346456693 \unit{pt} \\ |
| 1578 |
% 1 \unit{nc} & = 12 \unit{nd} = 12.80374015748031 \unit{pt} \\ |
| 1579 |
% 1 \unit{bp} & = \frac{1}{72} \unit{in} = 1.00375 \unit{pt} \\ |
| 1580 |
% 1 \unit{sp} & = 2^{-16} \unit{pt} = 1.52587890625 \times 10^{-5} \unit{pt}. |
| 1581 |
% \end{align*} |
| 1582 |
% The values of the (font-dependent) units \unit{em} and \unit{ex} are |
| 1583 |
% gathered from \TeX{} when the surrounding floating point expression |
| 1584 |
% is evaluated. |
| 1585 |
% \end{variable} |
| 1586 |
% |
| 1587 |
% \begin{variable}[tested = m3fp-parse001]{true, false} |
| 1588 |
% Other names for $1$ and $+0$. |
| 1589 |
% \end{variable} |
| 1590 |
% |
| 1591 |
% \begin{function}[EXP, added = 2012-05-14, updated = 2012-07-08, |
| 1592 |
% tested = m3fp-convert003]{\fp_abs:n} |
| 1593 |
% \begin{syntax} |
| 1594 |
% \cs{fp_abs:n} \Arg{fp expr} |
| 1595 |
% \end{syntax} |
| 1596 |
% Evaluates the \meta{fp expr} as described for |
| 1597 |
% \cs{fp_eval:n} and leaves the absolute value of the result in the |
| 1598 |
% input stream. If the argument is $\pm\infty$, \nan{} or a tuple, |
| 1599 |
% \enquote{invalid operation} occurs. Within floating point |
| 1600 |
% expressions, |abs()| can be used; it accepts $\pm\infty$ and \nan{} |
| 1601 |
% as arguments. |
| 1602 |
% \end{function} |
| 1603 |
% |
| 1604 |
% \begin{function}[EXP, added = 2012-09-26, tested = m3fp-convert003] |
| 1605 |
% {\fp_max:nn, \fp_min:nn} |
| 1606 |
% \begin{syntax} |
| 1607 |
% \cs{fp_max:nn} \Arg{fp expr_1} \Arg{fp expr_2} |
| 1608 |
% \end{syntax} |
| 1609 |
% Evaluates the \meta{fp exprs} as described for |
| 1610 |
% \cs{fp_eval:n} and leaves the resulting larger (\texttt{max}) or |
| 1611 |
% smaller (\texttt{min}) value in the input stream. If the argument |
| 1612 |
% is a tuple, \enquote{invalid operation} occurs, but no other case |
| 1613 |
% raises exceptions. Within floating point expressions, |max()| and |
| 1614 |
% |min()| can be used. |
| 1615 |
% \end{function} |
| 1616 |
% |
| 1617 |
% \section{Disclaimer and roadmap} |
| 1618 |
% |
| 1619 |
% This module may break if the escape character is among |
| 1620 |
% |0123456789_+|, or if it receives a \TeX{} primitive conditional affected |
| 1621 |
% by \cs{exp_not:N}. |
| 1622 |
% |
| 1623 |
% The following need to be done. I'll try to time-order the items. |
| 1624 |
% \begin{itemize} |
| 1625 |
% \item Function to count items in a tuple (and to determine if something is a tuple). |
| 1626 |
% \item Decide what exponent range to consider. |
| 1627 |
% \item Support signaling \texttt{nan}. |
| 1628 |
% \item Modulo and remainder, and rounding function |quantize| (and its friends analogous to |trunc|, |ceil|, |floor|). |
| 1629 |
% \item \cs{fp_format:nn} \Arg{fp expr} \Arg{format}, but what should |
| 1630 |
% \meta{format} be? More general pretty printing? |
| 1631 |
% \item Add |and|, |or|, |xor|? Perhaps under the names \texttt{all}, |
| 1632 |
% \texttt{any}, and \texttt{xor}? |
| 1633 |
% \item Add $\log(x,b)$ for logarithm of $x$ in base $b$. |
| 1634 |
% \item \texttt{hypot} (Euclidean length). |
| 1635 |
% Cartesian-to-polar transform. |
| 1636 |
% \item Hyperbolic functions \texttt{cosh}, \texttt{sinh}, \texttt{tanh}. |
| 1637 |
% \item Inverse hyperbolics. |
| 1638 |
% \item Base conversion, input such as \texttt{0xAB.CDEF}. |
| 1639 |
% \item Factorial (not with |!|), gamma function. |
| 1640 |
% \item Improve coefficients of the \texttt{sin} and \texttt{tan} |
| 1641 |
% series. |
| 1642 |
% \item Treat upper and lower case letters identically in |
| 1643 |
% identifiers, and ignore underscores. |
| 1644 |
% \item Add an |array(1,2,3)| and |i=complex(0,1)|. |
| 1645 |
% \item Provide an experimental |map| function? Perhaps easier to |
| 1646 |
% implement if it is a single character, |@sin(1,2)|? |
| 1647 |
% \item Provide an |isnan| function analogue of \cs{fp_if_nan:nTF}? |
| 1648 |
% \item Support keyword arguments? |
| 1649 |
% \end{itemize} |
| 1650 |
% \pkg{Pgfmath} also provides box-measurements (depth, height, width), but |
| 1651 |
% boxes are not possible expandably. |
| 1652 |
% |
| 1653 |
% Bugs, and tests to add. |
| 1654 |
% \begin{itemize} |
| 1655 |
% \item Check that functions are monotonic when they should. |
| 1656 |
% \item Add exceptions to |?:|, |!<=>?|, |&&|, \verb"||", and |!|. |
| 1657 |
% \item Logarithms of numbers very close to $1$ are inaccurate. |
| 1658 |
% \item When rounding towards $-\infty$, |\dim_to_fp:n {0pt}| should |
| 1659 |
% return $-0$, not $+0$. |
| 1660 |
% \item The result of $(\pm0)+(\pm0)$, of $x+(-x)$, and of $(-x)+x$ |
| 1661 |
% should depend on the rounding mode. |
| 1662 |
% \item \texttt{0e9999999999} gives a \TeX{} \enquote{number too |
| 1663 |
% large} error. |
| 1664 |
% \item Subnormals are not implemented. |
| 1665 |
% \end{itemize} |
| 1666 |
% |
| 1667 |
% Possible optimizations/improvements. |
| 1668 |
% \begin{itemize} |
| 1669 |
% \item Document that \pkg{l3trial/l3fp-types} introduces tools for |
| 1670 |
% adding new types. |
| 1671 |
% \item In subsection~\ref{sec:l3fp:fp-floats}, write a grammar. |
| 1672 |
% \item It would be nice if the \texttt{parse} auxiliaries for each |
| 1673 |
% operation were set up in the corresponding module, rather than |
| 1674 |
% centeralizing in \pkg{l3fp-parse}. |
| 1675 |
% \item Some functions should get an |_o| ending to indicate that they |
| 1676 |
% expand after their result. |
| 1677 |
% \item More care should be given to distinguish expandable/restricted |
| 1678 |
% expandable (auxiliary and internal) functions. |
| 1679 |
% \item The code for the \texttt{ternary} set of functions is ugly. |
| 1680 |
% \item There are many |~| missing in the doc to avoid bad line-breaks. |
| 1681 |
% \item The algorithm for computing the logarithm of the significand |
| 1682 |
% could be made to use a $5$ terms Taylor series instead of $10$ |
| 1683 |
% terms by taking $c = 2000/(\lfloor 200x\rfloor +1) \in [10,95]$ |
| 1684 |
% instead of $c\in [1,10]$. Also, it would then be possible to |
| 1685 |
% simplify the computation of $t$. However, we would then have to |
| 1686 |
% hard-code the logarithms of $44$ small integers instead of $9$. |
| 1687 |
% \item Improve notations in the explanations of the division |
| 1688 |
% algorithm (\pkg{l3fp-basics}). |
| 1689 |
% \item Understand and document \cs[no-index]{__fp_basics_pack_weird_low:NNNNw} |
| 1690 |
% and \cs[no-index]{__fp_basics_pack_weird_high:NNNNNNNNw} better. Move the |
| 1691 |
% other \texttt{basics_pack} auxiliaries to \pkg{l3fp-aux} under a |
| 1692 |
% better name. |
| 1693 |
% \item Find out if underflow can really occur for trigonometric |
| 1694 |
% functions, and redoc as appropriate. |
| 1695 |
% \item Add bibliography. Some of Kahan's articles, some previous |
| 1696 |
% \TeX{} fp packages, the international standards,\ldots{} |
| 1697 |
% \item Also take into account the \enquote{inexact} exception? |
| 1698 |
% \item Support multi-character prefix operators (\emph{e.g.}, |@/| or |
| 1699 |
% whatever)? |
| 1700 |
% \end{itemize} |
| 1701 |
% |
| 1702 |
% \end{documentation} |
| 1703 |
% |
| 1704 |
% \begin{implementation} |
| 1705 |
% |
| 1706 |
% \section{\pkg{l3fp} implementation} |
| 1707 |
% |
| 1708 |
% Nothing to see here: everything is in the subfiles! |
| 1709 |
% |
| 1710 |
% \end{implementation} |
| 1711 |
% |
| 1712 |
% \PrintIndex |