Corrected the bigint_int() routine with regards to large integers (as
[infodrom/dtaus] / bigint.c
index 059c71b..99fc7b0 100644 (file)
--- a/bigint.c
+++ b/bigint.c
@@ -1,6 +1,6 @@
 /*
     bigint.c - Manage big positive integer numbers
-    Copyright (c) 1996  Martin Schulze <joey@artis.uni-oldenburg.de>
+    Copyright (c) 1996,2001,5  Martin Schulze <joey@infodrom.org>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -34,10 +34,10 @@ bigint bigint_add(bigint a, bigint b)
     tmp = a.val[i] + b.val[i];
     if (tmp >= BIGINT_MAX) {
       c.val[i] = tmp - BIGINT_MAX;
-      if (i<BIGINT_LEN)
+      if (i<BIGINT_LEN-1)
        a.val[i+1]++;
       else
-       fprintf(stderr, "Overflow in bigint addition.");
+       fprintf(stderr, "Overflow in bigint addition.\n");
     } else
       c.val[i] = tmp;
   }
@@ -57,8 +57,13 @@ bigint bigint_int(int num)
   int i;
 
   for (i=0; i<BIGINT_LEN; i++) x.val[i] = 0L;
-  x.val[0] = num;
-  x.val[1] = 0;
+  if (num < BIGINT_MAX)
+    x.val[0] = num;
+  else {
+    x.val[0] = num-BIGINT_MAX;
+    x.val[1] = num/BIGINT_MAX;
+  }
+
   return x;
 }
 
@@ -95,10 +100,11 @@ void bigint_sprintf (char *res, char *format, bigint a)
   char form[6];
   int i, max;
 
-  bzero(s, sizeof(s));
+  memset (s, 0, sizeof(s));
   sprintf(form, "%%0%dlu", BIGINT_PREC);
   max = BIGINT_LEN;
   for (;max>0 && !a.val[max-1];max--);
+  if (max == 0) s[0] = '0';
   for (i=0; i<max; i++) {
     if (i<max-1)
       sprintf(tmp, form, a.val[i]);