-
Notifications
You must be signed in to change notification settings - Fork 0
/
proj2.c
520 lines (453 loc) · 12.4 KB
/
proj2.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/**
* @name Projekt 2 - Iterační výpočty
* @author Dominik Harmim <xharmi00@stud.fit.vutbr.cz>
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define PRINT_ERR(s, ...) fprintf(stderr, s "\n", __VA_ARGS__)
#define RESULT_PRECISION_FOR_OUTPUT 12
#define EPS 1e-8
/**
* constant string - help
*/
const char usage_string[] =
" proj2 --log X N (calculating the natural logarithm of the number X in\n"
" N iterations)\n"
" proj2 --pow X Y N (calculate exponential function of the number Y with\n"
" the basis X in N iterations)";
/**
* function prototypes
*/
double taylor_log(double x, unsigned int n);
double cfrac_log(double x, unsigned int n);
double taylor_pow(double x, double y, unsigned int n);
double taylorcf_pow(double x, double y, unsigned int n);
double mylog(double x);
double mypow(double x, double y);
float check_spc_argv_of_log(double x);
float check_spc_argvs_of_pow(double x, double y);
/**
* check special value of logarithm argument
*
* @param x number from which we calculate the logarithm
* @return if argument of logaritm is special value, return special result, otherwise -1
*/
float check_spc_argv_of_log(double x)
{
if (fabs(x) == 0.0) {
return -INFINITY;
} else if (x < 0) {
return NAN;
} else if (x == 1.0) {
return 0.0;
} else if (isinf(x)) {
return INFINITY;
} else if (isnan(x)) {
return NAN;
}
return -1;
}
/**
* check special values of exponencial function arguments
*
* @param x number from which we calculate the exponencial function
* @param y exponent
* @return if arguments of exponencial function are special values, return special result, otherwise -1
*/
float check_spc_argvs_of_pow(double x, double y)
{
if (x <= 0.0) {
return NAN;
} else if (x == 1.0) {
return 1.0;
} else if (fabs(y) == 0.0) {
return 1.0;
} else if (isnan(x) || isnan(y)) {
return NAN;
} else if (y == -INFINITY) {
if (fabs(x) < 1.0) {
return INFINITY;
}
return 0.0;
} else if (y == INFINITY) {
if (fabs(x) < 1.0) {
return 0.0;
}
return INFINITY;
} else if (x == INFINITY) {
if (y < 0.0) {
return 0.0;
}
return INFINITY;
}
return -1;
}
/**
* calculating the natural logarithm of the number `x` in `n` iterations using Taylor polynomial
*
* @param x number from which we calculate the logarithm
* @param n number of members in polynomial > 0
* @return logarithm of the number x
*/
double taylor_log(double x, unsigned int n)
{
double spc_result;
if ((spc_result = check_spc_argv_of_log(x)) != -1) {
return spc_result;
}
double sum = 0.0, numerator = 1.0, frac;
unsigned int i;
if (x < 1.0) {
// algorithm for (0,1)
x = 1.0 - x;
for (i = 1; i <= n; i++) {
numerator *= x;
frac = numerator / i;
if (isinf(frac)) {
break;
}
sum -= frac;
}
} else {
// algorithm for <1,INF)
for (i = 1; i <= n; i++) {
numerator *= (x - 1.0) / x;
frac = numerator / i;
if (isinf(frac)) {
break;
}
sum += frac;
}
}
return sum;
}
/**
* calculating the natural logarithm of the number `x` in `n` iterations using continued fractions
*
* @param x number from which we calculate the logarithm
* @param n number of steps in continued fraction > 0
* @return logarithm of the number x
*/
double cfrac_log(double x, unsigned int n)
{
double spc_result;
if ((spc_result = check_spc_argv_of_log(x)) != -1) {
return spc_result;
}
x = (x - 1.0) / (x + 1.0);
unsigned int coef = 2 * n - 1, i = n;
double sum = coef, pow2x = x * x, frac;
while (i > 1) {
i--;
coef -= 2;
frac = i * i * pow2x / sum;
if (isinf(frac)) {
break;
}
sum = coef - frac;
}
return 2.0 * x / sum;
}
/**
* calculate exponential function of the number `y` with the basis `x` in `n` iterations
* for the calculation of the natural logarithm uses function taylor_log
*
* @see taylor_log
* @param x number from which we calculate the exponencial function
* @param y exponent
* @param n number of members in polynomial > 0
* @return the base to the exponent power
*/
double taylor_pow(double x, double y, unsigned int n)
{
double spc_result;
if ((spc_result = check_spc_argvs_of_pow(x, y)) != -1) {
return spc_result;
}
double log_x = taylor_log(x, n);
if (isinf(log_x)) {
return log_x;
}
double sum = 1.0, pow_y = 1.0, pow_ln_x = 1.0, denominator = 1.0, frac;
for (unsigned int i = 1; i <= n; i++) {
pow_y *= y;
pow_ln_x *= log_x;
denominator *= i;
frac = pow_y * pow_ln_x / denominator;
if (isinf(frac)) {
break;
}
sum += frac;
}
return sum;
}
/**
* calculate exponential function of the number `y` with the basis `x` in `n` iterations
* for the calculation of the natural logarithm usees function cfrac_log
*
* @see cfrac_log
* @param x number from which we calculate the exponencial function
* @param y exponent
* @param n number of members in polynomial > 0
* @return the base to the exponent power
*/
double taylorcf_pow(double x, double y, unsigned int n)
{
double spc_result;
if ((spc_result = check_spc_argvs_of_pow(x, y)) != -1) {
return spc_result;
}
double log_x = cfrac_log(x, n);
if (isinf(log_x)) {
return log_x;
}
double sum = 1.0, pow_y = 1.0, pow_ln_x = 1.0, denominator = 1.0, frac;
for (unsigned int i = 1; i <= n; i++) {
pow_y *= y;
pow_ln_x *= log_x;
denominator *= i;
frac = pow_y * pow_ln_x / denominator;
if (isinf(frac)) {
break;
}
sum += frac;
}
return sum;
}
/**
* calculating the natural logarithm of the number `x`
* elects to accurate calculation type and minimum number of iterations for required accuracy EPS
*
* @param x number from which we calculate the logarithm
* @return logarithm of the number x
*/
double mylog(double x)
{
double spc_result;
if ((spc_result = check_spc_argv_of_log(x)) != -1) {
return spc_result;
}
double taylor_result = 0.0, taylor_prev_result, taylor_numerator = 1.0, taylor_frac;
double cfrac_result = 0.0, cfrac_prev_result;
unsigned int n = 1;
do {
// Taylor
taylor_prev_result = taylor_result;
if (x < 1.0) {
// algorithm for (0,1)
taylor_numerator *= 1.0 - x;
taylor_frac = taylor_numerator / n;
if (isinf(taylor_frac)) {
break;
}
taylor_result -= taylor_frac;
} else {
// algorithm for <1,INF)
taylor_numerator *= (x - 1.0) / x;
taylor_frac = taylor_numerator / n;
if (isinf(taylor_frac)) {
break;
}
taylor_result += taylor_frac;
}
// Cfrac
cfrac_prev_result = cfrac_result;
cfrac_result = cfrac_log(x, n);
n++;
} while (fabs(taylor_result - taylor_prev_result) > EPS && fabs(cfrac_result - cfrac_prev_result) > EPS);
return fabs(taylor_result - taylor_prev_result) <= EPS ? taylor_result : cfrac_result;
}
/**
* calculate exponential function of the number `y` with the basis `x`
* elects to accurate calculation type and minimum number of iterations for required accuracy EPS
*
* @param x number from which we calculate the exponencial function
* @param y exponent
* @return the base to the exponent power
*/
double mypow(double x, double y)
{
double spc_result;
if ((spc_result = check_spc_argvs_of_pow(x, y)) != -1) {
return spc_result;
}
double log_x = mylog(x);
if (isinf(log_x)) {
return log_x;
}
double sum = 1.0, pow_y = 1.0, pow_ln_x = 1.0, denominator = 1.0, frac = 0.0, prev_frac;
unsigned int i = 1;
do {
prev_frac = frac;
pow_y *= y;
pow_ln_x *= log_x;
denominator *= i;
frac = pow_y * pow_ln_x / denominator;
if (isinf(frac)) {
break;
}
sum += frac;
i++;
} while (fabs(frac - prev_frac) > EPS);
return sum;
}
/**
* converts value of argument in string format to double
* if in value there is non-convertible part, print it to stderr
*
* @param value value of argument in string format (from argv)
* @param arg name of argument that value belongs to
* @param error set to true if in value there is non-convertible part
* @return converted double value
*/
double value_of_arg_to_double(const char *value, const char *arg, bool *error)
{
char *endptr = NULL;
double number = strtod(value, &endptr);
if (*endptr) {
PRINT_ERR("Value of argument %s must be real number but there is non-convertible part: %s.", arg, endptr);
*error = true;
}
return number;
}
/**
* converts value of argument in string format to unsigned int
* if in value there is non-convertible part, or minus sign, print it to stderr
*
* @param value value of argument in string format (from argv)
* @param arg name of argument that value belongs to
* @param error set to true if in value there is non-convertible part or minus sign
* @return converted unsigned int value
*/
unsigned int value_of_arg_to_unsigned_int(const char *value, const char *arg, bool *error)
{
if (strchr(value, '-') != NULL) {
PRINT_ERR("Value of argument %s must be a positive number, given %s.", arg, value);
*error = true;
return 0;
}
char *endptr = NULL;
unsigned int number = strtoul(value, &endptr, 10);
if (*endptr) {
PRINT_ERR("Value of argument %s must be integer but there is non-convertible part: %s.", arg, endptr);
*error = true;
}
return number;
}
/**
* print results of logarithm functions to stdout
*
* @param x number from which we calculate the logarithm
* @param n number of iterations for calculation
*/
void print_log_results(double x, unsigned int n)
{
printf(" log(%g) = %.*g\n", x, RESULT_PRECISION_FOR_OUTPUT, log(x));
printf(" cfrac_log(%g) = %.*g\n", x, RESULT_PRECISION_FOR_OUTPUT, cfrac_log(x, n));
printf("taylor_log(%g) = %.*g\n", x, RESULT_PRECISION_FOR_OUTPUT, taylor_log(x, n));
#ifdef DEBUG
printf(" log(%g) = %.7e\n", x, log(x));
printf(" mylog(%g) = %.7e\n", x, mylog(x));
#endif
}
/**
* print results of exponencial functions to stdout
*
* @param x basis of exponencial function
* @param y exponent
* @param n number of iterations for calculation
*/
void print_pow_results(double x, double y, unsigned int n)
{
printf(" pow(%g,%g) = %.*g\n", x, y, RESULT_PRECISION_FOR_OUTPUT, pow(x, y));
printf(" taylor_pow(%g,%g) = %.*g\n", x, y, RESULT_PRECISION_FOR_OUTPUT, taylor_pow(x, y, n));
printf("taylorcf_pow(%g,%g) = %.*g\n", x, y, RESULT_PRECISION_FOR_OUTPUT, taylorcf_pow(x, y, n));
#ifdef DEBUG
printf(" pow(%g,%g) = %.7e\n", x, y, pow(x, y));
printf(" mypow(%g,%g) = %.7e\n", x, y, mypow(x, y));
#endif
}
/**
* process input arguments
*
* @param argc count of program arguments
* @param argv program arguments
* @param help true if usage string should be listed, false otherwise
* @return true if arguments was processed successfully, false otherwise
*/
bool process_input_args(const int argc, const char *argv[], bool *help)
{
if (argc > 1) {
// option --log
if (strcmp(argv[1], "--log") == 0) {
// argument --log must be followed by argument X and N
if (argc != 4) {
*help = true;
return true;
}
//convert arguments and do validation
bool error = false;
double x = value_of_arg_to_double(argv[2], "--log X", &error);
if (error) {
return false;
}
unsigned int n = value_of_arg_to_unsigned_int(argv[3], "--log N", &error);
if (error) {
return false;
}
if (n == 0) {
PRINT_ERR("Value of argument --log N must be greater than 0, given %i.", n);
return false;
}
// print results to stdout
print_log_results(x, n);
return true;
// option --pow
} else if (strcmp(argv[1], "--pow") == 0) {
// argument --pow must be followed by argument X, Y and N
if (argc != 5) {
*help = true;
return true;
}
//convert arguments and do validation
bool error = false;
double x = value_of_arg_to_double(argv[2], "--pow X", &error);
if (error) {
return false;
}
double y = value_of_arg_to_double(argv[3], "--pow Y", &error);
if (error) {
return false;
}
unsigned int n = value_of_arg_to_unsigned_int(argv[4], "--pow N", &error);
if (error) {
return false;
}
if (n == 0) {
PRINT_ERR("Value of argument --pow N must be greater than 0, given %i.", n);
return false;
}
// print results to stdout
print_pow_results(x, y, n);
return true;
}
}
// unknown option
*help = true;
return true;
}
int main(const int argc, const char *argv[])
{
// start processing arguments
bool help = false, result = process_input_args(argc, argv, &help);
if (help) {
// to be listed help/usage string
printf("usage:\n%s\n", usage_string);
}
// return value of this program (of this main function)
// depends on return value of function process_input_args
return result ? EXIT_SUCCESS : EXIT_FAILURE;
}