testing task 1

This commit is contained in:
AZEN-SGG 2025-04-30 13:07:07 +03:00
parent 8e114b5c29
commit c2952fa8a1
2 changed files with 20 additions and 6 deletions

View file

@ -14,7 +14,7 @@ int main(int argc, char *argv[])
status ret; status ret;
double (*f) (double); double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin
int len_f = sizeof(f_lst) / sizeof(f_lst[0]); int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if ( if (
@ -37,6 +37,8 @@ int main(int argc, char *argv[])
cl = get_call_count(); cl = get_call_count();
// printf("x = %e\tf(x) = %e\n", x, f(x));
do { do {
switch (ret) switch (ret)
{ {

View file

@ -3,6 +3,9 @@
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
status t1_solve ( status t1_solve (
double (*f) (double), double (*f) (double),
@ -10,11 +13,17 @@ status t1_solve (
double eps, int m, double *x, int *m_it double eps, int m, double *x, int *m_it
) { ) {
int it = 0; int it = 0;
uint64_t bits;
status ret = SUCCESS; status ret = SUCCESS;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b); 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 (y_a * y_b >= -DBL_EPSILON) if (sgn_a == sgn_b)
{ {
*x = DBL_MAX; *x = DBL_MAX;
return MORE_ONE_ROOT; return MORE_ONE_ROOT;
@ -25,7 +34,10 @@ status t1_solve (
c = (a + b) * 0.5; c = (a + b) * 0.5;
y = f(c); y = f(c);
if (fabs(y) < eps) memcpy(&bits, &y, sizeof(bits));
sgn_c = (bits >> 63) & 1;
if (fabs(y) - eps < DBL_EPSILON)
{ {
ret = SUCCESS; ret = SUCCESS;
break; break;
@ -33,11 +45,11 @@ status t1_solve (
{ {
ret = HIGH_ERROR; ret = HIGH_ERROR;
break; break;
} else if (y * y_a > DBL_EPSILON) } else if (sgn_c == sgn_a)
{ {
a = c; a = c;
y_a = y; y_a = y;
} else if (y * y_b > DBL_EPSILON) } else if (sgn_c == sgn_b)
{ {
b = c; b = c;
y_b = y; y_b = y;