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;
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]);
if (
@ -37,6 +37,8 @@ int main(int argc, char *argv[])
cl = get_call_count();
// printf("x = %e\tf(x) = %e\n", x, f(x));
do {
switch (ret)
{

View file

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