Task 5 is already done

This commit is contained in:
AZEN-SGG 2025-05-07 19:58:57 +03:00
parent 7dc4921cef
commit 47a464aa9e
5 changed files with 120 additions and 55 deletions

View file

@ -20,7 +20,7 @@ else
CLEAN = rm -f
endif
TARGET = a015.$(EXE)
TARGET = a05.$(EXE)
OBJ = main.o solve.o init_f.o
%.o: %.c

View file

@ -9,10 +9,10 @@
int main(int argc, char *argv[])
{
double t, a, b, eps, x = 0;
int m, k, cl, it, task = 1;
int m, k, cl, it, task = 5;
double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; //TODO: Rem f7
int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if (
@ -31,7 +31,7 @@ int main(int argc, char *argv[])
f = f_lst[k];
t = clock();
it = t1_solve(f, a, b, eps, m, &x);
it = t5_solve(f, a, b, eps, m, &x);
t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count();

View file

@ -6,65 +6,73 @@
#include <stdint.h>
#include <string.h>
int t1_solve (
int t5_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it = 0;
uint64_t bits;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
bool sgn_a, sgn_b, sgn_c;
int it;
double c = (a + b) * 0.5;
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)
double y_a = f(a);
double y_c = f(c);
double y_b = f(b);
if (is_null(y_a))
{
*x = a;
return 1;
} if (fabs(y_b) - eps < DBL_EPSILON)
} else if (is_null(y_b))
{
*x = b;
return 1;
} else if (is_null(y_c))
{
*x = c;
return 1;
}
if (sgn_a == sgn_b)
{
*x = DBL_MAX;
if (is_equal(a, b))
return -1;
else if (is_equal(a, c))
return -1;
else if (is_equal(b, c))
return -1;
}
for (it = 1; it <= m; ++it)
{
c = (a + b) * 0.5;
y = f(c);
memcpy(&bits, &y, sizeof(bits));
sgn_c = (bits >> 63) & 1;
if (fabs(y) - eps < DBL_EPSILON)
break;
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;
} else if (sgn_c == sgn_b)
{
b = c;
y_b = y;
}
}
if (it > m)
it = -1;
*x = c;
return it;
for (it = 1; it < m+1; ++it)
{
double *temp_pnt = 0, *inner_max_pnt;
const double angle = (c - a) / (y_c - y_a);
const double x_new = a -
angle * y_a +
((((b - c) / (y_b - y_c)) - angle) / (y_b - y_a)) * y_a * y_c;
const double y_new = f(x_new);
if (is_eps(y_new, eps))
{
*x = x_new;
return it;
}
if (
is_equal(x_new, a) ||
is_equal(x_new, c) ||
is_equal(x_new, b)
)
return -1;
inner_max_pnt = fp_abs_max(&c, &b, &y_c, &y_b, &temp_pnt);
*fp_abs_max(&a, inner_max_pnt, &y_a, temp_pnt, &temp_pnt) = x_new;
*temp_pnt = y_new;
}
if (it > m)
return -2;
else
{
double temp = 0, *temp_x_pnt = fpmax(&c, &b, -y_c, -y_b, &temp);
*x = *fpmax(&a, temp_x_pnt, -y_a, temp, &temp);
return it;
}
}

View file

@ -1,7 +1,53 @@
#ifndef SOLVE_H
#define SOLVE_H
int t1_solve (
#include <math.h>
#include <float.h>
static inline double * fpmax (double *pa, double *pb, double fa, double fb, double *max_f_p)
{
if ((fa - fb) > DBL_EPSILON)
{
*max_f_p = fa;
return pa;
} else
{
*max_f_p = fb;
return pb;
}
}
static inline double * fp_abs_max (double *pa, double *pb, double *fa, double *fb, double **max_f_p)
{
if ((fabs(*fa) - fabs(*fb)) > DBL_EPSILON)
{
*max_f_p = fa;
return pa;
} else
{
*max_f_p = fb;
return pb;
}
}
static inline int is_equal (const double a, const double b)
{
double diff = a - b;
double max_val = (a > b) ? a : b;
return ((diff < 0) ? -diff : diff) < (DBL_EPSILON * max_val);
}
static inline int is_null (const double a)
{
return ((a < 0) ? -a : a) < DBL_EPSILON;
}
static inline int is_eps (const double a, const double eps)
{
return (((a < 0) ? -a : a) - eps) < DBL_EPSILON;
}
int t5_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x

View file

@ -2,25 +2,36 @@ script_name="$(basename "$0")"
iter="1000"
mkdir -p tests
if [ "$#" -ne 2 ]; then
echo "The number of parameters is less than 2"
if [ "$#" -ne 1 ]; then
echo "The number of parameters is less than 1"
exit 1
fi
make
if [ -f Makefile ]; then
echo "Компиляция..."
make clean
make
fi
echo "Тест запущен..."
for (( k = 3 ; k < 7; k++ )); do
echo "------- K = $k -------"
for (( a = -100 ; a < -40 ; a++ )); do
for (( b = -9 ; b < 10 ; b++ )); do
echo "a = $a b = $b Iter = $iter ---"
echo "./a0$1.out "$(echo "$a / 10" | bc -l)" "$(echo "$b / 10" | bc -l)" 1e-16 $iter $k"
./a0$1.out "$(echo "$a / 10" | bc -l)" "$(echo "$b / 10" | bc -l)" 1e-16 $iter $k
done
done
for (( a = -9 ; a < 10 ; a++ )); do
for (( b = 11 ; b < 100 ; b++ )); do
echo "a = $a b = $b Iter = $iter ---"
echo "./a0$1.out "$(echo "$a / 10" | bc -l)" "$(echo "$b / 10" | bc -l)" 1e-16 $iter $k"
./a0$1.out "$(echo "$a / 10" | bc -l)" "$(echo "$b / 10" | bc -l)" 1e-16 $iter $k
done
done
done >$(pwd)/tests/out_$script_name.log 2>$(pwd)/tests/err_$script_name.log
echo "Тест записан в >$(pwd)/tests/out_$script_name.log"
echo "Ошибки записаны в 2>$(pwd)/tests/err_$script_name.log"
echo "Тест завершен"