Orbiter 2022
Combinatorial Objects
longinteger_object.cpp
Go to the documentation of this file.
1// longinteger_object.cpp
2//
3// Anton Betten
4//
5// started: October 26, 2002
6
7
8
9
10#include "foundations.h"
11
12using namespace std;
13
14
15namespace orbiter {
16namespace layer1_foundations {
17namespace ring_theory {
18
19
20
22{
23 sgn = FALSE;
24 l = 0;
25 r = NULL;
26}
27
29{
30 freeself();
31}
32
34{
35 int f_v = FALSE;
36
37 if (r) {
38 if (f_v) {
39 cout << "longinteger_object::freeself ";
41 D.print_digits(rep(), len());
42 cout << endl;
43 //print(cout);
44 }
45
46 FREE_char(r);
47 r = NULL;
48 }
49 if (f_v) {
50 cout << "longinteger_object::freeself" << endl;
51 }
52}
53
54void longinteger_object::create(long int i, const char *file, int line)
55{
56 long int ii, j, dj;
57 int f_v = FALSE;
59
60 ii = i;
61 freeself();
62 if (i < 0) {
63 sgn = TRUE;
64 i = -i;
65 }
66 else {
67 sgn = FALSE;
68 }
69 if (i == 0) {
70 r = NEW_char_with_tracking(1, file, line);
71 r[0] = 0;
72 l = 1;
73 return;
74 }
75 l = NT.lint_log10(i);
76 if (f_v) {
77 cout << "longinteger_object::create "
78 "i=" << i << " log = " << l << endl;
79 }
80 r = NEW_char(l);
81 j = 0;
82 while (i) {
83 dj = i % 10;
84 r[j] = dj;
85 i /= 10;
86 j++;
87 }
88 if (f_v) {
89 cout << "longinteger_object::create "
90 "i=" << ii << " created ";
92 D.print_digits(rep(), len());
93 cout << " with j=" << j << " digits" << endl;
94 }
95}
96
97void longinteger_object::create_product(int nb_factors, int *factors)
98{
100
101 D.multiply_up(*this, factors, nb_factors, 0 /* verbose_level */);
102}
103
105// creates a^e
106{
108 int *factors;
109 int i;
110
111 factors = NEW_int(e);
112 for (i = 0; i < e; i++) {
113 factors[i] = a;
114 }
115
116 D.multiply_up(*this, factors, e, 0 /* verbose_level */);
117
118 FREE_int(factors);
119}
120
122// creates a^e - 1
123{
126 int *factors;
127 int i;
128
129 A.create(-1, __FILE__, __LINE__);
130 factors = NEW_int(e);
131 for (i = 0; i < e; i++) {
132 factors[i] = a;
133 }
134
135 D.multiply_up(*this, factors, e, 0 /* verbose_level */);
136 D.add_in_place(*this, A);
137
138 FREE_int(factors);
139}
140
142 int b, int *rep, int len)
143{
145 longinteger_object x, y, z, bb;
146 int i;
147
148 x.zero();
149 bb.create(b, __FILE__, __LINE__);
150 for (i = len - 1; i >= 0; i--) {
151 D.mult(x, bb, z);
152 y.create(rep[i], __FILE__, __LINE__);
153 D.add(z, y, x);
154 }
155 x.assign_to(*this);
156}
157
159 const char *str, int verbose_level)
160{
161 int f_v = (verbose_level >= 1);
163 longinteger_object x, y, z, bb;
164 int i, len;
165
166 len = strlen(str);
167 x.zero();
168 bb.create(10, __FILE__, __LINE__);
169 for (i = len - 1; i >= 0; i--) {
170 D.mult(x, bb, z);
171 y.create(str[len - 1 - i] - '0', __FILE__, __LINE__);
172 D.add(z, y, x);
173 }
174 if (f_v) {
175 cout << "longinteger_object::create_from_base_10_string "
176 "str = " << str << endl;
177 cout << "object = " << x << endl;
178 }
179 x.assign_to(*this);
180}
181
183{
185}
186
188{
189 create_from_base_10_string(str.c_str(), 0);
190}
191
193{
194 int i, x = 0;
195
196 for (i = l - 1; i >= 0; i--) {
197 x *= 10;
198 x += r[i];
199 }
200 if (sgn) {
201 x = -x;
202 }
203 return x;
204}
205
207{
208 int i;
209 long int x = 0;
210
211 for (i = l - 1; i >= 0; i--) {
212 x *= 10;
213 x += r[i];
214 }
215 if (sgn) {
216 x = -x;
217 }
218 return x;
219}
220
222{
223 int i;
224 int f_v = FALSE;
225
226 if (f_v) {
227 cout << "longinteger_object::assign_to "
228 "before b.freeself" << endl;
229 if (b.rep()) {
230 cout << "this is what we free: ";
232 D.print_digits(b.rep(), b.len());
233 cout << endl;
234 }
235 }
236 b.freeself();
237 if (f_v) {
238 cout << "longinteger_object::assign_to "
239 "after b.freeself" << endl;
240 }
241 b.sgn = sgn;
242 b.l = l;
243 b.r = NEW_char(b.l);
244 for (i = 0; i < l; i++) {
245 b.r[i] = r[i];
246 }
247 if (f_v) {
248 cout << "after assign: ";
250 D.print_digits(b.rep(), b.len());
251 cout << endl;
252 cout << "longinteger_object::assign_to done" << endl;
253 }
254}
255
257{
258 char s;
259 int length;
260 char *rep;
261
262 s = sgn;
263 length = l;
264 rep = r;
265 sgn = b.sgn;
266 l = b.l;
267 r = b.r;
268 b.sgn = s;
269 b.l = (int) length;
270 b.r = rep;
271}
272
273ostream& longinteger_object::print(ostream& ost)
274{
275 int i;
276 char c;
277
278 if (r == NULL) {
279 ost << "NULL";
280 }
281 else {
282 if (sgn) {
283 ost << "-";
284 }
285 for (i = l - 1; i >= 0; i--) {
286 c = '0' + r[i];
287 ost << c;
288 }
289 if (orbiter_kernel_system::Orbiter->longinteger_f_print_scientific) {
290 if (l > 5) {
291 char c1, c2;
292
293 c1 = '0' + r[l - 1];
294 c2 = '0' + r[l - 2];
295 ost << " = " << c1 << "." << c2 << " x 10^" << l - 1;
296 }
297 }
298 }
299 return ost;
300}
301
303{
304 int i;
305 char c;
306
307 if (r == NULL) {
308 ost << "NULL";
309 }
310 else {
311 if (sgn) {
312 ost << "-";
313 }
314 for (i = l - 1; i >= 0; i--) {
315 c = '0' + r[i];
316 ost << c;
317 }
318 }
319 return ost;
320}
321
323{
324 int h;
325
326 h = l;
327 return h;
328}
329
330
332{
333 int h;
334
335 h = l;
336 if (sgn) {
337 h++;
338 }
339 return h;
340}
341
342void longinteger_object::print_width(ostream& ost, int width)
343{
344 int i, len, w;
345 char c;
346
347 if (r == NULL) {
348 len = width - 4;
349 for (i = 0; i < len; i++) {
350 ost << " ";
351 }
352 ost << "NULL";
353 return;
354 }
355 w = output_width();
356 if (w > width) {
357 len = width - 5;
358 for (i = 0; i < len; i++) {
359 ost << " ";
360 }
361 ost << "large";
362 }
363 else {
364 len = width - w;
365 for (i = 0; i < len; i++) {
366 ost << " ";
367 }
368 if (sgn) {
369 ost << "-";
370 }
371 for (i = l - 1; i >= 0; i--) {
372 c = '0' + r[i];
373 ost << c;
374 }
375 }
376}
377
379{
380 int i, j = 0;
381 char c;
382
383 if (r == NULL) {
384 str[0] = 0;
385 }
386 else {
387 if (sgn) {
388 str[j++] = '-';
389 }
390 for (i = l - 1; i >= 0; i--) {
391 c = '0' + r[i];
392 str[j++] = c;
393 }
394 }
395 str[j] = 0;
396}
397
399{
400 int i;
401
402 for (i = l - 1; i > 0; i--) {
403 if (r[i] != 0) {
404 break;
405 }
406 }
407 l = (int) i + 1;
408 if (l == 1 && r[0] == 0) {
409 sgn = FALSE;
410 }
411}
412
414{
415 if (is_zero()) {
416 return;
417 }
418 if (sign()) {
419 sign() = FALSE;
420 }
421 else {
422 sign() = TRUE;
423 }
424
425}
426
428{
429 normalize();
430 if (l == 1 && r[0] == 0) {
431 return TRUE;
432 }
433 else {
434 return FALSE;
435 }
436}
437
439{
440 create(0, __FILE__, __LINE__);
441}
442
444{
445 normalize();
446 if (!sgn && l == 1 && r[0] == 1) {
447 return TRUE;
448 }
449 else {
450 return FALSE;
451 }
452}
453
455{
456 normalize();
457 if (sgn && l == 1 && r[0] == 1) {
458 return TRUE;
459 }
460 else {
461 return FALSE;
462 }
463}
464
466{
467 normalize();
468 if (l == 1 && r[0] == 1) {
469 return TRUE;
470 }
471 else {
472 return FALSE;
473 }
474}
475
477{
478 create(1, __FILE__, __LINE__);
479}
480
482{
485
486 b.create(1, __FILE__, __LINE__);
487 D.add(*this, b, c);
488 swap_with(c);
489}
490
492{
495
496 b.create(-1, __FILE__, __LINE__);
497 D.add(*this, b, c);
498 swap_with(c);
499}
500
502{
505
506 b.create(a, __FILE__, __LINE__);
507 D.add(*this, b, c);
508 swap_with(c);
509}
510
512{
514
515 create(i, __FILE__, __LINE__);
516 D.power_int(*this, j);
517}
518
519
520ostream& operator<<(ostream& ost, longinteger_object& p)
521{
522 // cout << "operator<< starting" << endl;
523 p.print(ost);
524 // cout << "operator<< finished" << endl;
525 return ost;
526}
527
529{
532
533 b.create(a, __FILE__, __LINE__);
534 return D.compare(*this, b);
535}
536
537}}}
538
539
domain to compute with objects of type longinteger
Definition: ring_theory.h:240
int compare(longinteger_object &a, longinteger_object &b)
void multiply_up(longinteger_object &a, int *x, int len, int verbose_level)
void add(longinteger_object &a, longinteger_object &b, longinteger_object &c)
void mult(longinteger_object &a, longinteger_object &b, longinteger_object &c)
void add_in_place(longinteger_object &a, longinteger_object &b)
a class to represent arbitrary precision integers
Definition: ring_theory.h:366
void create_from_base_10_string(const char *str, int verbose_level)
void create(long int i, const char *file, int line)
#define FREE_int(p)
Definition: foundations.h:640
#define NEW_char(n)
Definition: foundations.h:632
#define NEW_int(n)
Definition: foundations.h:625
#define NEW_char_with_tracking(n, file, line)
Definition: foundations.h:633
#define TRUE
Definition: foundations.h:231
#define FALSE
Definition: foundations.h:234
#define FREE_char(p)
Definition: foundations.h:646
orbiter_kernel_system::orbiter_session * Orbiter
global Orbiter session
ostream & operator<<(ostream &ost, longinteger_object &p)
the orbiter library for the classification of combinatorial objects