bigint_sub implementiert

From: Jens Rohler <jkcr@rohler.de>
Date: Mon Aug 22 2005 - 18:38:25 CEST

Hallo,

ich habe bigint_sub implementiert und ausserdem eine Funktion zum Vergleich
von zwei bigint-Zahlen (bigint_cmp) hinzugefuegt.

Nachfolgend die beiden patches fuer bigint.h und bigint.c.

Gruesse,
Jens

--- ../dtaus-0.7/bigint.h 2001-11-01 19:27:27.000000000 +0100
+++ ./bigint.h 2005-08-22 18:16:36.590776728 +0200
@@ -41,3 +41,11 @@
 bigint bigint_string(char *s);
 
 void bigint_sprintf (char *res, char *format, bigint a);
+
+/* compares two bigints
+ a < b => -1
+ a > b => 1
+ a == b => 0
+*/
+int bigint_cmp(bigint a, bigint b);
+
--- ../dtaus-0.7/bigint.c 2001-11-18 00:12:42.000000000 +0100
+++ ./bigint.c 2005-08-22 18:16:36.590776728 +0200
@@ -46,8 +46,50 @@
 
 bigint bigint_sub(bigint a, bigint b)
 {
- /* FIXME */
- fprintf (stderr, "bigint_sub not supported yet.\n");
+ unsigned long int tmp;
+ int t;
+ int i;
+ int cmp = bigint_cmp(a,b);
+ bigint rem = bigint_int(0);;
+
+ if (cmp == -1) {
+ fprintf(stderr, "Error in bigint_sub: result is negative.\n");
+ return bigint_int(0);
+ }
+ if (cmp == 0)
+ return bigint_int(0);
+
+ for (i=0;i<BIGINT_LEN;i++) {
+ if (a.val[i] >= b.val[i]) {
+ a.val[i] = a.val[i] - b.val[i];
+ continue;
+ }
+ tmp = b.val[i];
+ tmp = tmp - a.val[i];
+ a.val[i] = 0;
+ t = i;
+ while (1) {
+ if (t+1 >= BIGINT_LEN) {
+ fprintf(stderr, "Error in bigint_sub.\n");
+ return bigint_int(0);
+ }
+ if ( a.val[t+1] == 0 ) {
+ t++;
+ continue;
+ }
+ a.val[t+1] = a.val[t+1] - 1;
+ while(t >= 0) {
+ rem = bigint_add(rem, bigint_int(a.val[t]));
+ a.val[t] = BIGINT_MAX - 1;
+ t--;
+ }
+ a.val[i] = a.val[i] - tmp;
+ a = bigint_add(a, bigint_int(1));
+ a = bigint_add(a, rem);
+ break;
+ }
+ }
+
   return a;
 }
 
@@ -109,3 +151,19 @@
   }
   sprintf (res, format, s);
 }
+
+int bigint_cmp(bigint a, bigint b)
+{
+ int i;
+
+ for (i=BIGINT_LEN-1; i>=0; i--) {
+ if (a.val[i] > b.val[i])
+ return 1;
+ if (a.val[i] < b.val[i])
+ return -1;
+ if (a.val[i] == b.val[i])
+ continue;
+ }
+
+ return 0;
+}
Received on Mon, 22 Aug 2005 18:38:25 +0200

This archive was generated by hypermail 2.1.8 : Mon Aug 22 2005 - 18:38:20 CEST