Change smth

This commit is contained in:
AZEN-SGG 2025-05-02 04:20:19 +03:00
parent 4d61d7f22a
commit 1053f8e3c2
22 changed files with 93 additions and 297 deletions

View file

@ -1,16 +0,0 @@
#ifndef IO_STATUS_H
#define IO_STATUS_H
#define ERR_MEM "Error: Not enough memory!"
#define ERR_OPEN "Error: Cannot open file"
#define ERR_READ "Error: Cannot read file"
#define ERR_FUNC "Error: Algorithm is not applicable!"
typedef enum _io_status
{
SUCCESS,
ERROR_OPEN,
ERROR_READ
} io_status;
#endif

View file

@ -3,18 +3,16 @@
#include <math.h>
#include "init_f.h"
#include "status.h"
#include "solve.h"
/* ./a.out a b eps M k */
int main(int argc, char *argv[])
{
double t, a, b, eps, x = 0;
int m, k, cl, it = 0, task = 1;
status ret;
int m, k, cl, it, task = 1;
double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if (
@ -27,41 +25,25 @@ int main(int argc, char *argv[])
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return 1;
return -1;
}
f = f_lst[k];
t = clock();
ret = t1_solve(f, a, b, eps, m, &x, &it);
it = t1_solve(f, a, b, eps, m, &x);
t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count();
// printf("x = %e\tf(x) = %e\n", x, f(x));
do {
switch (ret)
{
case SUCCESS:
continue;
case RUN_TIME:
fprintf(stderr, "Error: with code %d - Not enough iterations!\n", ret);
break;
case MORE_ONE_ROOT:
fprintf(stderr, "Error: with code %d - The same signs on the boundaries of the segment!\n", ret);
break;
case HIGH_ERROR:
fprintf(stderr, "Error: with code %d - The solution was found with a high error rate!\n", ret);
break;
}
if (it < 0)
{
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return ret;
} while (0);
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
return 0;
return -2;
} else
{
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
return 0;
}
}

View file

@ -1,5 +1,4 @@
#include "solve.h"
#include "status.h"
#include <math.h>
#include <float.h>
@ -7,14 +6,13 @@
#include <stdint.h>
#include <string.h>
status t1_solve (
int t1_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x, int *m_it
double eps, int m, double *x
) {
int it = 0;
uint64_t bits;
status ret = SUCCESS;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
bool sgn_a, sgn_b, sgn_c;
@ -26,20 +24,20 @@ status t1_solve (
if (fabs(y_a) - eps < DBL_EPSILON)
{
*x = a;
return SUCCESS;
return 1;
} if (fabs(y_b) - eps < DBL_EPSILON)
{
*x = b;
return SUCCESS;
return 1;
}
if (sgn_a == sgn_b)
{
*x = DBL_MAX;
return MORE_ONE_ROOT;
return -1;
}
for (it = 0; it < m; ++it)
for (it = 1; it <= m; ++it)
{
c = (a + b) * 0.5;
y = f(c);
@ -48,14 +46,10 @@ status t1_solve (
sgn_c = (bits >> 63) & 1;
if (fabs(y) - eps < DBL_EPSILON)
{
ret = SUCCESS;
break;
} else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON))
{
ret = HIGH_ERROR;
break;
} else if (sgn_c == sgn_a)
else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON))
it = m+1;
else if (sgn_c == sgn_a)
{
a = c;
y_a = y;
@ -66,12 +60,11 @@ status t1_solve (
}
}
if (it >= m)
ret = RUN_TIME;
if (it > m)
it = -1;
*x = c;
*m_it = it;
return ret;
return it;
}

View file

@ -1,12 +1,10 @@
#ifndef SOLVE_H
#define SOLVE_H
#include "status.h"
status t1_solve (
int t1_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x, int *m_it
double eps, int m, double *x
);
#endif

View file

@ -1,11 +0,0 @@
#ifndef STATUS_H
#define STATUS_H
typedef enum _status {
SUCCESS,
MORE_ONE_ROOT,
RUN_TIME,
HIGH_ERROR,
} status;
#endif

View file

@ -1,16 +0,0 @@
#ifndef IO_STATUS_H
#define IO_STATUS_H
#define ERR_MEM "Error: Not enough memory!"
#define ERR_OPEN "Error: Cannot open file"
#define ERR_READ "Error: Cannot read file"
#define ERR_FUNC "Error: Algorithm is not applicable!"
typedef enum _io_status
{
SUCCESS,
ERROR_OPEN,
ERROR_READ
} io_status;
#endif

View file

@ -3,18 +3,16 @@
#include <math.h>
#include "init_f.h"
#include "status.h"
#include "solve.h"
/* ./a.out a b eps M k */
int main(int argc, char *argv[])
{
double t, a, b, eps, x = 0;
int m, k, cl, it = 0, task = 3;
status ret;
int m, k, cl, it, task = 3;
double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if (
@ -27,47 +25,25 @@ int main(int argc, char *argv[])
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return 1;
return -1;
}
f = f_lst[k];
t = clock();
ret = t3_solve(f, a, b, eps, m, &x, &it);
it = t3_solve(f, a, b, eps, m, &x);
t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count();
// printf("x = %e\tf(x) = %e\n", x, f(x));
do {
switch (ret)
{
case SUCCESS:
continue;
case RUN_TIME:
fprintf(stderr, "Error: with code %d - Not enough iterations!\n", ret);
break;
case MORE_ONE_ROOT:
fprintf(stderr, "Error: with code %d - The same signs on the boundaries of the segment!\n", ret);
break;
case HIGH_ERROR:
fprintf(stderr, "Error: with code %d - The solution was found with a high error rate!\n", ret);
break;
case DIVERGE:
fprintf(stderr, "Error: with code %d - The function is diverging!\n", ret);
break;
case BOUNDARIES:
fprintf(stderr, "Error: with code %d - Goes beyond the boundaries!\n", ret);
break;
}
if (it < 0)
{
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return ret;
} while (0);
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
return 0;
return -2;
} else
{
fprintf(stdout, "%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
return 0;
}
}

View file

@ -1,5 +1,4 @@
#include "solve.h"
#include "status.h"
#include <math.h>
#include <float.h>
@ -7,14 +6,13 @@
#include <stdint.h>
#include <string.h>
status t3_solve (
int t3_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x, int *m_it
double eps, int m, double *x
) {
int it = 0;
uint64_t bits;
status ret = SUCCESS;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
bool sgn_a, sgn_b, sgn_c;
@ -26,20 +24,20 @@ status t3_solve (
if (fabs(y_a) - eps < DBL_EPSILON)
{
*x = a;
return SUCCESS;
return 1;
} if (fabs(y_b) - eps < DBL_EPSILON)
{
*x = b;
return SUCCESS;
return 1;
}
if (sgn_a == sgn_b)
{
*x = DBL_MAX;
return MORE_ONE_ROOT;
return -1;
}
for (it = 0; it < m; ++it)
for (it = 1; it <= m; ++it)
{
c = a - ((a - b) / (y_a - y_b)) * y_a;
y = f(c);
@ -48,18 +46,13 @@ status t3_solve (
sgn_c = (bits >> 63) & 1;
if ((c - a < DBL_EPSILON) || (c - b > DBL_EPSILON))
{
ret = BOUNDARIES;
it = m+1;
else if (fabs(y) - eps < DBL_EPSILON)
break;
} else if (fabs(y) - eps < DBL_EPSILON)
{
ret = SUCCESS;
break;
} else if (sgn_c == sgn_a)
else if (sgn_c == sgn_a)
{
a = c;
y_a = y;
} else if (sgn_c == sgn_b)
{
b = c;
@ -67,12 +60,11 @@ status t3_solve (
}
}
if (it >= m)
ret = RUN_TIME;
if (it > m)
it = -1;
*x = c;
*m_it = it;
return ret;
return it;
}

View file

@ -1,12 +1,10 @@
#ifndef SOLVE_H
#define SOLVE_H
#include "status.h"
status t3_solve (
int t3_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x, int *m_it
double eps, int m, double *x
);
#endif

View file

@ -1,13 +0,0 @@
#ifndef STATUS_H
#define STATUS_H
typedef enum _status {
SUCCESS,
MORE_ONE_ROOT,
RUN_TIME,
HIGH_ERROR,
DIVERGE,
BOUNDARIES,
} status;
#endif

View file

@ -1,16 +0,0 @@
#ifndef IO_STATUS_H
#define IO_STATUS_H
#define ERR_MEM "Error: Not enough memory!"
#define ERR_OPEN "Error: Cannot open file"
#define ERR_READ "Error: Cannot read file"
#define ERR_FUNC "Error: Algorithm is not applicable!"
typedef enum _io_status
{
SUCCESS,
ERROR_OPEN,
ERROR_READ
} io_status;
#endif

View file

@ -3,18 +3,16 @@
#include <math.h>
#include "init_f.h"
#include "status.h"
#include "solve.h"
/* ./a.out a b eps M k */
int main(int argc, char *argv[])
{
double t, a, b, eps, x = 0;
int m, k, cl, it = 0, task = 4;
status ret;
int m, k, cl, it, task = 4;
double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if (
@ -27,38 +25,25 @@ int main(int argc, char *argv[])
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return 1;
return -1;
}
f = f_lst[k];
t = clock();
ret = t4_solve(f, a, b, eps, m, &x, &it);
it = t4_solve(f, a, b, eps, m, &x);
t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count();
// printf("x = %e\tf(x) = %e\n", x, f(x));
do {
switch (ret)
{
case SUCCESS:
continue;
case RUN_TIME:
fprintf(stderr, "Error: with code %d - Not enough iterations!\n", ret);
break;
case EQUAL:
fprintf(stderr, "Error: with code %d - The dots degenerated into one!\n", ret);
break;
}
if (it < 0)
{
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return ret;
} while (0);
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
return 0;
return -2;
} else
{
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
return 0;
}
}

View file

@ -1,42 +1,38 @@
#include "solve.h"
#include "status.h"
#include <math.h>
#include <float.h>
status t4_solve (
int t4_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x, int *m_it
double eps, int m, double *x
) {
int it = 0;
status ret = SUCCESS;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
if (fabs(y_a) - eps < DBL_EPSILON)
{
*x = a;
return SUCCESS;
return 1;
} if (fabs(y_b) - eps < DBL_EPSILON)
{
*x = b;
return SUCCESS;
return 1;
} if (fabs(fabs(y_b) - fabs(y_a)) < DBL_EPSILON)
{
*x = a;
return EQUAL;
return -1;
}
for (it = 0; it < m; ++it)
for (it = 1; it <= m; ++it)
{
c = b - ((b - a) / (y_b - y_a)) * y_b;
y = f(c);
if (fabs(y) - eps < DBL_EPSILON)
{
ret = SUCCESS;
break;
} else if (fabs(y_a) - fabs(y_b) > DBL_EPSILON)
else if (fabs(y_a) - fabs(y_b) > DBL_EPSILON)
{
a = c;
y_a = y;
@ -45,18 +41,14 @@ status t4_solve (
b = c;
y_b = y;
} else
{
ret = EQUAL;
break;
}
it = m+1;
}
if (it >= m)
ret = RUN_TIME;
if (it > m)
it = -1;
*x = c;
*m_it = it;
return ret;
return it;
}

View file

@ -1,12 +1,10 @@
#ifndef SOLVE_H
#define SOLVE_H
#include "status.h"
status t4_solve (
int t4_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x, int *m_it
double eps, int m, double *x
);
#endif

View file

@ -1,10 +0,0 @@
#ifndef STATUS_H
#define STATUS_H
typedef enum _status {
SUCCESS,
RUN_TIME,
EQUAL,
} status;
#endif

View file

@ -1,16 +0,0 @@
#ifndef IO_STATUS_H
#define IO_STATUS_H
#define ERR_MEM "Error: Not enough memory!"
#define ERR_OPEN "Error: Cannot open file"
#define ERR_READ "Error: Cannot read file"
#define ERR_FUNC "Error: Algorithm is not applicable!"
typedef enum _io_status
{
SUCCESS,
ERROR_OPEN,
ERROR_READ
} io_status;
#endif

View file

@ -12,7 +12,7 @@ int main(int argc, char *argv[])
int m, k, cl, it, task = 7;
double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if (
@ -34,7 +34,7 @@ int main(int argc, char *argv[])
cl = get_call_count();
if (it >= m)
if (it < 0)
{
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return -2;

View file

@ -17,7 +17,7 @@ int t7_solve (
return 1;
}
for (it = 0; it < m; ++it)
for (it = 1; it <= m; ++it)
{
x_0 = y;
y = f(x_0);
@ -25,6 +25,9 @@ int t7_solve (
if (fabs(y - x_0) - eps < DBL_EPSILON)
break;
}
if (it > m)
it = -1;
*x = x_0;

View file

@ -1,16 +0,0 @@
#ifndef IO_STATUS_H
#define IO_STATUS_H
#define ERR_MEM "Error: Not enough memory!"
#define ERR_OPEN "Error: Cannot open file"
#define ERR_READ "Error: Cannot read file"
#define ERR_FUNC "Error: Algorithm is not applicable!"
typedef enum _io_status
{
SUCCESS,
ERROR_OPEN,
ERROR_READ
} io_status;
#endif

View file

@ -12,7 +12,7 @@ int main(int argc, char *argv[])
int m, k, it, cl, task = 8;
double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if (
@ -36,13 +36,13 @@ int main(int argc, char *argv[])
cl = get_call_count();
if (it >= m)
if (it < 0)
{
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return -2;
} else
{
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
fprintf(stdout, "%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
return 0;
}
}

View file

@ -10,7 +10,7 @@ int t8_solve (
double a, double b,
double eps, int m, double *x
) {
int it = 0;
int it = 1;
double x_l = 0, y_l = 0, c = a, y = f(a);
if (fabs(b - a) < DBL_EPSILON)
@ -22,7 +22,7 @@ int t8_solve (
for (double h = (b - a) * 0.1; fabs(h) > DBL_EPSILON; h *= -0.1)
{
do {
if (it >= m)
if (it > m)
break;
it++;
@ -33,8 +33,11 @@ int t8_solve (
y = f(c);
} while (((y - y_l) - eps) > DBL_EPSILON);
if (it >= m)
if (it > m)
{
it = -1;
break;
}
if ((c - b) > DBL_EPSILON)
{

View file

@ -1,10 +0,0 @@
#ifndef STATUS_H
#define STATUS_H
typedef enum _status {
SUCCESS,
RUN_TIME,
EQUAL,
} status;
#endif