aboutsummaryrefslogtreecommitdiff
path: root/writeups/multiprecision/fibo-crude-mp.c
blob: c6a5b7f3b1b974b8a96f1d764a57fa03c23e3aee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>

#define N 101
#define MAX_CHAR 256

void do_ieee754 (void) {
	double a = 0.0;
	double b = 1.0;
	double c;

	for (int i = 0; i < N; i += 1) {
		c = a + b;
		printf("%.0lf\n", c);
		b = a;
		a = c;
	}
}

void add (char *out, const char *a, const char *b) {
	int i;
	int n;
	int carry = 0;
	bool flag;

	for (i = 0; i < MAX_CHAR - 1; i += 1) {
		n = carry;
		flag = false;
		if (a[i] != 0) {
			n += a[i] - '0';
			flag = true;
		}
		if (b[i] != 0) {
			n += b[i] - '0';
			flag = true;
		}
		carry = n / 10;
		if (!flag && carry == 0 && n == 0) {
			out[i] = 0;
			return;
		}

		out[i] = (n % 10) + '0';
	}

	assert(carry == 0); // detect "overflow"
}

void print_reversed (const char *s) {
	const size_t l = strlen(s);
	const char *p = s + l;

	for (size_t i = 0; i < l; i += 1) {
		p -= 1;
		putchar(*p);
	}
	putchar('\n');
}

void do_crude_mp (void) {
	static char a[MAX_CHAR];
	static char b[MAX_CHAR];
	static char c[MAX_CHAR];

	strcpy(a, "0");
	strcpy(b, "1");
	memset(c, 0, MAX_CHAR);

	for (int i = 0; i < N; i += 1) {
		add(c, a, b); // c = a + b;
		print_reversed(c);
		memcpy(b, a, MAX_CHAR); // b = a;
		memcpy(a, c, MAX_CHAR); // a = c;
	}
}

bool parse_flag (const char *s) {
	if (strcmp(s, "true") == 0) {
		return true;
	}
	else if (strcmp(s, "false") == 0) {
		return false;
	}
	else {
		double tmp = 0.0;

		sscanf(s, "%lf", &tmp);
		return tmp != 0.0;
	}
}

int main (const int argc, const char **argv) {
	if (argc == 1 || (argc > 1 && parse_flag(argv[1]))) {
		do_crude_mp();
	}
	else {
		do_ieee754();
	}

	return 0;
}