Browse Source

Implemented subtraction, compliment and less than

long_to_char
Cameron Weinfurt 3 years ago
parent
commit
174e7fff37
  1. 49
      infint.c
  2. 2
      infint.h
  3. 25
      infint_add.c
  4. 11
      infint_util.c

49
infint.c

@ -22,6 +22,18 @@ void test_utils() {
assert(ii2->data[0] = 0);
assert(ii2->size == ii1->capacity == 8);
assert(ii2->sign == 0);
ii2->data[0] = 25;
infint_comp(ii2);
assert(ii2->data[0] == 75);
infint_comp(ii2);
assert(ii2->data[0] == 25);
assert(infint_less_than(ii2, 50));
ii2->data[0] = 75;
assert(!infint_less_than(ii2, 50));
ii1->data[7] = 1;
assert(!infint_less_than(ii1, 50));
}
void test_add() {
@ -40,13 +52,46 @@ void test_add() {
assert(ii->data[0] == 20);
assert(ii->data[1] == 0);
assert(ii->data[2] == 1);
assert(ii2->size == 3);
assert(ii1->capacity == 4);
assert(ii->size == 3);
assert(ii->capacity == 4);
infint_free(ii);
ii = infint_new(2);
ii->data[0] = 0;
ii->data[1] = 1;
ii->sign = 1;
infint_add(ii, 50);
assert(ii->data[0] == 50);
assert(ii->data[1] == 0);
assert(ii->sign == 1);
}
void test_sub() {
infint_t *ii = infint_new(2);
ii->data[0] = 0;
ii->data[1] = 1;
ii->size = 2;
infint_sub(ii, 75);
assert(ii->data[0] == 25);
assert(ii->data[1] == 0);
assert(ii->sign == 0);
infint_sub(ii, 75);
assert(ii->data[0] == 50);
assert(ii->data[1] == 0);
assert(ii->sign == 1);
infint_sub(ii, 75);
assert(ii->data[0] == 25);
assert(ii->data[1] == 1);
assert(ii->sign == 1);
}
void test_infint() {
test_utils();
test_add();
test_sub();
}
#endif

2
infint.h

@ -20,6 +20,8 @@ infint_t *infint_new(size_t init_cap);
infint_t *infint_clone(infint_t *ii);
void infint_free(infint_t *ii);
char infint_grow_to(infint_t *ii, size_t size);
void infint_comp(infint_t *ii);
char infint_less_than(infint_t *ii, uint8_t n);
// Addition and Subtraction
void infint_add(infint_t *ii, uint8_t n);

25
infint_add.c

@ -4,15 +4,15 @@ void infint_add(infint_t *ii, uint8_t n) {
infint_sub(ii, n);
ii->sign ^= 1;
} else {
uint16_t sum = n;
sum += ii->data[0];
ii->data[0] = sum % 100;
sum /= 100;
for (int i = 1; sum > 0; ++i) {
uint8_t acc = n;
acc += ii->data[0];
ii->data[0] = acc % 100;
acc /= 100;
for (int i = 1; acc > 0; ++i) {
infint_grow_to(ii, i + 1);
sum += ii->data[i];
ii->data[i] = sum % 100;
sum /= 100;
acc += ii->data[i];
ii->data[i] = acc % 100;
acc /= 100;
}
}
}
@ -22,5 +22,14 @@ void infint_sub(infint_t *ii, uint8_t n) {
ii->sign = 0;
infint_add(ii, n);
ii->sign = 1;
} else if (infint_less_than(ii, n)) {
ii->data[0] = n - ii->data[0];
ii->sign = 1;
} else {
infint_comp(ii);
infint_add(ii, n);
ii->data[ii->size - 1] = 0;
while (ii->data[ii->size - 1] == 0) ii->size -= 1;
infint_comp(ii);
}
}

11
infint_util.c

@ -17,7 +17,7 @@ infint_t *infint_clone(infint_t *ii) {
return iic;
}
void infint_del(infint_t *ii) {
void infint_free(infint_t *ii) {
free(ii->data);
free(ii);
}
@ -38,3 +38,12 @@ char infint_grow_to(infint_t *ii, size_t size) {
ii->size = size;
return 0;
}
void infint_comp(infint_t *ii) {
for (int i = 0; i < ii->size; ++i)
ii->data[i] = 100 - ii->data[i];
}
char infint_less_than(infint_t *ii, uint8_t n) {
return ii->size == 1 && ii->data[0] < n;
}
Loading…
Cancel
Save