testing task 1
This commit is contained in:
parent
8e114b5c29
commit
c2952fa8a1
2 changed files with 20 additions and 6 deletions
|
@ -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 (
|
||||
|
@ -34,9 +34,11 @@ int main(int argc, char *argv[])
|
|||
t = clock();
|
||||
ret = t1_solve(f, a, b, eps, m, &x, &it);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
|
||||
cl = get_call_count();
|
||||
|
||||
// printf("x = %e\tf(x) = %e\n", x, f(x));
|
||||
|
||||
do {
|
||||
switch (ret)
|
||||
{
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue