| 1 |
% -*- coding: utf-8; time-stamp-format: "%02d-%02m-%:y at %02H:%02M:%02S %Z" ; time-stamp-active: nil -*- |
| 2 |
%<*dtx> |
| 3 |
\def\bnedtxtimestampaux #1<#2>{#2} |
| 4 |
\edef\bnedtxtimestamp{\bnedtxtimestampaux Time-stamp: <27-09-2025 at 18:04:58 CEST>} |
| 5 |
\catcode0 0 \catcode`\\ 12 |
| 6 |
^^@iffalse |
| 7 |
%</dtx> |
| 8 |
%<*drv> |
| 9 |
%% --------------------------------------------------------------- |
| 10 |
\def\bnepackdate{2025/09/27}% |
| 11 |
\def\bneversion {1.7b}% |
| 12 |
%</drv> |
| 13 |
%<*readme>-------------------------------------------------------- |
| 14 |
| Source: bnumexpr.dtx |
| 15 |
| Version: v1.7b, 2025/09/27 |
| 16 |
| Author: Jean-François Burnol |
| 17 |
| Info: Expressions with big integers |
| 18 |
| License: LPPL 1.3c |
| 19 |
|
| 20 |
Usage and aim |
| 21 |
============= |
| 22 |
|
| 23 |
The package provides `\bnumeval`. |
| 24 |
|
| 25 |
To use with LaTeX, add `\usepackage{bnumexpr}` to the preamble. |
| 26 |
To use with Plain TeX, execute `\input miniltx.tex` then |
| 27 |
`\input bnumexpr.sty`. |
| 28 |
|
| 29 |
`\bnumeval` is an expandable expression parser extending |
| 30 |
LaTeX's `\inteval` (a wrapper of eTeX's `\numexpr`). |
| 31 |
|
| 32 |
Compared to `\inteval`, the added functionalities are: |
| 33 |
|
| 34 |
- support for arbitrarily big integers, |
| 35 |
- `//` for floored division, |
| 36 |
- `/:` for the associated remainder, |
| 37 |
- `^` and `**` for powers, |
| 38 |
- `!` for factorials, |
| 39 |
- comma separated calculations, |
| 40 |
- space character or underscore may serve to separate digits, |
| 41 |
- `0b`, `0o` and `0x` are recognized as prefixes for binary, octal, |
| 42 |
and hexadecimal inputs (in addition to the TeX prefixes `'` and `"`), |
| 43 |
- letters used for hexadecimal input may be in lowercase, |
| 44 |
- optional argument `[b]`, `[o]` or `[h]` for converting the output |
| 45 |
to binary, octal, or hexadecimal (`[ha]` for lowercase hexadecimal), |
| 46 |
- quasi complete customizability and extensibility of the syntax. |
| 47 |
|
| 48 |
The last item means that not only is it possible to map any given |
| 49 |
operation to a macro of one's own choosing, it is also possible to add |
| 50 |
new infix or postfix operators to the syntax. |
| 51 |
|
| 52 |
Installation |
| 53 |
============ |
| 54 |
|
| 55 |
The package is provided by the major LaTeX distributions. |
| 56 |
|
| 57 |
For a manual installation obtain `bnumexpr.dtx`, from CTAN: |
| 58 |
|
| 59 |
> <http://www.ctan.org/pkg/bnumexpr> |
| 60 |
|
| 61 |
Run `"etex bnumexpr.dtx"` to extract in particular the style file |
| 62 |
`bnumexpr.sty` (and other files inclusive of this `README.md`). Put it |
| 63 |
where (La)TeX can find it and you are done. |
| 64 |
|
| 65 |
To build the documentation check the instructions in extracted |
| 66 |
file `bnumexpr.tex`. |
| 67 |
|
| 68 |
License |
| 69 |
======= |
| 70 |
|
| 71 |
Copyright © 2014-2022, 2025 Jean-François Burnol |
| 72 |
|
| 73 |
| This Work may be distributed and/or modified under the |
| 74 |
| conditions of the LaTeX Project Public License 1.3c. |
| 75 |
| This version of this license is in |
| 76 |
|
| 77 |
> <http://www.latex-project.org/lppl/lppl-1-3c.txt> |
| 78 |
|
| 79 |
| and version 1.3 or later is part of all distributions of |
| 80 |
| LaTeX version 2005/12/01 or later. |
| 81 |
|
| 82 |
This Work has the LPPL maintenance status "author-maintained". |
| 83 |
|
| 84 |
The Author and Maintainer of this Work is Jean-François Burnol. |
| 85 |
|
| 86 |
This Work consists of the main source file and its derived files |
| 87 |
|
| 88 |
bnumexpr.dtx, bnumexpr.sty, bnumexpr.pdf, bnumexpr.tex, |
| 89 |
bnumexprchanges.tex, README.md |
| 90 |
|
| 91 |
%</readme>-------------------------------------------------------- |
| 92 |
%<*!readme> |
| 93 |
%% --------------------------------------------------------------- |
| 94 |
%% The bnumexpr package: Expressions with big integers |
| 95 |
%% Copyright (C) 2014-2022, 2025 Jean-Francois Burnol |
| 96 |
%% |
| 97 |
%</!readme> |
| 98 |
%<*changes>------------------------------------------------------- |
| 99 |
\item[1.7b (2025/09/27)] Add to documentation an expandable macro |
| 100 |
computing Fibonacci numbers and illustrate it with the computation |
| 101 |
of $F_{10000}$. |
| 102 |
\item[1.7a (2025/09/14)]\mbox{}\newline |
| 103 |
\textbf{Bug fix:} inputs consisting exclusively of zeros and underscores |
| 104 |
following an hexadecimal or other prefix caused a crash due to a |1.7| |
| 105 |
regression. \emph{Yes, we have now added a long delayed test suite doing |
| 106 |
more than checking only those examples as included in the documentation.} |
| 107 |
|
| 108 |
\textbf{New feature:} if the parser expects a value and finds only |
| 109 |
underscores (they are allowed as separators for blocks of digits) before |
| 110 |
hitting an operator, an opening parenthesis (or |\bnumexpr| sub-expression), |
| 111 |
a comma, or the end of the expression, it will consider it has fetched the |
| 112 |
value zero. Formerly, for example expressions reduced to |_|, |__|, or |
| 113 |
entirely empty, or for example |_^_| all reported errors. |
| 114 |
|
| 115 |
\textbf{Other:} \cs{evaltohex} which signaled via an expandable |
| 116 |
error its deprecation since |1.6| has been removed. |
| 117 |
\item[1.7 (2025/09/13)]\mbox{}\newline |
| 118 |
\textbf{Bug fix:} inputs with an underscore immediately after a |
| 119 |
hexadecimal (or other) prefix caused a crash due to a |1.6| regression. |
| 120 |
|
| 121 |
\textbf{New features:} |
| 122 |
\begin{itemize} |
| 123 |
\item Support for hexadecimal input using letters in lowercase. |
| 124 |
\item Optional argument |[ha]| for lowercase hexadecimal output. |
| 125 |
\end{itemize} |
| 126 |
\item[1.6a (2025/09/07)]\mbox{}\newline |
| 127 |
\textbf{Bug fix:} the |1.6| support for Babel-active |
| 128 |
characters worked with |\bnumeval| (which is recommended interface) |
| 129 |
but not with |\bnumexpr|. |
| 130 |
\item[1.6 (2025/09/05)]\mbox{}\newline |
| 131 |
\textbf{Breaking changes:} |
| 132 |
\begin{itemize}[noitemsep] |
| 133 |
\item Release |1.4n| or later of the \xintname bundle is required |
| 134 |
(for those components actually used, which by default are |
| 135 |
\xintkernelname, \xintcorename and \xintbinhexname). |
| 136 |
\item \cs{evaltohex} is deprecated and causes |
| 137 |
an error signaling it. \emph{It was removed at |1.7a|}. Use new |\bnumeval||[h]|. |
| 138 |
\item \cs{bnumexprsetup} was deprecated at |1.5| and kept as alias of |
| 139 |
|\bnumsetup|. It has now been removed. |
| 140 |
\item \cs{bnumprintonetohex} and \cs{bnumhextodec}, which were documented |
| 141 |
as customizable do not exist anymore. Check the documentation for |
| 142 |
|\bnumprintonehex| and |\bnumsetup|'s key |hextodec|. |
| 143 |
\item Under the |custom| option, not only \xintcorename but |
| 144 |
also \xintbinhexname are not loaded. |
| 145 |
Use |customcore| to avoid that. There is also |custombinhex|. |
| 146 |
\end{itemize} |
| 147 |
|
| 148 |
\textbf{Bug fix:} |
| 149 |
An underscore |_| located in front of a number used to cause |
| 150 |
an error. It is now ignored. |
| 151 |
|
| 152 |
\textbf{New features:} |
| 153 |
\begin{itemize} |
| 154 |
\item |0b|, |0o| and |0x| are recognized as prefixes for binary, octal, and |
| 155 |
hexadecimal inputs. And |'| is recognized as prefix for octal input, in |
| 156 |
addition to |"| for hexadecimal. |
| 157 |
\item |\bnumeval| accepts an optional argument |[b]| or |[o]| or |
| 158 |
|[h]| for automatic conversion of the calculated value |
| 159 |
(or comma separated values) to |
| 160 |
respectively binary, octal, or hexadecimal. |
| 161 |
\item Babel-active characters (such as |:| and |!| with French) |
| 162 |
do not need any preventive measures anymore such as using |
| 163 |
|\string!| in place of |!|. |
| 164 |
\item |\bnumsetup| can now be used also to customize which macros |
| 165 |
implement conversion from decimal to other bases. |
| 166 |
\end{itemize} |
| 167 |
The documentation was extensively revised and made more user-friendly. |
| 168 |
\item[1.5 (2021/05/17)] |
| 169 |
\begin{itemize} |
| 170 |
\item \textbf{breaking change:} the power operators act now in a right |
| 171 |
associative way; this has been announced at \xintexprname as a probable |
| 172 |
future evolution, and is implemented in anticipation here now. |
| 173 |
\item \textbf{fix two bugs} (imported from upstream \xintexprname) regarding |
| 174 |
hexadecimal input: impossibility to use |"\foo| syntax (one had to do |
| 175 |
|\expandafter"\foo| which is unexpected constraint; a very longstanding |
| 176 |
\xintexprname bug) and issues with leading zeros (since \xintexprname |1.2m|). |
| 177 |
\item renamed \cs{bnumexprsetup} into |\bnumsetup|; the former remains |
| 178 |
available but is deprecated. [REMOVED AT |1.6|] |
| 179 |
\item the customizability and extendibility is now total: |
| 180 |
\begin{enumerate} |
| 181 |
\item |\bnumprintone|, |\bnumprintonetohex|, |\bnumprintonesep|, |\bnumhextodec|, |
| 182 |
\item |\bnumdefinfix| which allows to add extra infix operators, |
| 183 |
\item |\bnumdefpostfix| which allows to add extra postfix operators. |
| 184 |
\end{enumerate} |
| 185 |
\item |\bnumsetup|, |\bnumdefinfix|, |\bnumdefpostfix| obey the |
| 186 |
|\xintglobaldefstrue| and |\xintverbosetrue| settings. |
| 187 |
\item documentation is extended, providing details regarding the precedence |
| 188 |
model of the parser, as inherited from upstream \xintexprname; also an |
| 189 |
example of usage of |\bnumsetup| is included on how to transform |
| 190 |
|\bnumeval| into a calculator with fractions. |
| 191 |
\end{itemize} |
| 192 |
|
| 193 |
\item[1.4a (2021/05/13)] |
| 194 |
\begin{itemize} |
| 195 |
\item fix undefined control sequences errors encountered by the parser in |
| 196 |
case of either extra or missing closing parenthesis (due to a problem |
| 197 |
in technology transfer at |1.4| from upstream \xintexprname). |
| 198 |
\item fix |\BNE_Op_opp| must now be \emph{f}-expandable (also caused as a |
| 199 |
collateral to the technology transfer). |
| 200 |
\item fix user documentation regarding the constraints applying to the user |
| 201 |
replacement macros for the core algebra, as they have changed at |1.4|. |
| 202 |
\end{itemize} |
| 203 |
|
| 204 |
\item[1.4 (2021/05/12)] |
| 205 |
\begin{itemize} |
| 206 |
\item technology transfer from \xintexprname |1.4| of |
| 207 |
|2020/01/31|. The |\expanded| primitive is now required (TeXLive |
| 208 |
2019). |
| 209 |
\item addition to the syntax of the |"| prefix for hexadecimal |
| 210 |
input. |
| 211 |
\item addition of \cs{evaltohex} which is like |\bnumeval| with an extra |
| 212 |
conversion step to hexadecimal notation. |
| 213 |
\end{itemize} |
| 214 |
|
| 215 |
\item[1.2e (2019/01/08)] |
| 216 |
Fixes a documentation glitch (extra braces when mentioning |\the\numexpr| or |
| 217 |
|\thebnumexpr|). |
| 218 |
|
| 219 |
\item[1.2d (2019/01/07)] |
| 220 |
\begin{itemize} |
| 221 |
\item requires \xintcorename |1.3d| or later (if not using option |custom|). |
| 222 |
\item adds |\bnumeval|\marg{expression} user interface. |
| 223 |
\end{itemize} |
| 224 |
|
| 225 |
\item[1.2c (2017/12/05)] \textbf{Breaking changes:} |
| 226 |
\begin{itemize} |
| 227 |
\item requires \xintcorename |1.2p| or later (if not using option |custom|). |
| 228 |
\item |divtrunc| key of \cs{bnumexprsetup} is renamed to |div|. |
| 229 |
\item the |//| and |/:| operators are now by default associated to the |
| 230 |
\emph{floored} division. This is to keep in sync with the change of |
| 231 |
\xintcorename at |1.2p|. |
| 232 |
\item for backwards compatibility, one |
| 233 |
may add to existing document:\newline |
| 234 |
\string\bnumexprsetup\{div=\string\xintiiDivTrunc, |
| 235 |
mod=\string\xintiiModTrunc\} |
| 236 |
\end{itemize} |
| 237 |
|
| 238 |
\item[1.2b (2017/07/09)] |
| 239 |
\begin{itemize} |
| 240 |
\item the |_| may be used to separate visually blocks of digits in long |
| 241 |
numbers. |
| 242 |
\end{itemize} |
| 243 |
|
| 244 |
\item[1.2a (2015/10/14)] |
| 245 |
\begin{itemize} |
| 246 |
\item requires \xintcorename |1.2| or later (if not using option |custom|). |
| 247 |
\item additions to the syntax: factorial |!|, truncated division |
| 248 |
|//|, its associated modulo |/:| and |**| as alternative to |^|. |
| 249 |
\item all options removed except |custom|. |
| 250 |
\item new command \cs{bnumexprsetup} which replaces the commands such as |
| 251 |
|\bnumexprusesbigintcalc|. |
| 252 |
\item the parser is no more limited to numbers with at most 5000 digits. |
| 253 |
\end{itemize} |
| 254 |
|
| 255 |
\item[1.1b (2014/10/28)] |
| 256 |
\begin{itemize} |
| 257 |
\item README converted to |markdown/pandoc| syntax, |
| 258 |
\item the package now loads only |xintcore|, which belongs to |xint| bundle |
| 259 |
version |1.1| and extracts from the earlier |xint| package the core arithmetic |
| 260 |
operations as used by |bnumexpr|. |
| 261 |
\end{itemize} |
| 262 |
|
| 263 |
\item[1.1a (2014/09/22)] |
| 264 |
\begin{itemize} |
| 265 |
\item added |l3bigint| option to use experimental \LaTeX3 |
| 266 |
package of the same name, |
| 267 |
|
| 268 |
\item added Changes and Readme sections to the documentation, |
| 269 |
|
| 270 |
\item better |\BNE_protect| mechanism for use of |
| 271 |
|\bnumexpr...\relax| inside an |\edef| (without |\bnethe|). Previous one, |
| 272 |
inherited from |xintexpr.sty 1.09n|, assumed that the |\.=<digits>| dummy |
| 273 |
control sequence encapsulating the computation result had \cs{relax} |
| 274 |
meaning. But removing this assumption was only a matter of letting |
| 275 |
|\BNE_protect| protect two, not one, tokens. This will be backported to |
| 276 |
next version of \xintexprname, naturally (done with |xintexpr.sty 1.1|). |
| 277 |
\end{itemize} |
| 278 |
|
| 279 |
\item[1.1 (2014/09/21)] First release. This is down-scaled from the |
| 280 |
(development version of) \xintexprname. Motivation came the previous day |
| 281 |
from a chat with \textsc{Joseph Wright} over big int status in \LaTeX3. |
| 282 |
The |\bnumexpr...\relax| parser can be used on top of big int macros of |
| 283 |
one's choice. Functionalities limited to the basic operations. I leave |
| 284 |
the power operator |^| as an option. |
| 285 |
%</changes>------------------------------------------------------- |
| 286 |
%<*drv>----------------------------------------------------------- |
| 287 |
%% latex bnumexpr.tex (thrice) && dvipdfmx bnumexpr.dvi |
| 288 |
%% to produce bnumexpr.pdf |
| 289 |
%% |
| 290 |
%% or pdflatex bnumexpr.tex (no need to change \Withdvipdfmx toggle, |
| 291 |
%% pdf engine will be detected automatically) |
| 292 |
%% |
| 293 |
\NeedsTeXFormat{LaTeX2e}[2020/02/02] |
| 294 |
\ProvidesFile{bnumexpr.tex}% |
| 295 |
[\bnepackdate\space v\bneversion\space driver file for % |
| 296 |
bnumexpr documentation (JFB)]% |
| 297 |
\PassOptionsToClass{a4paper,fontsize=11pt}{scrartcl} |
| 298 |
\chardef\Withdvipdfmx 1 % |
| 299 |
\chardef\NoSourceCode 0 % replace 0 by 1 for not including source code |
| 300 |
\input bnumexpr.dtx |
| 301 |
%%% Local Variables: |
| 302 |
%%% mode: latex |
| 303 |
%%% End: |
| 304 |
%</drv>----------------------------------------------------------- |
| 305 |
%<*dtx> |
| 306 |
^^@fi ^^@catcode`\ 0 \catcode0 12 |
| 307 |
\chardef\noetex 0 |
| 308 |
\ifx\numexpr\undefined \chardef\noetex 1 \fi |
| 309 |
\ifnum\noetex=1 \chardef\extractfiles 0 % extract files, then stop |
| 310 |
\else |
| 311 |
\ifx\ProvidesFile\undefined |
| 312 |
\chardef\extractfiles 0 % no LaTeX2e; etex, ... on bnumexpr.dtx |
| 313 |
\else % latex/pdflatex on bnumexpr.tex or on bnumexpr.dtx |
| 314 |
\ifx\Withdvipdfmx\undefined |
| 315 |
% latex run is on bnumexpr.dtx, we will extract all files |
| 316 |
\chardef\extractfiles 1 % 1 = extract and typeset, 2=only typeset |
| 317 |
\chardef\Withdvipdfmx 0 % 0 = pdflatex or latex+dvips, 1 = dvipdfmx |
| 318 |
\chardef\NoSourceCode 0 % 0 = include source code, 1 = do not |
| 319 |
\NeedsTeXFormat{LaTeX2e}[2020/02/02]% |
| 320 |
\PassOptionsToClass{a4paper,fontsize=11pt}{scrartcl}% |
| 321 |
\else % latex run is on bnumexpr.tex, |
| 322 |
\chardef\extractfiles 2 % no extractions |
| 323 |
\fi |
| 324 |
\ProvidesFile{bnumexpr.dtx}[bundle source (\bnepackdate)]% |
| 325 |
\fi |
| 326 |
\fi |
| 327 |
\ifnum\extractfiles<2 % extract files |
| 328 |
\def\MessageDeFin{\newlinechar10 \let\Msg\message |
| 329 |
\Msg{^^J}% |
| 330 |
\Msg{********************************************************************^^J}% |
| 331 |
\Msg{*^^J}% |
| 332 |
\Msg{* To finish the installation you have to move the following^^J}% |
| 333 |
\Msg{* files into a directory searched by TeX:^^J}% |
| 334 |
\Msg{*^^J}% |
| 335 |
\Msg{*\space\space\space\space bnumexpr.sty^^J}% |
| 336 |
\Msg{*^^J}% |
| 337 |
\Msg{* To produce the documentation run latex thrice on bnumexpr.tex^^J}% |
| 338 |
\Msg{* then dvipdfmx on bnumexpr.dvi. (ignore the dvipdfmx warnings)^^J}% |
| 339 |
\Msg{*^^J}% |
| 340 |
\Msg{* Happy TeXing!^^J}% |
| 341 |
\Msg{*^^J}% |
| 342 |
\Msg{********************************************************************^^J}% |
| 343 |
}% |
| 344 |
\begingroup |
| 345 |
\input docstrip.tex |
| 346 |
\askforoverwritefalse |
| 347 |
\generate{\nopreamble\nopostamble |
| 348 |
\file{README.md}{\from{bnumexpr.dtx}{readme}} |
| 349 |
\usepreamble\defaultpreamble |
| 350 |
\usepostamble\defaultpostamble |
| 351 |
\file{bnumexprchanges.tex}{\from{bnumexpr.dtx}{changes}} |
| 352 |
\file{bnumexpr.tex}{\from{bnumexpr.dtx}{drv}} |
| 353 |
\file{bnumexpr.sty}{\from{bnumexpr.dtx}{package}}} |
| 354 |
\endgroup |
| 355 |
\fi % end of file extraction |
| 356 |
\ifnum\extractfiles=0 |
| 357 |
% direct tex/etex/xetex on bnumexpr.dtx, files now extracted, stop |
| 358 |
\MessageDeFin\expandafter\end |
| 359 |
\fi |
| 360 |
% no use of docstrip to extract files if latex compilation was on bnumexpr.tex |
| 361 |
\ifdefined\MessageDeFin\AtEndDocument{\MessageDeFin}\fi |
| 362 |
%------------------------------------------------------------------------------- |
| 363 |
\documentclass {scrartcl} |
| 364 |
%%% START OF CUSTOM doc.sty LOADING (May 21, 2022) |
| 365 |
\usepackage{doc}[=v2] |
| 366 |
% As explained above I was formerly using scrdoc hence ltxdoc indirectly. |
| 367 |
% Let's emulate here the little I appear to need from ltxdoc.cls and |
| 368 |
% srcdoc.cls. |
| 369 |
% |
| 370 |
\AtBeginDocument{\MakeShortVerb{\|}} |
| 371 |
\DeclareFontShape{OT1}{cmtt}{bx}{n}{<-> ssub * cmtt/m/n}{} |
| 372 |
\DeclareFontFamily{OMS}{cmtt}{\skewchar\font 48} % '60 |
| 373 |
\DeclareFontShape{OMS}{cmtt}{m}{n}{<-> ssub * cmsy/m/n}{} |
| 374 |
\DeclareFontShape{OMS}{cmtt}{bx}{n}{<-> ssub * cmsy/b/n}{} |
| 375 |
\DeclareFontShape{OT1}{cmss}{m}{it}{<->ssub*cmss/m/sl}{} |
| 376 |
\ifnum\NoSourceCode=1 |
| 377 |
\OnlyDescription |
| 378 |
\fi |
| 379 |
\CodelineNumbered |
| 380 |
\DisableCrossrefs |
| 381 |
% \setcounter{StandardModuleDepth}{1} % we don't use this mechanism currently |
| 382 |
\def\cmd#1{\cs{\expandafter\cmd@to@cs\string#1}} |
| 383 |
\def\cmd@to@cs#1#2{\char\number`#2\relax} |
| 384 |
% This is hyperref compatible (contrarily to using \bslash) |
| 385 |
% I need the \detokenize due to usage in implementation part with names |
| 386 |
% containing the underscore for example |
| 387 |
\DeclareRobustCommand\cs[1]{\texttt{\textbackslash\detokenize{#1}}} |
| 388 |
\providecommand\marg[1]{% |
| 389 |
{\ttfamily\char`\{}\meta{#1}{\ttfamily\char`\}}} |
| 390 |
\providecommand\oarg[1]{% |
| 391 |
{\ttfamily[}\meta{#1}{\ttfamily]}} |
| 392 |
\providecommand\parg[1]{% |
| 393 |
{\ttfamily(}\meta{#1}{\ttfamily)}} |
| 394 |
|
| 395 |
% There is very little we seem to need from the scrdoc extras: page geometry |
| 396 |
% is set by geometry package and a4paper option from bnumexpr.tex file. So it |
| 397 |
% seems I only need the hologo loading: |
| 398 |
\usepackage{hologo} |
| 399 |
\DeclareRobustCommand*{\eTeX}{\hologo{eTeX}}% |
| 400 |
%\DeclareRobustCommand*{\LuaTeX}{\hologo{LuaTeX}}% |
| 401 |
% |
| 402 |
%%% end of ltxdoc+srcdoc emulation. |
| 403 |
|
| 404 |
\ifnum\NoSourceCode=1 \OnlyDescription\fi |
| 405 |
\usepackage{ifpdf} |
| 406 |
\ifpdf\chardef\Withdvipdfmx 0 \fi |
| 407 |
\makeatletter |
| 408 |
\ifnum\Withdvipdfmx=1 |
| 409 |
% I think those are mostly obsolete... |
| 410 |
\@for\@tempa:=hyperref,bookmark,graphicx,xcolor,pict2e\do |
| 411 |
{\PassOptionsToPackage{dvipdfmx}\@tempa} |
| 412 |
% |
| 413 |
\PassOptionsToPackage{dvipdfm}{geometry} |
| 414 |
\PassOptionsToPackage{bookmarks=true}{hyperref} |
| 415 |
\PassOptionsToPackage{dvipdfmx-outline-open}{hyperref} |
| 416 |
%\PassOptionsToPackage{dvipdfmx-outline-open}{bookmark} |
| 417 |
% |
| 418 |
\def\pgfsysdriver{pgfsys-dvipdfm.def} |
| 419 |
\else |
| 420 |
\PassOptionsToPackage{bookmarks=true}{hyperref} |
| 421 |
\fi |
| 422 |
\makeatother |
| 423 |
|
| 424 |
\pagestyle{headings} |
| 425 |
|
| 426 |
\usepackage[T1]{fontenc} |
| 427 |
|
| 428 |
\usepackage[hscale=0.66,vscale=0.75]{geometry} |
| 429 |
|
| 430 |
% requires newtxtt 1.05 or later |
| 431 |
\usepackage[zerostyle=d,scaled=0.95,straightquotes]{newtxtt} |
| 432 |
\renewcommand\familydefault\ttdefault |
| 433 |
\usepackage[noendash]{mathastext} |
| 434 |
\renewcommand\familydefault\sfdefault |
| 435 |
|
| 436 |
\usepackage{graphicx} |
| 437 |
\usepackage[dvipsnames]{xcolor} |
| 438 |
\definecolor{joli}{RGB}{225,95,0} |
| 439 |
\definecolor{JOLI}{RGB}{225,95,0} |
| 440 |
\definecolor{BLUE}{RGB}{0,0,255} |
| 441 |
%% \definecolor{niceone}{RGB}{38,128,192}% utilisé avant pour urlcolor |
| 442 |
%% \colorlet{jfverbcolor}{yellow!5} |
| 443 |
\colorlet{smallverbcolor}{magenta}% 1.6 release NavyBlue RoyalPurple |
| 444 |
\colorlet{softwrapcolor}{blue} |
| 445 |
\colorlet{digitscolor}{olive}% 1.6 {OrangeRed} |
| 446 |
|
| 447 |
% transféré de xint-manual.tex (maintenant dans xint.dtx) |
| 448 |
\DeclareFontFamily{U}{MdSymbolC}{} |
| 449 |
\DeclareFontShape {U}{MdSymbolC}{m}{n}{<-> MdSymbolC-Regular}{} |
| 450 |
|
| 451 |
\makeatletter |
| 452 |
\newbox\cdbx@SoftWrapIcon |
| 453 |
\def\cdbx@SetSoftWrapBox{% |
| 454 |
\setbox\cdbx@SoftWrapIcon\hb@xt@\z@ |
| 455 |
{\hb@xt@\fontdimen2\font |
| 456 |
{\hss{\color{softwrapcolor}\usefont{U}{MdSymbolC}{m}{n}\char"97}\hss}% |
| 457 |
\hss}% |
| 458 |
} |
| 459 |
\makeatother |
| 460 |
|
| 461 |
\usepackage[french, main=english]{babel} |
| 462 |
|
| 463 |
\usepackage{hyperref} |
| 464 |
\hypersetup{% |
| 465 |
%linktoc=all,% |
| 466 |
breaklinks=true,% |
| 467 |
colorlinks=true,% |
| 468 |
urlcolor=Cerulean,% 1.6 ProcessBlue,%SkyBlue |
| 469 |
linkcolor=PineGreen,% |
| 470 |
pdfauthor={Jean-François Burnol},% |
| 471 |
pdftitle={The bnumexpr package},% |
| 472 |
pdfsubject={Arithmetic with TeX},% |
| 473 |
pdfkeywords={Expansion, arithmetic, TeX},% |
| 474 |
pdfstartview=FitH,% |
| 475 |
pdfpagemode=UseOutlines} |
| 476 |
\usepackage{bookmark} |
| 477 |
|
| 478 |
% importé de xint 2021/05/14 |
| 479 |
\newcommand\RaisedLabel[2][6]{% |
| 480 |
\vspace*{-#1\baselineskip}% |
| 481 |
\begingroup |
| 482 |
\let\leavevmode\relax\phantomsection |
| 483 |
\label{#2}% |
| 484 |
\endgroup |
| 485 |
\vspace*{#1\baselineskip}% |
| 486 |
} |
| 487 |
|
| 488 |
%---- \verb, and verbatim like `environments'. \MicroFont et \MacroFont |
| 489 |
\makeatletter |
| 490 |
\def\MicroFont {\ttfamily\cdbx@SetSoftWrapBox\color{smallverbcolor}} |
| 491 |
%\def\MicroFont {\ttfamily \color[named]{OrangeRed}\cdbx@SetSoftWrapBox } |
| 492 |
|
| 493 |
% \MacroFont est utilisé par macrocode, mais sa définition est figée dans |
| 494 |
% \macro@font au \begin{document} |
| 495 |
|
| 496 |
\def\MacroFont {\ttfamily \baselineskip12pt\relax } |
| 497 |
|
| 498 |
%--- November 4, 2014 making quotes straight. (maintenu pour *) |
| 499 |
% |
| 500 |
% there is no hook in \macrocode after \dospecials etc. Thus I will need to |
| 501 |
% take the risk that some future evolution of doc.sty (or perhaps scrdoc) |
| 502 |
% invalidates the following. |
| 503 |
% |
| 504 |
% Actually, I should not at all rely on the doc class, I should do it all by |
| 505 |
% myself. |
| 506 |
|
| 507 |
\def\macrocode{\macro@code |
| 508 |
\frenchspacing \@vobeyspaces |
| 509 |
\makestarlowast % \makequotesstraight (obsolète) |
| 510 |
\xmacro@code } |
| 511 |
|
| 512 |
%--- lower * symbol in text |
| 513 |
|
| 514 |
\def\lowast{\raisebox{-.25\height}{*}} |
| 515 |
\catcode`* \active |
| 516 |
\def\makestarlowast {\let*\lowast\catcode`\*\active}% |
| 517 |
\catcode`* 12 |
| 518 |
|
| 519 |
\def\verb #1% |
| 520 |
{% |
| 521 |
\relax\leavevmode\null |
| 522 |
\begingroup |
| 523 |
\MicroFont |
| 524 |
\def\@makeletter##1{\catcode`##1=11\relax}% |
| 525 |
% \scantokens will have a result of inserting a space after cs's. |
| 526 |
% hence the need to have the catcodes of things like _ right. |
| 527 |
% I also need < for > for code comments |
| 528 |
% \dospecials at begin document |
| 529 |
% \do\ \do\\\do\{\do\}\do\$\do\&\do\#\do\^\do\_\do\%\do\~\do\| |
| 530 |
\odef\dospecials{\dospecials\do\:\do\<\do\>\do\-\do\+}% |
| 531 |
% naturally won't work in footnotes though. |
| 532 |
% this code is truly not satisfying, but enough for my needs here. |
| 533 |
\catcode`\\ 11 \catcode`## 11 |
| 534 |
% I don't do \catcode`\% 11 to avoid surprises in code comments |
| 535 |
% if my |...| has a linebreak |
| 536 |
\def\@jfverb ##1#1{\let\do\@makeletter \dospecials |
| 537 |
% lowering a bit the * |
| 538 |
\makestarlowast |
| 539 |
%\let\do\do@noligs \verbatim@nolig@list % not needed here |
| 540 |
\@vobeyspaces\everyeof{\noexpand}% |
| 541 |
\expandafter\@@jfverb\scantokens{##1}\relax}% |
| 542 |
\@jfverb |
| 543 |
}% |
| 544 |
|
| 545 |
% Pour v1.2 je laisse tomber les colorbox. À la place je change la couleur |
| 546 |
% du texte. J'utilise le violet de xint. (mis dans \Microfont) |
| 547 |
% \def\@@@jfverb #1{\ifcat\noexpand#1$% $ |
| 548 |
% \endgroup\else |
| 549 |
% % \penalty\z@ |
| 550 |
% \discretionary{\copy\cdbx@SoftWrapIcon}{}{}% |
| 551 |
% % \colorbox{jfverbcolor}{\strut #1}% unfortunately, impacts pdf size |
| 552 |
% #1% |
| 553 |
% \expandafter\@@@jfverb\fi } |
| 554 |
|
| 555 |
\def\@@jfverb #1{\ifcat\noexpand#1\relax\endgroup\@\else |
| 556 |
\discretionary{\copy\cdbx@SoftWrapIcon}{}{}% |
| 557 |
#1\expandafter\@@jfverb\fi } |
| 558 |
|
| 559 |
\makeatother |
| 560 |
|
| 561 |
% Lundi 17 mai 2021 |
| 562 |
% J'ajoute un export automatique des exemples qui servira de poor man test |
| 563 |
% suite |
| 564 |
\newwrite\tests |
| 565 |
\immediate\openout\tests=\jobname-out.txt |
| 566 |
|
| 567 |
% mardi 13 octobre 2015 |
| 568 |
% le | n'est pas encore actif ici! |
| 569 |
\catcode`| \active |
| 570 |
% first one uses \bnumexpr deliberately with no \bnethe to test 1.4. |
| 571 |
\def\bneshow #1% |
| 572 |
{|\bnumexpr#1\relax|% |
| 573 |
\edef\TEMP{\bnumexpr#1\relax}% |
| 574 |
\immediate\write\tests{\detokenize{\bnumexpr#1\relax}^^J\TEMP}% |
| 575 |
\[\color{digitscolor}\TEMP\]\ignorespaces} |
| 576 |
\def\bneshowthe #1% |
| 577 |
{|\bnethe\bnumexpr#1\relax|% |
| 578 |
\edef\TEMP{\bnethe\bnumexpr#1\relax}% |
| 579 |
\immediate\write\tests{\detokenize{\bnethe\bnumexpr#1\relax}^^J\TEMP}% |
| 580 |
\[\color{digitscolor}\TEMP\]\ignorespaces} |
| 581 |
\def\bneshoweval #1% |
| 582 |
{|\bnumeval|\unskip|{{#1}}|% problème de grab de mon \verb d'où {{..}} |
| 583 |
\edef\TEMP{\bnumeval{#1}}% |
| 584 |
\immediate\write\tests{\detokenize{\bnumeval{#1}}^^J\TEMP}% |
| 585 |
\[\color{digitscolor}\TEMP\]\ignorespaces} |
| 586 |
% Ceci pour l'exemple avec " en output. Ici on utilise un display |
| 587 |
% donc en mode math, mais mathastext ne s'occupe pas de "... |
| 588 |
{\catcode`"\active\gdef"{\texttt{\string"}}} |
| 589 |
\def\bneshowevaltohex #1% |
| 590 |
{|\bnumeval|\unskip|[h]{#1}|% |
| 591 |
\edef\TEMP{\bnumeval[h]{#1}}% |
| 592 |
\immediate\write\tests{\detokenize{\bnumeval[h]{#1}}^^J\TEMP}% |
| 593 |
\[\mathcode`"="8000 \color{digitscolor}\TEMP\]\ignorespaces} |
| 594 |
\def\bneshowevaltolowhex #1% |
| 595 |
{|\bnumeval|\unskip|[ha]{#1}|% |
| 596 |
\edef\TEMP{\bnumeval[ha]{#1}}% |
| 597 |
\immediate\write\tests{\detokenize{\bnumeval[ha]{#1}}^^J\TEMP}% |
| 598 |
\[\mathcode`"="8000 \color{digitscolor}\TEMP\]\ignorespaces} |
| 599 |
\def\bneshowevaltooct #1% |
| 600 |
{|\bnumeval|\unskip|[o]{#1}|% |
| 601 |
\edef\TEMP{\bnumeval[o]{#1}}% |
| 602 |
\immediate\write\tests{\detokenize{\bnumeval[o]{#1}}^^J\TEMP}% |
| 603 |
\[\color{digitscolor}\TEMP\]\ignorespaces} |
| 604 |
\def\bneshowevaltobin #1% |
| 605 |
{|\bnumeval|\unskip|[b]{#1}|% |
| 606 |
\edef\TEMP{\bnumeval[b]{#1}}% |
| 607 |
\immediate\write\tests{\detokenize{\bnumeval[b]{#1}}^^J\TEMP}% |
| 608 |
\[\color{digitscolor}\TEMP\]\ignorespaces} |
| 609 |
\catcode`| 12 |
| 610 |
% Mardi 14 juin 2022 |
| 611 |
\def\bnedotest #1{\oodef\x{#1}\immediate\write\tests{\detokenize{#1}^^J\x}} |
| 612 |
|
| 613 |
% toujours pénible de devoir se battre avec les espaces verticaux de TeX |
| 614 |
\AtBeginDocument{% |
| 615 |
% \message{ICI below: \the\belowdisplayskip^^J% |
| 616 |
% belowshort: \the\belowdisplayshortskip^^J% |
| 617 |
% above: \the\abovedisplayskip^^J% |
| 618 |
% aboveshor: \the\abovedisplayshortskip^^J% |
| 619 |
% }% |
| 620 |
\belowdisplayskip\belowdisplayshortskip |
| 621 |
\abovedisplayskip\abovedisplayshortskip |
| 622 |
} |
| 623 |
|
| 624 |
\usepackage{xspace} |
| 625 |
|
| 626 |
\def\bnumname |
| 627 |
{\href{http://www.ctan.org/pkg/bnumexpr}{bnumexpr}\xspace }% |
| 628 |
|
| 629 |
\def\bnumnameimp |
| 630 |
{\texorpdfstring |
| 631 |
{\hyperref[sec:bnumexprcode]{{\color{smallverbcolor}\ttzfamily bnumexpr}}} |
| 632 |
{bnumexpr}% |
| 633 |
\xspace }% |
| 634 |
|
| 635 |
\def\bnumnameuserdoc |
| 636 |
{\texorpdfstring |
| 637 |
{\hyperref[titlepage]{{\color{PineGreen}\ttzfamily bnumexpr}}} |
| 638 |
{bnumexpr}% |
| 639 |
\xspace }% |
| 640 |
|
| 641 |
\def\xintkernelname |
| 642 |
{\href{http://www.ctan.org/pkg/xint}{xintkernel}\xspace }% |
| 643 |
\def\xintcorename |
| 644 |
{\href{http://www.ctan.org/pkg/xint}{xintcore}\xspace }% |
| 645 |
\def\xintname |
| 646 |
{\href{http://www.ctan.org/pkg/xint}{xint}\xspace }% |
| 647 |
\def\xintfracname |
| 648 |
{\href{http://www.ctan.org/pkg/xint}{xintfrac}\xspace }% |
| 649 |
\def\xintexprname |
| 650 |
{\href{http://www.ctan.org/pkg/xintexpr}{xintexpr}\xspace }% |
| 651 |
\def\xintbinhexname |
| 652 |
{\href{http://www.ctan.org/pkg/xint}{xintbinhex}\xspace }% |
| 653 |
|
| 654 |
\DeclareRobustCommand\ctanpackage[1]{\href{https://ctan.org/pkg/#1}{#1}} |
| 655 |
|
| 656 |
%\frenchspacing |
| 657 |
\usepackage{bnumexpr} |
| 658 |
\usepackage{xintfrac} |
| 659 |
\usepackage{etoc} |
| 660 |
\usepackage{centeredline} |
| 661 |
\usepackage{framed} |
| 662 |
\makeatletter\let\check@percent\relax\makeatother |
| 663 |
|
| 664 |
% 2022/06/15 -- comment updated 2025/09/07 |
| 665 |
% |
| 666 |
% latex builds at home during preparation for the release are done |
| 667 |
% using --translate-file=natural.tcx for the benefit of \ALERTE. |
| 668 |
% The shipped to CTAN dtx will not contain any \ALERTE execution |
| 669 |
% hence can be build using latex normally. |
| 670 |
% |
| 671 |
% AT LONG LAST LATEX HAS MADE THE UTF-8 MACROS PROTECTED |
| 672 |
% so many years waiting for that... |
| 673 |
{\catcode27=12 \gdef\babar{^^[}} |
| 674 |
\def\ALERTE{\edef\foo{\noexpand\AtEndDocument |
| 675 |
{\immediate\write128{\babar[1;31mATTENTION METTRE À JOUR COMMENTAIRE EN LIGNE |
| 676 |
\the\inputlineno\babar[0m}}}% |
| 677 |
\foo} |
| 678 |
|
| 679 |
% 2025/09/07 |
| 680 |
% https://tex.stackexchange.com/a/62032 |
| 681 |
% Et je mets [2020/02/02] dans les \NeedsTeXFormat de bnumexpr{.tex,.dtx}, |
| 682 |
% pour être sûr |
| 683 |
\ExplSyntaxOn |
| 684 |
\NewDocumentCommand{\getenv}{om} |
| 685 |
{ |
| 686 |
\sys_get_shell:nnN { kpsewhich ~ --var-value ~ #2 } { } \l_tmpa_tl |
| 687 |
\tl_trim_spaces:N \l_tmpa_tl |
| 688 |
\IfNoValueTF { #1 } |
| 689 |
{ |
| 690 |
\tl_use:N \l_tmpa_tl |
| 691 |
} |
| 692 |
{ |
| 693 |
\tl_set_eq:NN #1 \l_tmpa_tl |
| 694 |
} |
| 695 |
} |
| 696 |
\ExplSyntaxOff |
| 697 |
% |
| 698 |
\getenv[\SHIPPINGNOW]{BNUMEXPR_RELEASING} |
| 699 |
% \SHIPPINGNOW->\par si la variable n'est pas définie. |
| 700 |
% L'expansion de \par débute par un \scan_stop: (au moins en 2025) |
| 701 |
\if1\SHIPPINGNOW\def\ALERTE{\ERREUR}\fi |
| 702 |
|
| 703 |
\usepackage[autolanguage,np]{numprint} |
| 704 |
\usepackage{enumitem} |
| 705 |
|
| 706 |
\newenvironment{TeXnote}{\par\footnotesize\medskip\noindent\textbf{\TeX-nical note: |
| 707 |
}}{\par\medskip} |
| 708 |
|
| 709 |
\begin{document} |
| 710 |
\thispagestyle{empty} |
| 711 |
\ttzfamily |
| 712 |
\pdfbookmark[1]{Title page}{TOP} |
| 713 |
|
| 714 |
{% |
| 715 |
\normalfont\Large\parindent0pt \parfillskip 0pt\relax |
| 716 |
\leftskip 2cm plus 1fil \rightskip 2cm plus 1fil |
| 717 |
The \bnumname package\par |
| 718 |
} |
| 719 |
|
| 720 |
\RaisedLabel{titlepage} |
| 721 |
|
| 722 |
{\centering |
| 723 |
\textsc{Jean-François Burnol}\par |
| 724 |
\footnotesize |
| 725 |
jfbu (at) free (dot) fr\par |
| 726 |
Package version: \bneversion\ (\bnepackdate)\par |
| 727 |
{From source file \texttt{bnumexpr.dtx} of \texttt{\bnedtxtimestamp}}\par |
| 728 |
} |
| 729 |
|
| 730 |
\etocsetnexttocdepth{subsection} |
| 731 |
\etocsettagdepth{code}{section} |
| 732 |
\etocsettocstyle{}{} |
| 733 |
\tableofcontents |
| 734 |
|
| 735 |
\section{\cs{bnumeval}} |
| 736 |
\label{sec:bnumexpr} |
| 737 |
\label{sec:thebnumexpr} |
| 738 |
\label{sec:bnumeval} |
| 739 |
|
| 740 |
This \LaTeX\ package \bnumname provides |\bnumeval|, which is an expandable |
| 741 |
parser of numerical expressions with big integers. |
| 742 |
|
| 743 |
Recent \LaTeX\ has |\inteval|, which is a slim wrapper for \eTeX{}'s |
| 744 |
\cs{numexpr} (embedded for twenty years in most \TeX-engines except |
| 745 |
original Knuth's |tex|). |
| 746 |
\begin{TeXnote} |
| 747 |
More precisely \cs{inteval}\marg{expression} is equivalent (up to how \TeX{} |
| 748 |
handles spaces located after in the source during tokenization, as |
| 749 |
tokenization of control sequences such as \cs{relax} causes \TeX{} to ignore |
| 750 |
space characters or end-of-line space after it) to: |
| 751 |
\centeredline{\cs{the}\cs{numexpr}\meta{expression}\cs{relax}} In an analogous way |
| 752 |
|\bnumeval|\marg{expression} has equivalent forms: |
| 753 |
\centeredline{|\bnethe\bnumexpr|\meta{expression}\cs{relax}} |
| 754 |
\centeredline{|\thebnumexpr|\meta{expression}\cs{relax}} For contexts where |
| 755 |
the alternative forms may be useful, refer to the \autoref{sec:advanced}. |
| 756 |
Everyday use needs only |\bnumeval|. |
| 757 |
\end{TeXnote} |
| 758 |
|
| 759 |
Here are the extra features from |\bnumeval| compared to \cs{inteval}: |
| 760 |
\begin{enumerate}[noitemsep,label=\textbf{(\alph*)}] |
| 761 |
\item It allows arbitrarily big integers, whereas \cs{inteval} is limited |
| 762 |
to a maximal input equal to $\the\numexpr"7FFFFFFF\relax$ |
| 763 |
($2^{31}-1$, or hexadecimal |7FFFFFFF|). |
| 764 |
\item It recognizes |**| and |^| as infix operator for powers, |
| 765 |
\item It recognizes |!| as postfix operator for the factorials, |
| 766 |
\item The new operator |//| computes floored division with |/:| being the |
| 767 |
operator for the associated remainder (the operator |/| computes |
| 768 |
rounded division), |
| 769 |
\item In addition to the \TeX{} prefixes |'| and |"| for octal and |
| 770 |
hexadecimal, it recognizes |0b|, |0o| and |0x| for |
| 771 |
binary, octal, and hexadecimal, |
| 772 |
\item Letters in lowercase can be used for hexadecimal input, |
| 773 |
\item The space character is ignored% |
| 774 |
% |
| 775 |
\footnote{It is not completely ignored, |\count37<space>| will automatically |
| 776 |
be prefixed by \cs{number} and the space token delimits the integer indexing |
| 777 |
the count register. Also, devious inputs using nested braces around spaces |
| 778 |
may create unexpected internal situations and even break the parser.} |
| 779 |
% |
| 780 |
and can thus be used to separate in the |
| 781 |
source blocks of digits for better readability of long numbers, |
| 782 |
\item Also the underscore |_| may be used as visual digit |
| 783 |
separator, |
| 784 |
\item Braced material \string{...\string} encountered in the expression is automatically |
| 785 |
unbraced, |
| 786 |
\item Comma separated expressions are allowed, |
| 787 |
\item Some idiosyncrasies of \cs{numexpr} such as |\inteval{-(1)}| causing an |
| 788 |
error are avoided, |
| 789 |
\item Syntax is fully customizable and extensible. |
| 790 |
\end{enumerate} |
| 791 |
% |
| 792 |
Note the following about octal and binary inputs: |
| 793 |
\begin{quote} |
| 794 |
When parsing an octal number, if digits |8| or |9| are encountered, or when |
| 795 |
parsing binary any digit larger than |1|, this does not cause an error but |
| 796 |
ends the parsing and inserts a tacit multiplication: |\bnumeval{'118+3}| |
| 797 |
computes $9\times 8 + 3$, hence obtains \bnumeval{'118+3}. |
| 798 |
\end{quote} |
| 799 |
% |
| 800 |
Also note the following on precedences: |
| 801 |
\begin{quote} |
| 802 |
As is explained with complete details in \autoref{sssec:prec}, the |
| 803 |
multiplication operator, as well as the division operators (including |
| 804 |
modulo) are all at the same precedence level and behave in a |
| 805 |
left-associative way. Example: |\bnumeval{10*10//3, 10*10/:3}| |
| 806 |
evaluates to \textcolor{digitscolor}{33, 1}, not to |
| 807 |
\textcolor{digitscolor}{30, 10}. |
| 808 |
|
| 809 |
Also note that powers (which obviouly have |
| 810 |
a higher precedence) work in a right-associative way so that |
| 811 |
|\bnumeval{2^3^4}| will compute \textcolor{digitscolor}{$2^{81}$}, not |
| 812 |
\textcolor{digitscolor}{$8^{4}$}. |
| 813 |
|
| 814 |
The best among our users know how to set operator precedences to their own |
| 815 |
liking, as they have read all the fine print in \autoref{ssec:expert}. |
| 816 |
\end{quote} |
| 817 |
% |
| 818 |
Furthermore, |\bnumeval| recognizes an optional argument |[b]|, |[o]|, |[h]|, |
| 819 |
or |[ha]| which says to have the calculation result (or comma separated results) |
| 820 |
be converted to respectively binary, octal, hexadecimal (uppercase) or |
| 821 |
lowercase hexadecimal digits. |
| 822 |
|
| 823 |
\section{Dependencies} |
| 824 |
|
| 825 |
\bnumname is a \LaTeX{} package but it can also be used with Plain \TeX, |
| 826 |
thanks to \ctanpackage{miniltx}. Use for this |
| 827 |
\cs{input} \ctanpackage{miniltx}|.tex| |
| 828 |
and then \cs{input} \bnumname \unskip|.sty|. Do not use \cs{input} but only |
| 829 |
\cs{usepackage} to load the package with \LaTeX. |
| 830 |
|
| 831 |
Addition, subtraction, multiplication, division(s), modulo operator, powers, |
| 832 |
and factorials are all by default executed by macros provided by the |
| 833 |
\xintcorename package. |
| 834 |
|
| 835 |
Conversions between decimal, binary, octal and |
| 836 |
hexadecimal bases are done using the macros from the \xintbinhexname package. |
| 837 |
|
| 838 |
|\bnumeval| is a scaled-down variant of |\xintiieval| from package |
| 839 |
\xintexprname, lacking support for nested structures, functions, variables, |
| 840 |
booleans, sequence generators, etc... . |
| 841 |
The \xintexprname package is NOT loaded, only as said previously |
| 842 |
\xintcorename and \xintbinhexname. |
| 843 |
|
| 844 |
\begin{TeXnote} |
| 845 |
Power users can use |\bnumsetup| to configure usage of alternative support |
| 846 |
macros of their own choosing. Options can disable the loading of |
| 847 |
\xintcorename and/or \xintbinhexname. But \xintkernelname is always loaded. |
| 848 |
See \autoref{sec:advanced}. Expert users can even add new operators to the |
| 849 |
syntax, even change the built-in ones or their precedences. See |
| 850 |
\autoref{ssec:expert}. |
| 851 |
\end{TeXnote} |
| 852 |
|
| 853 |
\section{Examples} |
| 854 |
|
| 855 |
Some of these examples use the ancient syntax |
| 856 |
|\bnethe\bnumexpr...\relax| from the initial release (in 2014). The |
| 857 |
|\bnethe| prefix converts from some private format (using braces and |
| 858 |
other things). Some examples do not even have the |\bnethe| prefix to |
| 859 |
|\bnumexpr| because it is allowed in typesetting context to omit it (but |
| 860 |
in an \cs{edef} without it expansion gives the private format). For |
| 861 |
details refer to \autoref{sec:advanced} on advanced topics. |
| 862 |
|
| 863 |
Some further examples found in this documentation use the other ancient |
| 864 |
syntax |\thebnumexpr...\relax| where |\thebnumexpr| is equivalent to |
| 865 |
|\bnethe\bnumexpr|. |
| 866 |
|
| 867 |
The recommended interface is |\bnumeval|, as it has optional |
| 868 |
arguments to cause conversion to hexadecimal, octal or binary. They |
| 869 |
have no equivalent with |\bnethe\bnumexpr| or with |\thebnumexpr|. |
| 870 |
|
| 871 |
Some inputs are weird (such as the first one with three minus signs) |
| 872 |
because they served originally to check the syntax. |
| 873 |
|
| 874 |
% NOTE 2026/09/07. After the 1.6 blunder which shipped with \bnumexpr not |
| 875 |
% accepting Babel active contrarily to what announcement (but \bnumeval was |
| 876 |
% ok, and it is the recommened interface) and code comments said, I modified |
| 877 |
% the preamble to add "french" as language in order to test it here. This |
| 878 |
% means though that ! and : will be catcode active throughout the document |
| 879 |
% (even though French is used only locally) so this means my unit testing here |
| 880 |
% is not anymore one for the standard context with default catcodes but well. |
| 881 |
% |
| 882 |
% The \bneshow and other macros not only display the values but also save |
| 883 |
% input and output to an auxiliary file which is tested via git diff to check |
| 884 |
% for changes and detect regressions. |
| 885 |
% |
| 886 |
% Even with French active, one should beware that in vertical mode it |
| 887 |
% self-silences itself, and one can be fooled to think no error happened, if |
| 888 |
% the \bnumeval or \bnumexpr is first thing in the paragraph. But here the |
| 889 |
% \bneshow and other macros will quit vertical mode prior to computing the |
| 890 |
% expression, so that trap is avoided. |
| 891 |
\noindent\bneshow {--- 1 208 637 867 * (2 187 917 891 - 3 109 197 072)}% |
| 892 |
\bneshow { ( 13_8089_1090 - 300_1890_2902 ) * ( 1083_1908_3901 - 109_8290_3890 )}% |
| 893 |
\bneshoweval {92_874_927_979 ** 5 - 31_9792_7979 ** 6}% |
| 894 |
\foreignlanguage{french}{% |
| 895 |
\bneshowthe {10!, 20!, 30!}% broken at 1.6... fixed at 1.6a |
| 896 |
Testing tacit multiplication elevated precedence\string:\newline |
| 897 |
\bneshoweval {30!/(21*22*23*24*25)(26*27*28*29*30), 20!}% worked as expected at 1.6 |
| 898 |
\bneshoweval {13^50//12^50, 13^50/:12^50}% idem |
| 899 |
}% |
| 900 |
\bneshoweval {13^50/12^50, 12^50}% |
| 901 |
\bneshoweval {(1^10+2^10+3^10+4^10+5^10+6^10+7^10+8^10+9^10)^3}% |
| 902 |
\bneshoweval {100! /: 10^50}% |
| 903 |
% |
| 904 |
Let's check hexadecimal input:\newline |
| 905 |
\bneshoweval {"0010 * "0100 * 0x1000 * 0xA0000, 16^(1+2+3+4)*10}% |
| 906 |
\bneshoweval {"000_abc_def, 0xABCDEF, "abcdef + "543210 + 1, 0x10^6}% |
| 907 |
And also hexadecimal output:\newline |
| 908 |
\bneshowevaltohex{"_7f_fff_fff+1, 0x_0400^3, "aBcDeF*"0000fedcba, |
| 909 |
1234} |
| 910 |
And also in lowercase\newline |
| 911 |
\bneshowevaltolowhex{"abcdef, "ABCDEF, "999_999_999, 16^10-1, 167772160} |
| 912 |
% |
| 913 |
Let's make a few checks of octal and binary:\newline |
| 914 |
\bneshowevaltooct {'75316420 * 0o44445555} |
| 915 |
\bneshowevaltobin {'75316420 * 0o44445555} |
| 916 |
\bneshowevaltobin {0xFFFF, 0o77, 0b1000001^3} |
| 917 |
% |
| 918 |
We end with some strange non-recommended things to check details |
| 919 |
of how the parser expands the input:\newline |
| 920 |
\bneshoweval {"0000\bnumeval[h]{00000012345678}FFFF, 000012345679*16**4-1} |
| 921 |
% |
| 922 |
\bneshowevaltooct{0b000\bnumeval[b]{'123456}, 0x\bnumeval[h]{0o00000123456}} |
| 923 |
|
| 924 |
\section{Customizing how output is ``printed out''} |
| 925 |
|
| 926 |
\subsection{Printing big numbers} |
| 927 |
\label{sec:printing} |
| 928 |
\TeX{} and \LaTeX{} will not split long numbers at the end of lines. I personally |
| 929 |
often use helper macros (not in the package) of the following type: |
| 930 |
|
| 931 |
\begin{verbatim} |
| 932 |
\def\allowsplits #1{\ifx #1\relax \else #1\hskip 0pt plus 1pt\relax |
| 933 |
\expandafter\allowsplits\fi}% |
| 934 |
\def\printnumber #1{\expandafter\allowsplits \romannumeral-`0#1\relax }% |
| 935 |
\end{verbatim} |
| 936 |
|
| 937 |
\def\allowsplits #1{\ifx #1\relax \else #1\hskip 0pt plus 1pt\relax |
| 938 |
\expandafter\allowsplits\fi}% |
| 939 |
\def\printnumber #1{\expandafter\allowsplits \romannumeral-`0#1\relax }% |
| 940 |
|
| 941 |
Here is an example of use and its output: |
| 942 |
\begin{verbatim} |
| 943 |
\noindent|\bnumeval{1000!} =| |
| 944 |
\textcolor{digitscolor}{\printnumber{\bnumeval{1000!}}} |
| 945 |
\end{verbatim} |
| 946 |
\noindent|\bnumeval{1000!} =|\phantom{0}% |
| 947 |
\textcolor{digitscolor}{\printnumber{\bnumeval{1000!}}} |
| 948 |
|
| 949 |
\begin{TeXnote} |
| 950 |
|\bnumeval| is \emph{f}-expandable, so the |\romannumeral-`0| as used |
| 951 |
here in |\printnumber| causes its full expansion (even if for example |
| 952 |
the output contains multiple values, separated by commas). So then |
| 953 |
|\printnumber|'s auxiliary can simply loop over the tokens. |
| 954 |
\end{TeXnote} |
| 955 |
|
| 956 |
\begin{TeXnote} |
| 957 |
Note that inside math mode, the inserted \cs{hskip}'s have no effect. |
| 958 |
There should be some \cs{allowbreak}'s. By the way, we allow some |
| 959 |
stretch so that line endings match the actual linewidth. |
| 960 |
\end{TeXnote} |
| 961 |
|
| 962 |
\subsection{\cs{bnumprintone}, \cs{bnumprintonesep}} |
| 963 |
|
| 964 |
The output values are each fetched to |
| 965 |
|\bnumprintone| and separated by |\bnumprintonesep|. |
| 966 |
|
| 967 |
Here are the default definitions (or rather some quasi equivalents in \LaTeX's |
| 968 |
lingua): |
| 969 |
\begin{verbatim} |
| 970 |
\newcommand{\bnumprintone}[1]{#1} |
| 971 |
\newcommand{\bnumprintonesep}{, } |
| 972 |
\end{verbatim} |
| 973 |
|
| 974 |
In other terms |\bnumprintone| produces its argument ``as is'', and multiple |
| 975 |
values get separated by a comma and a space. |
| 976 |
|
| 977 |
Let's say you want the output to be boxed. Doing |
| 978 |
|\fbox{\bnumeval{...}}| will make one single frame even in case |
| 979 |
of multiple values. Redefining |\bnumprintone| |
| 980 |
is the way to go: |
| 981 |
\begingroup\RenewDocumentCommand{\bnumprintone}{m}{\fbox{#1}} |
| 982 |
\begin{verbatim} |
| 983 |
\RenewDocumentCommand{\bnumprintone}{m}{\fbox{#1}} |
| 984 |
\bnumeval{2^10, 3^10, 5^10, 7^10} |
| 985 |
\end{verbatim} |
| 986 |
\textcolor{digitscolor}{\bnumeval{2^10, 3^10, 5^10, 7^10}} |
| 987 |
\endgroup |
| 988 |
|
| 989 |
It is important to have used |\RenewDocumentCommand| and not \cs{renewcommand} |
| 990 |
here, because |\bnumprintone| and |\bnumprintonesep| have to be compatible |
| 991 |
with expansion only context. |
| 992 |
\begin{TeXnote} |
| 993 |
That means that |\bnumprintone| in an \cs{edef} should not give rise to any |
| 994 |
\cs{newcommand}, lower level \cs{def}, count or dimen assignments, etc.... |
| 995 |
|
| 996 |
This constraint is due to the fact that |\bnumeval| wraps the final |
| 997 |
print-out inside of \cs{expanded}, for \TeX nical reasons. |
| 998 |
|
| 999 |
The simplest way for |\bnumprintone| (considering that |
| 1000 |
its argument will already have been fully expanded to digit tokens) and |
| 1001 |
|\bnumprintonesep| to be ``safe'' is that they do not expand at all in |
| 1002 |
\cs{edef}. This is the case if they are defined using |
| 1003 |
\cs{RenewDocumentCommand}. |
| 1004 |
% |
| 1005 |
With an older \LaTeX{}, or Plain \eTeX{} |
| 1006 |
(but having some \cs{fbox} at our disposal), we would have used here |
| 1007 |
|\protected\def\bnumprintone#1{\fbox{#1}}|. |
| 1008 |
\end{TeXnote} |
| 1009 |
|
| 1010 |
A more common use case will be to have the outputs be typeset according to the |
| 1011 |
conventions of the document language. This is easily done redefining |
| 1012 |
|\bnumprintone| in terms of (for example) the \cs{np} macro of package |
| 1013 |
\ctanpackage{numprint}. |
| 1014 |
\begingroup\RenewDocumentCommand{\bnumprintone}{m}{\np{#1}} |
| 1015 |
\renewcommand{\bnumprintonesep}{ --- } |
| 1016 |
\begin{verbatim} |
| 1017 |
\RenewDocumentCommand{\bnumprintone}{m}{\np{#1}} |
| 1018 |
\renewcommand{\bnumprintonesep}{ --- } |
| 1019 |
\bnumeval{2^10, 3^10, 5^10, 7^10} |
| 1020 |
\end{verbatim} |
| 1021 |
\textcolor{digitscolor}{\bnumeval{2^10, 3^10, 5^10, 7^10}} |
| 1022 |
\endgroup |
| 1023 |
|
| 1024 |
\begin{TeXnote} |
| 1025 |
Usage of \cs{RenewDocumentCommand} for |\bnumprintonesep| was not |
| 1026 |
needed here, obviously its expansion could cause no trouble. |
| 1027 |
\end{TeXnote} |
| 1028 |
|
| 1029 |
Let's give another use case. Assume you are computing in one go multiple |
| 1030 |
large values, too large to fit on a line. The simple-minded |\printnumber| of |
| 1031 |
the previous section will (due to some \TeX nicality) swallow the spaces |
| 1032 |
injected by |\bnumprintonesep|. To fix this, the simplest is to redefine |
| 1033 |
|\bnumprintone| to execute |\printnumber|: |
| 1034 |
\begingroup\let\bnumprintone\printnumber |
| 1035 |
\begin{verbatim} |
| 1036 |
\renewcommand{\bnumprintone}[1]{\printnumber{#1}} |
| 1037 |
\bnumeval{2^100, 3^100, 5^100, 7^100} |
| 1038 |
\end{verbatim} |
| 1039 |
% je ne peux pas utiliser \bneshoweval car il passe ne mode mathématique |
| 1040 |
\textcolor{digitscolor}{\bnumeval{2^100, 3^100, 5^100}} |
| 1041 |
\endgroup |
| 1042 |
|
| 1043 |
\begin{TeXnote} |
| 1044 |
Our |\printnumber| belongs to this family of macros causing no damage |
| 1045 |
if expanding in an \cs{edef}. So, it was not needed to use |
| 1046 |
\cs{RenewDocumentCommand}. |
| 1047 |
\end{TeXnote} |
| 1048 |
|
| 1049 |
\subsection[\cs{bnumprintonehex}, \cs{bnumprintonelowerhex}, |
| 1050 |
\cs{bnumprintoneoct},\texorpdfstring{\newline}{} |
| 1051 |
\cs{bnumprintonebin}]{\cs{bnumprintonehex}, \cs{bnumprintonelowerhex}, |
| 1052 |
\cs{bnumprintoneoct}, \cs{bnumprintonebin}} |
| 1053 |
|
| 1054 |
When |\bnumeval| is exerted with |[h]|, |[ha]|, |[o]| or |[b]| |
| 1055 |
it does not use |\bnumprintone| but one of |\bnumprintonehex|, |
| 1056 |
|\bnumprintonelowerhex|, |
| 1057 |
|\bnumprintoneoct| or |\bnumprintonebin|. |
| 1058 |
The same |\bnumprintonesep| is used as with decimal numbers. |
| 1059 |
|
| 1060 |
The default definitions are as for |\bnumprintone| to ``print as is''. |
| 1061 |
|
| 1062 |
\begingroup |
| 1063 |
\def\bnumprintonehex#1{0x#1} |
| 1064 |
\def\bnumprintonelowerhex#1{"#1} |
| 1065 |
To give an example of a custom |
| 1066 |
definition, one may want hexadecimal to use the |0x| prefix with |
| 1067 |
uppercase output or the |"| prefix with lowercase output. This is |
| 1068 |
very easy: |
| 1069 |
\begin{verbatim} |
| 1070 |
\renewcommand{\bnumprintonehex}[1]{0x#1} |
| 1071 |
\renewcommand{\bnumprintonelowerhex}[1]{"#1} |
| 1072 |
\end{verbatim} |
| 1073 |
\bneshowevaltohex{7^30, 13^20, 20!} |
| 1074 |
\bneshowevaltolowhex{7^30, 13^20, 20!} |
| 1075 |
\endgroup |
| 1076 |
|
| 1077 |
\begin{TeXnote} |
| 1078 |
It was unneeded to use \cs{RenewDocumentCommand} here because prefixing |
| 1079 |
with |0x| is obviously compatible with expansion-only context. |
| 1080 |
\end{TeXnote} |
| 1081 |
|
| 1082 |
\section{Babel-active characters are not a problem!} |
| 1083 |
|
| 1084 |
Some languages use active characters with PDF\LaTeX. For example the |
| 1085 |
|babel-french| module turns the colon |:| and the exclamation mark |!| |
| 1086 |
into active characters (whose expansions would cause |\bnumeval| to |
| 1087 |
crash). It used to be necessary to take preventive measures such as |
| 1088 |
either turning the activation off altogether or use in the input |
| 1089 |
|/\string:| and |\string!| as clumsy replacements of |/:| and |!|. |
| 1090 |
|
| 1091 |
Those troubled times are gone! With release |1.6| they will work fine |
| 1092 |
as is in |\bnumeval|. The same applies to all other characters if |
| 1093 |
babel-active. There are miracles sometimes! |
| 1094 |
|
| 1095 |
Warning: characters made active otherwise still need the \cs{string} or |
| 1096 |
other workaround to be usable as operators in the syntax. |
| 1097 |
|
| 1098 |
\section{Fine print (not needed to read this for regular use)} |
| 1099 |
\label{sec:advanced} |
| 1100 |
|
| 1101 |
\footnotesize |
| 1102 |
|
| 1103 |
\subsection{Adding support for binomial coefficients} |
| 1104 |
\label{ssec:binomial} |
| 1105 |
|
| 1106 |
As will be documented in the section for expert users, it is possible |
| 1107 |
to extend the syntax with one's own operators. |
| 1108 |
|
| 1109 |
Let's turn the semicolon into an operator which computes binomial |
| 1110 |
coefficients: |a;b| will evaluate to ``|a| choose |b|''. The precedence will |
| 1111 |
be chosen stronger than addition and multiplication but less than powers. |
| 1112 |
This only needs adding those two lines to the preamble: |
| 1113 |
\begin{verbatim} |
| 1114 |
\usepackage{xint}% as xintcore does not have \xintiiBinomial |
| 1115 |
\bnumdefinfix{;}{\xintiiBinomial}{15}{15} |
| 1116 |
\end{verbatim} |
| 1117 |
\foreignlanguage{french}{% |
| 1118 |
We can now test it:\newline |
| 1119 |
\bnumdefinfix{\string;}{\xintiiBinomial}{15}{15}% |
| 1120 |
\bneshoweval{100;50} |
| 1121 |
\bneshoweval{3^5;2^5} |
| 1122 |
} |
| 1123 |
|
| 1124 |
\subsection{An expandable macro for computing large Fibonacci numbers} |
| 1125 |
\label{ssec:fibo} |
| 1126 |
|
| 1127 |
% added 2025/09/22 |
| 1128 |
We are going to define an expandable macro which takes on input a positive |
| 1129 |
integer $N$ (we did not bother about case of zero or negative input) and |
| 1130 |
computes the $N$\textup{th} Fibonacci number $F_N$. |
| 1131 |
|
| 1132 |
The algorithm to compute $F_N$ is based upon the quadratic relations which |
| 1133 |
allow to go from $(F_n,F_{n-1})$ to either $(F_{2n}, F_{2n-1})$ (operation of |
| 1134 |
type A) or to $(F_{2n+1}, F_{2n})$ (operation of type B). We first convert |
| 1135 |
$N$ into binary base, and read the digits from most significant to least |
| 1136 |
significant applying each time either type A or type B. Each one of type A or |
| 1137 |
B will cost only 3 multiplications and a bunch of additions, which of course |
| 1138 |
are done using |\bnumexpr|. In total the algorithm costs (because $3\log_2 |
| 1139 |
10\approx 10$) about $10L$ multiplications (doubling is not counted as a |
| 1140 |
multiplication) where $L$ is the number of decimal digits of $N$. Here is the |
| 1141 |
code: |
| 1142 |
\begin{verbatim} |
| 1143 |
\makeatletter |
| 1144 |
\newcommand\Fibonacci[1]{% |
| 1145 |
\expandafter\Fibo@start |
| 1146 |
\romannumeral0\xintdectobin{\the\numexpr#1\relax};% |
| 1147 |
} |
| 1148 |
% To chain \bnumexpr's easily we use the once-expanded form which is |
| 1149 |
% \romannumeral0\bnumexpro. We could also have used \expanded and \bnumeval |
| 1150 |
% calls, that would have been simpler but old habits die hard. |
| 1151 |
% |
| 1152 |
% Gobble the first digit which is 1 (this is where we could have tested for a |
| 1153 |
% negative or zero input). Prepare encapsulated 1 and 0. Then repeatedly |
| 1154 |
% dispatch according to binary digits (which are strategically located at end |
| 1155 |
% of input stream to optimize), or the semicolon which signals completion. |
| 1156 |
% |
| 1157 |
% We use \csname-governed branching (because it is cool). We could have |
| 1158 |
% used some \if test or even an \ifcase with an {-1} at end, or any other |
| 1159 |
% of the many possibilities opened up by TeX syntax. |
| 1160 |
\def\Fibo@start 1#1{% |
| 1161 |
\csname Fibo@#1\expandafter\endcsname |
| 1162 |
\romannumeral0\bnumexpro 1\expandafter\relax\expandafter;% |
| 1163 |
\romannumeral0\bnumexpro 0\relax;% |
| 1164 |
} |
| 1165 |
\@namedef{Fibo@0}#1;#2;#3{% |
| 1166 |
\csname Fibo@#3\expandafter\endcsname |
| 1167 |
\romannumeral0\bnumexpro (#1+2*#2)*#1\expandafter\relax\expandafter;% |
| 1168 |
\romannumeral0\bnumexpro #1*#1+#2*#2\relax;% |
| 1169 |
} |
| 1170 |
\@namedef{Fibo@1}#1;#2;#3{% |
| 1171 |
\csname Fibo@#3\expandafter\endcsname |
| 1172 |
\romannumeral0\bnumexpro 2*(#1+#2)*#1+#2*#2\expandafter\relax\expandafter;% |
| 1173 |
\romannumeral0\bnumexpro (#1+2*#2)*#1\relax;% |
| 1174 |
} |
| 1175 |
% #1 is F_N (and #2 is F_{N-1}) but in need of unpacking. |
| 1176 |
\@namedef{Fibo@;}#1;#2;{\bnethe#1} |
| 1177 |
\makeatother |
| 1178 |
\end{verbatim} |
| 1179 |
|
| 1180 |
\makeatletter |
| 1181 |
\newcommand\Fibonacci[1]{% |
| 1182 |
\expandafter\Fibo@start |
| 1183 |
\romannumeral0\xintdectobin{\the\numexpr#1\relax};% |
| 1184 |
} |
| 1185 |
\def\Fibo@start 1#1{% |
| 1186 |
\csname Fibo@#1\expandafter\endcsname |
| 1187 |
\romannumeral0\bnumexpro 1\expandafter\relax\expandafter;% |
| 1188 |
\romannumeral0\bnumexpro 0\relax;% |
| 1189 |
} |
| 1190 |
\@namedef{Fibo@0}#1;#2;#3{% |
| 1191 |
\csname Fibo@#3\expandafter\endcsname |
| 1192 |
\romannumeral0\bnumexpro (#1+2*#2)*#1\expandafter\relax\expandafter;% |
| 1193 |
\romannumeral0\bnumexpro #1*#1+#2*#2\relax;% |
| 1194 |
} |
| 1195 |
\@namedef{Fibo@1}#1;#2;#3{% |
| 1196 |
\csname Fibo@#3\expandafter\endcsname |
| 1197 |
\romannumeral0\bnumexpro 2*(#1+#2)*#1+#2*#2\expandafter\relax\expandafter;% |
| 1198 |
\romannumeral0\bnumexpro (#1+2*#2)*#1\relax;% |
| 1199 |
} |
| 1200 |
\@namedef{Fibo@;}#1;#2;{\bnethe#1} |
| 1201 |
\makeatother |
| 1202 |
|
| 1203 |
We can now compute for example $F_{10}$, $F_{100}$, $F_{1000}$ and $F_{10000}$: |
| 1204 |
\begin{verbatim} |
| 1205 |
\noindent\textcolor{cyan}{$F_{10}={}$}% |
| 1206 |
\textcolor{digitscolor}{\Fibonacci{10}}\newline |
| 1207 |
\textcolor{cyan}{$F_{100}={}$}% |
| 1208 |
\textcolor{digitscolor}{\Fibonacci{100}}\newline |
| 1209 |
\textcolor{cyan}{$F_{1000}={}$}% |
| 1210 |
\textcolor{digitscolor}{\printnumber{\Fibonacci{1000}}}\newline |
| 1211 |
\textcolor{cyan}{$F_{10000} ={}$}% |
| 1212 |
\textcolor{digitscolor}{\printnumber{\Fibonacci{10000}}} |
| 1213 |
\end{verbatim} |
| 1214 |
\noindent\textcolor{cyan}{$F_{10}={}$}% |
| 1215 |
\textcolor{digitscolor}{\Fibonacci{10}}\newline |
| 1216 |
\textcolor{cyan}{$F_{100}={}$}% |
| 1217 |
\textcolor{digitscolor}{\Fibonacci{100}}\newline |
| 1218 |
\textcolor{cyan}{$F_{1000}={}$}% |
| 1219 |
\textcolor{digitscolor}{\printnumber{\Fibonacci{1000}}}\newline |
| 1220 |
\textcolor{cyan}{$F_{10000} ={}$}% |
| 1221 |
\textcolor{digitscolor}{\printnumber{\Fibonacci{10000}}} |
| 1222 |
\begin{TeXnote} |
| 1223 |
We did not use math mode because our |\printnumber| is a bit dumb and does |
| 1224 |
not work in math mode. See the comment at the end of \autoref{sec:printing}. |
| 1225 |
\end{TeXnote} |
| 1226 |
\begin{TeXnote} |
| 1227 |
In the code above we used |;| as delimiter. But the macro definitions are |
| 1228 |
located in the body of this document, not in the preamble, and |;| is |
| 1229 |
catcode active here due to French being among the languages used by this |
| 1230 |
\LaTeX{} document. This is not a problem in itself for the uses of the |
| 1231 |
semicolon as delimiter for macro definitions. It would be a problem if |
| 1232 |
the semicolon had been used to terminate a \cs{numexpr} (we would then |
| 1233 |
have injected a \cs{relax}). One could also be |
| 1234 |
very worried seeing |\@namedef{Fibo@;}| where |;| will expand in |
| 1235 |
|\csname...\endcsname|, but \ctanpackage{babel} has a built-in protection |
| 1236 |
for such things. This all being said, it is safer to have such definitions |
| 1237 |
located in the preamble of the \LaTeX{} document. But in next section we |
| 1238 |
make a modification which can only be done after the original |\Fibonacci| |
| 1239 |
has already been used (as it changes its interface), so putting all things |
| 1240 |
into the preamble is not always logical nor convenient. |
| 1241 |
\end{TeXnote} |
| 1242 |
|
| 1243 |
\subsection{More fun with Fibonacci numbers} |
| 1244 |
|
| 1245 |
% added 2025/09/27 |
| 1246 |
Our |\Fibonacci|\marg{N} macro expands to compute $F_N$. But what if we want |
| 1247 |
a whole sequence $F_N, F_{N+1}, \dots$? Then using directly the additive |
| 1248 |
recurrence $F_{n+2}=F_{n+1}+F_n$ will be more efficient than computing again |
| 1249 |
and again via our fancy multiplicative algorithm new values. So it is a bit |
| 1250 |
sad that expansion of |\Fibonacci|\marg{N} actually computed both of $F_N$ and |
| 1251 |
$F_{N-1}$ but discarded the latter. We will modify its ending to keep also |
| 1252 |
$F_{N-1}$. And we switch to a down-to-earth style based on assignments. The |
| 1253 |
\xintname documentation provides a variant construct which obtains expandably |
| 1254 |
a sequence of braced items |{F_a}{F_{a+1}}...{F_b}|, but for this, expandable |
| 1255 |
loops from \ctanpackage{xinttools} or from \LaTeX3 are the appropriate tools, |
| 1256 |
and we then need extra tools to handle such output. |
| 1257 |
\begin{verbatim} |
| 1258 |
\makeatletter |
| 1259 |
% reuse \Fibonacci's earlier meaning, then make \Fibonacci undefined |
| 1260 |
% because \Fibo@; will be modified. |
| 1261 |
\let\Fibonacci@\Fibonacci |
| 1262 |
\let\Fibonacci\undefined |
| 1263 |
\newcommand\SetToFibonacciPair[3]{\Fibonacci@{#1}{#2}{#3}} |
| 1264 |
\@namedef{Fibo@;}#1;#2;#3#4{\def#3{#1}\def#4{#2}} |
| 1265 |
\makeatother |
| 1266 |
\end{verbatim} |
| 1267 |
\makeatletter |
| 1268 |
\let\Fibonacci@\Fibonacci |
| 1269 |
\let\Fibonacci\undefined |
| 1270 |
\newcommand\SetToFibonacciPair[3]{\Fibonacci@{#1}{#2}{#3}} |
| 1271 |
\@namedef{Fibo@;}#1;#2;#3#4{\def#3{#1}\def#4{#2}} |
| 1272 |
\makeatother |
| 1273 |
\SetToFibonacciPair{1001}{\FibB}{\FibA} |
| 1274 |
|
| 1275 |
We add the above code to our document and now execute |
| 1276 |
% |
| 1277 |
\centeredline{|\SetToFibonacciPair{1001}{\FibB}{\FibA}|} |
| 1278 |
% |
| 1279 |
Now |\FibB| holds $F_{1001}$ and |\FibA| holds $F_{1000}$ (both being |
| 1280 |
|\bnumexpr|-encapsulated and needing |\bnethe| to get converted to explicit |
| 1281 |
digits). We can use LaTeX's |\loop| to organize the iterative computation and |
| 1282 |
printing of new values. But we first abstract the ``printing'' into a |
| 1283 |
\LaTeX{} ``command''. |
| 1284 |
\begin{verbatim} |
| 1285 |
\newcommand\PrintOneFib[2]{% |
| 1286 |
\noindent\textcolor{cyan}{$F_{#1}={}$}\textcolor{digitscolor}{\printnumber{\bnethe#2}}% |
| 1287 |
} |
| 1288 |
\def\myN{1000} |
| 1289 |
\loop |
| 1290 |
\PrintOneFib{\myN}{\FibA}\newline |
| 1291 |
\edef\FibC{\bnumexpr \FibB + \FibA\relax}% compute and assign a new Fibonacci number |
| 1292 |
\let\FibA\FibB \let\FibB\FibC % update the storage macros |
| 1293 |
\ifnum\myN<1010 % terminate the loop when F_{1010} has been printed |
| 1294 |
\edef\myN{\the\numexpr\myN+1}% |
| 1295 |
\repeat |
| 1296 |
\end{verbatim} |
| 1297 |
\newcommand\PrintOneFib[2]{% |
| 1298 |
\noindent\textcolor{cyan}{$F_{#1}={}$}% |
| 1299 |
\textcolor{digitscolor}{\printnumber{\bnethe#2}}% |
| 1300 |
} |
| 1301 |
\def\myN{1000} |
| 1302 |
\loop |
| 1303 |
\PrintOneFib{\myN}{\FibA}\newline |
| 1304 |
\edef\FibC{\bnumexpr \FibB + \FibA\relax}% |
| 1305 |
\let\FibA\FibB |
| 1306 |
\let\FibB\FibC |
| 1307 |
\ifnum\myN<1010 |
| 1308 |
\edef\myN{\the\numexpr\myN+1}% |
| 1309 |
\repeat |
| 1310 |
|
| 1311 |
At this stage we have printed up to $F_{1010}$. On exit of the loop |\FibA| |
| 1312 |
holds $F_{1011}$ and |\FibB| holds $F_{1012}$. We print them out too: |
| 1313 |
\begin{verbatim} |
| 1314 |
\PrintOneFib{\the\numexpr\myN+1}{\FibA}\newline |
| 1315 |
\PrintOneFib{\the\numexpr\myN+2}{\FibB}\par |
| 1316 |
\end{verbatim} |
| 1317 |
\PrintOneFib{\the\numexpr\myN+1}{\FibA}\newline |
| 1318 |
\PrintOneFib{\the\numexpr\myN+2}{\FibB}\par |
| 1319 |
|
| 1320 |
\makeatletter |
| 1321 |
\@namedef{Fibo@;}#1;#2;{\bnethe#1} |
| 1322 |
\ifnum\pdfstrcmp{\bnethe\FibA}{\Fibonacci@{1011}}=0 \else\ERROR\fi |
| 1323 |
\ifnum\pdfstrcmp{\bnethe\FibB}{\Fibonacci@{1012}}=0 \else\ERROR\fi |
| 1324 |
\makeatother |
| 1325 |
|
| 1326 |
\subsection{The \cs{bnumsetup} command} |
| 1327 |
|
| 1328 |
Package \bnumname needs that some \emph{big integer engine} provides the macros doing |
| 1329 |
the actual computations. |
| 1330 |
|
| 1331 |
By default, it loads package \xintcorename (a subset of \xintexprname) and |
| 1332 |
package \xintbinhexname. |
| 1333 |
\begin{verbatim} |
| 1334 |
\usepackage{xintcore} |
| 1335 |
\usepackage{xintbinhex} |
| 1336 |
\end{verbatim} |
| 1337 |
|
| 1338 |
It then uses |\bnumsetup| in the following way (the final comma is optional, |
| 1339 |
and spaces around equal signs also; there can also be spaces before the commas |
| 1340 |
but the author dislikes such style a lot so they are not used here): |
| 1341 |
\begin{verbatim} |
| 1342 |
\bnumsetup{% |
| 1343 |
add = \xintiiAdd, sub = \xintiiSub, opp = \xintiiOpp, |
| 1344 |
mul = \xintiiMul, pow = \xintiiPow, fac = \xintiiFac, |
| 1345 |
div=\xintiiDivFloor, mod=\xintiiMod, divround=\xintiiDivRound, |
| 1346 |
hextodec=\xintHexToDec, octtodec=\xintOctToDec, bintodec=\xintBinToDec, |
| 1347 |
dectohex=\xintDecToHex, dectooct=\xintDecToOct, dectobin=\xintDecToBin, |
| 1348 |
}% |
| 1349 |
\end{verbatim} |
| 1350 |
|
| 1351 |
One can use |\bnumsetup| to map one, some, or all keys to macros of one's |
| 1352 |
own choosing. Of course it is then up to user to load the suitable packages. |
| 1353 |
|
| 1354 |
If one has alternatives for all of the above \xintcorename macros, so that |
| 1355 |
this package is not needed at all, one can pass option |customcore| to |
| 1356 |
\bnumname at loading time: |
| 1357 |
\centeredline{|\usepackage[customcore]|\{\bnumname\}} This tells to not load |
| 1358 |
\xintcorename. |
| 1359 |
|
| 1360 |
Similarly there is an option |custombinhex| to not load \xintbinhexname. |
| 1361 |
Make sure then to provide suitable replacements to all base conversion macros! |
| 1362 |
|
| 1363 |
Option |custom| means doing both of |customcore| and |custombinhex|. |
| 1364 |
Even under this option package \xintkernelname will always be loaded. |
| 1365 |
|
| 1366 |
Here are the conditions that the custom macros must obey: |
| 1367 |
\begin{enumerate}[noitemsep] |
| 1368 |
\item They all must be \emph{f}-expandable, with some exceptions |
| 1369 |
\begin{enumerate}[noitemsep] |
| 1370 |
\item The macro for computing factorials only has to be \emph{x}-expandable. |
| 1371 |
\item If |\bnumprintonehex| (or |\bnumprintoneoct|, or |\bnumprintonebin|) |
| 1372 |
is redeinfed as a \cs{protected}, then conversion to hexadecimal |
| 1373 |
(resp. octal, resp. binary) only has to be \emph{x}-expandable, except |
| 1374 |
that |
| 1375 |
|[ha]| option will break if the |dectohex| macro is not \emph{f}-expandable. |
| 1376 |
\end{enumerate} |
| 1377 |
Note that any \emph{x}-expandable macro can be wrapped into an |
| 1378 |
\emph{f}-expandable one, using \cs{expanded}. |
| 1379 |
\item It is sufficient for them to be able to handle arguments in \emph{raw |
| 1380 |
normalized form}, i.e., sequences of explicit decimal (or hexadecimal for |
| 1381 |
the macro associated with key |hextodec|) digits, no leading zeros, with at |
| 1382 |
most one minus sign and no plus sign. |
| 1383 |
\item Their output format is limited only by the fact that it should be |
| 1384 |
acceptable input to all the other operators, as well as to the user optional |
| 1385 |
re-definition of |\bnumprintone|. If one cares about hexadecimal (et al.) |
| 1386 |
output one must ensure the macros output format is suitable input for those |
| 1387 |
macros actually doing the conversion from decimal to other bases. |
| 1388 |
\item \emph{Important}: hence if only some macros among those associated to |
| 1389 |
operators (i.e. those by default originating in \xintcorename), or to |
| 1390 |
conversions into decimal, are custom, their output \textbf{must} be produced |
| 1391 |
in \emph{raw normalized form}, as this is the format required by the |
| 1392 |
\xintcorename macros and by the \xintbinhexname macros converting from |
| 1393 |
decimal to other bases. However if one does not care about producing output |
| 1394 |
in binary, octal or hexadecimal (as is the case in the next section), |
| 1395 |
and if one has replaced all \xintcorename macros, the output format can be |
| 1396 |
as one likes. |
| 1397 |
\item If |dectohex| key is used and the associated custom macro produces |
| 1398 |
\emph{lowercase} hexadecimal, then |\bnumeval[h]{...}| produces as |
| 1399 |
instructed hexadecimal in lowercase. Mind that the |[ha]| option is now |
| 1400 |
broken because its internals expect the input to be in uppercase |
| 1401 |
hexadecimal, and this input is computed using the macro assigned to the |
| 1402 |
|dectohex| key. This is of course unimportant because |[h]| now has |
| 1403 |
replaced |[ha]|, but I figured I could mention it here. |
| 1404 |
\end{enumerate} |
| 1405 |
|
| 1406 |
\subsection{Let's handle fractions!} |
| 1407 |
|
| 1408 |
I will show how to transform |\bnumeval| into a calculator with fractions! We |
| 1409 |
will use the \xintfracname macros, but coerce them into always producing |
| 1410 |
fractions in lowest terms (except for powers). For optimization we use |
| 1411 |
the |[0]| post-fix which speeds-up the input parsing by the \xintfracname |
| 1412 |
macros. We remove it on output via a custom |\bnumprintone|. |
| 1413 |
|
| 1414 |
Note that the |/| operator is associated to |divround| key but of course here |
| 1415 |
the used macro will simply do an exact division of fractions, not a rounded-to-% |
| 1416 |
an integer division. This is the whole point of using a macro of our own |
| 1417 |
choosing! |
| 1418 |
\begin{verbatim} |
| 1419 |
\usepackage{xintfrac} |
| 1420 |
\newcommand\myIrrAdd[2]{\xintIrr{\xintAdd{#1}{#2}}[0]} |
| 1421 |
\newcommand\myIrrSub[2]{\xintIrr{\xintSub{#1}{#2}}[0]} |
| 1422 |
\newcommand\myIrrMul[2]{\xintIrr{\xintMul{#1}{#2}}[0]} |
| 1423 |
\newcommand\myDiv[2]{\xintIrr{\xintDiv{#1}{#2}}[0]} |
| 1424 |
\newcommand\myDivFloor[2]{\xintDivFloor{#1}{#2}[0]} |
| 1425 |
\newcommand\myMod[2]{\xintIrr{\xintMod{#1}{#2}}[0]} |
| 1426 |
\newcommand\myPow[2]{\xintPow{#1}{#2}}% will have already postfix [0] |
| 1427 |
\newcommand\myFac[1]{\xintFac{#1}}% will have already postfix [0] |
| 1428 |
\makeatletter |
| 1429 |
\def\myRemovePostFix#1{\@myRemovePostFix#1[0]\relax}% |
| 1430 |
\def\@myRemovePostFix#1[0]#2\relax{#1} |
| 1431 |
\makeatother |
| 1432 |
\let\bnumprintone\myRemovePostFix |
| 1433 |
\bnumsetup{add=\myIrrAdd, sub=\myIrrSub, mul=\myIrrMul, |
| 1434 |
divround=\myDiv, div=\myDivFloor, |
| 1435 |
mod=\myMod, pow=\myPow, fac=\myFac}% |
| 1436 |
\end{verbatim} |
| 1437 |
\begingroup |
| 1438 |
\newcommand\myIrrAdd[2]{\xintIrr{\xintAdd{#1}{#2}}[0]} |
| 1439 |
\newcommand\myIrrSub[2]{\xintIrr{\xintSub{#1}{#2}}[0]} |
| 1440 |
\newcommand\myIrrMul[2]{\xintIrr{\xintMul{#1}{#2}}[0]} |
| 1441 |
\newcommand\myDiv[2]{\xintIrr{\xintDiv{#1}{#2}}[0]} |
| 1442 |
\newcommand\myDivFloor[2]{\xintDivFloor{#1}{#2}[0]} |
| 1443 |
\newcommand\myMod[2]{\xintIrr{\xintMod{#1}{#2}}[0]} |
| 1444 |
\newcommand\myPow[2]{\xintPow{#1}{#2}}% will have already postfix [0] |
| 1445 |
\newcommand\myFac[1]{\xintFac{#1}}% will have already postfix [0] |
| 1446 |
\makeatletter |
| 1447 |
\def\myRemovePostFix#1{\@myRemovePostFix#1[0]\relax}% |
| 1448 |
\def\@myRemovePostFix#1[0]#2\relax{#1} |
| 1449 |
\makeatother |
| 1450 |
\let\bnumprintone\myRemovePostFix |
| 1451 |
\bnumsetup{add=\myIrrAdd, sub=\myIrrSub, mul=\myIrrMul, |
| 1452 |
divround=\myDiv, div=\myDivFloor, |
| 1453 |
mod=\myMod, pow=\myPow, fac=\myFac}% |
| 1454 |
\bneshoweval{1000000*(1/100+1/2^7-20/5^4)/(1/3-5/7+9/11)^2} |
| 1455 |
\bneshoweval{(1-1/2)(1-1/3)(1-1/4)(1-1/5)(1-1/6)(1-1/7)} |
| 1456 |
\bneshoweval{(1-1/3+1/9-1/27-1/81+1/243-1/729+1/2187)^5} |
| 1457 |
\bneshoweval{(1+1/10)^10 /: (1-1/10)^10} |
| 1458 |
\bneshoweval{2^-3^4} |
| 1459 |
\endgroup |
| 1460 |
|
| 1461 |
Computations with fractions quickly give birth to big results, see |
| 1462 |
\autoref{sec:printing} on how to modify |\bnumprintone| to coerce \TeX\ into |
| 1463 |
wrapping numbers too long for the available width. |
| 1464 |
|
| 1465 |
\subsection{For the expert user: expression syntax and its customizability} |
| 1466 |
\label{ssec:expert} |
| 1467 |
|
| 1468 |
\subsubsection{Significant differences between \cs{bnumexpr} and \cs{numexpr}} |
| 1469 |
\label{sec:differences} |
| 1470 |
|
| 1471 |
Apart from the extension to big integers and the added operators, there |
| 1472 |
are a number of important differences between |\bnumexpr| and |
| 1473 |
|\numexpr|: |
| 1474 |
|
| 1475 |
\begin{enumerate}[noitemsep] |
| 1476 |
\item Contrarily to \cs{numexpr}, the |\bnumexpr| parser stops only after having |
| 1477 |
found (and swallowed) a mandatory ending \cs{relax} token (it can arise from expansion), |
| 1478 |
|
| 1479 |
\item In particular note that spaces between digits do not stop |\bnumexpr|, in contrast |
| 1480 |
with \cs{numexpr}: |
| 1481 |
|
| 1482 |
|\the\numexpr 3 5+79\relax| expands (in one step) to \expandafter|\the\numexpr 3 5+79\relax| |
| 1483 |
|
| 1484 |
|\thebnumexpr 3 5+79\relax| expands (in two steps) to |
| 1485 |
\expandafter\expandafter\expandafter|\thebnumexpr 3 5+79\relax| |
| 1486 |
|
| 1487 |
\item With |\edef\myvariable{\bnumexpr 1+2\relax}|, the computation is |
| 1488 |
done at time of the \cs{edef}. It prepares |\myvariable| as a |
| 1489 |
self-contained pre-computed unit which is recognized as such when |
| 1490 |
inserted in a \bnumname expressions. It triggers tacit |
| 1491 |
multiplication: |7\myvariable| is like |7*\myvariable|. This is |
| 1492 |
different from what would happen if we had used |
| 1493 |
|\edef\myvariable{\bnethe\bnumexpr...}| which would simply have |
| 1494 |
|\myvariable| expand to digit tokens so |7\myvariable| then constructs |
| 1495 |
a number with |7| as first digit. |
| 1496 |
|
| 1497 |
\begin{quote} |
| 1498 |
Let's give an example. Note that \cs{edef} has the effect of |
| 1499 |
pre-evaluating. With \cs{def} the outputs would be the same, but the |
| 1500 |
computations would be delayed to |\bnumeval| execution. |
| 1501 |
|
| 1502 |
% attention mon \verb incapable de gérer % |
| 1503 |
\noindent|\edef\x{\bnumexpr 3^10\relax}|% |
| 1504 |
\% precomputes but keeps private format\newline |
| 1505 |
\edef\x{\bnumexpr 3^10\relax}\bneshoweval{10000\x} |
| 1506 |
|
| 1507 |
\noindent|\edef\y{\bnethe\bnumexpr 3^10\relax}|% |
| 1508 |
\% evaluates to explicit digits\newline |
| 1509 |
\edef\y{\bnethe\bnumexpr 3^10\relax}\bneshoweval{10000\y} |
| 1510 |
|
| 1511 |
In the example with |\x|, tacit multiplication applied, whereas in the |
| 1512 |
example with |\y| it is as if the digits had been input by hand in place |
| 1513 |
of |\y|. |
| 1514 |
% |
| 1515 |
Note that the tacit multiplication behaves as expected relative to |
| 1516 |
powers:\newline \bneshoweval{10^10\x} And we certainly do no want to try |
| 1517 |
|10^10\y| which is like |10^10|\expandafter|\y|. |
| 1518 |
\end{quote} |
| 1519 |
|
| 1520 |
There is no analog with \cs{numexpr}: |
| 1521 |
\begin{enumerate}[noitemsep] |
| 1522 |
\item |\edef\foo{\numexpr1+2\relax}| will define |\foo| as |
| 1523 |
|\numexpr1+2\relax| where the calculation is not yet done. |
| 1524 |
\item Inserting the |\foo| as is in the document text causes an error. |
| 1525 |
\item Trying |\the\numexpr 7\foo\relax| with such a |\foo| causes an |
| 1526 |
error. One must use the multiplication sign |*| explicitly. |
| 1527 |
\end{enumerate} |
| 1528 |
|
| 1529 |
\item Expressions may be comma separated. On input, spaces are ignored, |
| 1530 |
and on output the values are comma separated with a space after |
| 1531 |
each comma, |
| 1532 |
|
| 1533 |
\item |\thebnumexpr -(1+1)\relax| is legal contrarily to |\the\numexpr |
| 1534 |
-(1+1)\relax| which errors (also with |-(1)| but |-(1+1)| has a more |
| 1535 |
legitimate use of parentheses), and one has to employ workarounds such as |
| 1536 |
|\the\numexpr 0-(1+1)\relax|, |
| 1537 |
|
| 1538 |
\item |\the\numexpr 2+-(1+1)\relax| (or with |-+|) errors (but |+-| and |-+| |
| 1539 |
if not followed by parentheses do work), whereas |\bnumexpr| has no such |
| 1540 |
counter-intuitive limitation, |
| 1541 |
|
| 1542 |
\item |\the\numexpr 2\cnta\relax| is illegal (with \cs{cnta} a |
| 1543 |
\cs{count}-variable) (note that |\the\dimexpr 2\mydimen\relax| or |
| 1544 |
|\the\dimexpr2\dimen0\relax| do work; even |\the\dimexpr 1\count0\relax| |
| 1545 |
does work, but |\the\dimexpr \count0\relax| errors!). On the other hand |
| 1546 |
|\thebnumexpr 2\cnta\relax| is perfectly legal and does the tacit |
| 1547 |
multiplication; also |\thebnumexpr 2\mydimen\relax| works, with |\mydimen| |
| 1548 |
getting replaced by its value in scaled points, |
| 1549 |
|
| 1550 |
\item |\the\numexpr3(4+5)\relax| stops at the parenthesis, i.e.\@ it expands to |
| 1551 |
|3(4+5)\relax|, whereas of course |\thebnumexpr 3(4+5)\relax| does the |
| 1552 |
computation and expands to |
| 1553 |
|27|, |
| 1554 |
|
| 1555 |
\item |\the\numexpr(4+5)3\relax| expands to |93\relax| whereas |
| 1556 |
|\thebnumexpr(4+5)3\relax| does the computation and expands to |27|, |
| 1557 |
|
| 1558 |
\item |\thebnumexpr (4+5)\count0\relax| expands to nine time the current |
| 1559 |
value of |\count0| register, but with |\the\numexpr| there would be a |
| 1560 |
\emph{Missing number} error, |
| 1561 |
|
| 1562 |
\item Generally speaking |\numexpr| does not implement the tacit |
| 1563 |
multiplication which |\bnumexpr| does in front of parentheses and |
| 1564 |
sub |\numexpr...\relax| expression: or after parentheses, or in-between |
| 1565 |
two parenthesized expressions such as happens with |(1+2)(3+4)|, |
| 1566 |
|
| 1567 |
\item The underscore |_| is accepted within the digits composing a number and |
| 1568 |
is silently ignored by |\bnumexpr|. |
| 1569 |
|
| 1570 |
\end{enumerate} |
| 1571 |
|
| 1572 |
Regarding constructs such as |\edef\myvariable{\bnumexpr 1+2\relax}|, it |
| 1573 |
was explained |\myvariable| behaves then in a special way in another |
| 1574 |
|\bnumexpr| expression (or |\bnumeval|). It is also worth mentioning |
| 1575 |
that it can be used directly in the typesetting stream. But if written |
| 1576 |
to an external file it will expand to some internal format which is not |
| 1577 |
documented as it may vary in future. |
| 1578 |
|
| 1579 |
One can NOT use a |\myvariable| as above in an \cs{ifnum} test, |
| 1580 |
even if representing a single small integer. It will work with syntax |
| 1581 |
such as |\ifnum\bnethe\myvariable=7 ...|. |
| 1582 |
|
| 1583 |
A point of note is that |\bnethe\myvariable| or |
| 1584 |
|\bnethe\bnumexpr...\relax| expand to explicit digits so (assuming here |
| 1585 |
there no other comma separated value computed), |
| 1586 |
\begin{verbatim} |
| 1587 |
\ifnum 3>\bnethe\bnumexpr...\relax |
| 1588 |
... |
| 1589 |
\fi |
| 1590 |
\end{verbatim} |
| 1591 |
is dangerous, because the integer is not properly terminated. |
| 1592 |
Here one could reverse the order, but the simplest way is simply |
| 1593 |
to use |\bnumeval|: |
| 1594 |
\begin{verbatim} |
| 1595 |
\ifnum 3>\bnumeval{...} |
| 1596 |
... |
| 1597 |
\fi |
| 1598 |
\end{verbatim} |
| 1599 |
Now, the end of line space injected by \TeX{} will terminate the integer |
| 1600 |
and make the \cs{ifnum} test safe. |
| 1601 |
|
| 1602 |
\subsubsection{Expression syntax} |
| 1603 |
The implemented syntax is the expected one with infix operators and |
| 1604 |
parentheses, the recognized operators being |+|, |-|, |*|, |/| (rounded |
| 1605 |
division), |^| (power), |**| (power), |//| (by default floored division), |/:| |
| 1606 |
(the associated modulo) and |!| (factorial). |
| 1607 |
|
| 1608 |
One can input hexadecimal numbers as familiar from the \TeX\ number |
| 1609 |
assignments syntax, i.e.\@ using the |"| prefix. But also lowercase letters |
| 1610 |
|abcdef| are accepted in addition to uppercase |ABCDEF| (feature added at |
| 1611 |
|1.7|). Release |1.6| added support for the |0x| prefix. It also added |
| 1612 |
support for octal input via either |'| or |0o| prefixes, and for binary input |
| 1613 |
via |0b| prefix. |
| 1614 |
|
| 1615 |
Commas separating multiple expressions are allowed. The whole expression is |
| 1616 |
handled token by token, any component (digit, operator, parentheses... even |
| 1617 |
the ending \cs{relax}) may arise on the spot from macro expansions. The |
| 1618 |
underscore |_| can be used to separate digits in long numbers, for readability |
| 1619 |
of the input. |
| 1620 |
|
| 1621 |
The precedence rules are as expected and detailed in the next section. |
| 1622 |
Operators on the same level of precedence (like |*|, |/|, |//|, |/:|) behave |
| 1623 |
in a left-associative way, and these examples behave as e.g.\@ with Python |
| 1624 |
analogous operators:\newline \bneshoweval{100//3*4, 100*4//3, 100/:3*4, |
| 1625 |
100*4/:3, 100//3/:5} |
| 1626 |
|
| 1627 |
At |1.5| a change was made to the power operators which became |
| 1628 |
right-associative. Again, this matches the behaviour e.g.\@ of Python:\newline |
| 1629 |
\bneshoweval{2^3^4, 2^(3^4)} |
| 1630 |
|
| 1631 |
It is possible to customize completely the behaviour of the parser, in two ways: |
| 1632 |
\begin{itemize} |
| 1633 |
\item via |\bnumsetup| which has a simple interface to replace the macros |
| 1634 |
associated with |+|, |-|, |*|, |/|, |//|, |/:|, |**|, |^| and |!| by |
| 1635 |
custom macros, |
| 1636 |
\item or even more completely via |\bnumdefinfix| and |\bnumdefpostfix| which |
| 1637 |
allow to add new operators to the syntax! They can also be used to replace |
| 1638 |
the built-in ones or modify their precedence levels. |
| 1639 |
\end{itemize} |
| 1640 |
|
| 1641 |
\subsubsection{Precedences} |
| 1642 |
\label{sssec:prec} |
| 1643 |
|
| 1644 |
The parser implements precedence rules based on concepts which are summarized |
| 1645 |
below. I am providing them for users who will use the customizing macros. |
| 1646 |
\begin{itemize} |
| 1647 |
\item an infix operator has two associated precedence levels, say |L| and |
| 1648 |
|R|, |
| 1649 |
\item the parser proceeds from left to right, pausing each time it has found a |
| 1650 |
new number and an operator following it, |
| 1651 |
\item the parser compares the left-precedence |L| of the new found operator to |
| 1652 |
the right-precedence |R_last| of the last delayed operation (which already |
| 1653 |
has one argument and would like to know if it can use the new found one): if |
| 1654 |
|L| is at most equal to it, the delayed operation is now executed, else the |
| 1655 |
new-found operation is kept around to be executed first, once it will have |
| 1656 |
gathered its arguments, of which only one is known at this stage. |
| 1657 |
\end{itemize} |
| 1658 |
|
| 1659 |
Although there is thus internally all the needed room for sophistication, the |
| 1660 |
implemented table of precedences simply puts all of multiplication and |
| 1661 |
division related operations at the same level, which means that left |
| 1662 |
associativity will apply with these operators. I could see that Python behaves |
| 1663 |
the same way for its analogous operators. |
| 1664 |
|
| 1665 |
Here is the default table of precedences as implemented by the package: |
| 1666 |
\begin{center} |
| 1667 |
\DeleteShortVerb{\|}% |
| 1668 |
\begin{tabular}{|c|c|c|} |
| 1669 |
\multicolumn3{c}{Table of precedences}\\\hline |
| 1670 |
\MakeShortVerb{\|}% |
| 1671 |
operator& left &right\\\hline |
| 1672 |
|+|,|-|& 12&12\\ |
| 1673 |
|*|,|/|,|//|,|/:|& 14&14\\ |
| 1674 |
tacit |*|&16&14\\ |
| 1675 |
|**|, |^|& 18&17\\ |
| 1676 |
|!| &20&n/a\\\hline |
| 1677 |
\end{tabular} |
| 1678 |
\end{center} |
| 1679 |
Tacit multiplication applies in front of parentheses, and after them, also in |
| 1680 |
front of count variables or registers. As shown in the table it has an |
| 1681 |
elevated precedence compared to multiplication explicitly induced by |*|, so |
| 1682 |
|100/4(9)| is computed as |100/36| and not as |25*9|:\newline |
| 1683 |
\bneshoweval{100/4(9), (100/4)9, 1000 // (100/4) 9 (1+1) * 13} |
| 1684 |
More generally |A/B(C)(D)(E)*F| will compute |(A/(B*C*D*E))*F|.% |
| 1685 |
% |
| 1686 |
\footnote{The |B(C)(D)(E)| product will be computed as |B*(C*(D*E))| because |
| 1687 |
the right-precedence of tacit multiplication is |14| but its left-precedence |
| 1688 |
is |16|, creating right associativity. As the underlying mathematical |
| 1689 |
operation is associative this is irrelevant to final result.} |
| 1690 |
|
| 1691 |
The unary |-|, as prefix, has a special behaviour: after an infix operator it |
| 1692 |
will acquire a right-precedence which is the minimum of |12| (i.e.\@ the |
| 1693 |
precedence of addition and subtraction) and of the right-precedence of the |
| 1694 |
infix operator. For example |2^-3^4| will be parsed as |2^(-(3^4))|, raising |
| 1695 |
an error because the parser is by default integer only, but see the section |
| 1696 |
about |\bnumsetup| which explains how to let |\bnumeval| compute fractions! |
| 1697 |
|
| 1698 |
|
| 1699 |
\subsubsection{\cs{bnumdefinfix}} |
| 1700 |
|
| 1701 |
It is possible to define infix binary operators of one's own choosing.% |
| 1702 |
% |
| 1703 |
\footnote{The effect of |\bnumdefinfix| is global if under |\xintglobaldefstrue| setting.} |
| 1704 |
|
| 1705 |
For an example see the \autoref{ssec:binomial} on adding |;| as operator |
| 1706 |
computing binomial coefficients. Other examples will also be given here. |
| 1707 |
|
| 1708 |
The syntax is |
| 1709 |
\centeredline{|\bnumdefinfix|\marg{operator}\marg{\string\macro}\marg{L-prec}\marg{R-prec}} |
| 1710 |
\begin{description} |
| 1711 |
\item[\marg{operator}] The characters for the operator, they may be letters or |
| 1712 |
non-letters. Digits are not allowed to be first or last in \meta{operator}. |
| 1713 |
The following characters are not allowed at all: |\|, |
| 1714 |
\textcolor{smallverbcolor}{\{}, \textcolor{smallverbcolor}{\}}, and |
| 1715 |
{\catcode`\% 12 |%|}. |
| 1716 |
Spaces get removed. |
| 1717 |
|
| 1718 |
\begin{quote} |
| 1719 |
\textbf{Warning:} \meta{operator} will be expanded in an \cs{edef} and then |
| 1720 |
further processed. If it is (or contains) a Babel-active character chances |
| 1721 |
are that the further processing by |\bnumdefinfix| will give breakage. |
| 1722 |
There will be no problem if |\bnumdefinfix| is used in the document |
| 1723 |
preamble. Else, use |\string|, for example: |
| 1724 |
|\bnumdefinfix{\string;}{\xintiiBinomial}{15}{15}|. |
| 1725 |
\end{quote} |
| 1726 |
|
| 1727 |
The underscore and the hash-tag both are allowed, but come with their |
| 1728 |
specific provisos: |
| 1729 |
|
| 1730 |
\begin{quote} |
| 1731 |
The |_| character can be used, but not as first character of the operator, |
| 1732 |
as it would be mis-construed on usage as part of the previous number, and |
| 1733 |
ignored as such. Although |\bnumdefinfix{_}...| will not complain, it |
| 1734 |
will remain ineffective. There must be some other character first. |
| 1735 |
\end{quote} |
| 1736 |
|
| 1737 |
\begin{quote} |
| 1738 |
The |#| character can be used as an operator name or a character in such a |
| 1739 |
name but the definition with |\bnumdefinfix| must be done either with |
| 1740 |
|\string#| or with |####| (or even double that if the definition is done |
| 1741 |
inside the replacement text of a macro; doubling also applies with the |
| 1742 |
|\string| method, one will need |\string##| there) or while having |
| 1743 |
temporarily set the catcode to for example letter or other. |
| 1744 |
|
| 1745 |
Usage can be done without any special precaution (but as usual |#| |
| 1746 |
may need doubling if in the replacement body of some macro definition). |
| 1747 |
\end{quote} |
| 1748 |
|
| 1749 |
\item[\marg{\string\macro}] The expandable macro (expecting two mandatory |
| 1750 |
arguments) which is to assign to the infix operator. This macro must be |
| 1751 |
\emph{f}-expandable. Also it must (if the default package configuration is |
| 1752 |
not modified for the core operators) produce integers in the ``strict'' |
| 1753 |
format which is expected by the \xintcorename macros for arithmetic: no |
| 1754 |
leading zeros, at most one minus sign, no plus sign, no spaces. |
| 1755 |
\item[\marg{L-prec}] An integer, minimal |4|, maximal |22|, which governs |
| 1756 |
the left-\hskip0pt precedence of the infix operator. |
| 1757 |
\item[\marg{R-prec}] An integer, minimal |4|, maximal |22|, which governs the |
| 1758 |
right-\hskip0pt precedence of the infix operator. |
| 1759 |
\end{description} |
| 1760 |
Generally, the two precedences are set to the same value. |
| 1761 |
|
| 1762 |
Once a multi-character operator is defined, the first characters of its name |
| 1763 |
can be used if no ambiguity. In case of ambiguity, it is the earliest defined |
| 1764 |
shortcut which prevails, except for the full name. So for example if |$abc| |
| 1765 |
operator is defined, and |$ab| is defined next, then |$| and |$a| will still |
| 1766 |
serve as shortcuts to the original |$abc|, but |$ab| will refer to the newly |
| 1767 |
defined operator. |
| 1768 |
|
| 1769 |
Fully qualified names are never ambiguous, and a shortcut once defined will |
| 1770 |
change meaning only under two circumstances: |
| 1771 |
\begin{itemize} |
| 1772 |
\item it is re-defined as the full name of a new operator, |
| 1773 |
\item the original operator to which the shortcut refers is defined again; then |
| 1774 |
the shortcut is automatically updated to point to the new meaning. |
| 1775 |
\end{itemize} |
| 1776 |
\begin{verbatim} |
| 1777 |
\def\equals#1#2{\ifnum\pdfstrcmp{#1}{#2}=0 \expandafter1\else |
| 1778 |
\expandafter0\fi} |
| 1779 |
% or: |
| 1780 |
\def\equals#1#2{\expanded{\ifnum\pdfstrcmp{#1}{#2}=0 1\else0\fi}} |
| 1781 |
\def\differ#1#2{\expanded{\ifnum\pdfstrcmp{#1}{#2}=0 0\else1\fi}} |
| 1782 |
\bnumdefinfix{==}{\equals}{10}{10} |
| 1783 |
\bnumdefinfix{!=}{\differ}{10}{10} |
| 1784 |
\bnumdefinfix{times}{\xintiiMul}{14}{14} |
| 1785 |
\bnumdefinfix{++}{\xintiiAdd}{19}{19} |
| 1786 |
\end{verbatim} |
| 1787 |
%\def\equals#1#2{\ifnum\pdfstrcmp{#1}{#2}=0 \expandafter1\else\expandafter0\fi} |
| 1788 |
\def\equals#1#2{\expanded{\ifnum\pdfstrcmp{#1}{#2}=0 1\else0\fi}} |
| 1789 |
\def\differ#1#2{\expanded{\ifnum\pdfstrcmp{#1}{#2}=0 0\else1\fi}} |
| 1790 |
\bnumdefinfix{==}{\equals}{10}{10} |
| 1791 |
\bnumdefinfix{!=}{\differ}{10}{10} |
| 1792 |
\bnumdefinfix{times}{\xintiiMul}{14}{14} |
| 1793 |
\bnumdefinfix{++}{\xintiiAdd}{19}{19} |
| 1794 |
% |
| 1795 |
\bneshoweval{2 + 3! = 5, 2 + (3!) == 8} Notice in the |2+3! = 5| example that |
| 1796 |
the existence of |!=| prevails on applying the factorial, so this is test |
| 1797 |
whether |2+3| and |5| differ; it is not a matter of precedence here, but of |
| 1798 |
input parsing ignoring spaces. And |2+3! == 8| would create an error as after |
| 1799 |
having found the |!=| operator and now expecting a digit (as there is no |!==| |
| 1800 |
operator) the parser would find an unexpected |=| and report an error. Hence |
| 1801 |
the usage of parentheses in the input.% |
| 1802 |
% |
| 1803 |
\footnote{With \xintexprname, whose \cs{xinteval} has a |!=| operator, |
| 1804 |
|2+3!==8| is interpreted automatically as |2+(3!)==8|, thanks to internal |
| 1805 |
work-around added at |1.4g|. This has not been backported to \bnumname as |
| 1806 |
it does not per default support operators such as |!=| or |==| and only has |
| 1807 |
generic support for adding multi-character operators. |
| 1808 |
|
| 1809 |
Regarding |2 + 3! = 5|, trying to let this be interpreted as |2+(3!)=5| |
| 1810 |
makes sense only if a |=| operator has been defined. If no |!=| operator |
| 1811 |
exists, the magic will be automatic. If however both |=| and |!=| exist, |
| 1812 |
then it would need special overhead to the parser dealings when finding |!| |
| 1813 |
to avoid the |!=| interpretation. One could imagine distinguishing |! =| |
| 1814 |
from |!=| but the swallowing of spaces is deeply coded in the parser. As |
| 1815 |
\bnumname default infix operators do not include one starting with |!|, it is not |
| 1816 |
worth it to include in the package extra overhead to solve such issues when |
| 1817 |
extending the syntax. At the level of \xintexprname, there is no issue |
| 1818 |
because there is no |=| operator.} |
| 1819 |
% RAPPEL j'ai dû utiliser à un moment \pagebreak[3] ici. Encore un |
| 1820 |
% comportement inattendu et sous-optimal de TeX/LaTeX. |
| 1821 |
\newline |
| 1822 |
\noindent\bneshoweval{2^5 == 4 times 8, 11 t 14} |
| 1823 |
\bneshoweval{100 ++ -10 ^ 3, (100 - 10)^3, 2 ^ 5 ++ 3, 2^(5+3)} |
| 1824 |
|
| 1825 |
|
| 1826 |
\subsubsection{\cs{bnumdefpostfix}} |
| 1827 |
|
| 1828 |
It is possible to define postfix unary operators of one's own choosing.% |
| 1829 |
% |
| 1830 |
\footnote{The effect of |\bnumdefpostfix| is global if under |\xintglobaldefstrue| setting.} |
| 1831 |
% |
| 1832 |
The syntax is |
| 1833 |
\centeredline{|\bnumdefpostfix|\marg{operator}\marg{\string\macro}\marg{L-prec}} |
| 1834 |
|
| 1835 |
\begin{description} |
| 1836 |
\item[\marg{operator}] The characters for the operator name: same |
| 1837 |
conditions as for |\bnumdefinfix|. Postfix and infix operators share the |
| 1838 |
same name-space, regarding abbreviated names. |
| 1839 |
\begin{quote} |
| 1840 |
\textbf{Warning:} \meta{operator} will be expanded in an \cs{edef} and then |
| 1841 |
further processed. If it is (or contains) a Babel-active character chances |
| 1842 |
are that the further processing by |\bnumdefpostfix| will give breakage. |
| 1843 |
There will be no problem if |\bnumdefpostfix| is used in the document |
| 1844 |
preamble. |
| 1845 |
\end{quote} |
| 1846 |
|
| 1847 |
\item[\marg{\string\macro}] The one argument expandable macro to assign to |
| 1848 |
the postfix operator. This macro only needs to be \emph{x}-expandable. |
| 1849 |
\item[\marg{L-prec}] An integer, minimal |4|, maximal |22|, which governs the |
| 1850 |
left-\hskip0pt precedence of the infix operator. |
| 1851 |
\end{description} |
| 1852 |
|
| 1853 |
Examples below which use the maximal precedence are typical of what is |
| 1854 |
expected of a ``function'' (and I even used |.len()| notation with parentheses |
| 1855 |
in one example, the parentheses are part of the postfix operator name). And |
| 1856 |
indeed such postfix operators are thus a way to implement functions in |
| 1857 |
disguise, circumventing the fact that the \bnumname parser will never be |
| 1858 |
extended to work with functional syntax (for this, see \xintexprname). With |
| 1859 |
the convention (followed in some examples) that such postfix operators start |
| 1860 |
with a full stop, but never contain another one, we can chain simply by using |
| 1861 |
concatenation (no need for parentheses), as there will be no |
| 1862 |
ambiguity. |
| 1863 |
\begin{verbatim} |
| 1864 |
\usepackage{xint}% for \xintiiSum, \xintiiSqrt |
| 1865 |
\def\myRev#1{\xintNum{\xintReverseOrder{#1}}}% reverse and trim leading zeros |
| 1866 |
\bnumdefpostfix{$}{\myRev}{22}% the $ will have top precedence |
| 1867 |
\bnumdefpostfix{:}{\myRev}{4}% the : will have lowest precedence |
| 1868 |
\bnumdefpostfix{::}{\xintiiSqr}{4}% the :: is a completely different operator |
| 1869 |
\bnumdefpostfix{.len()}{\xintLength}{22}% () for fun but a single . will be enough! |
| 1870 |
\bnumdefpostfix{.sumdigits}{\xintiiSum}{22}% .s will abbreviate |
| 1871 |
\bnumdefpostfix{.sqrt}{\xintiiSqrt}{22}% .sq will be unambiguous (but confusing) |
| 1872 |
\bnumdefpostfix{.rep}{\xintReplicate3}{22}% .r will be unambiguous |
| 1873 |
\end{verbatim} |
| 1874 |
\def\myRev#1{\xintNum{\xintReverseOrder{#1}}}% reverse digits and trim leading zeros |
| 1875 |
\bnumdefpostfix{$}{\myRev}{22}% $ |
| 1876 |
\bnumdefpostfix{:}{\myRev}{4}% |
| 1877 |
\bnumdefpostfix{::}{\xintiiSqr}{4}% |
| 1878 |
\bnumdefpostfix{.len()}{\xintLength}{22}% |
| 1879 |
\bnumdefpostfix{.sumdigits}{\xintiiSum}{22}% |
| 1880 |
\bnumdefpostfix{.sqrt}{\xintiiSqrt}{22}% |
| 1881 |
\bnumdefpostfix{.rep}{\xintReplicate3}{22}% |
| 1882 |
\bneshoweval{(2^31).len(), (2^31)., 2^31$, 2^31:, (2^31)$} |
| 1883 |
\bneshoweval{(2^31).sqrt, 100000000.sq.sq} |
| 1884 |
\bneshoweval{(2^31).sumdigits, 123456789.s, 123456789.s.s, 123456789.s.s.s} |
| 1885 |
\bneshoweval{10^10+10000+2000+300+40+5:} |
| 1886 |
\bneshoweval{1+2+3+4+5+6+7+8+9+10 :: +1 :: *2 :: :: :} |
| 1887 |
\bneshoweval{123456789.r}\par\vspace*{-\baselineskip}% Toujours LaTeX avec ses |
| 1888 |
% espaces verticaux TROP GRANDS DEPUIS 30 ANS |
| 1889 |
\begin{verbatim} |
| 1890 |
\bnumdefpostfix{.rep}{\xintReplicate5}{22}% .rep modified --> .r too |
| 1891 |
\end{verbatim} |
| 1892 |
\bnumdefpostfix{.rep}{\xintReplicate5}{22}% |
| 1893 |
\bneshoweval{123456789.r} |
| 1894 |
|
| 1895 |
|
| 1896 |
\normalsize |
| 1897 |
|
| 1898 |
\section{Changes} |
| 1899 |
|
| 1900 |
\begin{description} |
| 1901 |
\input bnumexprchanges.tex |
| 1902 |
\end{description} |
| 1903 |
|
| 1904 |
\section{License} |
| 1905 |
|
| 1906 |
\begingroup\makeatletter |
| 1907 |
\baselineskip10pt |
| 1908 |
\begin{verbatim} |
| 1909 |
Copyright © 2014-2022, 2025 Jean-François Burnol |
| 1910 |
|
| 1911 |
| This Work may be distributed and/or modified under the |
| 1912 |
| conditions of the LaTeX Project Public License 1.3c. |
| 1913 |
| This version of this license is in |
| 1914 |
|
| 1915 |
> <http://www.latex-project.org/lppl/lppl-1-3c.txt> |
| 1916 |
|
| 1917 |
| and version 1.3 or later is part of all distributions of |
| 1918 |
| LaTeX version 2005/12/01 or later. |
| 1919 |
|
| 1920 |
This Work has the LPPL maintenance status "author-maintained". |
| 1921 |
|
| 1922 |
The Author and Maintainer of this Work is Jean-François Burnol. |
| 1923 |
|
| 1924 |
This Work consists of the main source file and its derived files |
| 1925 |
|
| 1926 |
bnumexpr.dtx, bnumexpr.sty, bnumexpr.pdf, bnumexpr.tex, |
| 1927 |
bnumexprchanges.tex, README.md |
| 1928 |
\end{verbatim} |
| 1929 |
\endgroup |
| 1930 |
|
| 1931 |
\normalsize |
| 1932 |
\StopEventually{\end{document}\endinput} |
| 1933 |
|
| 1934 |
% workaround |
| 1935 |
% https://github.com/latex3/latex2e/issues/563 |
| 1936 |
\makeatletter |
| 1937 |
\AddToHook{env/macrocode/begin}{\partopsep0pt\relax} |
| 1938 |
\AddToHook{env/macrocode/after}{\@nobreakfalse} |
| 1939 |
\makeatother |
| 1940 |
\newgeometry{hscale=0.75,vscale=0.75}% ATTENTION \newgeometry fait |
| 1941 |
% un reset de vscale si on ne le |
| 1942 |
% précise pas ici !!! |
| 1943 |
|
| 1944 |
\MakePercentIgnore |
| 1945 |
\AddToHook{env/macrocode/begin}{\bfseries} |
| 1946 |
% |
| 1947 |
% \catcode`\<=0 \catcode`\>=11 \catcode`\*=11 \catcode`\/=11 |
| 1948 |
% \let</dtx>\relax |
| 1949 |
% \def<*package>{\catcode`\<=12 \catcode`\>=12 \catcode`\*=12 \catcode`\/=12 } |
| 1950 |
%</dtx> |
| 1951 |
%<*package> |
| 1952 |
% \etocdepthtag.toc{code} |
| 1953 |
% \section{Commented source code} |
| 1954 |
% \label{sec:bnumexprcode} |
| 1955 |
% \etocdefaultlines |
| 1956 |
% \etocignoredepthtags |
| 1957 |
% \etocsetnexttocdepth{subsection} |
| 1958 |
% \localtableofcontents |
| 1959 |
% \etocmarkbothnouc{\bnumnameuserdoc \hyperref[sec:bnumexprcode]{implementation}} |
| 1960 |
% |
| 1961 |
% At |1.7|, support for lowercase hexadecimal was added. |
| 1962 |
% |
| 1963 |
% At |1.6|, |\bnumeval| requires the |1.4n| release of \xintcorename and |
| 1964 |
% \xintbinhexname (or at least of \xintkernelname if option |custom| is used). |
| 1965 |
% It adds |0b|, |0o|, |'|, and |0x| to the syntax, and |
| 1966 |
% admits optional parameters |[b]|, |[o]|, and |[h]| to produce the |
| 1967 |
% output converted to binary, octal, or hexadecimal. |
| 1968 |
% |
| 1969 |
% It is amusing that implementing the support for the optional argument |
| 1970 |
% had the un-anticipated corollary that Babel active characters (such as |
| 1971 |
% |!| with French) are auto-taming. See the code comments. |
| 1972 |
% |
| 1973 |
% A problem with |_| if upfront in numbers was fixed. |
| 1974 |
% |
| 1975 |
% There was some refactoring, relative to extending |\bnumsetup| with new keys |
| 1976 |
% related to base conversion macros and this lead to the removal of |
| 1977 |
% \cs{bnumprintonetohex} and \cs{bnumhextodec}. |
| 1978 |
% |
| 1979 |
% At |1.5|, right-associativity was enforced for powers in anticipation of |
| 1980 |
% upstream \xintexprname |1.4g 2021/05/25|, and the customizability and |
| 1981 |
% extendibility of the package is made total via added |\bnumdefinfix| and |
| 1982 |
% |\bnumdefpostfix|. |
| 1983 |
% |
| 1984 |
% |
| 1985 |
% \footnotesize |
| 1986 |
% |
| 1987 |
% Older comments at time of |1.4| and |1.4a| releases: |
| 1988 |
% |
| 1989 |
% I transferred mid-May 2021 from \xintexprname its \cs{expanded} based |
| 1990 |
% infra-structure from its own |1.4| release of January 2020 and bumped |
| 1991 |
% version to |1.4|. Also I added support for hexadecimal input and output, via |
| 1992 |
% \xintbinhexname. |
| 1993 |
% |
| 1994 |
% A few comments added here at |1.4a|: |
| 1995 |
% \begin{itemize} |
| 1996 |
% \item It looked a bit costly and probably would have been mostly useless to |
| 1997 |
% end users to integrate in \bnumname support for nested structures via |
| 1998 |
% square brackets [, ], which is in \xintexprname since its January 2020 |
| 1999 |
% |1.4| release. But some of the related architecture remains here; we |
| 2000 |
% could make some gains probably but diverging from upstream code |
| 2001 |
% would make maintenance a nightmare. |
| 2002 |
% \item Formerly, the |\csname...\endcsname| encapsulation technique had the |
| 2003 |
% after-effect to allow the macros supporting the infix operators to be only |
| 2004 |
% \emph{x}-expandable. At |1.4|, I could have still allowed support macros |
| 2005 |
% being only \emph{x}-expandable, but, keeping in sync with upstream, I have |
| 2006 |
% used only a \cs{romannumeral} trigger and did not insert an \cs{expanded}, so |
| 2007 |
% now the support macros must be \emph{f}-expandable. The |1.4a| release |
| 2008 |
% fixes the related user documentation of |\bnumsetup| which was not |
| 2009 |
% updated at |1.4|. The support macro for the factorial however needs only |
| 2010 |
% be \emph{x}-expandable. |
| 2011 |
% \item Also, I simply do not understand why the legacy (|1.2e|) user documentation |
| 2012 |
% said that the support macros were supposed to \emph{f}-expand their arguments, as |
| 2013 |
% they are used only with arguments being explicit digit tokens (and |
| 2014 |
% optional minus sign). |
| 2015 |
% \item The |\bnumexpr\relax| syntax creating an empty ople is by itself now |
| 2016 |
% legal, and can be injected (comma separated) in an expression, keeping it |
| 2017 |
% invariant, however |\bnumeval{}| ends in a |
| 2018 |
% |Paragraph ended before \BNE_print_c was complete| |
| 2019 |
% error because |\BNEprint| makes the tacit requirement |
| 2020 |
% that the 1D ople to output has at least one item. |
| 2021 |
% \end{itemize} |
| 2022 |
% |
| 2023 |
% \normalsize |
| 2024 |
% \subsection{Package identification} |
| 2025 |
% \begin{macrocode} |
| 2026 |
\NeedsTeXFormat{LaTeX2e}% |
| 2027 |
\ProvidesPackage{bnumexpr}[2025/09/27 v1.7b Expressions with big integers (JFB)]% |
| 2028 |
% \end{macrocode} |
| 2029 |
% \subsection{Load \xintkernelname} |
| 2030 |
% At |1.6|, in order to make the base conversion macros also customizable, |
| 2031 |
% hence not mandate loading of \xintbinhexname, we only load unconditionally |
| 2032 |
% \xintkernelname. |
| 2033 |
% |
| 2034 |
% We then switch to the familiar catcode regime of the \xintexprname sources. |
| 2035 |
% \begin{macrocode} |
| 2036 |
\RequirePackage{xintkernel}[2025/09/05]% |
| 2037 |
% \end{macrocode} |
| 2038 |
% \subsection{Save catcode regime and switch to our own} |
| 2039 |
% \begin{macrocode} |
| 2040 |
\edef\BNErestorecatcodesendinput{\XINTrestorecatcodes\noexpand\endinput}% |
| 2041 |
\XINTsetcatcodes% |
| 2042 |
% \end{macrocode} |
| 2043 |
% \subsection{Load optionally \xintcorename and \xintbinhexname} |
| 2044 |
% |1.6| adds |customcore| as alias of legacy |custom|. It adds |custombinhex| |
| 2045 |
% to add possibility of not loading \xintbinhexname either. |
| 2046 |
% Option |custom| now means both of |customcore| and |custombinhex|. |
| 2047 |
% |
| 2048 |
% But who on Earth isn't going to use with delight both my \xintcorename and |
| 2049 |
% \xintbinhexname? |
| 2050 |
% \begin{macrocode} |
| 2051 |
\def\BNE_tmpa{1}\def\BNE_tmpb{1}% |
| 2052 |
\DeclareOption{custom}{\def\BNE_tmpa{0}\def\BNE_tmpb{0}}% |
| 2053 |
\DeclareOption{customcore}{\def\BNE_tmpa{0}}% |
| 2054 |
\DeclareOption{custombinhex}{\def\BNE_tmpb{0}}% |
| 2055 |
\ProcessOptions\relax |
| 2056 |
\if1\BNE_tmpa\RequirePackage{xintcore}[2025/09/05]\fi |
| 2057 |
\if1\BNE_tmpb\RequirePackage{xintbinhex}[2025/09/05]\fi |
| 2058 |
% \end{macrocode} |
| 2059 |
% \subsection{\cs{bnumsetup}} |
| 2060 |
% |\bnumsetup| is the new name at |1.5| of \cs{bnumexprsetup}. |
| 2061 |
% The old name was kept as an alias at |1.5|, and deleted at |1.6|. |
| 2062 |
% |
| 2063 |
% Note that a final comma will cause no harm. |
| 2064 |
% \begin{macrocode} |
| 2065 |
\catcode`! 3 |
| 2066 |
\def\bnumsetup #1{\BNE_parsekeys #1,=!,}% |
| 2067 |
\def\BNE_parsekeys #1=#2#3,% |
| 2068 |
{% |
| 2069 |
\ifx!#2\expandafter\BNE_parsedone\fi |
| 2070 |
\XINT_global |
| 2071 |
\expandafter |
| 2072 |
\let\csname BNE_Op_\xint_zapspaces #1 \xint_gobble_i\endcsname% |
| 2073 |
=#2% |
| 2074 |
\ifxintverbose |
| 2075 |
\PackageInfo{bnumexpr}{assigned |
| 2076 |
\ifxintglobaldefs globally \fi |
| 2077 |
\string#2 to \xint_zapspaces #1 \xint_gobble_i\MessageBreak |
| 2078 |
% \end{macrocode} |
| 2079 |
% Workaround for the space inserted by \cs{on@line}. |
| 2080 |
% \begin{macrocode} |
| 2081 |
\expandafter\xint_firstofone}% |
| 2082 |
\fi |
| 2083 |
\BNE_parsekeys |
| 2084 |
}% |
| 2085 |
\def\BNE_parsedone #1\BNE_parsekeys {}% |
| 2086 |
\catcode`! 12 |
| 2087 |
% \end{macrocode} |
| 2088 |
% Final comma and spaces are only to check if it does work. But I will |
| 2089 |
% NOT insert spaces before commas, even though they are allowed! |
| 2090 |
% |
| 2091 |
% |1.6| also handles base conversion macros here. Prior to |1.6| this |
| 2092 |
% |\bnumsetup| configuration was not executed if package received option |
| 2093 |
% |custom| (now |customcore|). But as the user is then responsible for |
| 2094 |
% redefining all keys, why bother. |
| 2095 |
% \begin{macrocode} |
| 2096 |
\bnumsetup{% |
| 2097 |
add = \xintiiAdd, sub = \xintiiSub, opp = \xintiiOpp, |
| 2098 |
mul = \xintiiMul, pow = \xintiiPow, fac = \xintiiFac, |
| 2099 |
div = \xintiiDivFloor, mod = \xintiiMod, divround = \xintiiDivRound, |
| 2100 |
hextodec=\xintHexToDec, octtodec=\xintOctToDec, bintodec=\xintBinToDec, |
| 2101 |
dectohex=\xintDecToHex, dectooct=\xintDecToOct, dectobin=\xintDecToBin, |
| 2102 |
}% |
| 2103 |
% \end{macrocode} |
| 2104 |
% By the way the keys should have been |Add|, |Sub|, \dots, not |add|, |sub|, |
| 2105 |
% \dots, so internally |\BNE_Op_Add| etc\dots\ would have been the macros |
| 2106 |
% defined by |\bnumsetup| and used in the code, not |\BNE_Op_add| (et al.) |
| 2107 |
% whose casing does not match my naming conventions. |
| 2108 |
% |
| 2109 |
% \subsection{Some extra constants needed for user defined precedences} |
| 2110 |
% |
| 2111 |
% For the mechanism of |\bnumdefinfix| we need precedence levels to be |
| 2112 |
% available as \cs{chardef}'s. \xintkernelname already provides 0-10, 12, 14, |
| 2113 |
% 16, 17, 18, 20, 22. |
| 2114 |
% |
| 2115 |
% Left levels need to be represented by one token; right levels are |
| 2116 |
% hard-coded into |checkp_<op>| macros and could have been there explicit |
| 2117 |
% digit tokens but we will use the |\xint_c_...| \cs{char}-tokens. |
| 2118 |
% \begin{macrocode} |
| 2119 |
\chardef\xint_c_xi 11 |
| 2120 |
\chardef\xint_c_xiii 13 |
| 2121 |
\chardef\xint_c_xv 15 |
| 2122 |
\chardef\xint_c_xix 19 |
| 2123 |
\chardef\xint_c_xxi 21 |
| 2124 |
% \end{macrocode} |
| 2125 |
% \subsection{\cs{bnumexpr}, \cs{bnethe}, \cs{bnumeval}} |
| 2126 |
% |
| 2127 |
% |\XINTfstop| has to be the same as defined in \xintexprname, in order for a |
| 2128 |
% subexpression |\xintiiexpr...\relax| to get recognized in |\bnumeval| or |
| 2129 |
% conversely for |\bnumexpr...\relax| to possibly serve inside an |
| 2130 |
% |\xinteval|. But why use \bnumname then? Besides a sub \xintexprname-ession |
| 2131 |
% will break |\bnumeval| if it is anything else than a 1D flat sequence. And |
| 2132 |
% even then it can work only if internal storage format are kept in sync. |
| 2133 |
% |
| 2134 |
% |1.6| deprecates \cs{evaltohex} (and |1.7a| removed it) and has |
| 2135 |
% |\bnumeval||[h]| as equivalent replacement. |
| 2136 |
% |
| 2137 |
% The \cs{protected} |\BNEprint| will survive to |\bnumexpr| being expanded in |
| 2138 |
% a \cs{write} or \cs{edef}. But its expansion will be forced by the |
| 2139 |
% \cs{expanded} from |\bnethe|. |
| 2140 |
% |
| 2141 |
% I now really dislike |\thebnumexpr| macro name and at some point had |
| 2142 |
% replaced it with |\bnumtheexpr| but this got reverted. |
| 2143 |
% |
| 2144 |
% |1.6a| uses the strange |\csname| in place of directly |\BNE_wrap| |
| 2145 |
% in order to fix the |1.6| blunder which had done a similar thing, |
| 2146 |
% but too late. This is to tame Babel active characters. The |\bnumeval| |
| 2147 |
% was OK, though. Sadly the blunder was first done in \xintexprname, |
| 2148 |
% after I had reverted perfectly valid implementation there, having |
| 2149 |
% thought I could apply a shortcut, which was simply a brain fault. |
| 2150 |
% And I backported it here... alas... |
| 2151 |
% \begin{macrocode} |
| 2152 |
\def\XINTfstop {\noexpand\XINTfstop}% |
| 2153 |
\def\bnumexpr {\romannumeral0\bnumexpro}% |
| 2154 |
\def\bnumexpro {\csname BNE_wrap\expandafter\endcsname |
| 2155 |
\romannumeral0\BNE_bareeval}% |
| 2156 |
% \end{macrocode} |
| 2157 |
% While preparing |1.6| I wondered why the ``|.|'' after |\BNEprint| in |
| 2158 |
% |\BNE_wrap| which is then gobbled by |\BNEprint|. It was clear it came from |
| 2159 |
% \xintexprname, but why was it kept here? |
| 2160 |
% |
| 2161 |
% The reason is to support having a sub |\bnumexpr...\relax| inside |
| 2162 |
% |\bnumeval| or |\xinteval|. Indeed such a sub-expression is identified via |
| 2163 |
% the presence of the |\XINTfstop| after its expansion, and the code inside |
| 2164 |
% \bnumname handling this is inherited from \xintexprname, so it expects the |
| 2165 |
% structure |\XINTfstop| then a ``print'' macro, then possibly some stuff |
| 2166 |
% delimited by a full stop (this is related to the implementation of the |
| 2167 |
% optional arguments of |\xintfloateval| and |\xintieval|). |
| 2168 |
% |
| 2169 |
% As we keep this stuff handled the same way we must inject the seemingly |
| 2170 |
% silly full stop here for |\bnumexpr...\relax| (or a macro defined from it |
| 2171 |
% via an \cs{edef}) to be usable inside |\bnumval| or another |
| 2172 |
% |\bnumexpr...\relax|. |
| 2173 |
% \begin{macrocode} |
| 2174 |
\def\BNE_wrap {\XINTfstop\BNEprint.}% |
| 2175 |
% \end{macrocode} |
| 2176 |
% It is important to keep in mind that |#1| has the structure |
| 2177 |
% \{|{...}{...}...{...}|\} with an external brace pair, which here gets |
| 2178 |
% removed. ^^A attention que mon |...| va retirer une paire d'accolades. |
| 2179 |
% In the replacement the external \{|...|\} are for \cs{expanded}. |
| 2180 |
% |
| 2181 |
% We also define a non \cs{protected} variant without the strange extra full |
| 2182 |
% stop, it will serve for |\bnumeval| (and |\thebnumexpr|) and thus does not |
| 2183 |
% need it. |
| 2184 |
% \begin{macrocode} |
| 2185 |
\protected\def\BNEprint.#1{{\BNE_unpack#1.}}% |
| 2186 |
\def\BNEprint_#1{{\BNE_unpack#1.}}% |
| 2187 |
% \end{macrocode} |
| 2188 |
% |\bnethe| removes the |\XINTfstop| and activates the printing via |
| 2189 |
% |\BNEprint|. |
| 2190 |
% |
| 2191 |
% Attention that prior to |1.6| |\bnethe| grabbed a |#1|, hence would |
| 2192 |
% work to print a braced |\bnumexpr...\relax|, but I don't see the reason |
| 2193 |
% for doing that. Removed. |
| 2194 |
% |
| 2195 |
% |1.6a| modifies |\thebnumexpr| here for the Babel active thing. |
| 2196 |
% \begin{macrocode} |
| 2197 |
\def\bnethe{\expanded\expandafter\xint_gobble_i\romannumeral`&&@}% |
| 2198 |
\def\thebnumexpr{\expanded\csname BNEprint_\expandafter\endcsname |
| 2199 |
\romannumeral0\BNE_bareeval}% |
| 2200 |
% \end{macrocode} |
| 2201 |
% At |1.6| after implementing the |[h]| optional argument of |\bnumeval|, |
| 2202 |
% there was the un-anticipated result that this tamed Babel active characters. |
| 2203 |
% This is explained by the expansion happening while a \cs{csname} is not yet |
| 2204 |
% closed. And by the fact that during its expansion |\bnumeval| does not |
| 2205 |
% use delimited macros, for example to fetch up to a closing parenthesis. |
| 2206 |
% |
| 2207 |
% There used to be here a |\BNE_start| but it got replaced by its expansion. |
| 2208 |
% |
| 2209 |
% The |\BNE_check| is defined in the section \hyperref[ssec:span]{``Expansion |
| 2210 |
% spanning''}. |
| 2211 |
% |
| 2212 |
% Prior to |1.6| |\BNE_bareeval| was named \cs{bnebareeval}, but this was |
| 2213 |
% outside of the package namespace (it should have been \cs{bnumbareeval}, or |
| 2214 |
% \cs{bnumexprbareeval}). Upstream has |\xintbareeval| without underscores |
| 2215 |
% for legacy reasons. |
| 2216 |
% \begin{macrocode} |
| 2217 |
\def\BNE_bareeval{\expandafter\BNE_check\romannumeral`&&@\BNE_getnext}% |
| 2218 |
% \end{macrocode} |
| 2219 |
% These next are not \cs{protected} because they are only used with |
| 2220 |
% |\bnumeval|, there is no analog of the private format which |\bnumexpr| |
| 2221 |
% expands to. This also spares us having to define macros with names which |
| 2222 |
% can be written to an external file and re-read using the standard catcodes. |
| 2223 |
% |
| 2224 |
% MEMO: |\BNEprint_| (with the trailing underscore) will be used in case of |
| 2225 |
% absence of optional argument and has been defined already. It is important |
| 2226 |
% for compatibility with the others here that it did not use the strange full |
| 2227 |
% stop in its parameter pattern. It is also important that it is \emph{not} |
| 2228 |
% |\protected|, as we want |\bnumeval| to expand fully in an |\edef|. |
| 2229 |
% \begin{macrocode} |
| 2230 |
\expandafter\def\csname BNEprint_[h]\endcsname#1{{\BNE_unpack_tohex#1.}}% |
| 2231 |
\expandafter\def\csname BNEprint_[ha]\endcsname#1{{\BNE_unpack_tolowhex#1.}}% |
| 2232 |
\expandafter\def\csname BNEprint_[o]\endcsname#1{{\BNE_unpack_tooct#1.}}% |
| 2233 |
\expandafter\def\csname BNEprint_[b]\endcsname#1{{\BNE_unpack_tobin#1.}}% |
| 2234 |
\expandafter\let\csname BNEprint_[]\endcsname\BNEprint_ |
| 2235 |
% \end{macrocode} |
| 2236 |
% |[b]|, |[o]| and |[h]| added at |1.6|. |
| 2237 |
% |
| 2238 |
% We break here the \xintname legacy pattern to always use |\romannumeral0| |
| 2239 |
% trigger first, via a CamelCase/lowercase pair of macros. |
| 2240 |
% |
| 2241 |
% The |\expanded| will \emph{not} control the actual computation, only launch |
| 2242 |
% it via propagation across the |\csname...\endcsname| to a |\romannumeral0| |
| 2243 |
% for the \emph{f}-expansion of |\BNE_bareeval|. After computation is done |
| 2244 |
% the |\BNEprint_[...]| will present the braced result (maybe containing |
| 2245 |
% multiple items) to this |\expanded|, and the unpacking is thus done via an |
| 2246 |
% \cs{edef}-like expansion. This does not mean that all helpers doing |
| 2247 |
% conversion to the binary bases only have to be \emph{x}-expandable, because |
| 2248 |
% their full expansion must be achieved prior to the ``printone'' macros |
| 2249 |
% activate. If the latter are |\protected|, then conversions do only need |
| 2250 |
% \emph{x}-expandability. Except for the support of the |[ha]| option which |
| 2251 |
% needs |\BNE_Op_dectohex| to be \emph{f}-expandable as the helper doing |
| 2252 |
% conversion to lowercase must act on a fully prepared uppercase hexadecimal |
| 2253 |
% input. |
| 2254 |
% \begin{macrocode} |
| 2255 |
\def\bnumeval #1#{\expanded\bnumeval_a{#1}}% |
| 2256 |
\def\bnumeval_a#1#2{% |
| 2257 |
\csname BNEprint_\xint_zapspaces #1 \xint_gobble_i\expandafter |
| 2258 |
\endcsname\romannumeral0\BNE_bareeval#2\relax |
| 2259 |
}% |
| 2260 |
% \end{macrocode} |
| 2261 |
% This code is more compact at |1.6| than at |1.5|. Various renamings at |
| 2262 |
% |1.7| and addition of the |[ha]| optional argument. |
| 2263 |
% \begin{macrocode} |
| 2264 |
\def\BNE_unpack#1{% |
| 2265 |
\bnumprintone{#1}\expandafter\BNE_unpack_a\string |
| 2266 |
}% |
| 2267 |
\def\BNE_unpack_a#1{% |
| 2268 |
\if#1.\BNE_allitemsdone\fi\bnumprintonesep |
| 2269 |
\expandafter\BNE_unpack\expandafter{\iffalse}\fi |
| 2270 |
}% |
| 2271 |
\def\BNE_allitemsdone\fi#1\fi{\fi}% |
| 2272 |
% \end{macrocode} |
| 2273 |
% There is a breaking change at |1.6| as formerly there was a |
| 2274 |
% \cs{bnumprintonetohex}. Now, the decimal to hexadecimal conversion is done |
| 2275 |
% always, and the customizable wrapper was thus renamed to |\bnumprintonehex|. |
| 2276 |
% \begin{macrocode} |
| 2277 |
\def\BNE_unpack_tohex#1{% |
| 2278 |
\expandafter\bnumprintonehex |
| 2279 |
\expandafter{\romannumeral`&&@\BNE_Op_dectohex{#1}}% |
| 2280 |
\expandafter\BNE_unpack_tohex_a\string |
| 2281 |
}% |
| 2282 |
\def\BNE_unpack_tohex_a#1{% |
| 2283 |
\if#1.\BNE_allitemsdone\fi\bnumprintonesep |
| 2284 |
\expandafter\BNE_unpack_tohex\expandafter{\iffalse}\fi |
| 2285 |
}% |
| 2286 |
% \end{macrocode} |
| 2287 |
% Conversion to lowercase hexadecimal added at |1.7|. By the way |[ha]| |
| 2288 |
% option is broken (but also is not needed) if the |\BNE_Op_dectohex| is |
| 2289 |
% customized to produce lowercase, not uppercase as |\xintiiDecToHex|. |
| 2290 |
% |
| 2291 |
% As we inject ending pattern already here, to skip one ``grab argument'' |
| 2292 |
% step, we do not provide a wrapper which could be configured via |
| 2293 |
% |\bnumsetup| with something such as |dectolowhex=\macro|. |
| 2294 |
% \begin{macrocode} |
| 2295 |
\def\BNE_unpack_tolowhex#1{% |
| 2296 |
\expandafter\bnumprintonelowerhex |
| 2297 |
\expanded{{\BNE_dectolowhex_a{#1}\xint_bye23456789\xint_bye\endcsname}}% |
| 2298 |
\expandafter\BNE_unpack_tolowhex_a\string |
| 2299 |
}% |
| 2300 |
\def\BNE_unpack_tolowhex_a#1{% |
| 2301 |
\if#1.\BNE_allitemsdone\fi\bnumprintonesep |
| 2302 |
\expandafter\BNE_unpack_tolowhex\expandafter{\iffalse}\fi |
| 2303 |
}% |
| 2304 |
% \end{macrocode} |
| 2305 |
% So the |dectohex| must be \emph{f}-expandable: even with a |
| 2306 |
% |\bnumprintonelowerhex| redefined |\protected|, the conversion |
| 2307 |
% to lowercase needs the |\BNE_Op_dectohex| action to be complete |
| 2308 |
% before |\BNE_dectolowhex_b| kicks in. |
| 2309 |
% \begin{macrocode} |
| 2310 |
\def\BNE_dectolowhex_a{% |
| 2311 |
\expandafter\BNE_dectolowhex_b\romannumeral`&&@\BNE_Op_dectohex |
| 2312 |
}% |
| 2313 |
% \end{macrocode} |
| 2314 |
% When preparing the |1.7| release I hesitated about testing the token to |
| 2315 |
% check if a digit or a letter (which could have been done on the basis of the |
| 2316 |
% catcode only, but would have then put a constraint on the catcodes used by |
| 2317 |
% the |\BNE_Op_dectohex| macro), and let digits go through ``as is''. But I |
| 2318 |
% was not willing to think too much and the advantage of the approach chosen |
| 2319 |
% is the elegance of its termination. I did not do comparative efficiency |
| 2320 |
% tests, which would have cost me at least one or two hours in setting them up |
| 2321 |
% and comparing with alternative implementation, besides I would have had to |
| 2322 |
% decide if I base decision on behavior of very long inputs or of the short |
| 2323 |
% ones. All of that for something perhaps nobody on Earth will ever use |
| 2324 |
% anyhow. |
| 2325 |
% \begin{macrocode} |
| 2326 |
\def\BNE_dectolowhex_b #1#2#3#4#5#6#7#8#9{% |
| 2327 |
\csname BNE_lower #1\endcsname |
| 2328 |
\csname BNE_lower #2\endcsname |
| 2329 |
\csname BNE_lower #3\endcsname |
| 2330 |
\csname BNE_lower #4\endcsname |
| 2331 |
\csname BNE_lower #5\endcsname |
| 2332 |
\csname BNE_lower #6\endcsname |
| 2333 |
\csname BNE_lower #7\endcsname |
| 2334 |
\csname BNE_lower #8\endcsname |
| 2335 |
\csname BNE_lower #9\endcsname |
| 2336 |
\BNE_dectolowhex_b |
| 2337 |
}% |
| 2338 |
\expandafter\let\csname BNE_lower \endcsname\empty |
| 2339 |
\expandafter\def\csname BNE_lower 0\endcsname{0}% |
| 2340 |
\expandafter\def\csname BNE_lower 1\endcsname{1}% |
| 2341 |
\expandafter\def\csname BNE_lower 2\endcsname{2}% |
| 2342 |
\expandafter\def\csname BNE_lower 3\endcsname{3}% |
| 2343 |
\expandafter\def\csname BNE_lower 4\endcsname{4}% |
| 2344 |
\expandafter\def\csname BNE_lower 5\endcsname{5}% |
| 2345 |
\expandafter\def\csname BNE_lower 6\endcsname{6}% |
| 2346 |
\expandafter\def\csname BNE_lower 7\endcsname{7}% |
| 2347 |
\expandafter\def\csname BNE_lower 8\endcsname{8}% |
| 2348 |
\expandafter\def\csname BNE_lower 9\endcsname{9}% |
| 2349 |
\expandafter\def\csname BNE_lower A\endcsname{a}% |
| 2350 |
\expandafter\def\csname BNE_lower B\endcsname{b}% |
| 2351 |
\expandafter\def\csname BNE_lower C\endcsname{c}% |
| 2352 |
\expandafter\def\csname BNE_lower D\endcsname{d}% |
| 2353 |
\expandafter\def\csname BNE_lower E\endcsname{e}% |
| 2354 |
\expandafter\def\csname BNE_lower F\endcsname{f}% |
| 2355 |
% \end{macrocode} |
| 2356 |
% Octal and binary added at |1.6|. |
| 2357 |
% \begin{macrocode} |
| 2358 |
\def\BNE_unpack_tooct#1{% |
| 2359 |
\expandafter\bnumprintoneoct |
| 2360 |
\expandafter{\romannumeral`&&@\BNE_Op_dectooct{#1}}% |
| 2361 |
\expandafter\BNE_unpack_tooct_a\string |
| 2362 |
}% |
| 2363 |
\def\BNE_unpack_tooct_a#1{% |
| 2364 |
\if#1.\BNE_allitemsdone\fi\bnumprintonesep |
| 2365 |
\expandafter\BNE_unpack_tooct\expandafter{\iffalse}\fi |
| 2366 |
}% |
| 2367 |
\def\BNE_unpack_tobin#1{% |
| 2368 |
\expandafter\bnumprintonebin |
| 2369 |
\expandafter{\romannumeral`&&@\BNE_Op_dectobin{#1}}% |
| 2370 |
\expandafter\BNE_unpack_tobin_a\string |
| 2371 |
}% |
| 2372 |
\def\BNE_unpack_tobin_a#1{% |
| 2373 |
\if#1.\BNE_allitemsdone\fi\bnumprintonesep |
| 2374 |
\expandafter\BNE_unpack_tobin\expandafter{\iffalse}\fi |
| 2375 |
}% |
| 2376 |
\let\bnumprintone \xint_firstofone |
| 2377 |
\let\bnumprintonehex\xint_firstofone |
| 2378 |
\let\bnumprintonelowerhex\xint_firstofone |
| 2379 |
\let\bnumprintoneoct\xint_firstofone |
| 2380 |
\let\bnumprintonebin\xint_firstofone |
| 2381 |
\def\bnumprintonesep{, }% |
| 2382 |
% \end{macrocode} |
| 2383 |
% \subsection{\cs{BNE_getnext}} |
| 2384 |
% The upstream |\BNE_put_op_first| has a string of included \cs{expandafter}, |
| 2385 |
% which was imported here at |1.4| and |1.4a| but they serve nothing in our |
| 2386 |
% context. Removed this useless overhead at |1.5|. |
| 2387 |
% |
| 2388 |
% This |\BNE_getnext| token is injected via "start" macros associated to |
| 2389 |
% operators or similar syntax elements, as will be seen later on. It tries |
| 2390 |
% to get next operand. |
| 2391 |
% \begin{macrocode} |
| 2392 |
\def\BNE_getnext #1% |
| 2393 |
{% |
| 2394 |
\expandafter\BNE_put_op_first\romannumeral`&&@% |
| 2395 |
\expandafter\BNE_getnext_a\romannumeral`&&@#1% |
| 2396 |
}% |
| 2397 |
\def\BNE_put_op_first #1#2#3{#2#3{#1}}% |
| 2398 |
\def\BNE_getnext_a #1% |
| 2399 |
{% |
| 2400 |
\ifx\relax #1\xint_dothis\BNE_foundprematureend\fi |
| 2401 |
\ifx\XINTfstop#1\xint_dothis\BNE_subexpr\fi |
| 2402 |
\ifcat\relax#1\xint_dothis\BNE_countetc\fi |
| 2403 |
\xint_orthat{}\BNE_getnextfork #1% |
| 2404 |
}% |
| 2405 |
% \end{macrocode} |
| 2406 |
% This is executed when the parser was trying to find an operand but ended up |
| 2407 |
% hitting the \cs{relax} end-marker. This happens if the expression is empty |
| 2408 |
% for example. It trickled down here from \xintexprname, but |\xinteval| has |
| 2409 |
% a notion of empty value. This is not the case of the \bnumname parser. So |
| 2410 |
% let's at |1.7a| allow |\bnumeval{}| or |\bnumeval{_}|. Indeed |
| 2411 |
% |\bnumeval{"}| and |\bnumeval{"_}| which use an other codebranch worked |
| 2412 |
% already. This goes via replacing |{{}}| ^^A attention my verb |
| 2413 |
% by |{{0}}|. |
| 2414 |
% \begin{macrocode} |
| 2415 |
\def\BNE_foundprematureend\BNE_getnextfork #1{{0}\xint_c_\relax}% |
| 2416 |
\def\BNE_subexpr #1.#2% |
| 2417 |
{% |
| 2418 |
\expanded{\unexpanded{{#2}}\expandafter}\romannumeral`&&@\BNE_getop |
| 2419 |
}% |
| 2420 |
% \end{macrocode} |
| 2421 |
% At |1.6| this also filters for \cs{catcode} (as per \xintname |1.4g| |
| 2422 |
% |2021/05/25|). |
| 2423 |
% \begin{macrocode} |
| 2424 |
\def\BNE_countetc\BNE_getnextfork#1% |
| 2425 |
{% |
| 2426 |
\if0\ifx\count#11\fi |
| 2427 |
\ifx\numexpr#11\fi |
| 2428 |
\ifx\catcode#11\fi |
| 2429 |
\ifx\dimen#11\fi |
| 2430 |
\ifx\dimexpr#11\fi |
| 2431 |
\ifx\skip#11\fi |
| 2432 |
\ifx\glueexpr#11\fi |
| 2433 |
\ifx\fontdimen#11\fi |
| 2434 |
\ifx\ht#11\fi |
| 2435 |
\ifx\dp#11\fi |
| 2436 |
\ifx\wd#11\fi |
| 2437 |
\ifx\fontcharht#11\fi |
| 2438 |
\ifx\fontcharwd#11\fi |
| 2439 |
\ifx\fontchardp#11\fi |
| 2440 |
\ifx\fontcharic#11\fi |
| 2441 |
0\expandafter\BNE_fetch_as_number\fi |
| 2442 |
\expandafter\BNE_getnext_a\number #1% |
| 2443 |
}% |
| 2444 |
\def\BNE_fetch_as_number |
| 2445 |
\expandafter\BNE_getnext_a\number #1% |
| 2446 |
{% |
| 2447 |
\expanded{{{\number#1}}\expandafter}\romannumeral`&&@\BNE_getop |
| 2448 |
}% |
| 2449 |
% \end{macrocode} |
| 2450 |
% In the case of hitting a |(|, previous release inserted directly a |
| 2451 |
% |\BNE_oparen|. But the expansion architecture imported from upstream |
| 2452 |
% |\xintiiexpr| has been refactored, and the |..._oparen| meaning and |
| 2453 |
% usage evolved. We stick with |{}\xint_c_ii^v (| from upstream. |
| 2454 |
% |
| 2455 |
% Also, at |1.6|, slight refactoring to handle digit tokens and opening |
| 2456 |
% parenthesis a bit faster (but this is only first token...); and to ignore an |
| 2457 |
% underscore as first character (rather than raise an error in this case). |
| 2458 |
% |
| 2459 |
% This merges former |\BNE_getnextfork| and |\BNE_scan_number|. |
| 2460 |
% \begin{macrocode} |
| 2461 |
\def\BNE_getnextfork #1{% |
| 2462 |
\if#1-\xint_dothis {{}{}-}\fi |
| 2463 |
\if#1(\xint_dothis {{}\xint_c_ii^v (}\fi |
| 2464 |
\ifnum\xint_c_ix<1\string#1 \xint_dothis {\BNE_startint#1}\fi |
| 2465 |
\xint_orthat {\BNE_getnextfork_a #1}% |
| 2466 |
}% |
| 2467 |
% \end{macrocode} |
| 2468 |
% Prior to |1.7a|, the |_| was handled as is a |+| encountered while looking |
| 2469 |
% for a value, i.e.\@ was simply ignored and expansion continued with |
| 2470 |
% |\BNE_get_next_a|. This made |_^| raise an ``unexpected token'' error when |
| 2471 |
% hitting the |^|. But in contrast |"_^| does work without complaining and is |
| 2472 |
% the same as |0^|. So at |1.7a| we jump to |\BNE_startint|. Mind that the |
| 2473 |
% latter differs from |\BNE_startoct| and |\BNE_starthex| and expects to grab |
| 2474 |
% an already expanded argument, and naturally we inject a zero. As a |
| 2475 |
% corollary of this |1.7a| change, also |_x|, |_o| and |_b| can serve as |
| 2476 |
% prefixes for hexadecimal, octal and binary. |
| 2477 |
% \begin{macrocode} |
| 2478 |
\def\BNE_getnextfork_a #1{% |
| 2479 |
\if#1_\xint_dothis {\BNE_startint 0}\fi |
| 2480 |
\if#1+\xint_dothis \BNE_getnext_a \fi |
| 2481 |
\if#1'\xint_dothis \BNE_startoct\fi |
| 2482 |
\if#1"\xint_dothis \BNE_starthex\fi |
| 2483 |
\xint_orthat {\BNE_unexpected #1}% |
| 2484 |
}% |
| 2485 |
% \end{macrocode} |
| 2486 |
% If user employs |\bnumdefinfix| with |\string#|, and then tries |100##3|, |
| 2487 |
% the first |#| will be interpreted as operator (assuming no |
| 2488 |
% operator starting with |##| has actually been defined) and the error |
| 2489 |
% "message" (which is not using \cs{message} or a \cs{write}) |
| 2490 |
% will then be |
| 2491 |
% \centeredline{|! xint error: Unexpected token `##'. Ignoring.|} |
| 2492 |
% because the parser is actually looking for a digit but finds the second |#|, |
| 2493 |
% and TeX displays it doubled. This is doubly |
| 2494 |
% confusing, but well, let's not dwell on that. |
| 2495 |
% |
| 2496 |
% |\BNE_unexpected| replaced here |\BNE_notadigit| at |1.6|. |
| 2497 |
% \begin{macrocode} |
| 2498 |
\def\BNE_unexpected#1% |
| 2499 |
{% |
| 2500 |
\XINT_expandableerror{Unexpected token `#1'. Ignoring.}\BNE_getnext_a |
| 2501 |
}% |
| 2502 |
% \end{macrocode} |
| 2503 |
% \subsection{Parsing decimal, hexadecimal, octal, and binary} |
| 2504 |
% |
| 2505 |
% Somewhat refactored at |1.6| compared to upstream |1.4m|, on the occasion of |
| 2506 |
% adding support for prefixes beyond only |"| as earlier. |
| 2507 |
% |
| 2508 |
% Ironically, |1.6| fixed the case of an underscore |_| as first character in |
| 2509 |
% decimal input, but caused a regression for |"| prefixed hexadecimal, which |
| 2510 |
% ceased to allow a |_| right after the |"|. Fixed at |1.7|. |
| 2511 |
% |
| 2512 |
% But |1.7| introduced a regression causing |"0| input to now break... pfff... |
| 2513 |
% hence |1.7a|. A secondary (longstanding) issue is that the test suite was |
| 2514 |
% limited to examples as shown in the docs (which used a special mark-up to |
| 2515 |
% check automatically for regressions), but this way made inconvenient to test |
| 2516 |
% for special inputs such as an hexadecimal prefix only followed with zeros |
| 2517 |
% and underscores, which is what got broken in a too speedy refactoring at |
| 2518 |
% |1.7|. The development sources now have a better way of checking against |
| 2519 |
% regression prior to releases. |
| 2520 |
% |
| 2521 |
% \subsubsection{Prefix dispatch} |
| 2522 |
% \begin{macrocode} |
| 2523 |
\def\BNE_startint #1% |
| 2524 |
{% |
| 2525 |
\if #10\expandafter\BNE_scanint_gobz_a\else\expandafter\BNE_scanint_a\fi #1% |
| 2526 |
}% |
| 2527 |
\def\BNE_wrapint_after{\iffalse{{{\fi}}}}% |
| 2528 |
\def\BNE_scanint_gobz_a #1#2{% |
| 2529 |
\expandafter\BNE_scanint_gobz_b\romannumeral`&&@#2% |
| 2530 |
}% |
| 2531 |
% \end{macrocode} |
| 2532 |
% It is important in case of |x|, |o|, or |b| to jump to |
| 2533 |
% |\BNE_starthex| (et al.) and not for example to |\BNE_scanhex_a| |
| 2534 |
% because the latter expects an \emph{f}-expansion to have been |
| 2535 |
% applied already to what comes next (this comment is half-obsolete |
| 2536 |
% at |1.7| which has no |\BNE_scanhex_a| anymore). |
| 2537 |
% |
| 2538 |
% Besides, we do want to trim out |
| 2539 |
% leading zeroes after the |0b|, |0o|, or |0x| prefix: although the |
| 2540 |
% macros of \xintbinhexname do accept leading zeros on input, they may |
| 2541 |
% then produce decimal output with leading zeros, and the ``|ii|'' |
| 2542 |
% macros of \xintcorename consider that an input is vanishing as soon |
| 2543 |
% as the first digit is |0|. |
| 2544 |
% \begin{macrocode} |
| 2545 |
\def\BNE_scanint_gobz_b #1% |
| 2546 |
{% |
| 2547 |
\ifx b#1\xint_dothis \BNE_startbin \fi |
| 2548 |
\ifx o#1\xint_dothis \BNE_startoct \fi |
| 2549 |
\ifx x#1\xint_dothis \BNE_starthex \fi |
| 2550 |
\xint_orthat {\BNE_scanint_gobz_c #1}% |
| 2551 |
}% |
| 2552 |
\def\BNE_scanint_gobz_c #1% |
| 2553 |
{% |
| 2554 |
\expandafter{\expanded{{\iffalse}}}\fi |
| 2555 |
\BNE_scanint_gobz_main#1% |
| 2556 |
}% |
| 2557 |
% \end{macrocode} |
| 2558 |
% \subsubsection{Decimal} |
| 2559 |
% No |\BNE_wrapint_before|, it has been inlined here at |1.7| (and in |
| 2560 |
% |\BNE_scanint_gobz_c|). |
| 2561 |
% \begin{macrocode} |
| 2562 |
\def\BNE_scanint_a #1#2{% |
| 2563 |
\expandafter{\expanded{{\iffalse}}}\fi #1% |
| 2564 |
\expandafter\BNE_scanint_main\romannumeral`&&@#2% |
| 2565 |
}% |
| 2566 |
\def\BNE_scanint_main #1% |
| 2567 |
{% |
| 2568 |
\ifcat \relax #1\expandafter\BNE_scanint_hit_cs \fi |
| 2569 |
\ifnum\xint_c_ix<1\string#1 \else\expandafter\BNE_scanint_checkagain\fi |
| 2570 |
#1\BNE_scanint_again |
| 2571 |
}% |
| 2572 |
\def\BNE_scanint_again #1% |
| 2573 |
{% |
| 2574 |
\expandafter\BNE_scanint_main\romannumeral`&&@#1% |
| 2575 |
}% |
| 2576 |
% \end{macrocode} |
| 2577 |
% Upstream (at |1.4f|) has |_getop| here, but let's jump directly to |BNE_getop_a|. |
| 2578 |
% \begin{macrocode} |
| 2579 |
\def\BNE_scanint_hit_cs \ifnum#1\fi#2\BNE_scanint_again |
| 2580 |
{% |
| 2581 |
\expandafter\BNE_wrapint_after\romannumeral`&&@\BNE_getop_a#2% |
| 2582 |
}% |
| 2583 |
\def\BNE_scanint_checkagain #1\BNE_scanint_again |
| 2584 |
{% |
| 2585 |
\if _#1\BNE_scanint_checkagain_skip\fi |
| 2586 |
\expandafter\BNE_wrapint_after\romannumeral`&&@\BNE_getop_a#1% |
| 2587 |
}% |
| 2588 |
% \end{macrocode} |
| 2589 |
% |#1| is \cs{fi}. |
| 2590 |
% \begin{macrocode} |
| 2591 |
\def\BNE_scanint_checkagain_skip#1#2\BNE_getop_a#3{#1\BNE_scanint_again}% |
| 2592 |
\def\BNE_scanint_gobz_main #1% |
| 2593 |
{% |
| 2594 |
\ifcat \relax #1\expandafter\BNE_scanint_gobz_hit_cs\fi |
| 2595 |
\ifnum\xint_c_x<1\string#1 \else\expandafter\BNE_scanint_gobz_checkagain\fi |
| 2596 |
#1\BNE_scanint_again |
| 2597 |
}% |
| 2598 |
% \end{macrocode} |
| 2599 |
% Upstream (at |1.4f|) has |_getop| here, but let's jump directly to |
| 2600 |
% |BNE_getop_a|. The |#2| has been grabbed already and f-expanded. |
| 2601 |
% But this means one less brace-stripping. |
| 2602 |
% \begin{macrocode} |
| 2603 |
\def\BNE_scanint_gobz_hit_cs\ifnum#1\fi#2\BNE_scanint_again |
| 2604 |
{% |
| 2605 |
0\expandafter\BNE_wrapint_after\romannumeral`&&@\BNE_getop_a#2% |
| 2606 |
}% |
| 2607 |
% \end{macrocode} |
| 2608 |
% Fix at |1.6| for when an underscore is used as first character followed by |
| 2609 |
% digits. No need to worry about being very efficient here. |
| 2610 |
% \begin{macrocode} |
| 2611 |
\def\BNE_scanint_gobz_checkagain #1\BNE_scanint_again |
| 2612 |
{% |
| 2613 |
\if _#1\xint_dothis\BNE_scanint_gobz_again\fi |
| 2614 |
\if 0#1\xint_dothis\BNE_scanint_gobz_again\fi |
| 2615 |
\xint_orthat |
| 2616 |
{0\expandafter\BNE_wrapint_after\romannumeral`&&@\BNE_getop_a#1}% |
| 2617 |
}% |
| 2618 |
\def\BNE_scanint_gobz_again #1% |
| 2619 |
{% |
| 2620 |
\expandafter\BNE_scanint_gobz_main\romannumeral`&&@#1% |
| 2621 |
}% |
| 2622 |
% \end{macrocode} |
| 2623 |
% \subsubsection{Hexadecimal} |
| 2624 |
% |
| 2625 |
% At |1.6| the code here is refactored to follow closely the |scanint| one, |
| 2626 |
% rather than downscaling upstream \xintexprname which also has to handle |
| 2627 |
% fractional input. This avoids gathering the hexadecimal digits then |
| 2628 |
% grabbing them again as a whole via a delimited macro. |
| 2629 |
% |
| 2630 |
% Removing leading zeros and leading underscores |
| 2631 |
% is the job of |\BNE_scanhex_gobz_main|. |
| 2632 |
% |
| 2633 |
% Things could be done better if |\BNE_Op_hextodec| was only required to be |
| 2634 |
% \emph{x}-expandable. Already now we could inline the |\BNE_wraphex_before| |
| 2635 |
% sparing its use of |\iffalse..\fi| to move closing braces to the one after |
| 2636 |
% |\expanded|. But we would still have three |\expandafter|'s in total. |
| 2637 |
% \begin{macrocode} |
| 2638 |
\def\BNE_starthex #1% |
| 2639 |
{% |
| 2640 |
\expandafter\BNE_wraphex_before |
| 2641 |
\expanded{{\iffalse}}\fi |
| 2642 |
\expandafter\BNE_scanhex_gobz_main\romannumeral`&&@#1% |
| 2643 |
}% |
| 2644 |
\def\BNE_wraphex_before{\expandafter{\expandafter{% |
| 2645 |
\romannumeral`&&@\iffalse}}\fi\BNE_Op_hextodec}% |
| 2646 |
\def\BNE_wraphex_after{\iffalse{{{{\fi}}}}}% |
| 2647 |
% \end{macrocode} |
| 2648 |
% At |1.6| we apply exact same scheme as for the |scanint| code. The sole |
| 2649 |
% difference is the more complicated test for recognizing a digit. |
| 2650 |
% |
| 2651 |
% At |1.7| the code evolved to support |a..f| as hexadecimal input. |
| 2652 |
% As |\bnumeval| supports no functions or user variables, there is |
| 2653 |
% no breaking change with tacit multiplication as would be the case |
| 2654 |
% for |\xinteval| if we did the same addition to it. |
| 2655 |
% |
| 2656 |
% The expansion is |\expanded|-governed which spares us quite |
| 2657 |
% some annoyances with exiting from \TeX{} nested conditionals. |
| 2658 |
% |
| 2659 |
% Hesitation whether to check for lowercase hexadecimal |
| 2660 |
% before or after checking for the underscore. |
| 2661 |
% \begin{macrocode} |
| 2662 |
\def\BNE_scanhex_main #1% |
| 2663 |
{% |
| 2664 |
\ifcat \relax #1\BNE_scanhex_done_hit_cs #1\fi |
| 2665 |
\if\ifnum`#1>`/ |
| 2666 |
\ifnum`#1>`9 |
| 2667 |
\ifnum`#1>`@ |
| 2668 |
\ifnum`#1>`F |
| 2669 |
0\else1\fi\else0\fi\else1\fi\else0\fi 1% |
| 2670 |
#1% |
| 2671 |
\else |
| 2672 |
\if _#1\else |
| 2673 |
\if\ifnum`#1>`` |
| 2674 |
\ifnum`#1>`f 0\else1\fi\else0\fi 1% |
| 2675 |
\csname BNE_upper #1\endcsname |
| 2676 |
\else |
| 2677 |
\BNE_scanhex_done #1% |
| 2678 |
\fi |
| 2679 |
\fi |
| 2680 |
\fi |
| 2681 |
\BNE_scanhex_again |
| 2682 |
}% |
| 2683 |
\expandafter\def\csname BNE_upper a\endcsname{A}% |
| 2684 |
\expandafter\def\csname BNE_upper b\endcsname{B}% |
| 2685 |
\expandafter\def\csname BNE_upper c\endcsname{C}% |
| 2686 |
\expandafter\def\csname BNE_upper d\endcsname{D}% |
| 2687 |
\expandafter\def\csname BNE_upper e\endcsname{E}% |
| 2688 |
\expandafter\def\csname BNE_upper f\endcsname{F}% |
| 2689 |
% \end{macrocode} |
| 2690 |
% |#2| is \cs{fi}. |
| 2691 |
% \begin{macrocode} |
| 2692 |
\def\BNE_scanhex_done_hit_cs #1#2#3\BNE_scanhex_again |
| 2693 |
{% |
| 2694 |
#2\expandafter\BNE_wraphex_after\romannumeral`&&@\BNE_getop_a#1% |
| 2695 |
}% |
| 2696 |
% \end{macrocode} |
| 2697 |
% |#2| is \cs{fi}\cs{fi}\cs{fi} or \cs{fi}\cs{fi}\cs{fi}\cs{fi} (if called |
| 2698 |
% from the |gobz| variant). |
| 2699 |
% \begin{macrocode} |
| 2700 |
\def\BNE_scanhex_done #1#2\BNE_scanhex_again |
| 2701 |
{% |
| 2702 |
#2\expandafter\BNE_wraphex_after\romannumeral`&&@\BNE_getop_a#1% |
| 2703 |
}% |
| 2704 |
\def\BNE_scanhex_again #1% |
| 2705 |
{% |
| 2706 |
\expandafter\BNE_scanhex_main\romannumeral`&&@#1% |
| 2707 |
}% |
| 2708 |
% \end{macrocode} |
| 2709 |
% Initial scanning which skips leading zeros and underscores. Priority in |
| 2710 |
% efficiency is when there are none. Slight refactoring to check for lowercase |
| 2711 |
% hexadecimal earlier at |1.7a|. But the |\BNE_scanhex_main| will then again |
| 2712 |
% check for underscores prior to them. |
| 2713 |
% |
| 2714 |
% Also |1.7a| fixes the unfortunate |1.7| regression when only zeros (or |
| 2715 |
% underscores) are found after the hexadecimal (or other) prefixes. |
| 2716 |
% \begin{macrocode} |
| 2717 |
\def\BNE_scanhex_gobz_main #1% |
| 2718 |
{% |
| 2719 |
\ifcat \relax #1% |
| 2720 |
0\BNE_scanhex_done_hit_cs #1\fi |
| 2721 |
\if\ifnum`#1>`0 |
| 2722 |
\ifnum`#1>`9 |
| 2723 |
\ifnum`#1>`@ |
| 2724 |
\ifnum`#1>`F |
| 2725 |
0\else1\fi\else0\fi\else1\fi\else0\fi 1% |
| 2726 |
#1% |
| 2727 |
\else |
| 2728 |
\if\ifnum`#1>`` \ifnum`#1>`f 0\else1\fi\else0\fi 1% |
| 2729 |
\csname BNE_upper #1\endcsname |
| 2730 |
\else |
| 2731 |
\if 0#1\BNE_scanhex_gobzero\else |
| 2732 |
\if _#1\BNE_scanhex_gobunderscore\else |
| 2733 |
0\BNE_scanhex_done #1% |
| 2734 |
\fi\fi |
| 2735 |
\fi |
| 2736 |
\fi |
| 2737 |
\BNE_scanhex_again |
| 2738 |
}% |
| 2739 |
\def\BNE_scanhex_gobzero #1\BNE_scanhex_again #2% |
| 2740 |
{% |
| 2741 |
\fi\fi\fi |
| 2742 |
\expandafter\BNE_scanhex_gobz_main\romannumeral`&&@#2% |
| 2743 |
}% |
| 2744 |
\def\BNE_scanhex_gobunderscore #1\BNE_scanhex_again #2% |
| 2745 |
{% |
| 2746 |
\fi\fi\fi\fi |
| 2747 |
\expandafter\BNE_scanhex_gobz_main\romannumeral`&&@#2% |
| 2748 |
}% |
| 2749 |
% \end{macrocode} |
| 2750 |
% \subsubsection{Octal} |
| 2751 |
% |
| 2752 |
% Added at |1.6|. Leading |
| 2753 |
% zeros are removed. |
| 2754 |
% |
| 2755 |
% At |1.7| the code for hexadecimal was a bit refactored and |
| 2756 |
% the one here was changed to follow same pattern. |
| 2757 |
% \begin{macrocode} |
| 2758 |
\def\BNE_startoct #1% |
| 2759 |
{% |
| 2760 |
\expandafter\BNE_wrapoct_before |
| 2761 |
\expanded{{\iffalse}}\fi |
| 2762 |
\expandafter\BNE_scanoct_gobz_main\romannumeral`&&@#1% |
| 2763 |
}% |
| 2764 |
\def\BNE_wrapoct_before{\expandafter{\expandafter{% |
| 2765 |
\romannumeral`&&@\iffalse}}\fi\BNE_Op_octtodec}% |
| 2766 |
\def\BNE_wrapoct_after{\iffalse{{{{\fi}}}}}% |
| 2767 |
\def\BNE_scanoct_main #1% |
| 2768 |
{% |
| 2769 |
\ifcat \relax #1\expandafter\BNE_scanoct_done_hit_cs #1\fi |
| 2770 |
\if\ifnum`#1>`/ \ifnum`#1>`7 0\else1\fi\else0\fi 1% |
| 2771 |
#1% |
| 2772 |
\else |
| 2773 |
\if _#1\else |
| 2774 |
\BNE_scanoct_done #1% |
| 2775 |
\fi |
| 2776 |
\fi |
| 2777 |
\BNE_scanoct_again |
| 2778 |
}% |
| 2779 |
% \end{macrocode} |
| 2780 |
% |#2| is \cs{fi}. |
| 2781 |
% \begin{macrocode} |
| 2782 |
\def\BNE_scanoct_done_hit_cs #1#2#3\BNE_scanoct_again |
| 2783 |
{% |
| 2784 |
#2\expandafter\BNE_wrapoct_after\romannumeral`&&@\BNE_getop_a#1% |
| 2785 |
}% |
| 2786 |
% \end{macrocode} |
| 2787 |
% |#2| is \cs{fi}\cs{fi} or \cs{fi}\cs{fi}\cs{fi} (if called |
| 2788 |
% from the |gobz| variant). |
| 2789 |
% \begin{macrocode} |
| 2790 |
\def\BNE_scanoct_done #1#2\BNE_scanoct_again |
| 2791 |
{% |
| 2792 |
#2\expandafter\BNE_wrapoct_after\romannumeral`&&@\BNE_getop_a#1% |
| 2793 |
}% |
| 2794 |
\def\BNE_scanoct_again #1% |
| 2795 |
{% |
| 2796 |
\expandafter\BNE_scanoct_main\romannumeral`&&@#1% |
| 2797 |
}% |
| 2798 |
\def\BNE_scanoct_gobz_main #1% |
| 2799 |
{% |
| 2800 |
\ifcat \relax #1% |
| 2801 |
0\BNE_scanoct_done_hit_cs #1\fi |
| 2802 |
\if\ifnum`#1>`0 \ifnum`#1>`7 0\else1\fi\else0\fi 1% |
| 2803 |
#1% |
| 2804 |
\else |
| 2805 |
\if 0#1\BNE_scanoct_gobzero\else |
| 2806 |
\if _#1\BNE_scanoct_gobunderscore\else |
| 2807 |
0\BNE_scanoct_done #1% |
| 2808 |
\fi\fi |
| 2809 |
\fi |
| 2810 |
\BNE_scanoct_again |
| 2811 |
}% |
| 2812 |
\def\BNE_scanoct_gobzero #1\BNE_scanoct_again #2% |
| 2813 |
{% |
| 2814 |
\fi\fi |
| 2815 |
\expandafter\BNE_scanoct_gobz_main\romannumeral`&&@#2% |
| 2816 |
}% |
| 2817 |
\def\BNE_scanoct_gobunderscore #1\BNE_scanoct_again #2% |
| 2818 |
{% |
| 2819 |
\fi\fi\fi |
| 2820 |
\expandafter\BNE_scanoct_gobz_main\romannumeral`&&@#2% |
| 2821 |
}% |
| 2822 |
% \end{macrocode} |
| 2823 |
% \subsubsection{Binary} |
| 2824 |
% |
| 2825 |
% Added at |1.6|. Same code skeleton as for octal and hexadecimal. |
| 2826 |
% \begin{macrocode} |
| 2827 |
\def\BNE_startbin #1% |
| 2828 |
{% |
| 2829 |
\expandafter\BNE_wrapbin_before |
| 2830 |
\expanded{{\iffalse}}\fi |
| 2831 |
\expandafter\BNE_scanbin_gobz_main\romannumeral`&&@#1% |
| 2832 |
}% |
| 2833 |
\def\BNE_wrapbin_before{\expandafter{\expandafter{% |
| 2834 |
\romannumeral`&&@\iffalse}}\fi\BNE_Op_bintodec}% |
| 2835 |
\def\BNE_wrapbin_after{\iffalse{{{{\fi}}}}}% |
| 2836 |
\def\BNE_scanbin_main #1% |
| 2837 |
{% |
| 2838 |
\ifcat \relax #1\expandafter\BNE_scanbin_done_hit_cs #1\fi |
| 2839 |
\if \if0#11\else\if1#11\else0\fi\fi 1% |
| 2840 |
#1% |
| 2841 |
\else |
| 2842 |
\if _#1\else |
| 2843 |
\BNE_scanbin_done #1% |
| 2844 |
\fi |
| 2845 |
\fi |
| 2846 |
\BNE_scanbin_again |
| 2847 |
}% |
| 2848 |
% \end{macrocode} |
| 2849 |
% |#2| is \cs{fi}. |
| 2850 |
% \begin{macrocode} |
| 2851 |
\def\BNE_scanbin_done_hit_cs #1#2#3\BNE_scanbin_again |
| 2852 |
{% |
| 2853 |
#2\expandafter\BNE_wrapbin_after\romannumeral`&&@\BNE_getop_a#1% |
| 2854 |
}% |
| 2855 |
% \end{macrocode} |
| 2856 |
% |#2| is \cs{fi}\cs{fi} or \cs{fi}\cs{fi}\cs{fi} (if called |
| 2857 |
% from the |gobz| variant). |
| 2858 |
% \begin{macrocode} |
| 2859 |
\def\BNE_scanbin_done #1#2\BNE_scanbin_again |
| 2860 |
{% |
| 2861 |
#2\expandafter\BNE_wrapbin_after\romannumeral`&&@\BNE_getop_a#1% |
| 2862 |
}% |
| 2863 |
\def\BNE_scanbin_again #1% |
| 2864 |
{% |
| 2865 |
\expandafter\BNE_scanbin_main\romannumeral`&&@#1% |
| 2866 |
}% |
| 2867 |
\def\BNE_scanbin_gobz_main #1% |
| 2868 |
{% |
| 2869 |
\ifcat \relax #1% |
| 2870 |
0\BNE_scanbin_done_hit_cs #1\fi |
| 2871 |
\if \if0#11\else\if1#11\else0\fi\fi 1% |
| 2872 |
#1% |
| 2873 |
\else |
| 2874 |
\if 0#1\BNE_scanbin_gobzero\else |
| 2875 |
\if _#1\BNE_scanbin_gobunderscore\else |
| 2876 |
0\BNE_scanbin_done #1% |
| 2877 |
\fi\fi |
| 2878 |
\fi |
| 2879 |
\BNE_scanbin_again |
| 2880 |
}% |
| 2881 |
\def\BNE_scanbin_gobzero #1\BNE_scanbin_again #2% |
| 2882 |
{% |
| 2883 |
\fi\fi |
| 2884 |
\expandafter\BNE_scanbin_gobz_main\romannumeral`&&@#2% |
| 2885 |
}% |
| 2886 |
\def\BNE_scanbin_gobunderscore #1\BNE_scanbin_again #2% |
| 2887 |
{% |
| 2888 |
\fi\fi\fi |
| 2889 |
\expandafter\BNE_scanbin_gobz_main\romannumeral`&&@#2% |
| 2890 |
}% |
| 2891 |
% \end{macrocode} |
| 2892 |
% \subsection{\cs{BNE_getop}} |
| 2893 |
% The upstream analog to |\BNE_getop_a| applies \cs{string} to |#1| in its |
| 2894 |
% thirdofthree branch before handing over to analog of |\BNE_scanop_a|, but I |
| 2895 |
% see no reason for doing it here (and I do have to check if upstream has |
| 2896 |
% any valid reason to do it). Removed. First branch was a |\BNE_foundend|, |
| 2897 |
% used only here, and expanding to |\xint_c_\relax|, let's move the |
| 2898 |
% |#1| (which will be \cs{relax}) last and simply insert |\xint_c_|. |
| 2899 |
% |
| 2900 |
% The |_scanop| macros have been refactored at upstream and here |1.5|. |
| 2901 |
% \begin{macrocode} |
| 2902 |
\def\BNE_getop #1% |
| 2903 |
{% |
| 2904 |
\expandafter\BNE_getop_a\romannumeral`&&@#1% |
| 2905 |
}% |
| 2906 |
\catcode`* 11 |
| 2907 |
\def\BNE_getop_a #1% |
| 2908 |
{% |
| 2909 |
\ifx \relax #1\xint_dothis\xint_firstofthree\fi |
| 2910 |
\ifcat \relax #1\xint_dothis\xint_secondofthree\fi |
| 2911 |
\ifnum\xint_c_ix<1\string#1 \xint_dothis\xint_secondofthree\fi |
| 2912 |
\if (#1\xint_dothis \xint_secondofthree\fi %) |
| 2913 |
\xint_orthat \xint_thirdofthree |
| 2914 |
\xint_c_ |
| 2915 |
{\BNE_prec_tacit *}% |
| 2916 |
\BNE_scanop_a |
| 2917 |
#1% |
| 2918 |
}% |
| 2919 |
\catcode`* 12 |
| 2920 |
\def\BNE_scanop_a #1#2% |
| 2921 |
{% |
| 2922 |
\expandafter\BNE_scanop_b\expandafter#1\romannumeral`&&@#2% |
| 2923 |
}% |
| 2924 |
\def\BNE_scanop_b #1#2% |
| 2925 |
{% |
| 2926 |
\unless\ifcat#2\relax |
| 2927 |
\ifcsname BNE_itself_#1#2\endcsname |
| 2928 |
\BNE_scanop_c |
| 2929 |
\fi\fi |
| 2930 |
\BNE_foundop_a #1#2% |
| 2931 |
}% |
| 2932 |
\def\BNE_scanop_c #1#2#3#4#5% #1#2=\fi\fi |
| 2933 |
{% |
| 2934 |
#1#2% |
| 2935 |
\expandafter\BNE_scanop_d\csname BNE_itself_#4#5\expandafter\endcsname |
| 2936 |
\romannumeral`&&@% |
| 2937 |
}% |
| 2938 |
\def\BNE_scanop_d #1#2% |
| 2939 |
{% |
| 2940 |
\unless\ifcat#2\relax |
| 2941 |
\ifcsname BNE_itself_#1#2\endcsname |
| 2942 |
\BNE_scanop_c |
| 2943 |
\fi\fi |
| 2944 |
\BNE_foundop #1#2% |
| 2945 |
}% |
| 2946 |
% \end{macrocode} |
| 2947 |
% If a postfix say |?s| is defined and |?r| is encountered the |?| will have |
| 2948 |
% been interpreted as a shortcut to |?s| and then the |r| will be found with |
| 2949 |
% the parser (after having executed the already found postfix) now looking for |
| 2950 |
% another operator so the error message will be |Operator? (got `r')| which is |
| 2951 |
% doubly confusing... well, let's not dwell on that. |
| 2952 |
% |
| 2953 |
% Update 2021/05/22, I have changed the message, as part of a systematic |
| 2954 |
% removal of |I<something>| invites, in part because |xint 1.4g| changed its |
| 2955 |
% expandable error method and now has a nice message saying \xintname will try |
| 2956 |
% to recover by itself. And now I have about 55 characters available for the |
| 2957 |
% message. |
| 2958 |
% \begin{macrocode} |
| 2959 |
\def\BNE_foundop_a #1% |
| 2960 |
{% |
| 2961 |
\ifcsname BNE_precedence_#1\endcsname |
| 2962 |
\csname BNE_precedence_#1\expandafter\endcsname |
| 2963 |
\expandafter #1% |
| 2964 |
\else |
| 2965 |
\expandafter\BNE_getop_a\romannumeral`&&@% |
| 2966 |
\xint_afterfi{\XINT_expandableerror |
| 2967 |
{Expected an operator but got `#1'. Ignoring.}}% |
| 2968 |
\fi |
| 2969 |
}% |
| 2970 |
\def\BNE_foundop #1{\csname BNE_precedence_#1\endcsname #1}% |
| 2971 |
% \end{macrocode} |
| 2972 |
% \subsection{Expansion spanning; opening and closing parentheses} |
| 2973 |
% \label{ssec:span} |
| 2974 |
% There was refactoring of expandable error messages at \xintname |1.4g| and I |
| 2975 |
% can now use up to 55 characters, but should not really invite user to |
| 2976 |
% |I|nsert something as it does not fit well with generic message saying |
| 2977 |
% \xintname will go ahead "hoping repair was complete". |
| 2978 |
% |
| 2979 |
% At |1.6|, we removed the |\BNE_start|, current |\BNE_bareeval| has its |
| 2980 |
% meaning rather than expanding to it as formerly. |
| 2981 |
% |
| 2982 |
% Also here macros are defined one by one so that it is easier to understand |
| 2983 |
% what is happening. Formerly |\BNE_tmpa| defined all of them in one go |
| 2984 |
% (as is still the case in upstream \xintexprname). |
| 2985 |
% \begin{macrocode} |
| 2986 |
\def\BNE_tmpa#1{% |
| 2987 |
\def\BNE_check##1% |
| 2988 |
{% |
| 2989 |
\xint_UDsignfork |
| 2990 |
##1{\expandafter\BNE_checkp\romannumeral`&&@#1}% |
| 2991 |
-{\BNE_checkp##1}% |
| 2992 |
\krof |
| 2993 |
}% |
| 2994 |
}\expandafter\BNE_tmpa\csname BNE_op_-xii\endcsname |
| 2995 |
\def\BNE_tmpa#1{% |
| 2996 |
\def\BNE_checkp##1##2% |
| 2997 |
{% |
| 2998 |
\ifcase ##1% |
| 2999 |
\expandafter\BNE_done |
| 3000 |
\or\expandafter#1% |
| 3001 |
\else |
| 3002 |
\expandafter\BNE_checkp |
| 3003 |
\romannumeral`&&@\csname BNE_op_##2\expandafter\endcsname |
| 3004 |
\fi |
| 3005 |
}% |
| 3006 |
}\expandafter\BNE_tmpa\csname BNE_extra_)\endcsname |
| 3007 |
\expandafter\def\csname BNE_extra_)\endcsname{% |
| 3008 |
\XINT_expandableerror |
| 3009 |
{An extra ) was removed. Hit <return>, fingers crossed.}% |
| 3010 |
\expandafter\BNE_check\romannumeral`&&@\expandafter\BNE_put_op_first |
| 3011 |
\romannumeral`&&@\BNE_getop_legacy |
| 3012 |
}% |
| 3013 |
\let\BNE_done\space |
| 3014 |
\def\BNE_getop_legacy #1% |
| 3015 |
{% |
| 3016 |
\expanded{\unexpanded{{#1}}\expandafter}\romannumeral`&&@\BNE_getop |
| 3017 |
}% |
| 3018 |
% \end{macrocode} |
| 3019 |
% Code style left untouched at |1.6|. |
| 3020 |
% \begin{macrocode} |
| 3021 |
\catcode`) 11 |
| 3022 |
\def\BNE_tmpa #1#2#3#4#5#6% |
| 3023 |
{% |
| 3024 |
\def #1##1% op_( |
| 3025 |
{% |
| 3026 |
\expandafter #4\romannumeral`&&@\BNE_getnext |
| 3027 |
}% |
| 3028 |
\def #2##1% op_) |
| 3029 |
{% |
| 3030 |
\expanded{\unexpanded{\BNE_put_op_first{##1}}\expandafter}% |
| 3031 |
\romannumeral`&&@\BNE_getop |
| 3032 |
}% |
| 3033 |
\def #3% oparen |
| 3034 |
{% |
| 3035 |
\expandafter #4\romannumeral`&&@\BNE_getnext |
| 3036 |
}% |
| 3037 |
\def #4##1% check- |
| 3038 |
{% |
| 3039 |
\xint_UDsignfork |
| 3040 |
##1{\expandafter#5\romannumeral`&&@#6}% |
| 3041 |
-{#5##1}% |
| 3042 |
\krof |
| 3043 |
}% |
| 3044 |
\def #5##1##2% checkp |
| 3045 |
{% |
| 3046 |
\ifcase ##1\expandafter\BNE_missing_) |
| 3047 |
\or \csname BNE_op_##2\expandafter\endcsname |
| 3048 |
\else |
| 3049 |
\expandafter #5\romannumeral`&&@\csname BNE_op_##2\expandafter\endcsname |
| 3050 |
\fi |
| 3051 |
}% |
| 3052 |
}% |
| 3053 |
\expandafter\BNE_tmpa |
| 3054 |
\csname BNE_op_(\expandafter\endcsname |
| 3055 |
\csname BNE_op_)\expandafter\endcsname |
| 3056 |
\csname BNE_oparen\expandafter\endcsname |
| 3057 |
\csname BNE_check-_)\expandafter\endcsname |
| 3058 |
\csname BNE_checkp_)\expandafter\endcsname |
| 3059 |
\csname BNE_op_-xii\endcsname |
| 3060 |
\let\BNE_precedence_)\xint_c_i |
| 3061 |
\def\BNE_missing_) |
| 3062 |
{\XINT_expandableerror{Missing ). Hit <return> to proceed.}% |
| 3063 |
\xint_c_ \BNE_done }% |
| 3064 |
\catcode`) 12 |
| 3065 |
% \end{macrocode} |
| 3066 |
% \subsection{The comma as binary operator} |
| 3067 |
% At |1.4|, it is simply a union operator for 1D oples. Inserting directly |
| 3068 |
% here a |<comma><space>| separator (as in earlier releases) in accumulated |
| 3069 |
% result would avoid having to do it on output but to the cost of diverging |
| 3070 |
% from \xintexprname upstream code, and to have to let the ``|unpack|'' |
| 3071 |
% (new name at |1.7|) |
| 3072 |
% routines handle comma separated values rather than braced values. |
| 3073 |
% |
| 3074 |
% Note bene: contiguous commas |,,| will cause the parser to raise an |
| 3075 |
% ``unexpected token'' error on the second comma, contrarily to |\xinteval| |
| 3076 |
% which silently ignores it since its |1.4| release (see the first subsection |
| 3077 |
% about basic terminology in \emph{Oples and nutples: the 1.4 terminology} in |
| 3078 |
% |xint.pdf|). |
| 3079 |
% |
| 3080 |
% It does work to use |\bnumdefinfix{,,}{\macro}{N}{N}|. Then |,,| acts as a |
| 3081 |
% custom infix operator without breaking the use of a single comma to |
| 3082 |
% separate inputs. |
| 3083 |
% \begin{macrocode} |
| 3084 |
\def\BNE_tmpa #1#2#3#4#5% |
| 3085 |
{% |
| 3086 |
\def #1##1% \BNE_op_, |
| 3087 |
{% |
| 3088 |
\expanded{\unexpanded{#2{##1}}\expandafter}% |
| 3089 |
\romannumeral`&&@\expandafter#3\romannumeral`&&@\BNE_getnext |
| 3090 |
}% |
| 3091 |
\def #2##1##2##3##4{##2##3{##1##4}}% \BNE_exec_, |
| 3092 |
\def #3##1% \BNE_check-_, |
| 3093 |
{% |
| 3094 |
\xint_UDsignfork |
| 3095 |
##1{\expandafter#4\romannumeral`&&@#5}% |
| 3096 |
-{#4##1}% |
| 3097 |
\krof |
| 3098 |
}% |
| 3099 |
\def #4##1##2% \BNE_checkp_, |
| 3100 |
{% |
| 3101 |
\ifnum ##1>\xint_c_iii |
| 3102 |
\expandafter#4% |
| 3103 |
\romannumeral`&&@\csname BNE_op_##2\expandafter\endcsname |
| 3104 |
\else |
| 3105 |
\expandafter##1\expandafter##2% |
| 3106 |
\fi |
| 3107 |
}% |
| 3108 |
}% |
| 3109 |
\expandafter\BNE_tmpa |
| 3110 |
\csname BNE_op_,\expandafter\endcsname |
| 3111 |
\csname BNE_exec_,\expandafter\endcsname |
| 3112 |
\csname BNE_check-_,\expandafter\endcsname |
| 3113 |
\csname BNE_checkp_,\expandafter\endcsname |
| 3114 |
\csname BNE_op_-xii\endcsname |
| 3115 |
\expandafter\let\csname BNE_precedence_,\endcsname\xint_c_iii |
| 3116 |
% \end{macrocode} |
| 3117 |
% \subsection{The minus as prefix operator of variable precedence level} |
| 3118 |
% This |\BNE_Op_opp| caused trouble at |1.4| as it must be \emph{f}-expandable, |
| 3119 |
% whereas earlier it expanded inside |\csname...\endcsname| context, so I |
| 3120 |
% could define it as \centeredline{|\if-#1\else\if0#10\else-#1\fi\fi|} where |
| 3121 |
% |#1| was the |
| 3122 |
% first token of unbraced argument but this meant at |1.4| an added |
| 3123 |
% |\xint_firstofone| here. Well let's return to sanity at |1.4a| and not add |
| 3124 |
% the |\xint_firstofone| and simply default |\BNE_Op_opp| to |\xintiiOpp|, |
| 3125 |
% which it should have been all along! And on this occasion let's trim user |
| 3126 |
% documentation of complications. |
| 3127 |
% |
| 3128 |
% The package used to need to define unary minus operator with precedences 12, |
| 3129 |
% 14, and 18. It also defined it at level 16 but this was unneedeed actually, |
| 3130 |
% no operator possibly generating usage of an |op_-xvi|. |
| 3131 |
% |
| 3132 |
% At |1.5| the right precedence of powers was lowered to 17, so we now need |
| 3133 |
% here only 12, 14, and 17. |
| 3134 |
% |
| 3135 |
% Due to |\bnumdefinfix| it is needed to support also, perhaps, the other |
| 3136 |
% levels 13, 15, 16, 18, .... This will be done only if necessary and is the |
| 3137 |
% reason why the macros |\BNE_defminus_a| and |\BNE_defminus_b| are given |
| 3138 |
% permanent names. In fact it is now |\BNE_defbin_b| which will decide to invoke |
| 3139 |
% or not the |\BNE_defminus_a|, and we activate it here only for the base |
| 3140 |
% precedence 12. |
| 3141 |
% |
| 3142 |
% The |\XINT_global|'s are absent from upstream \xintexprname as it does not |
| 3143 |
% incorporate yet some analog to |\bnumdefinfix/\bnumdefpostfix|. |
| 3144 |
% \begin{macrocode} |
| 3145 |
\def\BNE_defminus_b #1#2#3#4#5% |
| 3146 |
{% |
| 3147 |
\XINT_global\def #1% \BNE_op_-<level> |
| 3148 |
{% |
| 3149 |
\expandafter #2\romannumeral`&&@\expandafter#3% |
| 3150 |
\romannumeral`&&@\BNE_getnext |
| 3151 |
}% |
| 3152 |
\XINT_global\def #2##1##2##3% \BNE_exec_-<level> |
| 3153 |
{% |
| 3154 |
\expandafter ##1\expandafter ##2\expandafter |
| 3155 |
{\expandafter{\romannumeral`&&@\BNE_Op_opp##3}}% |
| 3156 |
}% |
| 3157 |
\XINT_global\def #3##1% \BNE_check-_-<level> |
| 3158 |
{% |
| 3159 |
\xint_UDsignfork |
| 3160 |
##1{\expandafter #4\romannumeral`&&@#1}% |
| 3161 |
-{#4##1}% |
| 3162 |
\krof |
| 3163 |
}% |
| 3164 |
\XINT_global\def #4##1##2% \BNE_checkp_-<level> |
| 3165 |
{% |
| 3166 |
\ifnum ##1>#5% |
| 3167 |
\expandafter #4% |
| 3168 |
\romannumeral`&&@\csname BNE_op_##2\expandafter\endcsname |
| 3169 |
\else |
| 3170 |
\expandafter ##1\expandafter ##2% |
| 3171 |
\fi |
| 3172 |
}% |
| 3173 |
}% |
| 3174 |
\def\BNE_defminus_a #1% |
| 3175 |
{% |
| 3176 |
\expandafter\BNE_defminus_b |
| 3177 |
\csname BNE_op_-#1\expandafter\endcsname |
| 3178 |
\csname BNE_exec_-#1\expandafter\endcsname |
| 3179 |
\csname BNE_check-_-#1\expandafter\endcsname |
| 3180 |
\csname BNE_checkp_-#1\expandafter\endcsname |
| 3181 |
\csname xint_c_#1\endcsname |
| 3182 |
}% |
| 3183 |
\BNE_defminus_a {xii}% |
| 3184 |
% \end{macrocode} |
| 3185 |
% \subsection{The infix operators.} |
| 3186 |
% I could have at the |1.4| refactoring injected usage of \cs{expanded} here, |
| 3187 |
% but kept in sync with upstream \xintexprname code. Any \emph{x}-expandable |
| 3188 |
% macro can easily be converted into an \emph{f}-expandable one using |
| 3189 |
% \cs{expanded}, so this is no serious limitation. |
| 3190 |
% |
| 3191 |
% Macro names are somewhat bad and there is much risk of confusion in future |
| 3192 |
% maintenance of |\BNE_Op_| prefix (used for |\BNE_Op_add| etc...; besides |
| 3193 |
% this should have been |\BNE_Op_Add|) and |\BNE_op_| prefix (used for |
| 3194 |
% |\BNE_op_+| etc...). |
| 3195 |
% |
| 3196 |
% At |1.5| decision is made to anticipate the announced upstream change to let |
| 3197 |
% the power operators be right associative, matching Python behaviour. This |
| 3198 |
% change is simply implemented by hardcoding in |\BNE_checkp_<op>| the right |
| 3199 |
% precedence which so far, for such operators, had been identical with the |
| 3200 |
% left precedence (upstream has examples of direct coding without |
| 3201 |
% formalization). In fact the right precedence existed already as argument to |
| 3202 |
% |\BNE_defbin_b| as the precedence to assign to unary minus following |<op>|. |
| 3203 |
% |
| 3204 |
% Note1: although it is easy to change the left precedence at user level, the |
| 3205 |
% right precedence is now more inaccessible. But on the other hand \bnumname |
| 3206 |
% provides |\bnumdefinfix| so all is customizable at user level. |
| 3207 |
% |
| 3208 |
% Note2: Tacit multiplication is not really a separate operator, it is |
| 3209 |
% the |*| with an elevated left precedence, which costs nothing to create and |
| 3210 |
% this precedence is stored in chardef token |\BNE_prec_tacit|. |
| 3211 |
% |
| 3212 |
% Compared to upstream, we use here numbers as arguments to |\BNE_defbin_b|, |
| 3213 |
% and convert to roman numerals internally, also the operator macro is passed |
| 3214 |
% as a control sequence not as its name (and |#6| and |#7| are permuted in |
| 3215 |
% |\BNE_defbin_c|). |
| 3216 |
% \begin{macrocode} |
| 3217 |
\def\BNE_defbin_c #1#2#3#4#5#6#7% |
| 3218 |
{% |
| 3219 |
\XINT_global\def #1##1% \BNE_op_<op> |
| 3220 |
{% |
| 3221 |
\expanded{\unexpanded{#2{##1}}\expandafter}% |
| 3222 |
\romannumeral`&&@\expandafter#3\romannumeral`&&@\BNE_getnext |
| 3223 |
}% |
| 3224 |
\XINT_global\def #2##1##2##3##4% \BNE_exec_<op> |
| 3225 |
{% |
| 3226 |
\expandafter##2\expandafter##3\expandafter |
| 3227 |
{\expandafter{\romannumeral`&&@#7##1##4}}% |
| 3228 |
}% |
| 3229 |
\XINT_global\def #3##1% \BNE_check-_<op> |
| 3230 |
{% |
| 3231 |
\xint_UDsignfork |
| 3232 |
##1{\expandafter#4\romannumeral`&&@#5}% |
| 3233 |
-{#4##1}% |
| 3234 |
\krof |
| 3235 |
}% |
| 3236 |
\XINT_global\def #4##1##2% \BNE_checkp_<op> |
| 3237 |
{% |
| 3238 |
\ifnum ##1>#6% |
| 3239 |
\expandafter#4% |
| 3240 |
\romannumeral`&&@\csname BNE_op_##2\expandafter\endcsname |
| 3241 |
\else |
| 3242 |
\expandafter ##1\expandafter ##2% |
| 3243 |
\fi |
| 3244 |
}% |
| 3245 |
}% |
| 3246 |
\def\BNE_defbin_b #1#2#3#4% |
| 3247 |
{% |
| 3248 |
\expandafter\BNE_defbin_c |
| 3249 |
\csname BNE_op_#1\expandafter\endcsname |
| 3250 |
\csname BNE_exec_#1\expandafter\endcsname |
| 3251 |
\csname BNE_check-_#1\expandafter\endcsname |
| 3252 |
\csname BNE_checkp_#1\expandafter\endcsname |
| 3253 |
\csname BNE_op_-\romannumeral\ifnum#3>12 #3\else 12\fi |
| 3254 |
\expandafter\endcsname |
| 3255 |
\csname xint_c_\romannumeral#3\endcsname #4% |
| 3256 |
\XINT_global |
| 3257 |
\expandafter |
| 3258 |
\let\csname BNE_precedence_#1\expandafter\endcsname |
| 3259 |
\csname xint_c_\romannumeral#2\endcsname |
| 3260 |
\unless |
| 3261 |
\ifcsname BNE_exec_-\romannumeral\ifnum#3>12 #3\else 12\fi\endcsname |
| 3262 |
% \end{macrocode} |
| 3263 |
% This will execute only for |#3>12| as |\BNE_exec_-xii| exists. |
| 3264 |
% \begin{macrocode} |
| 3265 |
\expandafter\BNE_defminus_a\expandafter{\romannumeral#3}% |
| 3266 |
\fi |
| 3267 |
}% |
| 3268 |
\BNE_defbin_b + {12} {12} \BNE_Op_add |
| 3269 |
\BNE_defbin_b - {12} {12} \BNE_Op_sub |
| 3270 |
\BNE_defbin_b * {14} {14} \BNE_Op_mul |
| 3271 |
\BNE_defbin_b / {14} {14} \BNE_Op_divround |
| 3272 |
\BNE_defbin_b {//} {14} {14} \BNE_Op_div |
| 3273 |
\BNE_defbin_b {/:} {14} {14} \BNE_Op_mod |
| 3274 |
\BNE_defbin_b ^ {18} {17} \BNE_Op_pow |
| 3275 |
% \end{macrocode} |
| 3276 |
% \xintexprname uses shortcut |
| 3277 |
% \centeredline{|\expandafter\def\csname XINT_expr_itself_**\endcsname {^}|} |
| 3278 |
% But doing it would mean that any redefinition of |^| propagates to |**|. |
| 3279 |
% And it |
| 3280 |
% creates a special case which would need consideration by |
| 3281 |
% |\BNE_dotheitselves|, or special restrictions to add to user documentation. |
| 3282 |
% Better to simply handle |**| as a full operator. |
| 3283 |
% \begin{macrocode} |
| 3284 |
\BNE_defbin_b {**} {18} {17} \BNE_Op_pow |
| 3285 |
\expandafter\def\csname BNE_itself_**\endcsname {**}% |
| 3286 |
\expandafter\def\csname BNE_itself_//\endcsname {//}% |
| 3287 |
\expandafter\def\csname BNE_itself_/:\endcsname {/:}% |
| 3288 |
\let\BNE_prec_tacit\xint_c_xvi |
| 3289 |
% \end{macrocode} |
| 3290 |
% \subsection{Extending the syntax: \cs{bnumdefinfix}, \cs{bnumdefpostfix}} |
| 3291 |
% \subsubsection{\cs{bnumdefinfix}} |
| 3292 |
% |#1| gives the operator characters, |#2| the associated macro, |#3| its |
| 3293 |
% left-precedence and |#4| its right precedence (as integers). |
| 3294 |
% |
| 3295 |
% The "itself" definitions are done in such a way that unambiguous abbreviations |
| 3296 |
% work; but in case of ambiguity the first defined operator is used. |
| 3297 |
% |
| 3298 |
% However, if for example operator |$a| was defined after |$ab|, then although |
| 3299 |
% |$| will use |$ab| which was defined first, |$a| will use as expected the |
| 3300 |
% second defined operator. |
| 3301 |
% |
| 3302 |
% The mismatch |\BNE_defminus_a| vs |\BNE_defbin_b| is inherited from |
| 3303 |
% upstream, I keep it to simplify maintenance. |
| 3304 |
% \begin{macrocode} |
| 3305 |
\def\bnumdefinfix #1#2#3#4% |
| 3306 |
{% |
| 3307 |
\edef\BNE_tmpa{#1}% |
| 3308 |
\edef\BNE_tmpa{\xint_zapspaces_o\BNE_tmpa}% |
| 3309 |
\edef\BNE_tmpL{\the\numexpr#3\relax}% |
| 3310 |
\edef\BNE_tmpL{\ifnum\BNE_tmpL<4 4\else\ifnum\BNE_tmpL<23 \BNE_tmpL\else 22\fi\fi}% |
| 3311 |
\edef\BNE_tmpR{\the\numexpr#4\relax}% |
| 3312 |
\edef\BNE_tmpR{\ifnum\BNE_tmpR<4 4\else\ifnum\BNE_tmpR<23 \BNE_tmpR\else 22\fi\fi}% |
| 3313 |
\BNE_defbin_b \BNE_tmpa\BNE_tmpL\BNE_tmpR #2% |
| 3314 |
\expandafter\BNE_dotheitselves\BNE_tmpa\relax |
| 3315 |
\ifxintverbose |
| 3316 |
\PackageInfo{bnumexpr}{infix operator \BNE_tmpa\space |
| 3317 |
\ifxintglobaldefs globally \fi |
| 3318 |
does |
| 3319 |
\unexpanded{#2}\MessageBreak with precedences \BNE_tmpL, \BNE_tmpR;}% |
| 3320 |
\fi |
| 3321 |
}% |
| 3322 |
\def\BNE_dotheitselves#1#2% |
| 3323 |
{% |
| 3324 |
\if#2\relax\expandafter\xint_gobble_ii |
| 3325 |
\else |
| 3326 |
\XINT_global |
| 3327 |
\expandafter\edef\csname BNE_itself_#1#2\endcsname{#1#2}% |
| 3328 |
\unless\ifcsname BNE_precedence_#1\endcsname |
| 3329 |
\XINT_global |
| 3330 |
\expandafter\edef\csname BNE_precedence_#1\endcsname |
| 3331 |
{\csname BNE_precedence_\BNE_tmpa\endcsname}% |
| 3332 |
\XINT_global |
| 3333 |
\expandafter\odef\csname BNE_op_#1\endcsname |
| 3334 |
{\csname BNE_op_\BNE_tmpa\endcsname}% |
| 3335 |
\fi |
| 3336 |
\fi |
| 3337 |
\BNE_dotheitselves{#1#2}% |
| 3338 |
}% |
| 3339 |
% \end{macrocode} |
| 3340 |
% \subsubsection{\cs{bnumdefpostfix}} |
| 3341 |
% Support macros for postfix operators only need to be \emph{x}-expandable. |
| 3342 |
% \begin{macrocode} |
| 3343 |
\def\bnumdefpostfix #1#2#3% |
| 3344 |
{% |
| 3345 |
\edef\BNE_tmpa{#1}% |
| 3346 |
\edef\BNE_tmpa{\xint_zapspaces_o\BNE_tmpa}% |
| 3347 |
\edef\BNE_tmpL{\the\numexpr#3\relax}% |
| 3348 |
\edef\BNE_tmpL{\ifnum\BNE_tmpL<4 4\else\ifnum\BNE_tmpL<23 \BNE_tmpL\else 22\fi\fi}% |
| 3349 |
\XINT_global |
| 3350 |
\expandafter\let\csname BNE_precedence_\BNE_tmpa\expandafter\endcsname |
| 3351 |
\csname xint_c_\romannumeral\BNE_tmpL\endcsname |
| 3352 |
\XINT_global |
| 3353 |
\expandafter\def\csname BNE_op_\BNE_tmpa\endcsname ##1% |
| 3354 |
{% |
| 3355 |
\expandafter\BNE_put_op_first |
| 3356 |
\expanded{{{#2##1}}\expandafter}\romannumeral`&&@\BNE_getop |
| 3357 |
}% |
| 3358 |
\expandafter\BNE_dotheitselves\BNE_tmpa\relax |
| 3359 |
\ifxintverbose |
| 3360 |
\PackageInfo{bnumexpr}{postfix operator \BNE_tmpa\space |
| 3361 |
\ifxintglobaldefs globally \fi |
| 3362 |
does \unexpanded{#2}\MessageBreak |
| 3363 |
with precedence \BNE_tmpL;}% |
| 3364 |
\fi |
| 3365 |
}% |
| 3366 |
% \end{macrocode} |
| 3367 |
% \subsection{! as postfix factorial operator} |
| 3368 |
% \begin{macrocode} |
| 3369 |
\bnumdefpostfix{!}{\BNE_Op_fac}{20}% |
| 3370 |
% \end{macrocode} |
| 3371 |
% \subsection{Cleanup} |
| 3372 |
% \begin{macrocode} |
| 3373 |
\let\BNE_tmpa\relax \let\BNE_tmpb\relax \let\BNE_tmpc\relax |
| 3374 |
\let\BNE_tmpR\relax \let\BNE_tmpL\relax |
| 3375 |
\BNErestorecatcodesendinput% |
| 3376 |
% \end{macrocode} |
| 3377 |
% \MakePercentComment |
| 3378 |
%</package> |
| 3379 |
%<*dtx> |
| 3380 |
\CheckSum {1670}% |
| 3381 |
\makeatletter\check@checksum\makeatother% |
| 3382 |
\Finale% |
| 3383 |
%% End of file xint.dtx |