| 1 |
/* |
| 2 |
|
| 3 |
arithmetic.w |
| 4 |
|
| 5 |
Copyright 2009-2010 Taco Hoekwater <taco@@luatex.org> |
| 6 |
|
| 7 |
This file is part of LuaTeX. |
| 8 |
|
| 9 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 10 |
the terms of the GNU General Public License as published by the Free |
| 11 |
Software Foundation; either version 2 of the License, or (at your |
| 12 |
option) any later version. |
| 13 |
|
| 14 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 15 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 17 |
License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License along |
| 20 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
*/ |
| 23 |
|
| 24 |
#include "ptexlib.h" |
| 25 |
|
| 26 |
/*tex |
| 27 |
|
| 28 |
The principal computations performed by \TeX\ are done entirely in terms of |
| 29 |
integers less than $2^{31}$ in magnitude; and divisions are done only when both |
| 30 |
dividend and divisor are nonnegative. Thus, the arithmetic specified in this |
| 31 |
program can be carried out in exactly the same way on a wide variety of |
| 32 |
computers, including some small ones. Why? Because the arithmetic calculations |
| 33 |
need to be spelled out precisely in order to guarantee that \TeX\ will produce |
| 34 |
identical output on different machines. If some quantities were rounded |
| 35 |
differently in different implementations, we would find that line breaks and even |
| 36 |
page breaks might occur in different places. Hence the arithmetic of \TeX\ has |
| 37 |
been designed with care, and systems that claim to be implementations of \TeX82 |
| 38 |
should follow precisely the @:TeX82}{\TeX82@> calculations as they appear in the |
| 39 |
present program. |
| 40 |
|
| 41 |
Actually there are three places where \TeX\ uses |div| with a possibly negative |
| 42 |
numerator. These are harmless; see |div| in the index. Also if the user sets the |
| 43 |
\.{\\time} or the \.{\\year} to a negative value, some diagnostic information |
| 44 |
will involve negative-numerator division. The same remarks apply for |mod| as |
| 45 |
well as for |div|. |
| 46 |
|
| 47 |
Here is a routine that calculates half of an integer, using an unambiguous |
| 48 |
convention with respect to signed odd numbers. |
| 49 |
|
| 50 |
*/ |
| 51 |
|
| 52 |
int half(int x) |
| 53 |
{ |
| 54 |
if (odd(x)) |
| 55 |
return ((x + 1) / 2); |
| 56 |
else |
| 57 |
return (x / 2); |
| 58 |
} |
| 59 |
|
| 60 |
/*tex |
| 61 |
|
| 62 |
The following function is used to create a scaled integer from a given decimal |
| 63 |
fraction $(.d_0d_1\ldots d_{k-1})$, where |0<=k<=17|. The digit $d_i$ is |
| 64 |
given in |dig[i]|, and the calculation produces a correctly rounded result. |
| 65 |
|
| 66 |
*/ |
| 67 |
|
| 68 |
scaled round_decimals(int k) |
| 69 |
{ |
| 70 |
int a = 0; |
| 71 |
while (k-- > 0) { |
| 72 |
a = (a + dig[k] * two) / 10; |
| 73 |
} |
| 74 |
return ((a + 1) / 2); |
| 75 |
} |
| 76 |
|
| 77 |
/*tex |
| 78 |
|
| 79 |
Conversely, here is a procedure analogous to |print_int|. If the output of this |
| 80 |
procedure is subsequently read by \TeX\ and converted by the |round_decimals| |
| 81 |
routine above, it turns out that the original value will be reproduced exactly; |
| 82 |
the ``simplest'' such decimal number is output, but there is always at least one |
| 83 |
digit following the decimal point. |
| 84 |
|
| 85 |
The invariant relation in the \&{repeat} loop is that a sequence of decimal |
| 86 |
digits yet to be printed will yield the original number if and only if they form |
| 87 |
a fraction~$f$ in the range $s-\delta\L10\cdot2^{16}f<s$. We can stop if and only |
| 88 |
if $f=0$ satisfies this condition; the loop will terminate before $s$ can |
| 89 |
possibly become zero. |
| 90 |
|
| 91 |
The next one prints a scaled real, rounded to five digits. |
| 92 |
|
| 93 |
*/ |
| 94 |
|
| 95 |
void print_scaled(scaled s) |
| 96 |
{ |
| 97 |
/*tex The amount of allowable inaccuracy: */ |
| 98 |
scaled delta; |
| 99 |
char buffer[20]; |
| 100 |
int i = 0; |
| 101 |
if (s < 0) { |
| 102 |
/*tex Print the sign, if negative. */ |
| 103 |
print_char('-'); |
| 104 |
negate(s); |
| 105 |
} |
| 106 |
/*tex Print the integer part. */ |
| 107 |
print_int(s / unity); |
| 108 |
buffer[i++] = '.'; |
| 109 |
s = 10 * (s % unity) + 5; |
| 110 |
delta = 10; |
| 111 |
do { |
| 112 |
if (delta > unity) { |
| 113 |
/*tex Round the last digit. */ |
| 114 |
s = s + 0100000 - 50000; |
| 115 |
} |
| 116 |
buffer[i++] = '0' + (s / unity); |
| 117 |
s = 10 * (s % unity); |
| 118 |
delta = delta * 10; |
| 119 |
} while (s > delta); |
| 120 |
buffer[i++] = '\0'; |
| 121 |
tprint(buffer); |
| 122 |
} |
| 123 |
|
| 124 |
/*tex |
| 125 |
|
| 126 |
Physical sizes that a \TeX\ user specifies for portions of documents are |
| 127 |
represented internally as scaled points. Thus, if we define an `sp' (scaled |
| 128 |
@^sp@> point) as a unit equal to $2^{-16}$ printer's points, every dimension |
| 129 |
inside of \TeX\ is an integer number of sp. There are exactly 4,736,286.72 sp per |
| 130 |
inch. Users are not allowed to specify dimensions larger than $2^{30}-1$ sp, |
| 131 |
which is a distance of about 18.892 feet (5.7583 meters); two such quantities can |
| 132 |
be added without overflow on a 32-bit computer. |
| 133 |
|
| 134 |
The present implementation of \TeX\ does not check for overflow when @^overflow |
| 135 |
in arithmetic@> dimensions are added or subtracted. This could be done by |
| 136 |
inserting a few dozen tests of the form `\ignorespaces|if x>=010000000000 then |
| 137 |
@t\\{report\_overflow}@>|', but the chance of overflow is so remote that such |
| 138 |
tests do not seem worthwhile. |
| 139 |
|
| 140 |
\TeX\ needs to do only a few arithmetic operations on scaled quantities, other |
| 141 |
than addition and subtraction, and the following subroutines do most of the work. |
| 142 |
A single computation might use several subroutine calls, and it is desirable to |
| 143 |
avoid producing multiple error messages in case of arithmetic overflow; so the |
| 144 |
routines set the global variable |arith_error| to |true| instead of reporting |
| 145 |
errors directly to the user. Another global variable, |tex_remainder|, holds the |
| 146 |
remainder after a division. |
| 147 |
|
| 148 |
*/ |
| 149 |
|
| 150 |
/*tex Has arithmetic overflow occurred recently? */ |
| 151 |
|
| 152 |
boolean arith_error; |
| 153 |
|
| 154 |
/*tex The amount subtracted to get an exact division. */ |
| 155 |
|
| 156 |
scaled tex_remainder; |
| 157 |
|
| 158 |
/*tex |
| 159 |
|
| 160 |
The first arithmetical subroutine we need computes $nx+y$, where |x| |
| 161 |
and~|y| are |scaled| and |n| is an integer. We will also use it to |
| 162 |
multiply integers. |
| 163 |
|
| 164 |
*/ |
| 165 |
|
| 166 |
scaled mult_and_add(int n, scaled x, scaled y, scaled max_answer) |
| 167 |
{ |
| 168 |
if (n == 0) |
| 169 |
return y; |
| 170 |
if (n < 0) { |
| 171 |
negate(x); |
| 172 |
negate(n); |
| 173 |
} |
| 174 |
if (((x <= (max_answer - y) / n) && (-x <= (max_answer + y) / n))) { |
| 175 |
return (n * x + y); |
| 176 |
} else { |
| 177 |
arith_error = true; |
| 178 |
return 0; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/*tex |
| 183 |
|
| 184 |
We also need to divide scaled dimensions by integers. |
| 185 |
|
| 186 |
*/ |
| 187 |
|
| 188 |
scaled x_over_n(scaled x, int n) |
| 189 |
{ |
| 190 |
/*tex Should |tex_remainder| be negated? */ |
| 191 |
boolean negative = false; |
| 192 |
if (n == 0) { |
| 193 |
arith_error = true; |
| 194 |
tex_remainder = x; |
| 195 |
return 0; |
| 196 |
} else { |
| 197 |
if (n < 0) { |
| 198 |
negate(x); |
| 199 |
negate(n); |
| 200 |
negative = true; |
| 201 |
} |
| 202 |
if (x >= 0) { |
| 203 |
tex_remainder = x % n; |
| 204 |
if (negative) |
| 205 |
negate(tex_remainder); |
| 206 |
return (x / n); |
| 207 |
} else { |
| 208 |
tex_remainder = -((-x) % n); |
| 209 |
if (negative) |
| 210 |
negate(tex_remainder); |
| 211 |
return (-((-x) / n)); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/*tex |
| 217 |
|
| 218 |
Then comes the multiplication of a scaled number by a fraction |n/d|, where |n| |
| 219 |
and |d| are nonnegative integers |<=@t$2^{16}$@>| and |d| is positive. It would |
| 220 |
be too dangerous to multiply by~|n| and then divide by~|d|, in separate |
| 221 |
operations, since overflow might well occur; and it would be too inaccurate to |
| 222 |
divide by |d| and then multiply by |n|. Hence this subroutine simulates |
| 223 |
1.5-precision arithmetic. |
| 224 |
|
| 225 |
*/ |
| 226 |
|
| 227 |
scaled xn_over_d(scaled x, int n, int d) |
| 228 |
{ |
| 229 |
nonnegative_integer t, u, v, xx, dd; |
| 230 |
boolean positive = true; |
| 231 |
if (x < 0) { |
| 232 |
negate(x); |
| 233 |
positive = false; |
| 234 |
} |
| 235 |
xx = (nonnegative_integer) x; |
| 236 |
dd = (nonnegative_integer) d; |
| 237 |
t = ((xx % 0100000) * (nonnegative_integer) n); |
| 238 |
u = ((xx / 0100000) * (nonnegative_integer) n + (t / 0100000)); |
| 239 |
v = (u % dd) * 0100000 + (t % 0100000); |
| 240 |
if (u / dd >= 0100000) |
| 241 |
arith_error = true; |
| 242 |
else |
| 243 |
u = 0100000 * (u / dd) + (v / dd); |
| 244 |
if (positive) { |
| 245 |
tex_remainder = (int) (v % dd); |
| 246 |
return (scaled) u; |
| 247 |
} else { |
| 248 |
/*tex The casts are for ms cl. */ |
| 249 |
tex_remainder = -(int) (v % dd); |
| 250 |
return -(scaled) (u); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
/*tex |
| 255 |
|
| 256 |
The next subroutine is used to compute the ``badness'' of glue, when a total~|t| |
| 257 |
is supposed to be made from amounts that sum to~|s|. According to {\sl The \TeX |
| 258 |
book}, the badness of this situation is $100(t/s)^3$; however, badness is simply |
| 259 |
a heuristic, so we need not squeeze out the last drop of accuracy when computing |
| 260 |
it. All we really want is an approximation that has similar properties. |
| 261 |
@:TeXbook}{\sl The \TeX book@> |
| 262 |
|
| 263 |
The actual method used to compute the badness is easier to read from the program |
| 264 |
than to describe in words. It produces an integer value that is a reasonably |
| 265 |
close approximation to $100(t/s)^3$, and all implementations of \TeX\ should use |
| 266 |
precisely this method. Any badness of $2^{13}$ or more is treated as infinitely |
| 267 |
bad, and represented by 10000. |
| 268 |
|
| 269 |
It is not difficult to prove that $$\hbox{|badness(t+1,s)>=badness(t,s) |
| 270 |
>= badness(t,s+1)|}.$$ The badness function defined here is capable of computing |
| 271 |
at most 1095 distinct values, but that is plenty. |
| 272 |
|
| 273 |
*/ |
| 274 |
|
| 275 |
halfword badness(scaled t, scaled s) |
| 276 |
{ |
| 277 |
/*tex Approximation to $\alpha t/s$, where $\alpha^3\approx 100\cdot2^{18}$ */ |
| 278 |
int r; |
| 279 |
if (t == 0) { |
| 280 |
return 0; |
| 281 |
} else if (s <= 0) { |
| 282 |
return inf_bad; |
| 283 |
} else { |
| 284 |
/*tex $297^3=99.94\times2^{18}$ */ |
| 285 |
if (t <= 7230584) { |
| 286 |
r = (t * 297) / s; |
| 287 |
} else if (s >= 1663497) { |
| 288 |
r = t / (s / 297); |
| 289 |
} else { |
| 290 |
r = t; |
| 291 |
} |
| 292 |
if (r > 1290) { |
| 293 |
/*tex $1290^3<2^{31}<1291^3$ */ |
| 294 |
return inf_bad; |
| 295 |
} else { |
| 296 |
/*tex This is $r^3/2^{18}$, rounded to the nearest integer. */ |
| 297 |
return ((r * r * r + 0400000) / 01000000); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/*tex |
| 303 |
|
| 304 |
When \TeX\ ``packages'' a list into a box, it needs to calculate the |
| 305 |
proportionality ratio by which the glue inside the box should stretch or shrink. |
| 306 |
This calculation does not affect \TeX's decision making, so the precise details |
| 307 |
of rounding, etc., in the glue calculation are not of critical importance for the |
| 308 |
consistency of results on different computers. |
| 309 |
|
| 310 |
We shall use the type |glue_ratio| for such proportionality ratios. A glue ratio |
| 311 |
should take the same amount of memory as an |integer| (usually 32 bits) if it is |
| 312 |
to blend smoothly with \TeX's other data structures. Thus |glue_ratio| should be |
| 313 |
equivalent to |short_real| in some implementations of PASCAL. Alternatively, it |
| 314 |
is possible to deal with glue ratios using nothing but fixed-point arithmetic; |
| 315 |
see {\sl TUGboat \bf3},1 (March 1982), 10--27. (But the routines cited there must |
| 316 |
be modified to allow negative glue ratios.) @^system dependencies@> |
| 317 |
|
| 318 |
*/ |
| 319 |
|
| 320 |
/* |
| 321 |
|
| 322 |
This section is (almost) straight from MetaPost. I (Taco) had to change the types |
| 323 |
(use |integer| instead of |fraction|), but that should not have any influence on |
| 324 |
the actual calculations (the original comments refer to quantities like |
| 325 |
|fraction_four| ($2^{30}$), and that is the same as the numeric representation of |
| 326 |
|max_dimen|). |
| 327 |
|
| 328 |
I've copied the low-level variables and routines that are needed, but only those |
| 329 |
(e.g. |m_log|), not the accompanying ones like |m_exp|. Most of the following |
| 330 |
low-level numeric routines are only needed within the calculation of |norm_rand|. |
| 331 |
I've been forced to rename |make_fraction| to |make_frac| because TeX already has |
| 332 |
a routine by that name with a wholly different function (it creates a |
| 333 |
|fraction_noad| for math typesetting) |
| 334 |
|
| 335 |
And now let's complete our collection of numeric utility routines by considering |
| 336 |
random number generation. \MP{} generates pseudo-random numbers with the additive |
| 337 |
scheme recommended in Section 3.6 of {\sl The Art of Computer Programming}; |
| 338 |
however, the results are random fractions between 0 and |fraction_one-1|, |
| 339 |
inclusive. |
| 340 |
|
| 341 |
There's an auxiliary array |randoms| that contains 55 pseudo-random fractions. |
| 342 |
Using the recurrence $x_n=(x_{n-55}-x_{n-31})\bmod 2^{28}$, we generate batches |
| 343 |
of 55 new $x_n$'s at a time by calling |new_randoms|. The global variable |
| 344 |
|j_random| tells which element has most recently been consumed. |
| 345 |
|
| 346 |
*/ |
| 347 |
|
| 348 |
/*tex The last 55 random values generated: */ |
| 349 |
|
| 350 |
static int randoms[55]; |
| 351 |
|
| 352 |
/*tex The number of unused |randoms|: */ |
| 353 |
|
| 354 |
static int j_random; |
| 355 |
|
| 356 |
/*tex The default random seed: */ |
| 357 |
|
| 358 |
scaled random_seed; |
| 359 |
|
| 360 |
/*tex A small bit of \METAPOST\ is needed. */ |
| 361 |
|
| 362 |
#define fraction_half 01000000000 /* $2^{27} $, represents 0.50000000 */ |
| 363 |
#define fraction_one 02000000000 /* $2^{28} $, represents 1.00000000 */ |
| 364 |
#define fraction_four 010000000000 /* $2^{30} $, represents 4.00000000 */ |
| 365 |
#define el_gordo 017777777777 /* $2^{31}-1$, the largest value that \MP\ likes */ |
| 366 |
|
| 367 |
/*tex |
| 368 |
|
| 369 |
The |make_frac| routine produces the |fraction| equivalent of |p/q|, given |
| 370 |
integers |p| and~|q|; it computes the integer |
| 371 |
$f=\lfloor2^{28}p/q+{1\over2}\rfloor$, when $p$ and $q$ are positive. If |p| and |
| 372 |
|q| are both of the same scaled type |t|, the ``type relation'' |
| 373 |
|make_frac(t,t)=fraction| is valid; and it's also possible to use the subroutine |
| 374 |
``backwards,'' using the relation |make_frac(t,fraction)=t| between scaled types. |
| 375 |
|
| 376 |
If the result would have magnitude $2^{31}$ or more, |make_frac| sets |
| 377 |
|arith_error:=true|. Most of \MP's internal computations have been designed to |
| 378 |
avoid this sort of error. |
| 379 |
|
| 380 |
If this subroutine were programmed in assembly language on a typical machine, we |
| 381 |
could simply compute |(@t$2^{28}$@>*p)div q|, since a double-precision product |
| 382 |
can often be input to a fixed-point division instruction. But when we are |
| 383 |
restricted to PASCAL arithmetic it is necessary either to resort to |
| 384 |
multiple-precision maneuvering or to use a simple but slow iteration. The |
| 385 |
multiple-precision technique would be about three times faster than the code |
| 386 |
adopted here, but it would be comparatively long and tricky, involving about |
| 387 |
sixteen additional multiplications and divisions. |
| 388 |
|
| 389 |
This operation is part of \MP's ``inner loop''; indeed, it will consume nearly |
| 390 |
10\%! of the running time (exclusive of input and output) if the code below is |
| 391 |
left unchanged. A machine-dependent recoding will therefore make \MP\ run faster. |
| 392 |
The present implementation is highly portable, but slow; it avoids multiplication |
| 393 |
and division except in the initial stage. System wizards should be careful to |
| 394 |
replace it with a routine that is guaranteed to produce identical results in all |
| 395 |
cases. @^system dependencies@> |
| 396 |
|
| 397 |
As noted below, a few more routines should also be replaced by machine-dependent |
| 398 |
code, for efficiency. But when a procedure is not part of the ``inner loop,'' |
| 399 |
such changes aren't advisable; simplicity and robustness are preferable to |
| 400 |
trickery, unless the cost is too high. |
| 401 |
|
| 402 |
*/ |
| 403 |
|
| 404 |
static int make_frac(int p, int q) |
| 405 |
{ |
| 406 |
/*tex The fraction bits, with a leading 1 bit: */ |
| 407 |
int f; |
| 408 |
/*tex The integer part of $\vert p/q\vert$: */ |
| 409 |
int n; |
| 410 |
/*tex Disables certain compiler optimizations: */ |
| 411 |
register int be_careful; |
| 412 |
/*tex Should the result be negated? */ |
| 413 |
boolean negative = false; |
| 414 |
if (p < 0) { |
| 415 |
negate(p); |
| 416 |
negative = true; |
| 417 |
} |
| 418 |
if (q <= 0) { |
| 419 |
negate(q); |
| 420 |
negative = !negative; |
| 421 |
} |
| 422 |
n = p / q; |
| 423 |
p = p % q; |
| 424 |
if (n >= 8) { |
| 425 |
arith_error = true; |
| 426 |
if (negative) |
| 427 |
return (-el_gordo); |
| 428 |
else |
| 429 |
return el_gordo; |
| 430 |
} else { |
| 431 |
n = (n - 1) * fraction_one; |
| 432 |
/*tex_remainder |
| 433 |
|
| 434 |
Compute $f=\lfloor 2^{28}(1+p/q)+{1\over2}\rfloor$. The |repeat| loop |
| 435 |
here preserves the following invariant relations between |f|, |p|, |
| 436 |
and~|q|: (i)~|0<=p<q|; (ii)~$fq+p=2^k(q+p_0)$, where $k$ is an |
| 437 |
integer and $p_0$ is the original value of~$p$. |
| 438 |
|
| 439 |
Notice that the computation specifies |(p-q)+p| instead of |(p+p)-q|, |
| 440 |
because the latter could overflow. Let us hope that optimizing |
| 441 |
compilers do not miss this point; a special variable |be_careful| is |
| 442 |
used to emphasize the necessary order of computation. Optimizing |
| 443 |
compilers should keep |be_careful| in a register, not store it in |
| 444 |
memory. |
| 445 |
|
| 446 |
*/ |
| 447 |
f = 1; |
| 448 |
do { |
| 449 |
be_careful = p - q; |
| 450 |
p = be_careful + p; |
| 451 |
if (p >= 0) |
| 452 |
f = f + f + 1; |
| 453 |
else { |
| 454 |
f += f; |
| 455 |
p = p + q; |
| 456 |
} |
| 457 |
} while (f < fraction_one); |
| 458 |
be_careful = p - q; |
| 459 |
if (be_careful + p >= 0) |
| 460 |
incr(f); |
| 461 |
|
| 462 |
if (negative) |
| 463 |
return (-(f + n)); |
| 464 |
else |
| 465 |
return (f + n); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
static int take_frac(int q, int f) |
| 470 |
{ |
| 471 |
/*tex The fraction so far: */ |
| 472 |
int p; |
| 473 |
/*tex Additional multiple of $q$: */ |
| 474 |
int n; |
| 475 |
/*tex Disables certain compiler optimizations. */ |
| 476 |
register int be_careful; |
| 477 |
/*tex Should the result be negated? */ |
| 478 |
boolean negative = false; |
| 479 |
/*tex Reduce to the case that |f>=0| and |q>0|. */ |
| 480 |
if (f < 0) { |
| 481 |
negate(f); |
| 482 |
negative = true; |
| 483 |
} |
| 484 |
if (q < 0) { |
| 485 |
negate(q); |
| 486 |
negative = !negative; |
| 487 |
} |
| 488 |
if (f < fraction_one) { |
| 489 |
n = 0; |
| 490 |
} else { |
| 491 |
n = f / fraction_one; |
| 492 |
f = f % fraction_one; |
| 493 |
if (q <= el_gordo / n) { |
| 494 |
n = n * q; |
| 495 |
} else { |
| 496 |
arith_error = true; |
| 497 |
n = el_gordo; |
| 498 |
} |
| 499 |
} |
| 500 |
f = f + fraction_one; |
| 501 |
/*tex |
| 502 |
|
| 503 |
Compute $p=\lfloor qf/2^{28}+{1\over2}\rfloor-q$. The invariant relations |
| 504 |
in this case are (i)~$\lfloor(qf+p)/2^k\rfloor =\lfloor |
| 505 |
qf_0/2^{28}+{1\over2}\rfloor$, where $k$ is an integer and $f_0$ is the |
| 506 |
original value of~$f$; (ii)~$2^k\L f<2^{k+1}$. |
| 507 |
|
| 508 |
Here |p| becomes $2^{27}$; the invariants hold now with $k=28$: |
| 509 |
|
| 510 |
*/ |
| 511 |
p = fraction_half; |
| 512 |
if (q < fraction_four) { |
| 513 |
do { |
| 514 |
if (odd(f)) |
| 515 |
p = halfp(p + q); |
| 516 |
else |
| 517 |
p = halfp(p); |
| 518 |
f = halfp(f); |
| 519 |
} while (f != 1); |
| 520 |
} else { |
| 521 |
do { |
| 522 |
if (odd(f)) |
| 523 |
p = p + halfp(q - p); |
| 524 |
else |
| 525 |
p = halfp(p); |
| 526 |
f = halfp(f); |
| 527 |
} while (f != 1); |
| 528 |
} |
| 529 |
be_careful = n - el_gordo; |
| 530 |
if (be_careful + p > 0) { |
| 531 |
arith_error = true; |
| 532 |
n = el_gordo - p; |
| 533 |
} |
| 534 |
if (negative) |
| 535 |
return (-(n + p)); |
| 536 |
else |
| 537 |
return (n + p); |
| 538 |
} |
| 539 |
|
| 540 |
/*tex |
| 541 |
|
| 542 |
The subroutines for logarithm and exponential involve two tables. The first is |
| 543 |
simple: |two_to_the[k]| equals $2^k$. The second involves a bit more calculation, |
| 544 |
which the author claims to have done correctly: |spec_log[k]| is $2^{27}$ times |
| 545 |
$\ln\bigl(1/(1-2^{-k})\bigr)= 2^{-k}+{1\over2}2^{-2k}+{1\over3}2^{-3k}+\cdots\,$, |
| 546 |
rounded to the nearest integer. |
| 547 |
|
| 548 |
*/ |
| 549 |
|
| 550 |
/*tex The powers of two: */ |
| 551 |
|
| 552 |
static int two_to_the[31]; |
| 553 |
|
| 554 |
/*tex Special logarithms: */ |
| 555 |
|
| 556 |
static int spec_log[29]; |
| 557 |
|
| 558 |
void initialize_arithmetic(void) |
| 559 |
{ |
| 560 |
int k; |
| 561 |
two_to_the[0] = 1; |
| 562 |
for (k = 1; k <= 30; k++) { |
| 563 |
two_to_the[k] = 2 * two_to_the[k - 1]; |
| 564 |
} |
| 565 |
spec_log [1] = 93032640; |
| 566 |
spec_log [2] = 38612034; |
| 567 |
spec_log [3] = 17922280; |
| 568 |
spec_log [4] = 8662214; |
| 569 |
spec_log [5] = 4261238; |
| 570 |
spec_log [6] = 2113709; |
| 571 |
spec_log [7] = 1052693; |
| 572 |
spec_log [8] = 525315; |
| 573 |
spec_log [9] = 262400; |
| 574 |
spec_log[10] = 131136; |
| 575 |
spec_log[11] = 65552; |
| 576 |
spec_log[12] = 32772; |
| 577 |
spec_log[13] = 16385; |
| 578 |
for (k = 14; k <= 27; k++) { |
| 579 |
spec_log[k] = two_to_the[27 - k]; |
| 580 |
} |
| 581 |
spec_log[28] = 1; |
| 582 |
} |
| 583 |
|
| 584 |
static int m_log(int x) |
| 585 |
{ |
| 586 |
/*tex Auxiliary registers: */ |
| 587 |
int y, z; |
| 588 |
/*tex Iteration counter: */ |
| 589 |
int k; |
| 590 |
if (x <= 0) { |
| 591 |
/*tex Handle non-positive logarithm. */ |
| 592 |
print_err("Logarithm of "); |
| 593 |
print_scaled(x); |
| 594 |
tprint(" has been replaced by 0"); |
| 595 |
help2( |
| 596 |
"Since I don't take logs of non-positive numbers,", |
| 597 |
"I'm zeroing this one. Proceed, with fingers crossed." |
| 598 |
); |
| 599 |
error(); |
| 600 |
return 0; |
| 601 |
} else { |
| 602 |
/*tex $14\times2^{27}\ln2\approx1302456956.421063$ */ |
| 603 |
y = 1302456956 + 4 - 100; |
| 604 |
/*tex $2^{16}\times .421063\approx 27595$ */ |
| 605 |
z = 27595 + 6553600; |
| 606 |
while (x < fraction_four) { |
| 607 |
x += x; |
| 608 |
/*tex $2^{27}\ln2\approx 93032639.74436163$ */ |
| 609 |
y = y - 93032639; |
| 610 |
/*tex $2^{16}\times.74436163\approx 48782$ */ |
| 611 |
z = z - 48782; |
| 612 |
} |
| 613 |
|
| 614 |
y = y + (z / unity); |
| 615 |
k = 2; |
| 616 |
while (x > fraction_four + 4) { |
| 617 |
/*tex |
| 618 |
Increase |k| until |x| can be multiplied by a factor of $2^{-k}$, |
| 619 |
and adjust $y$ accordingly. Here $z=\lceil x/2^k\rceil$. |
| 620 |
*/ |
| 621 |
z = ((x - 1) / two_to_the[k]) + 1; |
| 622 |
while (x < fraction_four + z) { |
| 623 |
z = halfp(z + 1); |
| 624 |
k = k + 1; |
| 625 |
} |
| 626 |
y = y + spec_log[k]; |
| 627 |
x = x - z; |
| 628 |
} |
| 629 |
return (y / 8); |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
/*tex |
| 634 |
|
| 635 |
The following somewhat different subroutine tests rigorously if $ab$ is greater |
| 636 |
than, equal to, or less than~$cd$, given integers $(a,b,c,d)$. In most cases a |
| 637 |
quick decision is reached. The result is $+1$, 0, or~$-1$ in the three respective |
| 638 |
cases. |
| 639 |
|
| 640 |
*/ |
| 641 |
|
| 642 |
static int ab_vs_cd(int a, int b, int c, int d) |
| 643 |
{ |
| 644 |
int q, r; |
| 645 |
/*tex Reduce to the case that |a,c>=0| and |b,d>0|. */ |
| 646 |
if (a < 0) { |
| 647 |
negate(a); |
| 648 |
negate(b); |
| 649 |
} |
| 650 |
if (c < 0) { |
| 651 |
negate(c); |
| 652 |
negate(d); |
| 653 |
} |
| 654 |
if (d <= 0) { |
| 655 |
if (b >= 0) |
| 656 |
return (((a == 0 || b == 0) && (c == 0 || d == 0)) ? 0 : 1); |
| 657 |
if (d == 0) |
| 658 |
return (a == 0 ? 0 : -1); |
| 659 |
q = a; |
| 660 |
a = c; |
| 661 |
c = q; |
| 662 |
q = -b; |
| 663 |
b = -d; |
| 664 |
d = q; |
| 665 |
} else if (b <= 0) { |
| 666 |
if (b < 0 && a > 0) |
| 667 |
return -1; |
| 668 |
return (c == 0 ? 0 : -1); |
| 669 |
} |
| 670 |
while (1) { |
| 671 |
q = a / d; |
| 672 |
r = c / b; |
| 673 |
if (q != r) |
| 674 |
return (q > r ? 1 : -1); |
| 675 |
q = a % d; |
| 676 |
r = c % b; |
| 677 |
if (r == 0) |
| 678 |
return (q == 0 ? 0 : 1); |
| 679 |
if (q == 0) |
| 680 |
return -1; |
| 681 |
a = b; |
| 682 |
b = q; |
| 683 |
c = d; |
| 684 |
d = r; |
| 685 |
/*tex Now |a>d>0| and |c>b>0|. */ |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
/*tex |
| 690 |
|
| 691 |
To consume a random integer, the program below will say `|next_random|' and then |
| 692 |
it will fetch |randoms[j_random]|. |
| 693 |
|
| 694 |
*/ |
| 695 |
|
| 696 |
#define next_random() do { \ |
| 697 |
if (j_random==0) \ |
| 698 |
new_randoms(); \ |
| 699 |
else \ |
| 700 |
decr(j_random); \ |
| 701 |
} while (0) |
| 702 |
|
| 703 |
static void new_randoms(void) |
| 704 |
{ |
| 705 |
/*tex The index into |randoms|. */ |
| 706 |
int k; |
| 707 |
/*tex The accumulator. */ |
| 708 |
int x; |
| 709 |
for (k = 0; k <= 23; k++) { |
| 710 |
x = randoms[k] - randoms[k + 31]; |
| 711 |
if (x < 0) |
| 712 |
x = x + fraction_one; |
| 713 |
randoms[k] = x; |
| 714 |
} |
| 715 |
for (k = 24; k <= 54; k++) { |
| 716 |
x = randoms[k] - randoms[k - 24]; |
| 717 |
if (x < 0) |
| 718 |
x = x + fraction_one; |
| 719 |
randoms[k] = x; |
| 720 |
} |
| 721 |
j_random = 54; |
| 722 |
} |
| 723 |
|
| 724 |
/*tex |
| 725 |
|
| 726 |
To initialize the |randoms| table, we call the following routine. |
| 727 |
|
| 728 |
*/ |
| 729 |
|
| 730 |
void init_randoms(int seed) |
| 731 |
{ |
| 732 |
/*tex Three more or less random integers. */ |
| 733 |
int j, jj, k; |
| 734 |
/*tex The index into |randoms|. */ |
| 735 |
int i; |
| 736 |
j = abs(seed); |
| 737 |
while (j >= fraction_one) |
| 738 |
j = halfp(j); |
| 739 |
k = 1; |
| 740 |
for (i = 0; i <= 54; i++) { |
| 741 |
jj = k; |
| 742 |
k = j - k; |
| 743 |
j = jj; |
| 744 |
if (k < 0) |
| 745 |
k = k + fraction_one; |
| 746 |
randoms[(i * 21) % 55] = j; |
| 747 |
} |
| 748 |
/*tex We ``warm up'' the array. */ |
| 749 |
new_randoms(); |
| 750 |
new_randoms(); |
| 751 |
new_randoms(); |
| 752 |
} |
| 753 |
|
| 754 |
/*tex |
| 755 |
|
| 756 |
To produce a uniform random number in the range |0<=u<x| or |0>=u>x| or |0=u=x|, |
| 757 |
given a |scaled| value~|x|, we proceed as shown here. |
| 758 |
|
| 759 |
Note that the call of |take_frac| will produce the values 0 and~|x| with about |
| 760 |
half the probability that it will produce any other particular values between 0 |
| 761 |
and~|x|, because it rounds its answers. |
| 762 |
|
| 763 |
*/ |
| 764 |
|
| 765 |
int unif_rand(int x) |
| 766 |
{ |
| 767 |
int y; |
| 768 |
next_random(); |
| 769 |
y = take_frac(abs(x), randoms[j_random]); |
| 770 |
if (y == abs(x)) |
| 771 |
return 0; |
| 772 |
else if (x > 0) |
| 773 |
return y; |
| 774 |
else |
| 775 |
return -y; |
| 776 |
} |
| 777 |
|
| 778 |
/*tex |
| 779 |
|
| 780 |
Finally, a normal deviate with mean zero and unit standard deviation can readily |
| 781 |
be obtained with the ratio method (Algorithm 3.4.1R in {\sl The Art of Computer |
| 782 |
Programming\/}. |
| 783 |
|
| 784 |
*/ |
| 785 |
|
| 786 |
int norm_rand(void) |
| 787 |
{ |
| 788 |
/*tex What the book would call $2^{16}X$, $2^{28}U$, and $-2^{24}\ln U$. */ |
| 789 |
int x, u, l; |
| 790 |
do { |
| 791 |
do { |
| 792 |
next_random(); |
| 793 |
x = take_frac(112429, randoms[j_random] - fraction_half); |
| 794 |
/*tex Which is $2^{16}\sqrt{8/e}\approx 112428.82793$. */ |
| 795 |
next_random(); |
| 796 |
u = randoms[j_random]; |
| 797 |
} while (abs(x) >= u); |
| 798 |
x = make_frac(x, u); |
| 799 |
/*tex More fuzzyness: $2^{24}\cdot12\ln2\approx139548959.6165$. */ |
| 800 |
l = 139548960 - m_log(u); |
| 801 |
} while (ab_vs_cd(1024, l, x, x) < 0); |
| 802 |
return x; |
| 803 |
} |
| 804 |
|
| 805 |
/*tex |
| 806 |
|
| 807 |
This function could also be expressed as a macro, but it is a useful breakpoint |
| 808 |
for debugging. |
| 809 |
|
| 810 |
*/ |
| 811 |
|
| 812 |
int fix_int(int val, int min, int max) |
| 813 |
{ |
| 814 |
return (val < min ? min : (val > max ? max : val)); |
| 815 |
} |