1 // Public integer operations.
6 #include "cln/number.h"
7 #include "cln/integer_class.h"
8 #include "cln/exception.h"
9 #include "cln/random.h"
13 CL_DEFINE_AS_CONVERSION(cl_I)
16 // Konversion Integer >=0, <2^32 nach uintL.
17 // Wandelt Integer >=0 in Unsigned Longword um.
19 // > obj: Integer, sollte >=0, <2^32 sein
20 // < ergebnis: der Wert des Integer als 32-Bit-Zahl.
21 extern uint32 cl_I_to_UL (const cl_I& obj);
23 // Konversion Integer >=-2^31, <2^31 nach sintL.
24 // Wandelt Integer in Signed Longword um.
26 // > obj: Integer, sollte >=-2^31, <2^31 sein
27 // < ergebnis: der Wert des Integer als 32-Bit-Zahl.
28 extern sint32 cl_I_to_L (const cl_I& obj);
30 // Convert an integer to a C `int' or `unsigned int'.
32 inline int cl_I_to_int (const cl_I& x) { return cl_I_to_L(x); }
33 inline unsigned int cl_I_to_uint (const cl_I& x) { return cl_I_to_UL(x); }
36 // Convert an integer to a 64-bit 'quad' type.
38 extern uint64 cl_I_to_UQ (const cl_I& obj);
39 extern sint64 cl_I_to_Q (const cl_I& obj);
42 // Convert an integer to a C `long' or `unsigned long'.
43 #if (long_bitsize==32)
44 inline long cl_I_to_long (const cl_I& x) { return cl_I_to_L(x); }
45 inline unsigned long cl_I_to_ulong (const cl_I& x) { return cl_I_to_UL(x); }
46 #elif (long_bitsize==64)
47 inline long cl_I_to_long (const cl_I& x) { return cl_I_to_Q(x); }
48 inline unsigned long cl_I_to_ulong (const cl_I& x) { return cl_I_to_UQ(x); }
51 // Convert an integer to a counter type.
52 #if (intCsize==long_bitsize)
53 inline uintC cl_I_to_UC (const cl_I& x) { return cl_I_to_ulong(x); }
54 inline sintC cl_I_to_C (const cl_I& x) { return cl_I_to_long(x); }
55 #elif (intCsize==int_bitsize)
56 inline uintC cl_I_to_UC (const cl_I& x) { return cl_I_to_uint(x); }
57 inline sintC cl_I_to_C (const cl_I& x) { return cl_I_to_int(x); }
60 // Convert an integer to an exponent type.
61 #if (intEsize==intLsize)
62 inline uintE cl_I_to_UE (const cl_I& x) { return cl_I_to_UL(x); }
63 inline sintE cl_I_to_E (const cl_I& x) { return cl_I_to_L(x); }
64 #elif (intEsize==intQsize)
65 inline uintE cl_I_to_UE (const cl_I& x) { return cl_I_to_UQ(x); }
66 inline sintE cl_I_to_E (const cl_I& x) { return cl_I_to_Q(x); }
70 // Logische Operationen auf Integers:
72 // (LOGIOR x y), wenn x, y Integers sind.
74 extern const cl_I logior (const cl_I& x, const cl_I& y);
76 // (LOGXOR x y), wenn x, y Integers sind.
78 extern const cl_I logxor (const cl_I& x, const cl_I& y);
80 // (LOGAND x y), wenn x, y Integers sind.
82 extern const cl_I logand (const cl_I& x, const cl_I& y);
84 // (LOGEQV x y), wenn x, y Integers sind.
86 extern const cl_I logeqv (const cl_I& x, const cl_I& y);
88 // (LOGNAND x y), wenn x, y Integers sind.
90 extern const cl_I lognand (const cl_I& x, const cl_I& y);
92 // (LOGNOR x y), wenn x, y Integers sind.
94 extern const cl_I lognor (const cl_I& x, const cl_I& y);
96 // (LOGANDC2 x y), wenn x, y Integers sind.
98 extern const cl_I logandc2 (const cl_I& x, const cl_I& y);
100 // (LOGANDC1 x y), wenn x, y Integers sind.
102 inline const cl_I logandc1 (const cl_I& x, const cl_I& y)
104 return logandc2(y,x);
107 // (LOGORC2 x y), wenn x, y Integers sind.
109 extern const cl_I logorc2 (const cl_I& x, const cl_I& y);
111 // (LOGORC1 x y), wenn x, y Integers sind.
113 inline const cl_I logorc1 (const cl_I& x, const cl_I& y)
118 // (LOGNOT x), wenn x ein Integer sind.
120 extern const cl_I lognot (const cl_I& x);
122 // Konstanten für BOOLE:
142 // (BOOLE op x y), wenn x und y Integers und op ein Objekt sind.
144 extern const cl_I boole (cl_boole op, const cl_I& x, const cl_I& y);
146 // Prüft, ob (LOGTEST x y), wo x und y Integers sind.
147 // (LOGTEST x y) = (NOT (ZEROP (LOGAND x y))).
148 // < ergebnis: /=0, falls ja; =0, falls nein.
149 extern bool logtest (const cl_I& x, const cl_I& y);
151 // Prüft, ob (LOGBITP x y), wo x und y Integers sind.
152 // Ergebnis: /=0, wenn ja; =0, wenn nein.
153 extern bool logbitp (uintC x, const cl_I& y);
154 extern bool logbitp (const cl_I& x, const cl_I& y);
156 // Prüft, ob (ODDP x), wo x ein Integer ist.
157 // Ergebnis: /=0, falls ja; =0, falls nein.
158 extern bool oddp (const cl_I& x);
160 // Prüft, ob (EVENP x), wo x ein Integer ist.
161 // Ergebnis: /=0, falls ja; =0, falls nein.
162 inline bool evenp (const cl_I& x)
165 // (ASH x y), wo x und y Integers sind. Ergebnis Integer.
166 extern const cl_I ash (const cl_I& x, sintC y);
167 extern const cl_I ash (const cl_I& x, const cl_I& y);
169 // Thrown when shift amount is too large.
170 class ash_exception : public runtime_exception {
172 explicit ash_exception (const cl_I& badamount);
175 // (LOGCOUNT x), wo x ein Integer ist. Ergebnis uintC.
176 extern uintC logcount (const cl_I& x);
178 // (INTEGER-LENGTH x), wo x ein Integer ist. Ergebnis uintC.
179 extern uintC integer_length (const cl_I& x);
181 // (ORD2 x) = max{n>=0: 2^n | x }, wo x ein Integer /=0 ist. Ergebnis uintC.
182 extern uintC ord2 (const cl_I& x);
184 // power2p(x) stellt fest, ob ein Integer x>0 eine Zweierpotenz ist.
185 // Ergebnis: n>0, wenn x=2^(n-1), 0 sonst.
186 extern uintC power2p (const cl_I& x);
188 inline const cl_I operator| (const cl_I& x, const cl_I& y)
189 { return logior(x,y); }
190 inline const cl_I operator^ (const cl_I& x, const cl_I& y)
191 { return logxor(x,y); }
192 inline const cl_I operator& (const cl_I& x, const cl_I& y)
193 { return logand(x,y); }
194 inline const cl_I operator~ (const cl_I& x)
195 { return lognot(x); }
196 // This could be optimized to use in-place operations.
197 inline cl_I& operator|= (cl_I& x, const cl_I& y) { return x = x | y; }
198 inline cl_I& operator^= (cl_I& x, const cl_I& y) { return x = x ^ y; }
199 inline cl_I& operator&= (cl_I& x, const cl_I& y) { return x = x & y; }
202 // Addition/Subtraktion von Integers
204 // (1+ x), wo x ein Integer ist. Ergebnis Integer.
205 extern const cl_I plus1 (const cl_I& x);
207 // (1- x), wo x ein Integer ist. Ergebnis Integer.
208 extern const cl_I minus1 (const cl_I& x);
210 // (+ x y), wo x und y Integers sind. Ergebnis Integer.
211 extern const cl_I operator+ (const cl_I& x, const cl_I& y);
212 // Dem C++-Compiler muß man auch das Folgende sagen:
213 inline const cl_I operator+ (const int x, const cl_I& y)
214 { return cl_I(x) + y; }
215 inline const cl_I operator+ (const unsigned int x, const cl_I& y)
216 { return cl_I(x) + y; }
217 inline const cl_I operator+ (const long x, const cl_I& y)
218 { return cl_I(x) + y; }
219 inline const cl_I operator+ (const unsigned long x, const cl_I& y)
220 { return cl_I(x) + y; }
222 inline const cl_I operator+ (const long long x, const cl_I& y)
223 { return cl_I(x) + y; }
224 inline const cl_I operator+ (const unsigned long long x, const cl_I& y)
225 { return cl_I(x) + y; }
227 inline const cl_I operator+ (const cl_I& x, const int y)
228 { return x + cl_I(y); }
229 inline const cl_I operator+ (const cl_I& x, const unsigned int y)
230 { return x + cl_I(y); }
231 inline const cl_I operator+ (const cl_I& x, const long y)
232 { return x + cl_I(y); }
233 inline const cl_I operator+ (const cl_I& x, const unsigned long y)
234 { return x + cl_I(y); }
236 inline const cl_I operator+ (const cl_I& x, const long long y)
237 { return x + cl_I(y); }
238 inline const cl_I operator+ (const cl_I& x, const unsigned long long y)
239 { return x + cl_I(y); }
242 // (- x), wenn x ein Integer ist. Ergebnis Integer.
243 extern const cl_I operator- (const cl_I& x);
245 // (- x y), wo x und y Integers sind. Ergebnis Integer.
246 extern const cl_I operator- (const cl_I& x, const cl_I& y);
247 // Dem C++-Compiler muß man auch das Folgende sagen:
248 inline const cl_I operator- (const int x, const cl_I& y)
249 { return cl_I(x) - y; }
250 inline const cl_I operator- (const unsigned int x, const cl_I& y)
251 { return cl_I(x) - y; }
252 inline const cl_I operator- (const long x, const cl_I& y)
253 { return cl_I(x) - y; }
254 inline const cl_I operator- (const unsigned long x, const cl_I& y)
255 { return cl_I(x) - y; }
257 inline const cl_I operator- (const long long x, const cl_I& y)
258 { return cl_I(x) - y; }
259 inline const cl_I operator- (const unsigned long long x, const cl_I& y)
260 { return cl_I(x) - y; }
262 inline const cl_I operator- (const cl_I& x, const int y)
263 { return x - cl_I(y); }
264 inline const cl_I operator- (const cl_I& x, const unsigned int y)
265 { return x - cl_I(y); }
266 inline const cl_I operator- (const cl_I& x, const long y)
267 { return x - cl_I(y); }
268 inline const cl_I operator- (const cl_I& x, const unsigned long y)
269 { return x - cl_I(y); }
271 inline const cl_I operator- (const cl_I& x, const long long y)
272 { return x - cl_I(y); }
273 inline const cl_I operator- (const cl_I& x, const unsigned long long y)
274 { return x - cl_I(y); }
277 // (abs x), wenn x ein Integer ist. Ergebnis Integer.
278 extern const cl_I abs (const cl_I& x);
281 inline const cl_I operator<< (const cl_I& x, sintC y) // assume 0 <= y < 2^(intCsize-1)
283 inline const cl_I operator<< (const cl_I& x, const cl_I& y) // assume y >= 0
285 inline const cl_I operator>> (const cl_I& x, sintC y) // assume 0 <= y < 2^(intCsize-1)
286 { return ash(x,-y); }
287 inline const cl_I operator>> (const cl_I& x, const cl_I& y) // assume y >= 0
288 { return ash(x,-y); }
291 // Vergleich von Integers
293 // equal(x,y) vergleicht zwei Integers x und y auf Gleichheit.
294 extern bool equal (const cl_I& x, const cl_I& y);
295 // equal_hashcode(x) liefert einen equal-invarianten Hashcode für x.
296 extern uint32 equal_hashcode (const cl_I& x);
298 // compare(x,y) vergleicht zwei Integers x und y.
299 // Ergebnis: 0 falls x=y, +1 falls x>y, -1 falls x<y.
300 extern cl_signean compare (const cl_I& x, const cl_I& y);
302 inline bool operator== (const cl_I& x, const cl_I& y)
303 { return equal(x,y); }
304 inline bool operator!= (const cl_I& x, const cl_I& y)
305 { return !equal(x,y); }
306 inline bool operator<= (const cl_I& x, const cl_I& y)
307 { return compare(x,y)<=0; }
308 inline bool operator< (const cl_I& x, const cl_I& y)
309 { return compare(x,y)<0; }
310 inline bool operator>= (const cl_I& x, const cl_I& y)
311 { return compare(x,y)>=0; }
312 inline bool operator> (const cl_I& x, const cl_I& y)
313 { return compare(x,y)>0; }
315 // minusp(x) == (< x 0)
316 extern bool minusp (const cl_I& x);
318 // plusp(x) == (> x 0)
319 extern bool plusp (const cl_I& x);
321 // zerop(x) stellt fest, ob ein Integer = 0 ist.
322 extern bool zerop (const cl_I& x);
325 // BYTE-Operationen auf Integers
331 cl_byte (uintC s, uintC p) : size (s), position (p) {}
334 // (LDB byte n), wo n ein Integer ist.
335 extern const cl_I ldb (const cl_I& n, const cl_byte& b);
337 // ldb_test(n,byte) führt (LDB-TEST byte n) aus, wobei n ein Integer ist.
338 // Ergebnis: false wenn nein (also alle fraglichen Bits =0), true wenn ja.
339 extern bool ldb_test (const cl_I& n, const cl_byte& b);
341 // (MASK-FIELD byte n), wo n ein Integer ist.
342 extern const cl_I mask_field (const cl_I& n, const cl_byte& b);
344 // (DEPOSIT-FIELD newbyte byte n), wo n und newbyte Integers sind.
345 extern const cl_I deposit_field (const cl_I& newbyte, const cl_I& n, const cl_byte& b);
347 // (DPB newbyte byte n), wo n und newbyte Integers sind.
348 extern const cl_I dpb (const cl_I& newbyte, const cl_I& n, const cl_byte& b);
351 // Multiplikation ganzer Zahlen
353 // (* x y), wo x und y Integers sind. Ergebnis Integer.
354 extern const cl_I operator* (const cl_I& x, const cl_I& y);
355 // Dem C++-Compiler muß man auch das Folgende sagen:
356 inline const cl_I operator* (const int x, const cl_I& y)
357 { return cl_I(x) * y; }
358 inline const cl_I operator* (const unsigned int x, const cl_I& y)
359 { return cl_I(x) * y; }
360 inline const cl_I operator* (const long x, const cl_I& y)
361 { return cl_I(x) * y; }
362 inline const cl_I operator* (const unsigned long x, const cl_I& y)
363 { return cl_I(x) * y; }
365 inline const cl_I operator* (const long long x, const cl_I& y)
366 { return cl_I(x) * y; }
367 inline const cl_I operator* (const unsigned long long x, const cl_I& y)
368 { return cl_I(x) * y; }
370 inline const cl_I operator* (const cl_I& x, const int y)
371 { return x * cl_I(y); }
372 inline const cl_I operator* (const cl_I& x, const unsigned int y)
373 { return x * cl_I(y); }
374 inline const cl_I operator* (const cl_I& x, const long y)
375 { return x * cl_I(y); }
376 inline const cl_I operator* (const cl_I& x, const unsigned long y)
377 { return x * cl_I(y); }
379 inline const cl_I operator* (const cl_I& x, const long long y)
380 { return x * cl_I(y); }
381 inline const cl_I operator* (const cl_I& x, const unsigned long long y)
382 { return x * cl_I(y); }
385 // (EXPT x 2), wo x Integer ist.
386 extern const cl_I square (const cl_I& x);
388 // (EXPT x y), wo x Integer, y Integer >0 ist.
389 extern const cl_I expt_pos (const cl_I& x, uintL y);
390 extern const cl_I expt_pos (const cl_I& x, const cl_I& y);
392 // Fakultät (! n), wo n Fixnum >=0 ist. Ergebnis Integer.
393 extern const cl_I factorial (uintL n);
394 //CL_REQUIRE(cl_I_factorial)
396 // Double factorial (!! n), with n Fixnum >=0. Returns integer.
397 extern const cl_I doublefactorial (uintL n);
399 // Binomialkoeffizient (n \choose k) = n! / k! (n-k)!, wo n,k >= 0 sind.
400 extern const cl_I binomial (uintL n, uintL k);
403 // Division ganzer Zahlen
405 // Return type for division operators.
406 // x / y --> (q,r) with x = y*q+r.
412 cl_I_div_t (const cl_I& q, const cl_I& r) : quotient(q), remainder(r) {}
415 // Dividiert zwei Integers x,y >=0 und liefert den Quotienten x/y >=0.
416 // Bei y=0 Error. Die Division muß aufgehen, sonst Error.
418 // > x,y: Integers >=0
419 // < ergebnis: Quotient x/y, ein Integer >=0
420 extern const cl_I exquopos (const cl_I& x, const cl_I& y);
422 // Dividiert zwei Integers x,y und liefert den Quotienten x/y.
423 // Bei y=0 Error. Die Division muß aufgehen, sonst Error.
426 // < ergebnis: Quotient x/y, ein Integer
427 extern const cl_I exquo (const cl_I& x, const cl_I& y);
429 // Thrown when quotient is no integer.
430 class exquo_exception : public runtime_exception {
432 exquo_exception (const cl_I& x, const cl_I& y);
435 // mod(x,y) = (mod x y), wo x,y Integers sind.
436 extern const cl_I mod (const cl_I& x, const cl_I& y);
438 // rem(x,y) = (rem x y), wo x,y Integers sind.
439 extern const cl_I rem (const cl_I& x, const cl_I& y);
441 // Dividiert zwei Integers x,y und liefert Quotient und Rest
442 // (q,r) := (floor x y)
445 // < q,r: Quotient q, Rest r
446 extern const cl_I_div_t floor2 (const cl_I& x, const cl_I& y);
447 extern const cl_I floor1 (const cl_I& x, const cl_I& y);
449 // Dividiert zwei Integers x,y und liefert Quotient und Rest
450 // (q,r) := (ceiling x y)
453 // < q,r: Quotient q, Rest r
454 extern const cl_I_div_t ceiling2 (const cl_I& x, const cl_I& y);
455 extern const cl_I ceiling1 (const cl_I& x, const cl_I& y);
457 // Dividiert zwei Integers x,y und liefert Quotient und Rest
458 // (q,r) := (truncate x y)
461 // < q,r: Quotient q, Rest r
462 extern const cl_I_div_t truncate2 (const cl_I& x, const cl_I& y);
463 extern const cl_I truncate1 (const cl_I& x, const cl_I& y);
465 // Dividiert zwei Integers x,y und liefert Quotient und Rest
466 // (q,r) := (round x y)
469 // < q,r: Quotient q, Rest r
470 extern const cl_I_div_t round2 (const cl_I& x, const cl_I& y);
471 extern const cl_I round1 (const cl_I& x, const cl_I& y);
474 // ggT und kgV von Integers
476 // Liefert den ggT zweier Integers.
478 // > a,b: zwei Integers
479 // < ergebnis: (gcd a b), ein Integer >=0
480 extern const cl_I gcd (const cl_I& a, const cl_I& b);
481 extern uintV gcd (uintV a, uintV b);
483 // Liefert den ggT zweier Integers samt Beifaktoren.
484 // g = xgcd(a,b,&u,&v)
485 // > a,b: zwei Integers
486 // < u, v, g: Integers mit u*a+v*b = g >= 0
487 extern const cl_I xgcd (const cl_I& a, const cl_I& b, cl_I* u, cl_I* v);
488 // Im Fall A/=0, B/=0 genügt das Ergebnis (g,u,v) den Ungleichungen:
489 // Falls |A| = |B| : g = |A|, u = (signum A), v = 0.
490 // Falls |B| | |A|, |B| < |A| : g = |B|, u = 0, v = (signum B).
491 // Falls |A| | |B|, |A| < |B| : g = |A|, u = (signum A), v = 0.
492 // Sonst: |u| <= |B| / (2*g), |v| <= |A| / (2*g).
493 // In jedem Fall |u| <= |B|/g, |v| < |A|/g.
494 // (Beweis: Im Prinzip macht man ja mehrere Euklid-Schritte auf einmal. Im
495 // letzten Fall - oBdA |A| > |B| - braucht man mindestens zwei Euklid-Schritte,
496 // also gilt im Euklid-Tableau
498 // --------------------------------------------
502 // n-1 -(-1)^n*x[n-1] (-1)^n*y[n-1] z[n-1]
503 // n (-1)^n*x[n] -(-1)^n*y[n] z[n]
504 // n+1 -(-1)^n*x[n+1] (-1)^n*y[n+1] z[n+1] = 0
505 // --------------------------------------------
506 // g = z[n], |u|=x[n], |v|=y[n]
507 // n>=2, z[0] > ... > z[n-1] > z[n] = g, g | z[n-1], also z[n-1] >= 2*g.
508 // Da aber mit (-1)^i*x[i]*|A| - (-1)^i*y[i]*|B| = z[i] für i=0..n+1
509 // und x[i]*y[i+1] - x[i+1]*y[i] = (-1)^i für i=0..n,
510 // x[i]*z[i+1] - x[i+1]*z[i] = (-1)^i*|B| für i=0..n,
511 // y[i]*z[i+1] - y[i+1]*z[i] = -(-1)^i*|A| für i=0..n
512 // auch |A| = y[i+1]*z[i] + y[i]*z[i+1], |B| = x[i+1]*z[i] + x[i]*z[i+1]
513 // für i=0..n (Cramersche Regel), folgt
514 // |A| = y[n]*z[n-1] + y[n-1]*z[n] >= y[n]*2*g + 0 = |v|*2*g,
515 // |B| = x[n]*z[n-1] + x[n-1]*z[n] >= x[n]*2*g + 0 = |u|*2*g.)
517 // Liefert den kgV zweier Integers.
519 // > a,b: zwei Integers
520 // < ergebnis: (lcm a b), ein Integer >=0
521 extern const cl_I lcm (const cl_I& a, const cl_I& b);
524 // Wurzel aus ganzen Zahlen
526 // Zieht die Wurzel (ISQRT x) aus einem Integer.
528 // > x: Integer (sollte >=0 sein)
530 // < ergebnis: true falls x Quadratzahl, false sonst
531 extern bool isqrt (const cl_I& x, cl_I* w);
532 // Wenn das boolesche Ergebnis uninteressant ist:
533 inline const cl_I isqrt (const cl_I& x) { cl_I w; isqrt(x,&w); return w; }
535 // Stellt fest, ob ein Integer >=0 eine Quadratzahl ist.
537 // > x: ein Integer >=0
538 // < w: Integer (sqrt x) falls x Quadratzahl
539 // < ergebnis: true ..................., false sonst
540 extern bool sqrtp (const cl_I& x, cl_I* w);
542 // Stellt fest, ob ein Integer >=0 eine n-te Potenz ist.
544 // > x: ein Integer >=0
545 // > n: ein Integer >0
546 // < w: Integer (expt x (/ n)) falls x eine n-te Potenz
547 // < ergebnis: true ........................, false sonst
548 extern bool rootp (const cl_I& x, uintL n, cl_I* w);
549 extern bool rootp (const cl_I& x, const cl_I& n, cl_I* w);
552 // max(x,y) liefert (max x y), wo x und y ganze Zahlen sind.
553 extern const cl_I max (const cl_I& x, const cl_I& y);
555 // min(x,y) liefert (min x y), wo x und y ganze Zahlen sind.
556 extern const cl_I min (const cl_I& x, const cl_I& y);
558 // signum(x) liefert (signum x), wo x eine ganze Zahl ist.
559 extern const cl_I signum (const cl_I& x);
562 // Multipliziert ein Integer mit 10 und addiert eine weitere Ziffer.
563 // mul_10_plus_x(y,x)
564 // > y: Integer Y (>=0)
565 // > x: Ziffernwert X (>=0,<10)
566 // < ergebnis: Integer Y*10+X (>=0)
567 extern const cl_I mul_10_plus_x (const cl_I& y, unsigned char x);
570 // 2-adische Inverse.
571 // cl_recip2adic(n,x)
573 // > x: Integer, ungerade
574 // < ergebnis: n-Bit-Zahl y == (x mod 2^n)^-1, d.h. y*x == 1 mod 2^n
575 extern const cl_I cl_recip2adic (uintL n, const cl_I& x);
577 // 2-adische Division.
578 // cl_div2adic(n,x,y)
581 // > y: Integer, ungerade
582 // < ergebnis: n-Bit-Zahl z == (x mod 2^n)/(y mod 2^n), d.h. z*y == x mod 2^n
583 extern const cl_I cl_div2adic (uintL n, const cl_I& x, const cl_I& y);
586 // numerator(r) liefert den Zähler des Integer r.
587 inline const cl_I numerator (const cl_I& r)
589 // denominator(r) liefert den Nenner (> 0) des Integer r.
590 inline const cl_I denominator (const cl_I& r)
591 { (void)r; return 1; }
594 // Konversion zu einem C "float".
595 extern float float_approx (const cl_I& x);
597 // Konversion zu einem C "double".
598 extern double double_approx (const cl_I& x);
601 // random_I(randomstate,n) liefert zu einem Integer n>0 ein zufälliges
602 // Integer x mit 0 <= x < n.
603 // > randomstate: ein Random-State, wird verändert
604 extern const cl_I random_I (random_state& randomstate, const cl_I& n);
606 inline const cl_I random_I (const cl_I& n)
607 { return random_I(default_random_state,n); }
609 // testrandom_I(randomstate) liefert ein zufälliges Integer zum Testen.
610 // > randomstate: ein Random-State, wird verändert
611 extern const cl_I testrandom_I (random_state& randomstate);
613 inline const cl_I testrandom_I ()
614 { return testrandom_I(default_random_state); }
617 // This could be optimized to use in-place operations.
618 inline cl_I& operator+= (cl_I& x, const cl_I& y) { return x = x + y; }
619 inline cl_I& operator+= (cl_I& x, const int y) { return x = x + y; }
620 inline cl_I& operator+= (cl_I& x, const unsigned int y) { return x = x + y; }
621 inline cl_I& operator+= (cl_I& x, const long y) { return x = x + y; }
622 inline cl_I& operator+= (cl_I& x, const unsigned long y) { return x = x + y; }
623 inline cl_I& operator++ /* prefix */ (cl_I& x) { return x = plus1(x); }
624 inline void operator++ /* postfix */ (cl_I& x, int dummy) { (void)dummy; x = plus1(x); }
625 inline cl_I& operator-= (cl_I& x, const cl_I& y) { return x = x - y; }
626 inline cl_I& operator-= (cl_I& x, const int y) { return x = x - y; }
627 inline cl_I& operator-= (cl_I& x, const unsigned int y) { return x = x - y; }
628 inline cl_I& operator-= (cl_I& x, const long y) { return x = x - y; }
629 inline cl_I& operator-= (cl_I& x, const unsigned long y) { return x = x - y; }
630 inline cl_I& operator-- /* prefix */ (cl_I& x) { return x = minus1(x); }
631 inline void operator-- /* postfix */ (cl_I& x, int dummy) { (void)dummy; x = minus1(x); }
632 inline cl_I& operator*= (cl_I& x, const cl_I& y) { return x = x * y; }
633 inline cl_I& operator<<= (cl_I& x, sintC y) // assume 0 <= y < 2^(intCsize-1)
634 { return x = x << y; }
635 inline cl_I& operator<<= (cl_I& x, const cl_I& y) // assume y >= 0
636 { return x = x << y; }
637 inline cl_I& operator>>= (cl_I& x, sintC y) // assume 0 <= y < 2^(intCsize-1)
638 { return x = x >> y; }
639 inline cl_I& operator>>= (cl_I& x, const cl_I& y) // assume y >= 0
640 { return x = x >> y; }
641 #if 0 // Defining operator/ collides with the operator/ (cl_RA, cl_RA).
642 // operator/ should perform exquo(x,y), but people believe in the C semantics.
643 // And it would be wiser to use floor1 and mod instead of truncate1 and rem,
644 // but again, many C compilers implement / and % like this and people believe
646 inline const cl_I operator/ (const cl_I& x, const cl_I& y) { return truncate1(x,y); }
647 inline const cl_I operator% (const cl_I& x, const cl_I& y) { return rem(x,y); }
648 inline cl_I& operator/= (cl_I& x, const cl_I& y) { return x = x / y; }
649 inline cl_I& operator%= (cl_I& x, const cl_I& y) { return x = x % y; }
653 // Runtime typing support.
654 extern cl_class cl_class_fixnum;
655 extern cl_class cl_class_bignum;
656 CL_FORCE_LINK(cl_I_classes_dummy, cl_class_fixnum)
659 // Debugging support.
661 extern int cl_I_debug_module;
662 CL_FORCE_LINK(cl_I_debug_dummy, cl_I_debug_module)
667 #endif /* _CL_INTEGER_H */