void divide_bignum(bignum *a, bignum *b, bignum *c) { bignum row, tmp; int asign, bsign, i, j; initialize_bignum(c); c->signbit = a->signbit * b->signbit; asign = a->signbit; bsign = b->signbit; a->signbit = PLUS; b->signbit = PLUS; initialize_bignum(&row); initialize_bignum(&tmp); c->lastdigit = a->lastdigit; for (i = a->lastdigit; i >= 0; i--) { digit_shift(&row, 1); row.digits[0] = a->digits[i]; c->digits[i] = 0; while (compare_bignum(&row, b) != PLUS) { c->digits[i]++; subtract_bignum(&row, b, &tmp); row = tmp; } } zero_justify(c); a->signbit = asign; b->signbit = bsign; }
add_bignum(bignum *a, bignum *b, bignum *c) { int carry; /* carry digit */ int i; /* counter */ initialize_bignum(c); if (a->signbit == b->signbit) c->signbit = a->signbit; else { if (a->signbit == MINUS) { a->signbit = PLUS; subtract_bignum(b,a,c); a->signbit = MINUS; } else { b->signbit = PLUS; subtract_bignum(a,b,c); b->signbit = MINUS; } return; } c->lastdigit = max(a->lastdigit,b->lastdigit)+1; carry = 0; for (i=0; i<=(c->lastdigit); i++) { c->digits[i] = (char) (carry+a->digits[i]+b->digits[i]) % 10; carry = (carry + a->digits[i] + b->digits[i]) / 10; } zero_justify(c); }
void subtract_bignum(bignum *a, bignum *b, bignum *c) { int borrow, v, i; if ((a->signbit == MINUS) || (b->signbit == MINUS)) { b->signbit = -1 * b->signbit; add_bignum(a, b, c); b->signbit = -1 * b->signbit; return; } if (compare_bignum(a, b) == PLUS) { subtract_bignum(b, a, c); c->signbit = MINUS; return; } c->lastdigit = max(a->lastdigit, b->lastdigit); borrow = 0; for (i = 0; i <= c->lastdigit; i++) { v = (a->digits[i] - borrow - b->digits[i]); if (a->digits[i] > 0) borrow = 0; if (v < 0) { v = v + 10; borrow = 1; } c->digits[i] = (char) v % 10; } zero_justify(c); }
void multiply_bignum(bignum *a, bignum *b, bignum *c) { bignum row, tmp; int i, j; initialize_bignum(c); row = *a; for (i = 0; i <= b->lastdigit; i++) { for (j = 1; j <= b->digits[i]; j++) { add_bignum(c, &row, &tmp); *c = tmp; } digit_shift(&row, 1); } c->signbit = a->signbit * b->signbit; zero_justify(c); }
/** Subtracts big numbers; i.e. c = a - b */ void subtract_bignum( bignum* a, bignum* b, bignum* c ) { int borrow; /* has anything been borrowed? */ int v; /* placeholder digit */ int i; /* counter */ initialize_bignum( c ); if( ( a->signbit == MINUS ) || ( b->signbit == MINUS ) ) { b->signbit = -1 * b->signbit; add_bignum( a, b, c ); b->signbit = -1 * b->signbit; return; } if( compare_bignum( a, b ) == PLUS ) { subtract_bignum( b, a, c ); c->signbit = MINUS; return; } c->lastdigit = max( a->lastdigit, b->lastdigit ); borrow = 0; for( i = 0; i <= ( c->lastdigit ); i++ ) { v = ( a->digits[ i ] - borrow - b->digits[ i ] ); if( a->digits[ i ] > 0) { borrow = 0; } if( v < 0 ) { v = v + 10; borrow = 1; } c->digits[ i ] = ( char ) v % 10; } zero_justify( c ); }
void multiply_bignum(bignum *a, bignum *b, bignum *c) { bignum row; /* represent shifted row */ bignum tmp; /* placeholder bignum */ int i,j; /* counters */ initialize_bignum(c); row = *a; for(i = 0; i <= b->lastdigit; i++) { for(j = 1; j <= b->digits[i]; j++) { add_bignum(c,&row,&tmp); *c = tmp; } digit_shift(&row,1); } c->signbit = a->signbit * b->signbit; zero_justify(c); }
void divide_bignum(bignum *a, bignum *b, bignum *c) { bignum row; /* represent shifted row */ bignum tmp; /* placeholder bignum */ int asign, bsign; /* temporary signs */ int i; /* counters */ initialize_bignum(c); c->signbit = a->signbit * b->signbit; asign = a->signbit; bsign = b->signbit; a->signbit = PLUS; b->signbit = PLUS; initialize_bignum(&row); initialize_bignum(&tmp); c->lastdigit = a->lastdigit; for(i = a->lastdigit; i >= 0; i--) { digit_shift(&row,1); row.digits[0] = a->digits[i]; c->digits[i] = 0; while(compare_bignum(&row,b) != PLUS) { c->digits[i] ++; subtract_bignum(&row,b,&tmp); row = tmp; } } zero_justify(c); a->signbit = asign; b->signbit = bsign; }