Task 4 is done!
This commit is contained in:
parent
62add41f64
commit
22ef2dda9b
18 changed files with 332 additions and 48 deletions
|
@ -20,7 +20,7 @@ else
|
|||
CLEAN = rm -f
|
||||
endif
|
||||
|
||||
TARGET = a01.$(EXE)
|
||||
TARGET = a04.$(EXE)
|
||||
OBJ = main.o solve.o init_f.o
|
||||
|
||||
%.o: %.c
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
double t, a, b, eps, x = 0;
|
||||
int m, k, cl, it = 0, task = 1;
|
||||
int m, k, cl, it = 0, task = 4;
|
||||
status ret;
|
||||
|
||||
double (*f) (double);
|
||||
|
@ -22,7 +22,7 @@ int main(int argc, char *argv[])
|
|||
sscanf(argv[1], "%lf", &a) == 1 &&
|
||||
sscanf(argv[2], "%lf", &b) == 1 &&
|
||||
(a <= b) &&
|
||||
sscanf(argv[3], "%lf", &eps) == 1 &&
|
||||
(sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) &&
|
||||
((sscanf(argv[4], "%d", &m) == 1) && m > 0) &&
|
||||
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
|
||||
) {
|
||||
|
@ -33,7 +33,7 @@ int main(int argc, char *argv[])
|
|||
f = f_lst[k];
|
||||
|
||||
t = clock();
|
||||
ret = t1_solve(f, a, b, eps, m, &x, &it);
|
||||
ret = t4_solve(f, a, b, eps, m, &x, &it);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
cl = get_call_count();
|
||||
|
@ -48,11 +48,8 @@ int main(int argc, char *argv[])
|
|||
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);
|
||||
case EQUAL:
|
||||
fprintf(stderr, "Error: with code %d - The dots degenerated into one!\n", ret);
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,25 +3,15 @@
|
|||
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
status t1_solve (
|
||||
status t4_solve (
|
||||
double (*f) (double),
|
||||
double a, double b,
|
||||
double eps, int m, double *x, int *m_it
|
||||
) {
|
||||
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;
|
||||
|
||||
memcpy(&bits, &y_a, sizeof(bits));
|
||||
sgn_a = (bits >> 63) & 1;
|
||||
memcpy(&bits, &y_b, sizeof(bits));
|
||||
sgn_b = (bits >> 63) & 1;
|
||||
|
||||
if (fabs(y_a) - eps < DBL_EPSILON)
|
||||
{
|
||||
|
@ -31,38 +21,33 @@ status t1_solve (
|
|||
{
|
||||
*x = b;
|
||||
return SUCCESS;
|
||||
} if (fabs(fabs(y_b) - fabs(y_a)) < DBL_EPSILON)
|
||||
{
|
||||
*x = a;
|
||||
return EQUAL;
|
||||
}
|
||||
|
||||
if (sgn_a == sgn_b)
|
||||
{
|
||||
*x = DBL_MAX;
|
||||
return MORE_ONE_ROOT;
|
||||
}
|
||||
|
||||
for (it = 0; it < m; ++it)
|
||||
{
|
||||
c = (a + b) * 0.5;
|
||||
c = b - ((b - a) / (y_b - y_a)) * y_b;
|
||||
y = f(c);
|
||||
|
||||
memcpy(&bits, &y, sizeof(bits));
|
||||
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(y_a) - fabs(y_b) > DBL_EPSILON)
|
||||
{
|
||||
a = c;
|
||||
y_a = y;
|
||||
} else if (sgn_c == sgn_b)
|
||||
} else if (fabs(y_b) - fabs(y_a) > DBL_EPSILON)
|
||||
{
|
||||
b = c;
|
||||
y_b = y;
|
||||
} else
|
||||
{
|
||||
ret = EQUAL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include "status.h"
|
||||
|
||||
status t1_solve (
|
||||
status t4_solve (
|
||||
double (*f) (double),
|
||||
double a, double b,
|
||||
double eps, int m, double *x, int *m_it
|
||||
|
|
|
@ -3,9 +3,8 @@
|
|||
|
||||
typedef enum _status {
|
||||
SUCCESS,
|
||||
MORE_ONE_ROOT,
|
||||
RUN_TIME,
|
||||
HIGH_ERROR,
|
||||
EQUAL,
|
||||
} status;
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue