aboutsummaryrefslogtreecommitdiffstats
path: root/src/lesson02a_arithmetic.cpp
blob: e83eb885918285eb81133fc2d56faacbe645e7b4 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//============================================================================
// Author      : Svenson Han Gothel and Sven Gothel
// Version     : 0.1
// Copyright   : MIT
// Description : C++ Lesson 0.2a Applied arithmetic using C++
//============================================================================

#include <cstdio>
#include <iostream>
#include <cmath>

#include <limits>

/**
 * Lesson 0.2
 *
 * Implementing simple arithmetic functionality
 */

struct DivisionResult {
	int factor;
	int remainder;
};
DivisionResult divide(const int z, const int n) {
	int r=0;
    int c=z;
    while ( c >= n ) {
    	c -= n; // c = c - n;
    	++r; // r = r + 1;
    }
	return DivisionResult { .factor=r, .remainder=c };
}

int mult(const int a, const int b) {
	int r=0;
	for (int i=0; i<a; ++i) {
		r += b; // r=r+b;
	}
	return r;
}

int summe_iter(const int a, const int b) {
	int r=0;
	for (int i=a; i<=b; ++i) {
		r += i; // r=r+i;
	}
	return r;
}

int summe(const int a, const int b) {
	// ( a + b ) * ( n / 2 )
	const int n = b - a + 1;
	return ( ( a + b ) * n ) / 2;
}

// function declaration
int my_pow(const int x, const int y);

// function definition
int my_pow(const int x, const int y) {
	int r=1;
	for (int i=0; i<y; ++i){
		r=mult(r, x); // r = r * x
	}
	return r;
}

int factorial(int v) {
    // factorial(5) = 1*2*3*4*5
    int r = v;
    while( v > 1 ) {
        // v = v - 1;
        // r = r * v;
        r *= --v;
    }
    return r;
}

float machineEpsilon() {
    const float one(1);
    const float two(2);
    float x = 1;
    float res;
    // float t;
    int i=0;
    do {
        res = x;
        x = x / two;
        // t = one + x;
        // fprintf(stderr, "%3d: %.48f -> %.48f, %.48f\n", i, res, x, t);
        // std::cerr << i << ": " << res << " -> " << x << std::endl;
        ++i;
    } while ( one + x  > one );
    (void)i;
    return res;
}

void test_limits() {
    unsigned int i = std::numeric_limits<unsigned int>::max() - 10;
    do {
        fprintf(stderr, "%u, bytes %zu\n", i, sizeof(i));
        if( i == std::numeric_limits<unsigned int>::max() ) {
            break; // end outter loop
        }
        ++i;
    } while( true );
}

int main(int argc, const char* argv[]) {
    {
        // loop through all program invocation arguments and print them
        for(int i=0; i<argc; ++i) {
            printf("cmd_arg[%d]: %s\n", i, argv[i]);
        }
    }
    {
        float e = machineEpsilon();
        fprintf(stderr, "machineEpsilon: %.48f\n", e);
    }
    {
        test_limits();
    }

    int error_counter = 0;

	{
		for(int numerator = 0; numerator < 100; ++numerator) {
			for(int denominator=1; denominator<=10; ++denominator) {
				const int expected = numerator / denominator;
				const int expected_r = numerator % denominator;
				const DivisionResult has = divide(numerator, denominator);

				if( expected != has.factor || expected_r != has.remainder ) {
					printf("Error-1: %d / %d != %d + %d / %d, error result %d + %d / %d\n",
							numerator, denominator,
							expected, expected_r, denominator,
							has.factor, has.remainder, denominator);
					++error_counter;
				}

				const int test_numerator = denominator*has.factor+has.remainder;
				if( test_numerator != numerator ) {
                    printf("Error-2: %d * %d + %d != %d, error result %d + %d / %d\n",
                            denominator, has.factor, has.remainder,
                            numerator,
                            has.factor, has.remainder, denominator);
                    ++error_counter;
				}
			}
		}
	}
	{
		for(int a = 0; a <= 10; ++a) {
			for(int b = 0; b <= 10; ++b) {
				const int expected = a*b;
				const int has = mult(a, b);
				if( expected != has ) {
					printf("Error: %d * %d != %d, error result %d\n",
							a, b, expected, has);
					++error_counter;
				}
			}
		}
	}
	if( true ) {
		for(int a = 0; a <= 10; ++a) {
			for(int b = a; b <= a + 10; ++b) {
				const int expected = summe_iter(a, b);
				const int has = summe(a, b);
				if( expected != has ) {
					printf("Error: Sum[%d .. %d] != %d, error result %d\n",
							a, b, expected, has);
					++error_counter;
				}
			}
		}
	}
	{
		for(int a = 0; a <= 10; ++a) {
			for(int b=0; b <= 5; ++b) {
				const int expected = (int)std::pow(a, b);
				const int has = my_pow(a, b);
				if( expected != has ) {
					printf("Error: %d ** %d != %d, error result %d ? ",
							a, b, expected, has);
					++error_counter;
				}
			}
		}
    }
	{
	    int last_exp = 1;
        for(int a = 0; a <= 10; ++a) {
            const int expected = last_exp * a;
            if( 0 < expected) {
                last_exp = expected;
            }
            const int has = factorial(a);
            if( expected != has ) {
                printf("Error: factorial(%d) != %d, error result %d ? ",
                        a, expected, has);
                ++error_counter;
            } else {
                printf("factorial(%d) = %d\n", a, has);
            }
        }
	}

	if( 0 == error_counter ) {
		std::cout << "No error" << std::endl;
		return 0;
	} else {
		std::cout << error_counter << " error(s)" << std::endl;
		return 1;
	}
}