Browse Source

Implemented division, fixed assert() and fixed uncaught problems.

long_to_char
Cameron Weinfurt 3 years ago
parent
commit
5e70f48e25
  1. 57
      infint.c
  2. 4
      infint.h
  3. 32
      infint_four_func.c

57
infint.c

@ -1,27 +1,26 @@
#include "infint.h"
#include "infint_util.c"
#include "infint_add.c"
#include "infint_four_func.c"
#ifdef UNIT_TESTS
#define NDEBUG
#include <assert.h>
void test_utils() {
infint_t *ii1, *ii2;
ii1 = infint_new(1);
assert(ii1->data[0] = 0);
assert(ii1->data[0] == 0);
assert(ii1->size == ii1->capacity == 1);
assert(ii1->sign == 0);
ii2 = infint_clone(ii1);
assert(ii2->data[0] = 0);
assert(ii2->data[0] == 0);
assert(ii2->size == ii1->capacity == 1);
assert(ii2->sign == 0);
infint_grow_to(ii1, 8);
assert(ii2->data[0] = 0);
assert(ii2->size == ii1->capacity == 8);
assert(ii2->sign == 0);
assert(ii1->data[0] == 0);
assert(ii1->size == 8 && ii1->capacity == 8);
assert(ii1->sign == 0);
ii2->data[0] = 25;
infint_comp(ii2);
@ -59,6 +58,7 @@ void test_add() {
ii = infint_new(2);
ii->data[0] = 0;
ii->data[1] = 1;
ii->size = 2;
ii->sign = 1;
infint_add(ii, 50);
assert(ii->data[0] == 50);
@ -88,10 +88,53 @@ void test_sub() {
assert(ii->sign == 1);
}
void test_mult() {
infint_t *ii = infint_new(2);
ii->data[0] = 1;
infint_mult(ii, 20);
assert(ii->data[0] == 20);
assert(ii->data[1] == 0);
assert(ii->size == 1);
infint_mult(ii, 20);
assert(ii->data[0] == 0);
assert(ii->data[1] == 4);
assert(ii->size == 2);
infint_mult(ii, 0);
assert(ii->data[0] == 0);
assert(ii->data[1] == 0);
assert(ii->size == 1);
}
void test_div() {
infint_t *ii = infint_new(2);
ii->data[0] = 0;
ii->data[1] = 10;
ii->size = 2;
assert(infint_div(ii, 2) == 0);
assert(ii->data[0] == 0);
assert(ii->data[1] == 5);
assert(infint_div(ii, 10) == 0);
assert(ii->data[0] == 50);
assert(ii->data[1] == 0);
assert(ii->size == 1);
assert(infint_div(ii, 3) == 2);
assert(ii->data[0] == 16);
assert(ii->data[1] == 0);
assert(ii->size == 1);
}
void test_infint() {
test_utils();
test_add();
test_sub();
test_mult();
test_div();
}
#endif

4
infint.h

@ -23,8 +23,10 @@ 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
// Four functions
void infint_add(infint_t *ii, uint8_t n);
void infint_sub(infint_t *ii, uint8_t n);
void infint_mult(infint_t *ii, uint8_t n);
uint8_t infint_div(infint_t *ii, uint8_t n);
#endif

32
infint_add.c → infint_four_func.c

@ -8,7 +8,7 @@ void infint_add(infint_t *ii, uint8_t n) {
acc += ii->data[0];
ii->data[0] = acc % 100;
acc /= 100;
for (int i = 1; acc > 0; ++i) {
for (size_t i = 1; acc > 0; ++i) {
infint_grow_to(ii, i + 1);
acc += ii->data[i];
ii->data[i] = acc % 100;
@ -33,3 +33,33 @@ void infint_sub(infint_t *ii, uint8_t n) {
infint_comp(ii);
}
}
void infint_mult(infint_t *ii, uint8_t n) {
if (n == 0) {
memset(ii->data, 0, ii->size);
ii->size = 1;
ii->sign = 0;
} else {
uint16_t acc = 0;
for (size_t i = 0; i < ii->size; ++i) {
acc += n * ii->data[i];
ii->data[i] = acc % 100;
acc /= 100;
}
if (acc > 0) {
infint_grow_to(ii, ii->size + 1);
ii->data[ii->size - 1] = acc;
}
}
}
uint8_t infint_div(infint_t *ii, uint8_t n) {
uint16_t acc = 0;
for (size_t i = ii->size - 1; i != -1; --i) {
acc += ii->data[i];
ii->data[i] = acc / n;
acc = (acc % n) * 100;
}
while (ii->data[ii->size - 1] == 0) ii->size -= 1;
return acc / 100;
}
Loading…
Cancel
Save