2 bigint.c - Manage big positive integer numbers
3 Copyright (c) 1996,2001 Martin Schulze <joey@artis.uni-oldenburg.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 bigint bigint_add(bigint a, bigint b)
30 unsigned long int tmp;
33 for (i=0; i<BIGINT_LEN; i++) {
34 tmp = a.val[i] + b.val[i];
35 if (tmp >= BIGINT_MAX) {
36 c.val[i] = tmp - BIGINT_MAX;
40 fprintf(stderr, "Overflow in bigint addition.");
47 bigint bigint_sub(bigint a, bigint b)
50 fprintf (stderr, "bigint_sub not supported yet.\n");
54 bigint bigint_int(int num)
59 for (i=0; i<BIGINT_LEN; i++) x.val[i] = 0L;
65 bigint bigint_string(char *s)
67 char tmp[BIGINT_PREC+1];
72 for (i=0; i<BIGINT_LEN; i++) a.val[i] = 0L;
73 if ((x = (char *)malloc(strlen(s)+1))) {
75 for (i=0; i<BIGINT_LEN; i++) {
78 if (strlen(x) > BIGINT_PREC)
79 cp += (strlen(x) - BIGINT_PREC);
82 sscanf(tmp, "%lu", &a.val[i]);
91 void bigint_sprintf (char *res, char *format, bigint a)
93 char s[(BIGINT_PREC*BIGINT_LEN)+1];
94 char tmp[BIGINT_PREC+1];
98 memset (s, 0, sizeof(s));
99 sprintf(form, "%%0%dlu", BIGINT_PREC);
101 for (;max>0 && !a.val[max-1];max--);
102 for (i=0; i<max; i++) {
104 sprintf(tmp, form, a.val[i]);
106 sprintf(tmp, "%lu", a.val[i]);
107 sprintf (res, "%s%s", tmp, s);
108 sprintf (s, "%s", res);
110 sprintf (res, format, s);