summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/bnumexpr/bnumexpr.dtx
blob: 2f9a86664957ac9e032d461985ed8ea47c681b68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
% -*- coding: iso-latin-1; time-stamp-format: "%02d-%02m-%:y at %02H:%02M:%02S %Z" -*-
%<*dtx>
\def\bnedtxtimestamp  {Time-stamp: <21-09-2014 at 15:24:58 CEST>}
%</dtx>
%
% File: bnumexpr.dtx
% Version:  2014/09/21, v1.1
% Info: Expressions with big integers
%
% Copyright (C) 2014 by Jean-Francois Burnol <jfbu at free dot fr>
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3c of this license or (at your option) any later
% version. This version of this license is in
%    http://www.latex-project.org/lppl/lppl-1-3c.txt
% and the latest version of this license is in
%    http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of
% LaTeX version 2005/12/01 or later.
%
% This work has the LPPL maintenance status "maintained".
%
% This Current Maintainer of this work is Jean-Francois Burnol.
%
% This work consists of the main source file bnumexpr.dtx
% and the derived files
%    bnumexpr.sty, bnumexpr.pdf, bnumexpr.ins, bnumexpr.tex
%
% Usage
% =====
%
% \usepackage{bnumexpr}
%
% Then \thebnumexpr <expression with +,-,*,/,(,)> \relax is like 
%      \the\numexpr <expression with +,-,*,/,(,)> \relax
% with the difference of accepting or producing arbitrarily big
% integers.
%
% Example:
%      \thebnumexpr 30*(21-43*(512-67*(6133-812*2897)))\relax
% outputs:
%      -202785405180
% which would create an arithmetic overflow in \numexpr.
%
% \bnumexpr...\relax is a scaled down version of \xintiiexpr...\relax
% from package xintexpr. 
%
% By default, bnumexpr.sty loads xint.sty for its arithmetic macros
% doing addition, subtraction, multiplication, division. 
%
% With option custom, xint.sty is not loaded and it is up to the user 
% to define \bnumexprAdd, \bnumexprSub, \bnumexprMul, \bnumexprDiv
%
% Option bigintcalc maps them to the macros from package bigintcalc.
%
% Option allowpower enables ^ as power operator.
%
% Installation
% ============
%
% obtain bnumexpr.dtx, bnumexpr.ins and the README from CTAN:
%   http://www.ctan.org/pkg/bnumexpr
%
% - "tex bnumexpr.ins" generates the style file
% (pre-existing file in the same repertory will be overwritten).
%
% - without bnumexpr.ins: "tex or latex or pdflatex or xelatex bnumexpr.dtx" 
% will also generate the style files (and bnumexpr.ins).
%
% bnumexpr.tex is also extracted, use it for the documentation:
%
% - with latex+dvipdfmx: latex bnumexpr.tex thrice then dvipdfmx bnumexpr.dvi
% Ignore dvipdfmx warnings, but if the pdf file has problems with fonts
% (possibly from an old dvipdfmx), use then rather pdflatex or xelatex.
%
% - with pdflatex or xelatex: run it directly thrice on bnumexpr.dtx, or run 
% it on bnumexpr.tex after having edited the suitable toggle therein.
%
% Finishing the installation: (on first installation the destination
% repertories may need to be created)
%
%            bnumexpr.sty   --> TDS:tex/latex/bnumexpr/ 
%
%            bnumexpr.dtx   --> TDS:source/latex/bnumexpr/
%            bnumexpr.ins   --> TDS:source/latex/bnumexpr/
%
%            bnumexpr.pdf   --> TDS:doc/latex/bnumexpr/
%                  README   --> TDS:doc/latex/bnumexpr/
%
%<*drv>
\def\bnedocdate {2014/09/21} % package bnumexpr documentation date
\def\bnepackdate{2014/09/21} % package bnumexpr date
\def\bneversion {1.1}        % package bnumexpr version
%</drv>
%%----------------------------------------------------------------
%% The bnumexpr package: Expressions with big integers
%% Copyright (C) 2014 by Jean-Francois Burnol 
%%----------------------------------------------------------------
%<*dtx>
\iffalse
%</dtx>
%<*drv>----------------------------------------------------------------------
%% This is a generated file. 
%%
%% latex bnumexpr.tex (thrice) && dvipdfmx bnumexpr.dvi --> bnumexpr.dtx
%%
%% See bnumexpr.dtx for the copyright and license statements.
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesFile{bnumexpr.tex}%
[\bnepackdate\space v\bneversion\space driver file for %
  bnumexpr documentation (jfB)]%
\PassOptionsToClass{a4paper,fontsize=11pt}{scrdoc}
\chardef\Withdvipdfmx 1 % replace 1 by 0 for using pdflatex
\chardef\NoSourceCode 0 % replace 0 by 1 for not including source code
\input bnumexpr.dtx
%%% Local Variables:
%%% mode: latex
%%% End:
%</drv>----------------------------------------------------------------------
%<*ins>-------------------------------------------------------------------------
%% This is a generated file.
%% 
%% tex bnumexpr.ins  will extract bnumexpr.sty and bnumexpr.tex
%% from source file bnumexpr.dtx
%%
%% See bnumexpr.dtx for the copyright and license statements.
%%
\input docstrip.tex
\askforoverwritefalse
\generate{\nopreamble
\file{bnumexpr.tex}{\from{bnumexpr.dtx}{drv}}
\usepreamble\defaultpreamble
\file{bnumexpr.sty}{\from{bnumexpr.dtx}{package}}}
\catcode32=13\relax% active space
\let =\space%
\Msg{************************************************************************}
\Msg{*}
\Msg{* To finish the installation you have to move the following}
\Msg{* files into a directory searched by TeX:}
\Msg{*}
\Msg{*     bnumexpr.sty} 
\Msg{*}
\Msg{* To produce the documentation run latex thrice on bnumexpr.tex}
\Msg{* then dvipdfmx on bnumexpr.dvi. (ignore the dvipdfmx warnings)}
\Msg{*}
\Msg{* Happy TeXing!}
\Msg{*}
\Msg{************************************************************************}
\endbatchfile
%</ins>-------------------------------------------------------------------------
%<*dtx>
\fi % end of \iffalse block
\chardef\noetex 0
\expandafter\ifx\csname numexpr\endcsname\relax \chardef\noetex 1 \fi
\ifnum\noetex=1 \chardef\extractfiles 0 % extract files, then stop
\else
    \expandafter\ifx\csname ProvidesFile\endcsname\relax
      \chardef\extractfiles 0 % etex etc.. on bnumexpr.dtx
    \else % latex/pdflatex on bnumexpr.tex or on bnumexpr.dtx
      \expandafter\ifx\csname Withdvipdfmx\endcsname\relax
        % latex run is on bnumexpr.dtx, we will extract all files
        \chardef\extractfiles 1 % 1 = extract and typeset, 2=only typeset
        \chardef\Withdvipdfmx 0 % 1 = use dvipdfmx, 0 = use pdflatex 
        \chardef\NoSourceCode 0 % 1 = do not include source code
        \NeedsTeXFormat{LaTeX2e}%
        \PassOptionsToClass{a4paper,fontsize=11pt}{scrdoc}% 
      \else % latex run is on etoc.tex, 
        \chardef\extractfiles 2 % no extractions
      \fi
      \ProvidesFile{bnumexpr.dtx}[bundle source (\bnedtxtimestamp)]%
    \fi
\fi
\ifnum\extractfiles<2 % extract files
\def\MessageDeFin{\newlinechar10 \let\Msg\message
\Msg{^^J}%
\Msg{********************************************************************^^J}%
\Msg{*^^J}%
\Msg{* To finish the installation you have to move the following^^J}%
\Msg{* files into a directory searched by TeX:^^J}%
\Msg{*^^J}%
\Msg{*\space\space\space\space bnumexpr.sty^^J}%
\Msg{*^^J}%
\Msg{* To produce the documentation run latex thrice on bnumexpr.tex^^J}%
\Msg{* then dvipdfmx on bnumexpr.dvi. (ignore the dvipdfmx warnings)^^J}%
\Msg{*^^J}%
\Msg{* Happy TeXing!^^J}%
\Msg{*^^J}%
\Msg{********************************************************************^^J}%
}%
\begingroup
    \input docstrip.tex
    \askforoverwritefalse
    \generate{\nopreamble
    \file{bnumexpr.ins}{\from{bnumexpr.dtx}{ins}}
    \file{bnumexpr.tex}{\from{bnumexpr.dtx}{drv}}
    \usepreamble\defaultpreamble
    \file{bnumexpr.sty}{\from{bnumexpr.dtx}{package}}}
\endgroup
\fi % end of file extraction
\ifnum\extractfiles=0 
% direct tex/etex/xetex/etc on bnumexpr.dtx, files now extracted, stop
  \MessageDeFin\expandafter\end
\fi
% no use of docstrip to extract files if latex compilation was on bnumexpr.tex
\ifdefined\MessageDeFin\AtEndDocument{\MessageDeFin}\fi
%-------------------------------------------------------------------------------
\documentclass {scrdoc}
\ifnum\NoSourceCode=1 \OnlyDescription\fi
\makeatletter
\ifnum\Withdvipdfmx=1
   \@for\@tempa:=hyperref,bookmark,graphicx,xcolor,pict2e\do
            {\PassOptionsToPackage{dvipdfmx}\@tempa}
   %
   \PassOptionsToPackage{dvipdfm}{geometry}
   \PassOptionsToPackage{bookmarks=true}{hyperref}
   \PassOptionsToPackage{dvipdfmx-outline-open}{hyperref}
   \PassOptionsToPackage{dvipdfmx-outline-open}{bookmark}
   %
   \def\pgfsysdriver{pgfsys-dvipdfm.def}
\else
   \PassOptionsToPackage{bookmarks=true}{hyperref}
\fi
\makeatother

\pagestyle{headings}
\makeatletter
\def\buggysectionmark #1{% KOMA 3.12 as released to CTAN December 2013
    \if@twoside\expandafter\markboth\else\expandafter\markright\fi
    {\MakeMarkcase{\ifnumbered{section}{\sectionmarkformat\fi}{}#1}}{}}
\ifx\buggysectionmark\sectionmark
\def\sectionmark #1{%
    \if@twoside\expandafter\markboth\else\expandafter\markright\fi
    {\MakeMarkcase{\ifnumbered{section}{\sectionmarkformat}{}#1}}{}}
\fi
\makeatother

\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}

\usepackage[hscale=0.66,vscale=0.75]{geometry}

\usepackage[zerostyle=a,scaled=0.95]{newtxtt}
\renewcommand\familydefault\ttdefault
\usepackage[noendash]{mathastext}
\renewcommand\familydefault\sfdefault

\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\definecolor{joli}{RGB}{225,95,0}
\definecolor{JOLI}{RGB}{225,95,0}
\definecolor{BLUE}{RGB}{0,0,255}
\definecolor{niceone}{RGB}{38,128,192}
\colorlet{jfverbcolor}{yellow!5}

\usepackage[english]{babel}
\usepackage[autolanguage,np]{numprint}
\AtBeginDocument{\npthousandsep{,\hskip .5pt plus .1pt minus .1pt}}

\usepackage[pdfencoding=pdfdoc]{hyperref}
\hypersetup{%
%linktoc=all,%
breaklinks=true,%
colorlinks=true,%
urlcolor=niceone,%
linkcolor=blue,%
pdfauthor={Jean-Fran\c cois Burnol},%
pdftitle={The bnumexpr package},%
pdfsubject={Arithmetic with TeX},%
pdfkeywords={Expansion, arithmetic, TeX},%
pdfstartview=FitH,%
pdfpagemode=UseOutlines}
\usepackage{bookmark} 

%---- \centeredline: OUR OWN LITTLE MACRO FOR CENTERING LINES

% 7 mars 2013
% This macro allows to conveniently center a line inside a paragraph and still
% use therein \verb or other commands changing catcodes.
% A proposito, the \LaTeX \centerline uses \hsize and not \linewidth ! 
% (which in my humble opinion is bad)

% \ignorespaces ajouté le 9 juin.

\makeatletter
\newcommand*\centeredline {%
      \ifhmode \\\relax
        \def\centeredline@{\hss\egroup\hskip\z@skip\ignorespaces }%
      \else 
        \def\centeredline@{\hss\egroup }%
      \fi
      \afterassignment\@centeredline
      \let\next=}
\def\@centeredline 
    {\hbox to \linewidth \bgroup \hss \bgroup \aftergroup\centeredline@ }
\makeatother


%---- \verb, and verbatim like `environments'. \MicroFont et \MacroFont
\def\MicroFont {\ttfamily }
\def\MacroFont {\ttfamily\baselineskip12pt\relax}
\makeatletter

\def\lowast{\raisebox{-.25\height}{*}}
\begingroup
   \catcode`* 13
   \gdef\makestarlowast {\let*\lowast\catcode`\*\active}%
\endgroup

% modif de \do@noligs: \char`#1} --> \char`#1 } 
\def\do@noligs #1%
{%
    \catcode `#1\active 
    \begingroup \lccode `\~=`#1\relax 
    \lowercase {\endgroup \def ~{\leavevmode \kern \z@ \char `#1 }}%
}% 
% Tentative, Mardi 09 septembre 2014 à 22:41:28
\def\verb 
% on pourrait mettre le #1 ici et économiser \@@jfverb
{%
  \relax\leavevmode\null
  \begingroup\MicroFont
  \let\do\do@noligs  \verbatim@nolig@list % voir plus tard si vraiment
                                % nécessaire maintenant
  \let\do\@makeother \dospecials 
                \makestarlowast
  \@vobeyspaces \fboxsep0pt
  \@@jfverb 
}% 

\def\@@jfverb #1{\catcode`#1 3 \@@@jfverb }

\def\@@@jfverb #1{\ifcat\noexpand#1$% $
                      \endgroup\else 
      \penalty\z@
      \colorbox{jfverbcolor}{\strut #1}%
      \expandafter\@@@jfverb\fi }

\makeatother
\catcode`\_=11

\def\csa_aux #1{\ttfamily\hyphenchar\font45 \char`\\%
                \scantokens{#1}\endgroup }

\DeclareRobustCommand\csa {\begingroup\catcode`\_=11
                           \everyeof{\noexpand}\endlinechar -1
                           \makeatother
                           \makestarlowast
                           \csa_aux }
\newcommand\csh[1]{\texorpdfstring{\csa{#1}}{\textbackslash #1}}
\catcode`\_=8

\usepackage{xspace}

\def\bnename
   {\texorpdfstring
          {\hyperref[sec:bnumexpr]%
             {{\color{joli}\bfseries\ttfamily\hyphenchar\font45 bnumexpr}}}
          {bnumexpr}%
     \xspace }%
\def\bnenameimp
   {\texorpdfstring
              {\hyperref[sec:bnumexprimp]%
                {{\color[named]{RoyalPurple}%
                 \bfseries\ttfamily\hyphenchar\font45 bnumexpr}}}
              {bnumexpr}%
    \xspace }%


\frenchspacing
% possible options: custom, bigintcalc, nocsv, notacitmul, allowpower
\usepackage[allowpower]{bnumexpr}

\usepackage{etoc}

\begin{document}
\thispagestyle{empty}
\ttzfamily
\pdfbookmark[1]{Title page}{TOP}

{%
\normalfont\Large\parindent0pt \parfillskip 0pt\relax 
 \leftskip 2cm plus 1fil \rightskip 2cm plus 1fil
 The \bnename package\par
}

{\centering
  \textsc{Jean-François Burnol}\par
  \footnotesize 
  jfbu (at) free (dot) fr\par
  Package version: \bneversion\ (\bnepackdate); 
            documentation date: \bnedocdate.\par
  {From source file \texttt{bnumexpr.dtx}. \bnedtxtimestamp.}\par
}

\etocsetnexttocdepth{section}
\tableofcontents

\section{Introduction}
\label{sec:bnumexpr}

Package \bnename provides |\bnumexpr...\relax| which is analogous to
|\numexpr...\relax|, while allowing arbitrarily big
integers. Important items:
\begin{enumerate}
\item the |\relax| token ending the expression is mandatory,
\item one must use either |\thebnumexpr| or |\bnethe\bnumexpr| to get a
  printable result, as |\bnumexpr...\relax| expands to a private format;
  however one may embed directly one |\bnumexpr...\relax| in another
  |\bnumexpr...\relax|,
\item one may do |\edef\tmp{\bnumexpr 1+2\relax}|, and then either use |\tmp| in
  another |\bnumexpr...\relax|, or print it via |\bnethe\tmp|. The computation
  is done at the time of the |\edef| (and two expansion steps suffice),
\item tacit multiplication applies in front of parenthesized sub-expressions, or
  sub |\bnumexpr...\relax| (or |\numexpr...\relax|), or in front of a |\count|
  or |\dimen| register. This may be de-activated by option |notacitmul|,
\item expressions may be comma separated. On input, spaces are ignored,
  naturally, and on output the values are comma separated with a space after
  each comma. This functionality may be turned off via option |nocsv|,
\item even with options |notacitmul| and |nocsv| the syntax is more flexible
  than with |\numexpr|: things such as 
  |\bnumexpr -(1+1)\relax| are legal.
\end{enumerate}

The parser |\bnumexpr| is a scaled-down version of parser |\xintiiexpr| from
package \href{http://www.ctan.org/pkg/xint}{xintexpr}: support for boolean
operators, functions such as |abs|, |max|, |lcm|, the |!| as factorial,
handling of hexadecimal numbers, etc\dots has
been removed. The goal here is to extend |\numexpr| only to the extent of
accepting big integers. Thus by default, the syntax allows |+,-,*,/|,
parentheses, and also |\count| or |\dimen| registers or variables. Option
|allowpower| enables |^| as power operator.

Of course, one
needs some underlying big integer engine to provide the macros doing the
actual computations. By default, \bnename uses package |xint| and its
|\xintiiAdd|, |\xintiiSub|, and |\xintiiMul| macros (also |\xintiiPow| if
option |allowpower| is made use of). As we want here |/| to do
rounded division while |xint|'s |\xintiiQuo| does Euclidean division,
\bnename contains a few extra code lines on top of the underlying
division macros from |xint.sty|.

See the discussion of options |bigintcalc| and |custom| in
\autoref{sec:options} for alternatives.

The starting point for the |\bnumexpr| parser was not the |\xintiiexpr| version
|1.09n| as available (at the time of writing) on CTAN, but a development
version for future release |1.1|. This is why the version number of package
|bnumexpr| is |1.1|.
It may well be that the code of the parser is in some places quite sub-optimal
from the fact that it was derived from code handling much more stuff.

The |\xintNewExpr| construct has been left out.

I recall from documentation of |xintexpr| that there is a potential impact on
the memory of \TeX{} (the hash table) because each arithmetic operation is
done inside a dummy |\csname...\endcsname| used as single token to move
around in one-go the possibly hundreds of digits composing a number.

\section{Options}\label{sec:options}

Option |bigintcalc| says to map the infix operators to the macros from
package \href{http://www.ctan.org/pkg/bigintcalc}{bigintcalc} by \textsc{Heiko
  Oberdiek}. Note though that |/| is mapped to |\bigintcalcDiv| which does
\emph{truncated} (not rounded) division.

Option |custom| leaves it up to the user to specify the macros to be used,
i.e. provide definitions for |\bnumexprAdd|, |\bnumexprSub|, |\bnumexprMul|,
|\bnumexprDiv| (and possibly |\bnumexprPow|). Without it, the package does by
default:
\begin{verbatim}
\RequirePackage{xint}
\let\bnumexprAdd\xintiiAdd
\let\bnumexprSub\xintiiSub
\let\bnumexprMul\xintiiMul
%% \bnumexprDiv has custom definition on top of macros from xint.sty
\let\bnumexprPow\xintiiPow % only if option allowpower
\end{verbatim}
To let |/| do euclidean division (like currently in |\xintiiexpr|) it is
thus sufficient to do |\let\bnumexprDiv\xintiiQuo| after loading the package.

If using option |custom|: the four arithmetic macros |\bnumexprAdd|,
|\bnumexprSub|, |\bnumexprMul|, |\bnumexprDiv| (and possibly |\bnumexprPow|)
must be expandable, and they must allow arguments in need to be first (`f'-)
expanded. They should produce on output (big) integers with no leading zeros, at
most one minus sign and no plus sign (else the \bnename macro used for
handling the |-| prefix operator may need to be modified). They will be
expanded inside |\csname...\endcsname|. The macros from |xint.sty| (as well as
those of |bigintcalc.sty|) are expandable in a stronger sense (only two
expansion steps suffice). Perhaps speed gains are achievable from dropping
these stronger requirements.


Option |nocsv| makes comma separated expressions illegal. 

Option |notacitmul| removes the possibility of tacit multiplication in front of
parentheses, \string\count\space registers, sub-expressions.

Option |allowpower| enables the |^| as power operator (left associative).

\section{Examples}

\centeredline{|\thebnumexpr 128637867168*2187917891279\relax|}
\thebnumexpr 128637867168*2187917891279\relax

\noindent\centeredline{|\thebnumexpr 30*(21-43*(512-67*(6133-812*2897)))\relax|}
\thebnumexpr 30*(21-43*(512-67*(6133-812*2897)))\relax

\noindent\centeredline{|\newcount\cnta \cnta 123|}
\centeredline{|\the\numexpr \cnta*\cnta*\cnta\relax=\thebnumexpr \cnta*\cnta*\cnta\relax|}
\newcount\cnta\cnta 123
\the\numexpr \cnta*\cnta*\cnta\relax=\thebnumexpr \cnta*\cnta*\cnta\relax

\noindent\centeredline{|\newcount\cntb \cntb 188|}
\centeredline{|\the\numexpr \cnta*\cnta*\cnta/\cntb+\cntb*\cntb*\cntb/\cnta\relax=|}
\centeredline{|\thebnumexpr \cnta*\cnta*\cnta/\cntb+\cntb*\cntb*\cntb/\cnta\relax|}
\newcount\cntb \cntb 188
\the\numexpr \cnta*\cnta*\cnta/\cntb+\cntb*\cntb*\cntb/\cnta\relax
=\thebnumexpr \cnta*\cnta*\cnta/\cntb+\cntb*\cntb*\cntb/\cnta\relax
\centeredline{|\the\numexpr \cnta*\cnta*(\cnta/\cntb)+\cntb*\cntb*(\cntb/\cnta)\relax=|}
\centeredline{|\thebnumexpr \cnta*\cnta*(\cnta/\cntb)+\cntb*\cntb*(\cntb/\cnta)\relax|}
\the\numexpr \cnta*\cnta*(\cnta/\cntb)+\cntb*\cntb*(\cntb/\cnta)\relax
=\thebnumexpr \cnta*\cnta*(\cnta/\cntb)+\cntb*\cntb*(\cntb/\cnta)\relax
\centeredline{|\the\numexpr 123/188*188\relax|, 
               |\the\numexpr 123/(188*188)\relax|,}
\centeredline{|\thebnumexpr 123/188*188\relax|,
               |\thebnumexpr 123/(188*188)\relax|.}
\the\numexpr 123/188*188\relax, 
               \the\numexpr 123/(188*188)\relax,
\thebnumexpr 123/188*188\relax, 
               \thebnumexpr 123/(188*188)\relax.

\noindent
\centeredline{|\edef\tmp {\bnumexpr 121873197*123-218137917*188\relax}|}
\centeredline{|\bnethe\tmp\ (\meaning\tmp)|}
\centeredline{|\thebnumexpr \tmp*(173197129797-\tmp)*(2179171982-\tmp)\relax|}
\edef\tmp {\bnumexpr 121873197*123-218137917*188\relax}%
\bnethe\tmp\ \mbox{(\meaning\tmp)}\\
\thebnumexpr \tmp*(173197129797-\tmp)*(2179171982-\tmp)\relax

\noindent
\centeredline {|\cnta \thebnumexpr 2152966419779999/987654321\relax\space \the\cnta|}
\cnta \thebnumexpr 2152966419779999/987654321\relax\space \the\cnta

\noindent
\centeredline {|\thebnumexpr 2179878*987654321-2152966419779999,2179879*987654321-2152966419779999\relax|}
\thebnumexpr  2179878*987654321-2152966419779999,2179879*987654321-2152966419779999\relax\ (there was indeed rounding of the exact quotient)

\medskip
\noindent
An example with the power operator |^| (option |allowpower|):
\centeredline {|\thebnumexpr (1^10+2^10+3^10+4^10+5^10+6^10)^3\relax|}
\thebnumexpr (1^10+2^10+3^10+4^10+5^10+6^10)^3\relax


\StopEventually{\end{document}\endinput}

\newgeometry{hscale=0.75}

\MakePercentIgnore
% 
% \catcode`\<=0 \catcode`\>=11 \catcode`\*=11 \catcode`\/=11
% \let</dtx>\relax
% \def<*package>{\catcode`\<=12 \catcode`\>=12 \catcode`\*=12 \catcode`\/=12 }
%</dtx>
%<*package>
%
% \section{Package \bnenameimp implementation}
%
% \localtableofcontents \bigskip 
%
% Comments are sparse. Error handling by the parser is kept to a minimum; if
% something goes wrong, the offensive token gets discarded, and some undefined
% control sequence attempts to trigger writing to the log of some sort of
% informative message. It is recommended to set |\errorcontextlines| to at
% least |2| for more meaningful context.
% \label{sec:bnumexprimp}
% \subsection{Package identification and catcode setup}
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}%
\ProvidesPackage{bnumexpr}[2014/09/21 v1.1 Expressions with big integers (jfB)]%
\edef\BNErestorecatcodes {\catcode`\noexpand\!\the\catcode`\!
                   \catcode`\noexpand\?\the\catcode`\?
                   \catcode`\noexpand\_\the\catcode`\_
                   \catcode`\noexpand\:\the\catcode`\:\relax }%
\catcode`\! 11
\catcode`\? 11
\catcode`\_ 11
\catcode`\: 11
%    \end{macrocode}
% \subsection{Package options}
%    \begin{macrocode}
\def\BNE_tmpa {0}%
\DeclareOption {custom}{\def\BNE_tmpa {1}%
    \PackageWarningNoLine{bnumexpr}{^^J
  Option custom: package xint not loaded. Definitions are needed for:^^J 
  \protect\bnumexprAdd, \protect\bnumexprSub,
  \protect\bnumexprMul\space and \protect\bnumexprDiv }%
}%
\DeclareOption {bigintcalc}{\def\BNE_tmpa {2}%
    \PackageWarningNoLine{bnumexpr}{^^J
    Option bigintcalc: the macros from package bigintcalc are used.^^J
    Notice that / is mapped to \protect\bigintcalcDiv\space which does truncated division}%
}%
\DeclareOption {nocsv}{%
    \PackageInfo{bnumexpr}{Comma separated expressions disabled}%
    \AtEndOfPackage{\expandafter\let\csname BNE_precedence_,\endcsname 
                                    \undefined }%
}%
\DeclareOption {notacitmul}{%
    \PackageInfo{bnumexpr}{Tacit multiplication disabled}%
    \AtEndOfPackage{\BNE_notacitmultiplication}%
}%
\def\BNE_allowpower {0}%
\DeclareOption {allowpower}{%
    \PackageInfo{bnumexpr}{Power operator ^ authorized}%
    \def\BNE_allowpower {1}%
}%
\ProcessOptions\relax
%    \end{macrocode}
% \subsection{Mapping to the arithmetic routines.}
% The |\xintiiQuo| macro from |xint.sty| does Euclidean division. Rounded
% division is available from |xintfrac.sty|, rather than loading it, we define
% directly here |\bnumexprDiv| as a suitable wrapper to the |xint.sty| division
% macros, to achieve, not really economically, rounded division.
%
% Current CTAN version of |xint| (1.09n) has some sub-optimal code for dealing
% with the signs of the divisor and dividend, this has been improved in
% development version 1.1, not yet released at time of writing.
%
% In case option |bigintcalc| is used, notice that |/| is mapped to the macro
% |\bigintcalcDiv| which does truncated division. We did not add the extra code
% for rounded division in that case.
%    \begin{macrocode}
\if2\BNE_tmpa % Toggle to load bigintcalc.sty
\RequirePackage{bigintcalc}%
\let\bnumexprAdd\bigintcalcAdd
\let\bnumexprSub\bigintcalcSub
\let\bnumexprMul\bigintcalcMul
\let\bnumexprDiv\bigintcalcDiv % NOTE: THIS DOES TRUNCATED DIVISION
\if1\BNE_allowpower\let\bnumexprPow\bigintcalcPow\fi
\fi
\if0\BNE_tmpa % Toggle to load xint.sty (and also xinttools.sty)
\RequirePackage{xint}%
\let\bnumexprAdd\xintiiAdd
\let\bnumexprSub\xintiiSub
\let\bnumexprMul\xintiiMul
\if1\BNE_allowpower\let\bnumexprPow\xintiiPow\fi
\def\bnumexprDiv      {\romannumeral0\bnumexprdiv }%
\def\bnumexprdiv    #1{\expandafter\BNE_div \romannumeral-`0#1\Z }%
\def\BNE_div #1#2\Z #3{\expandafter\BNE_div_a\expandafter #1%
                             \romannumeral-`0#3\Z #2\Z }%
\def\BNE_div_a #1#2% #1 de A, #2 de B.
{%
    \if0#2\xint_dothis\BNE_div_divbyzero\fi
    \if0#1\xint_dothis\BNE_div_aiszero\fi
    \if-#2\xint_dothis{\BNE_div_bneg #1}\fi
          \xint_orthat{\BNE_div_bpos #1#2}%
}%
\def\BNE_div_divbyzero #1\Z #2\Z {\BNE:DivisionByZero\space 0}%
\def\BNE_div_aiszero   #1\Z #2\Z { 0}%
\def\BNE_div_bpos #1%
{%
    \xint_UDsignfork
            #1{\xintiiopp\BNE_div_pos {}}%
             -{\BNE_div_pos #1}%
    \krof
}%
\def\BNE_div_bneg #1%
{%
    \xint_UDsignfork
            #1{\BNE_div_pos {}}%
             -{\xintiiopp\BNE_div_pos #1}%
    \krof
}%
\def\BNE_div_pos #1#2\Z #3\Z{\expandafter\BNE_div_pos_a
                          \romannumeral0\XINT_div_prepare {#2}{#1#30}}%
\def\BNE_div_pos_a #1#2{\xintReverseOrder {#1\BNE_div_pos_b}\Z }%
\def\BNE_div_pos_b #1#2{\xint_gob_til_Z #2\BNE_div_pos_small\Z 
                        \BNE_div_pos_c #1#2}%
\def\BNE_div_pos_c #1#2\Z {\ifnum #1>\xint_c_iv 
                              \expandafter\BNE_div_pos_up
                      \else   \expandafter\xintreverseorder
                      \fi {#2}}%
\def\BNE_div_pos_up #1{\xintinc {\xintReverseOrder{#1}}}% 
\def\BNE_div_pos_small\Z\BNE_div_pos_c #1#2{\ifnum #1>\xint_c_iv\expandafter
                                  \xint_secondoftwo\else\expandafter
                                  \xint_firstoftwo\fi { 0}{ 1}}%
\fi
%    \end{macrocode}
% \subsection{Some helper macros and constants from xint}
% These macros from xint should not change, hence overwriting them here should
% not be cause for alarm. I opted against renaming everything with |\BNE_|
% prefix rather than |\xint_|. The |\xint_dothis|/|\xint_orthat| thing is a new
% style I have adopted for expandably forking. The least probable branches
% should be specified first, for better efficiency. See examples of uses in the
% present code.
%    \begin{macrocode}
\chardef\xint_c_     0
\chardef\xint_c_i    1
\chardef\xint_c_ii   2
% \chardef\xint_c_iii  3
% \chardef\xint_c_iv   4
% \chardef\xint_c_v    5
\chardef\xint_c_vi   6
\chardef\xint_c_vii  7
\chardef\xint_c_viii 8
\chardef\xint_c_ix   9
% \chardef\xint_c_x     10
% \chardef\xint_c_xviii 18
\long\def\xint_gobble_i      #1{}%
\long\def\xint_gobble_iii    #1#2#3{}%
\long\def\xint_firstofone    #1{#1}%
\long\def\xint_firstoftwo    #1#2{#1}%
\long\def\xint_secondoftwo   #1#2{#2}%
\long\def\xint_firstofthree  #1#2#3{#1}%
\long\def\xint_secondofthree #1#2#3{#2}%
\long\def\xint_thirdofthree  #1#2#3{#3}%
\def\xint_gob_til_!   #1!{}% this ! has catcode 11
\def\xint_UDsignfork  #1-#2#3\krof {#2}%
\long\def\xint_afterfi       #1#2\fi {\fi #1}%
\long\def\xint_dothis        #1#2\xint_orthat #3{\fi #1}% new in v1.1
\let\xint_orthat             \xint_firstofone
%    \end{macrocode}
% \subsection{Encapsulation of numbers in pseudo cs names}
% We define here a |\BNE_num| to not have to invoke |\xintNum|; hence
% dependency on |xint.sty| is kept to the actual arithmetic operations. We
% only need to get rid of leading zeros as plus and minus signs have already
% been stripped off; generally speaking user input will have no leading zeros
% thus the macro is designed to go fast when it is not needed... and as
% everything happens inside a |\csname...\endcsname|, we can leave some
% trailing |\fi|'s.
%
% Note: the |1.09n| |\xintiiexpr| currently on CTAN has a
% bug related to leading zeros, |\xinttheiiexpr 001+1\relax| does not return
% |2|. This bug is absent from |\xintexpr|, |\xintfloatexpr|, |\xintiexpr| and
% only present in |\xintiiexpr|.
%    \begin{macrocode}
\edef\BNE_lock #1!{\noexpand\expandafter\space\noexpand
                            \csname .=\noexpand\BNE_num #1\endcsname }%
\def\BNE_num #1{\if #10\expandafter\BNE_num\else
                \ifcat #1\relax 0\expandafter\expandafter\expandafter #1\else
                #1\fi\fi }%
\def\BNE_unlock   {\expandafter\BNE_unlock_a\string }%
\def\BNE_unlock_a #1.={}%
%    \end{macrocode}
% \subsection{\csh{bnumexpr}, \csh{bnethe}, \csh{thebnumexpr}, \dots}
% In the full |\xintexpr|, the final unlocking may involve post-treatment of
% the comma separated values, hence there are |_print| macros to handle the
% possibly comma separated values. Here we may just identify |_print| with
% |_unlock|.
%    \begin{macrocode}
\def\bnumexpr {\romannumeral0\bnumeval }%
\def\bnumeval {\expandafter\BNE_wrap\romannumeral0\BNE_eval }%
\def\BNE_eval {\expandafter\BNE_until_end_a\romannumeral-`0\BNE_getnext }%
\def\BNE_wrap { !\BNE_usethe\BNE_protect\BNE_unlock }%
\protected\def\BNE_usethe\BNE_protect {\BNE:missing_bnethe!}%
\def\BNE_protect {\noexpand\BNE_protect\noexpand }%
\let\BNE_done\space
\def\thebnumexpr 
              {\romannumeral-`0\expandafter\BNE_unlock\romannumeral0\BNE_eval }%
\def\bnethe #1{\romannumeral-`0\expandafter\xint_gobble_iii\romannumeral-`0#1}%
%    \end{macrocode}
% \subsection{\csh{BNE\_getnext}}
% The getnext scans forward to find a number: after expansion of what comes
% next, an opening parenthesis signals a parenthesized sub-expression, a |!|
% with catcode 11 signals there was there a sub |\bnumexpr...\relax| (now
% evaluated), a minus sign is treated as a prefix operator inheriting its
% precedence level from the previous operator, a plus sign is swallowed, a
% |\count| or |\dimen| will get fetched to |\number| (in case of a count
% variable, this provides a full locked number but |\count0 1| for example is
% like |1231| if |\count0|'s value is |123|); a digit triggers the
% number scanner. After the digit scanner finishes the integer is trimmed of
% leading zeros and locked as a single token into a |\csname .=...\endcsname|.
% The flow then proceeds with |\BNE_getop| which looks for the next operator
% or possibly the end of the expression. Note: |\bnumexpr\relax| is illegal.
%    \begin{macrocode}
\def\BNE_getnext #1%
{%
    \expandafter\BNE_getnext_a\romannumeral-`0#1%
}%
\def\BNE_getnext_a #1%
{%
    \xint_gob_til_! #1\BNE_gn_foundexpr !% this ! has catcode 11
    \ifcat\relax#1% \count or \numexpr etc... token or count, dimen, skip cs
       \expandafter\BNE_gn_countetc
    \else
       \expandafter\expandafter\expandafter\BNE_gn_fork\expandafter\string
    \fi
    #1%
}%
\def\BNE_gn_foundexpr !#1\fi !{\expandafter\BNE_getop\xint_gobble_iii }%
\def\BNE_gn_countetc #1%
{%
    \ifx\count#1\else\ifx#1\dimen\else\ifx#1\numexpr\else\ifx#1\dimexpr\else
    \ifx\skip#1\else\ifx\glueexpr#1\else\ifx\fontdimen#1\else
      \BNE_gn_unpackvar
    \fi\fi\fi\fi\fi\fi\fi
    \expandafter\BNE_getnext\number #1%
}%
\def\BNE_gn_unpackvar\fi\fi\fi\fi\fi\fi\fi\expandafter
                            \BNE_getnext\number #1%
{%
  \fi\fi\fi\fi\fi\fi\fi
  \expandafter\BNE_getop\csname .=\number#1\endcsname 
}%
%    \end{macrocode}
% This is quite simplified here compared to |\xintexpr|, for various reasons: we
% have dropped the |\xintNewExpr| thing, and we can treat the |(| directly as we
% don't have to get back to check if we are in an |\xintexpr|,
% |\xintfloatexpr|, etc.. 
%    \begin{macrocode}
\def\BNE_gn_fork #1{%
    \if#1+\xint_dothis \BNE_getnext\fi
    \if#1-\xint_dothis -\fi
    \if#1(\xint_dothis \BNE_oparen \fi
    \xint_orthat       {\BNE_scan_number #1}%
}%
%    \end{macrocode}
% \subsection{Parsing an integer}
% We gather a string of digits, plus and minus prefixes have already been
% swallowed. There might be some leading string of zeros which will have to be
% removed. In the full |\xintexpr| the situation is more involved as it has to
% recognize and accept decimal numbers, numbers in scientific notation, also
% hexadecimal numbers, function names, etc... and variable names in current
% development version |1.1| (not yet finished).
%    \begin{macrocode}
\def\BNE_scan_number #1% this #1 has necessarily here catcode 12
{%
    \ifnum \xint_c_ix<1#1 \expandafter \BNE_scan_nbr\else
                          \expandafter \BNE_notadigit\fi #1%
}%
\def\BNE_notadigit #1{\BNE:not_a_digit! \xint_gobble_i {#1}}%
%    \end{macrocode}
% Scanning for a number. Once gathered, lock it and do |_getop|. If we hit
% against some catcode eleven |!|, this means there was a sub |\bnumexpr..\relax|.
% We then apply tacit multiplication.
%    \begin{macrocode}
\def\BNE_scan_nbr
{%
    \expandafter\BNE_getop\romannumeral-`0\expandafter
    \BNE_lock\romannumeral-`0\BNE_scan_nbr_c
}%
\def\BNE_scan_nbr_a #1%
{% careful that ! has catcode letter here
    \ifcat \relax #1\xint_dothis{!#1}\fi % stops the scan
    \ifx         !#1\xint_dothis{!*!}\fi % tacit multiplication before subexpr
    \xint_orthat {\expandafter\BNE_scan_nbr_b\string #1}%
}%
\def\BNE_scan_nbr_b #1% #1 with catcode 12
{%
    \ifnum \xint_c_ix<1#1 \expandafter\BNE_scan_nbr_c
    \else\expandafter !\fi #1%
}%
\def\BNE_scan_nbr_c #1#2%
{%
    \expandafter #1\romannumeral-`0\expandafter
                   \BNE_scan_nbr_a\romannumeral-`0#2%
}%
%    \end{macrocode}
%    \begin{macrocode}
%    \end{macrocode}
% \subsection{\csh{BNE\_getop}}
% This finds the next infix operator or closing parenthesis or expression end.
% It then leaves in the token flow <precedence> <operator> <locked number>. The
% <precedence> stops expansion and ultimately gives back
% control to a |\BNE_until_<op>| command. The code here is derived from more
% involved context where the actual macro associated to the operator may vary,
% depending if we are in |\xintexpr|, |\xintfloatexpr| or |\xintiiexpr|. Here
% things are simpler but I have kept the general scheme, thus the actual macro
% to be used for the <operator> is not decided immediately (development version
% of |xintexpr.sty| has extra things to allow multi-characters operators like 
% |&&|).  
%    \begin{macrocode}
\def\BNE_getop #1#2% this #1 is the current locked computed value
{%
    \expandafter\BNE_getop_a\expandafter #1\romannumeral-`0#2%
}%
\catcode`* 11
\def\BNE_getop_a #1#2%
{%  if a control sequence is found, must be \relax, or possibly register or
 %  variable if tacit multiplication is allowed
    \ifx \relax #2\xint_dothis\xint_firstofthree\fi
    % tacit multiplications:
    \ifcat \relax #2\xint_dothis\xint_secondofthree\fi
    \if    (#2\xint_dothis      \xint_secondofthree\fi
    \ifx   !#2\xint_dothis      \xint_secondofthree\fi
    \xint_orthat \xint_thirdofthree
    {\BNE_foundend #1}%
    {\BNE_precedence_* *#1#2}% tacit multiplication
    {\BNE_foundop #2#1}%
}%
\catcode`* 12
\def\BNE_foundend {\xint_c_ \relax }% \relax is only a place-holder here.
\def\BNE_foundop #1% 
{%
    \ifcsname BNE_precedence_#1\endcsname
        \csname BNE_precedence_#1\expandafter\endcsname
        \expandafter #1%
    \else
        \BNE_notanoperator {#1}\expandafter\BNE_getop
    \fi
}%
\def\BNE_notanoperator #1{\BNE:not_an_operator! \xint_gobble_i {#1}}%
%    \end{macrocode}
% \subsection{Until macros for global expression and parenthesized sub-ones}
% The minus sign as prefix is treated here. 
%    \begin{macrocode}
\catcode`) 11
\def\BNE_tmpa #1{%  #1=\BNE_op_-vi token
    \def\BNE_until_end_a ##1%
    {%
        \xint_UDsignfork
            ##1{\expandafter\BNE_until_end_a\romannumeral-`0#1}%
              -{\BNE_until_end_b ##1}%
        \krof 
    }%
}\expandafter\BNE_tmpa\csname BNE_op_-vi\endcsname
\def\BNE_until_end_b #1#2%
    {%
        \ifcase #1\expandafter\BNE_done 
        \or
        \xint_afterfi{\BNE:extra_)_?\expandafter
                      \BNE_until_end_a\romannumeral-`0\BNE_getop }%
        \else 
        \xint_afterfi{\expandafter\BNE_until_end_a
                      \romannumeral-`0\csname BNE_op_#2\endcsname }%
        \fi
    }%
\catcode`( 11
\def\BNE_op_( {\expandafter\BNE_until_)_a\romannumeral-`0\BNE_getnext }%
\let\BNE_oparen\BNE_op_(
\catcode`( 12
\def\BNE_tmpa #1{% #1=\BNE_op_-vi
    \def\BNE_until_)_a ##1{\xint_UDsignfork
                           ##1{\expandafter \BNE_until_)_a\romannumeral-`0#1}%
                             -{\BNE_until_)_b ##1}%
                      \krof }%
}\expandafter\BNE_tmpa\csname BNE_op_-vi\endcsname
\def \BNE_until_)_b #1#2%
    {%
     \ifcase  #1\expandafter    \BNE_missing_)_? % missing ) ?
                \or\expandafter \BNE_getop       % found closing )
                \else \xint_afterfi 
      {\expandafter \BNE_until_)_a\romannumeral-`0\csname BNE_op_#2\endcsname }%
        \fi
    }%
\def\BNE_missing_)_? {\BNE:missing_)_inserted \xint_c_ \BNE_done }%
\let\BNE_precedence_) \xint_c_i
\let\BNE_op_)   \BNE_getop 
\catcode`) 12 
%    \end{macrocode}
% \subsection{The arithmetic operators.}
% This is where the infix operators are mapped to actual macros. These macros
% must ``f-expand'' their arguments, and know how to handle then big integers
% having no leading zeros and at most a minus sign.
%    \begin{macrocode}
\def\BNE_tmpc #1#2#3#4#5#6#7%
{%
  \def #1##1% \BNE_op_<op>
  {% keep value, get next number and operator, then do until
    \expandafter #2\expandafter ##1\romannumeral-`0\expandafter\BNE_getnext }%
  \def #2##1##2% \BNE_until_<op>_a
  {\xint_UDsignfork 
    ##2{\expandafter #2\expandafter ##1\romannumeral-`0#4}%
      -{#3##1##2}%
   \krof }%
  \def #3##1##2##3##4% \BNE_until_<op>_b 
  {% either execute next operation now, or first do next (possibly unary)
    \ifnum ##2>#5%
    \xint_afterfi {\expandafter #2\expandafter ##1\romannumeral-`0%
      \csname BNE_op_##3\endcsname {##4}}%
    \else \xint_afterfi {\expandafter ##2\expandafter ##3%
      \csname .=#6{\BNE_unlock ##1}{\BNE_unlock ##4}\endcsname }%
    \fi }%
  \let #7#5%
}%
\def\BNE_tmpb #1#2#3%
{%
  \expandafter\BNE_tmpc 
  \csname BNE_op_#1\expandafter\endcsname
  \csname BNE_until_#1_a\expandafter\endcsname 
  \csname BNE_until_#1_b\expandafter\endcsname 
  \csname BNE_op_-#2\expandafter\endcsname 
  \csname xint_c_#2\expandafter\endcsname 
  \csname #3\expandafter\endcsname
  \csname BNE_precedence_#1\endcsname
}%
\BNE_tmpb  +{vi}{bnumexprAdd}%
\BNE_tmpb  -{vi}{bnumexprSub}%
\BNE_tmpb  *{vii}{bnumexprMul}%
\BNE_tmpb  /{vii}{bnumexprDiv}%
\if1\BNE_allowpower\BNE_tmpb ^{viii}{bnumexprPow}\fi
%    \end{macrocode}
% \subsection{The minus as prefix operator of variable precedence level}
% We only need here two levels of precedence, |vi| and |vii|. If the power |^|
% operation is authorized, then one further level |viii| is needed.
%    \begin{macrocode}
\def\BNE_tmpa #1% #1=vi or vii
{%
\expandafter\BNE_tmpb
    \csname BNE_op_-#1\expandafter\endcsname
    \csname BNE_until_-#1_a\expandafter\endcsname
    \csname BNE_until_-#1_b\expandafter\endcsname
    \csname xint_c_#1\endcsname
}%
\def\BNE_tmpb #1#2#3#4%
{%
    \def #1% \BNE_op_-<level> 
    {%  get next number+operator then switch to _until macro
        \expandafter #2\romannumeral-`0\BNE_getnext 
    }%
    \def #2##1% \BNE_until_-<level>_a
    {\xint_UDsignfork
        ##1{\expandafter #2\romannumeral-`0#1}%
          -{#3##1}%
     \krof }%
    \def #3##1##2##3% \BNE_until_-<level>_b
    {%
        \ifnum ##1>#4%
         \xint_afterfi {\expandafter #2\romannumeral-`0%
                        \csname BNE_op_##2\endcsname {##3}}%
        \else
         \xint_afterfi {\expandafter ##1\expandafter ##2%
                        \csname .=\expandafter\BNE_Opp
                                  \romannumeral-`0\BNE_unlock ##3\endcsname }%
        \fi
    }%
}%
\BNE_tmpa {vi}%
\BNE_tmpa {vii}%
\if1\BNE_allowpower\BNE_tmpa {viii}\fi
\def\BNE_Opp #1{\if-#1\else\if0#10\else-#1\fi\fi }%
%    \end{macrocode}
% \subsection{The comma may separate expressions.}
% It suffices to treat the comma as a binary operator of precedence |ii|. We
% insert a space after the comma. The current code in |\xintexpr| does not do
% it at this stage, but only later during the final unlocking, as there is
% anyhow need for some processing for final formatting and was considered to
% be as well the opportunity to insert the space. Here, let's do it
% immediately. These spaces are not an issue when |\bnumexpr| is
% identified as a sub-expression in |\xintexpr|, for example in:
% |\xinttheiiexpr lcm(\bnumexpr 175-12,123+34,56*31\relax)\relax| (this
% example requires package |xintgcd|).
%    \begin{macrocode}
\catcode`, 11
\def\BNE_op_, #1%
{%
    \expandafter \BNE_until_,_a\expandafter #1\romannumeral-`0\BNE_getnext 
}%
\def\BNE_tmpa #1{% #1 = \BNE_op_-vi
  \def\BNE_until_,_a ##1##2%
  {%
    \xint_UDsignfork
        ##2{\expandafter \BNE_until_,_a\expandafter ##1\romannumeral-`0#1}%
          -{\BNE_until_,_b ##1##2}%
     \krof }%
}\expandafter\BNE_tmpa\csname BNE_op_-vi\endcsname
\def\BNE_until_,_b #1#2#3#4%
{%
    \ifnum #2>\xint_c_ii
        \xint_afterfi {\expandafter \BNE_until_,_a
                   \expandafter #1\romannumeral-`0%
                   \csname BNE_op_#3\endcsname {#4}}%
    \else
        \xint_afterfi {\expandafter #2\expandafter #3%
                       \csname .=\BNE_unlock #1, \BNE_unlock #4\endcsname }%
    \fi
}%
\let \BNE_precedence_, \xint_c_ii
\catcode`, 12
%    \end{macrocode}
% \subsection{Disabling tacit multiplication}
%    \begin{macrocode}
\def\BNE_notacitmultiplication{%
  \def\BNE_getop_a ##1##2{%
    \ifx \relax ##2\expandafter\xint_firstoftwo\else
                   \expandafter\xint_secondoftwo\fi
    {\BNE_foundend ##1}%
    {\BNE_foundop  ##2##1}%
  }%
  \def\BNE_scan_nbr_a ##1{%
    \ifcat \relax ##1\expandafter\xint_firstoftwo\else
                     \expandafter\xint_secondoftwo\fi
    {!##1}{\expandafter\BNE_scan_nbr_b\string ##1}%
  }%
}%
%    \end{macrocode}
% \subsection{Cleanup}
%    \begin{macrocode}
\let\BNE_tmpa\relax \let\BNE_tmpb\relax \let\BNE_tmpc\relax \let\BNE_allowpower\relax
\BNErestorecatcodes
%    \end{macrocode}
% \DeleteShortVerb{\|}
% \MakePercentComment
%</package>
%<*dtx>
\CharacterTable
 {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
  Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
  Digits        \0\1\2\3\4\5\6\7\8\9
  Exclamation   \!     Double quote  \"     Hash (number) \#
  Dollar        \$     Percent       \%     Ampersand     \&
  Acute accent  \'     Left paren    \(     Right paren   \)
  Asterisk      \*     Plus          \+     Comma         \,
  Minus         \-     Point         \.     Solidus       \/
  Colon         \:     Semicolon     \;     Less than     \<
  Equals        \=     Greater than  \>     Question mark \?
  Commercial at \@     Left bracket  \[     Backslash     \\
  Right bracket \]     Circumflex    \^     Underscore    \_
  Grave accent  \`     Left brace    \{     Vertical bar  \|
  Right brace   \}     Tilde         \~}
\CheckSum {885}
\makeatletter\check@checksum\makeatother
\Finale
%% End of file xint.dtx