Try to correct 3th task

This commit is contained in:
AZEN-SGG 2025-05-08 16:36:02 +03:00
parent 0f13b05344
commit 031a771f7e
7 changed files with 194 additions and 49 deletions

50
2025.05.02/03Ex/comp.h Normal file
View file

@ -0,0 +1,50 @@
#ifndef COMP_H
#define COMP_H
#include <float.h>
#include <math.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;
}
#endif

View file

@ -26,36 +26,41 @@ double f1 (double x)
double f2 (double x)
{
double x_2 = x * x;
cl++;
return 4 - x * x;
return 4 - x_2;
}
double f3 (double x)
{
double x_2 = x * x;
double x_3 = x * x_2;
cl++;
return x * x_2 + 3 * x_2 + 16;
return x_3 + 3 * x_2 + 16;
}
double f4 (double x)
{
double x_2 = x * x;
double x_4 = x_2 * x_2;
cl++;
return 3 - 2 * x_2 - x_2 * x_2;
return 3 - 2 * x_2 - x_4;
}
double f5 (double x)
{
double sq_x = sqrt(fabs(x) + 1);
cl++;
return sqrt(fabs(x) + 1) - 2;
return sq_x - 2;
}
double f6 (double x)
{
double sq_x = sqrt(fabs(x) + 1);
double sq = sqrt(sq_x + 1);
cl++;
return sqrt(sqrt(fabs(x) + 1) + 1) - 2;
return sq - 2;
}

View file

@ -1,4 +1,5 @@
#include "solve.h"
#include "comp.h"
#include <math.h>
#include <float.h>
@ -21,11 +22,11 @@ int t3_solve (
memcpy(&bits, &y_b, sizeof(bits));
sgn_b = (bits >> 63) & 1;
if (fabs(y_a) - eps < DBL_EPSILON)
if (is_eps(y_a, eps))
{
*x = a;
return 1;
} if (fabs(y_b) - eps < DBL_EPSILON)
} else if (is_eps(y_b, eps))
{
*x = b;
return 1;
@ -39,15 +40,15 @@ int t3_solve (
for (it = 1; it <= m; ++it)
{
c = a - ((a - b) / (y_a - y_b)) * y_a;
c = a - ((b - a) / (y_b - y_a)) * y_a;
y = f(c);
memcpy(&bits, &y, sizeof(bits));
sgn_c = (bits >> 63) & 1;
if ((c - a < DBL_EPSILON) || (c - b > DBL_EPSILON))
if (is_equal(a, c) || is_equal(c, b))
it = m+1;
else if (fabs(y) - eps < DBL_EPSILON)
else if (is_eps(y, eps))
break;
else if (sgn_c == sgn_a)
{

View file

@ -11,7 +11,7 @@ if [ -f Makefile ]; then
make
fi
if [ -f a0$prog.out ]; then
if [ ! -f a0$prog.out ]; then
echo "Отсутствует исполняемый файл... [a0$prog.out]"
echo "Завершение..."
exit 1

51
2025.05.02/tests/main_test.sh Executable file
View file

@ -0,0 +1,51 @@
script_name="$(basename "$0")"
iter="1000"
if [ "$#" -ne 1 ]; then
echo "Не указан как параметр номер программы"
exit 1
fi
prog=$1
mkdir -p tests
if [ -f Makefile ]; then
echo "Компиляция..."
make clean
make
fi
if [ ! -f a0$prog.out ]; then
echo "Отсутствует исполняемый файл... [a0$prog.out]"
echo "Завершение..."
exit 2
fi
outlog="$(pwd)/tests/out_a0${prog}_$script_name.log"
errlog="$(pwd)/tests/err_a0${prog}_$script_name.log"
rm -f $outlog $errlog
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 "./a0$prog.out "$(echo "$a / 10" | bc -l)" "$(echo "$b / 10" | bc -l)" 1e-16 $iter $k"
./a0$prog.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 "./a0$prog.out "$(echo "$a / 10" | bc -l)" "$(echo "$b / 10" | bc -l)" 1e-16 $iter $k"
./a0$prog.out "$(echo "$a / 10" | bc -l)" "$(echo "$b / 10" | bc -l)" 1e-16 $iter $k
done
done
done >$outlog 2>$errlog
echo "Тест записан в $outlog"
echo "Ошибки записаны в $errlog"
echo "Тест завершен"

View file

@ -1,37 +0,0 @@
script_name="$(basename "$0")"
iter="1000"
mkdir -p tests
if [ "$#" -ne 1 ]; then
echo "The number of parameters is less than 1"
exit 1
fi
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 "./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 "./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 "Тест завершен"

75
scripts/sigdiff.sh Executable file
View file

@ -0,0 +1,75 @@
#!/usr/bin/env bash
#
# usage: ./sigdiff_color.sh old.txt new.txt [tolerance] [ignore_regex]
#
# tolerance абсолютный порог для чисел (по умолчанию 1e-9)
# ignore_regex токены, которые сравнивать не нужно
# (по умолчанию "^(Its=|Count=)")
#
# вывод: пары «существенно разных» строк с цветовой подсветкой
#
set -euo pipefail
if [[ $# -lt 2 ]]; then
echo "usage: $0 old_output new_output [tolerance] [ignore_regex]" >&2
exit 1
fi
F1="$1"
F2="$2"
TOL="${3:-1e-9}"
IGN_RE="${4:-^(Its=|Count=)}" # изменить, если нужно другие поля игнорировать
# ANSI-коды цветов
RED=$'\033[31m'
GRN=$'\033[32m'
RST=$'\033[0m'
awk -v f2="$F2" -v tol="$TOL" -v ign="$IGN_RE" -v red="$RED" -v grn="$GRN" -v rst="$RST" '
function abs(x){ return (x<0)?-x:x }
function isnum(s){ return (s ~ /^[+-]?[0-9]+([.][0-9]+)?([eE][+-]?[0-9]+)?$/) }
{
getline line2 < f2
split($0, a, " ")
split(line2, b, " ")
maxlen = (length(a) > length(b) ? length(a) : length(b))
significant = 0
# подготовим раскрашенные копии
for (i=1;i<=maxlen;++i){
tokenA = (i in a ? a[i] : "<MISSING>")
tokenB = (i in b ? b[i] : "<MISSING>")
if (tokenA == tokenB) { cA[i]=tokenA; cB[i]=tokenB; continue }
if (ign != "" && (tokenA ~ ign || tokenB ~ ign)) { cA[i]=tokenA; cB[i]=tokenB; continue }
if (isnum(tokenA) && isnum(tokenB) && abs(tokenA-tokenB) <= tol) {
cA[i]=tokenA; cB[i]=tokenB; continue
}
# токены действительно различаются
significant = 1
cA[i]=red tokenA rst
cB[i]=grn tokenB rst
}
if (significant) {
# склеиваем обратно
outA=""; outB=""
for (i=1;i<=maxlen;++i){ outA=outA (i in cA?cA[i]:"") (i<maxlen?" ":"") }
for (i=1;i<=maxlen;++i){ outB=outB (i in cB?cB[i]:"") (i<maxlen?" ":"") }
printf "<<< %s\n>>> %s\n\n", outA, outB
}
}
END {
if ((getline rest < f2) > 0) {
print "⚠️ второй файл длиннее — есть необработанные строки." > "/dev/stderr"
}
}
' "$F1"