mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-25 23:09:36 +03:00
better termination check
This commit is contained in:
parent
c27e045790
commit
7a7858a7fb
@ -38,10 +38,13 @@ const char *complex_str(long double complex x)
|
|||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
double get_rand(double lim)
|
char check_termination(long double delta)
|
||||||
{
|
{
|
||||||
const double max = fabs(lim), min = -max;
|
static long double past_delta = INFINITY;
|
||||||
return (double)rand() / (double)RAND_MAX * (max - min + 1.0);
|
if (fabsl(past_delta - delta) <= ACCURACY || delta < ACCURACY)
|
||||||
|
return 1;
|
||||||
|
past_delta = delta;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
@ -130,7 +133,7 @@ int main(int argc, char **argv)
|
|||||||
double tol_condition = 1;
|
double tol_condition = 1;
|
||||||
unsigned long iter = 0;
|
unsigned long iter = 0;
|
||||||
|
|
||||||
while (tol_condition > ACCURACY && iter < INT_MAX)
|
while (!check_termination(tol_condition) && iter < INT_MAX)
|
||||||
{
|
{
|
||||||
long double complex delta = 0;
|
long double complex delta = 0;
|
||||||
tol_condition = 0;
|
tol_condition = 0;
|
||||||
@ -152,19 +155,19 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
if (isnan(cabsl(delta)) || isinf(cabsl(delta)))
|
if (isnan(cabsl(delta)) || isinf(cabsl(delta)))
|
||||||
{
|
{
|
||||||
printf("Overflow/underrun error - got value = %Lg\n", cabsl(delta));
|
printf("\n\nOverflow/underrun error - got value = %Lg", cabsl(delta));
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
s0[n] -= delta;
|
s0[n] -= delta;
|
||||||
|
|
||||||
tol_condition += fabsl(cabsl(delta));
|
tol_condition = fmaxl(tol_condition, fabsl(cabsl(delta)));
|
||||||
|
|
||||||
#if defined(DEBUG) || !defined(NDEBUG)
|
#if defined(DEBUG) || !defined(NDEBUG)
|
||||||
fprintf(log_file, "%s,", complex_str(s0[n]));
|
fprintf(log_file, "%s,", complex_str(s0[n]));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
tol_condition /= (degree - 1);
|
// tol_condition /= (degree - 1);
|
||||||
|
|
||||||
if (iter % 500 == 0)
|
if (iter % 500 == 0)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user