Debian Codegraph @

texmf-dist/tex/generic/pgf/math/pgfint.code.tex

raw
1 % Copyright 2019 by Henri Menke
2 %
3 % This file may be distributed and/or modified
4 %
5 % 1. under the LaTeX Project Public License and/or
6 % 2. under the GNU Public License.
7 %
8 % See the file doc/generic/pgf/licenses/LICENSE for more details.
9
10
11 % Guard against reading twice
12 \ifx\pgfintloaded\pgfutil@undefined
13 \let\pgfintloaded=\relax
14 \else
15 \expandafter\endinput
16 \fi
17
18 % This module is pretty much a rip-off of the LaTeX3 l3int module for integer
19 % calculations. Since PGF supports e-TeX now, we can make use of those
20 % extended facilities.
21
22 % In contrast to l3int we will just admit that integers are regular TeX \count
23 % register and will not implement an extra "integer datatype".
24
25 % Evaluate an integer expression using \numexpr
26 \def\pgfinteval#1{\number\numexpr#1\relax}
27
28 % Get the absolute value of an integer by stripping off a leading minus sign
29 \def\pgfintabs#1{\number\expandafter\pgfint@abs\number\numexpr#1\relax}
30 \def\pgfint@abs#1{\ifx-#1\else\expandafter#1\fi}
31
32 % Minimum and maximum
33 \def\pgfintmax#1#2{%
34 \number\expandafter\pgfint@minmax
35 \number\numexpr#1\expandafter;%
36 \number\numexpr#2;%
37 >
38 }
39 \def\pgfintmin#1#2{%
40 \number\expandafter\pgfint@minmax
41 \number\numexpr#1\expandafter;%
42 \number\numexpr#2;%
43 <
44 }
45 \def\pgfint@minmax#1;#2;#3{%
46 \ifnum#1#3#2
47 #1%
48 \else
49 #2%
50 \fi
51 }
52
53 % \numexpr has the annoying property to round divisions rather than truncating
54 % (as you would expect for integer arithmetic). Therefore we need a truncating
55 % division function.
56 \def\pgfintdivtruncate#1#2{%
57 \number\numexpr\expandafter\pgfint@divtruncate
58 \number\numexpr#1\expandafter;%
59 \number\numexpr#2;%
60 \relax
61 }
62 \def\pgfint@divtruncate#1#2;#3#4;{%
63 \ifx0#1
64 0%
65 \else
66 (#1#2%
67 \ifx-#1 +\else-\fi
68 (\ifx-#3 -\fi#3#4-1)/2%
69 )%
70 \fi
71 /#3#4%
72 }
73
74 % \pgfintdivtruncate always truncates the result which leads to a rounding
75 % towards zero. Donald Knuth defines in TAoCP, Vol 1, 3rd Ed, Section 1.2.4
76 % that the modulo operation is
77 %
78 % x mod y = x - y * floor(x/y)
79 %
80 % In contrast to truncated division, floored division always rounds towards
81 % -inf, i.e. floor(1/2) = 0 and floor(-1/2) = -1. This is also how integer
82 % division is done in Lua.
83 \def\pgfintdivfloor#1#2{%
84 \number\numexpr\expandafter\pgfint@divfloor
85 \number\numexpr#1\expandafter;%
86 \number\numexpr#2;%
87 \relax
88 }
89 \def\pgfint@divfloor#1#2;#3#4;{%
90 \ifx0#1
91 0%
92 \else
93 \ifnum
94 0\ifx-#1\ifx#3-\else1\fi\fi
95 0\ifx-#3\ifx#1-\else1\fi\fi
96 >0
97 \pgfint@@divfloor#1#2;#3#4;-+%
98 \else
99 \pgfint@@divfloor#1#2;#3#4;+-%
100 \fi
101 \fi
102 /#3#4%
103 }
104 \def\pgfint@@divfloor#1#2;#3#4;#5#6{%
105 (#1#2%
106 \ifx-#1 #5\else#6\fi
107 (\ifx-#3 -\fi#3#4#61)/2%
108 )%
109 }
110
111 % Round
112 \def\pgfintdivround#1#2{\number\numexpr(#1)/(#2)\relax}
113
114 % Modulo
115 \def\pgfintmod#1#2{%
116 \number\numexpr\expandafter\pgfint@mod%
117 \number\numexpr#1\expandafter;%
118 \number\numexpr#2;%
119 \relax
120 }
121 \def\pgfint@mod#1;#2;{#1-(\pgfint@divfloor#1;#2;)*#2}
122
123 % Setter function so we don't always have to write \pgfinteval
124 \def\pgfintset#1#2{#1 \numexpr#2\relax}
125
126 \endinput