Increaced accurancy by switch from float to double

This commit is contained in:
Dmitriy Shishkov 2021-10-25 16:10:51 +03:00
parent f26825e77b
commit b7110e1bc1
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
2 changed files with 17 additions and 17 deletions

26
main.c
View File

@ -27,7 +27,7 @@ arr *init(int n)
a->size = n; a->size = n;
a->p = (float *)malloc(sizeof(float) * n); a->p = (double *)malloc(sizeof(double) * n);
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
insert(a, i, 0); insert(a, i, 0);
@ -39,7 +39,7 @@ arr *resize(arr *a, int new_size)
if (a->size == new_size) if (a->size == new_size)
return a; return a;
float *new_p = (float *)malloc(sizeof(float) * new_size); double *new_p = (double *)malloc(sizeof(double) * new_size);
for (int i = 0; i < min(new_size, a->size); i++) for (int i = 0; i < min(new_size, a->size); i++)
new_p[i] = a->p[i]; new_p[i] = a->p[i];
@ -54,7 +54,7 @@ arr *resize(arr *a, int new_size)
return a; return a;
} }
void insert(arr *a, int pos, float val) void insert(arr *a, int pos, double val)
{ {
pos = pos % a->size; pos = pos % a->size;
if (pos < 0) if (pos < 0)
@ -71,7 +71,7 @@ arr *add(arr *a, arr *b)
return a; return a;
} }
arr *mult(arr *a, float mul) arr *mult(arr *a, double mul)
{ {
arr *res = init(a->size); arr *res = init(a->size);
@ -159,7 +159,7 @@ int has_comb(int *arr, int n, int k)
int mult_by_index(arr *a, int *coords, int n) int mult_by_index(arr *a, int *coords, int n)
{ {
float res = 1; double res = 1;
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
res = res * a->p[coords[i]]; res = res * a->p[coords[i]];
@ -176,7 +176,7 @@ int sum_of_mult_of_n_combinations(arr *a, int n)
return a->p[0]; return a->p[0];
} }
float acc = 0; double acc = 0;
int coords[n]; int coords[n];
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
@ -189,9 +189,9 @@ int sum_of_mult_of_n_combinations(arr *a, int n)
return acc; return acc;
} }
int compose_denominator(arr *a, int pos) double compose_denominator(arr *a, int pos)
{ {
float res = 1; double res = 1;
for (int i = 0; i < a->size; i++) for (int i = 0; i < a->size; i++)
{ {
if (i == pos) if (i == pos)
@ -210,14 +210,14 @@ arr *compose_interpolation_polynomial(arr *xes, arr *ys)
for (int j = 0; j < xes->size; j++) for (int j = 0; j < xes->size; j++)
{ {
int minus = !(xes->size % 2); int minus = !(xes->size % 2);
float denominator = compose_denominator(xes, j); double denominator = compose_denominator(xes, j);
float multiplicator = ys->p[j]; double multiplicator = ys->p[j];
arr *xis = arr_without_el(xes, j); arr *xis = arr_without_el(xes, j);
for (int i = 0; i < xes->size; i++) for (int i = 0; i < xes->size; i++)
{ {
float k_sum = sum_of_mult_of_n_combinations(xis, xes->size - 1 - i); double k_sum = sum_of_mult_of_n_combinations(xis, xes->size - 1 - i);
insert(jcoef, i, (minus ? -1 : 1) * (multiplicator * k_sum) / denominator); insert(jcoef, i, (minus ? -1 : 1) * (multiplicator * k_sum) / denominator);
minus = !minus; minus = !minus;
} }
@ -245,8 +245,8 @@ int main()
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
float x, y; double x, y;
scanf("%f %f", &x, &y); scanf("%lf %lf", &x, &y);
insert(xes, i, x); insert(xes, i, x);
insert(ys, i, y); insert(ys, i, y);

View File

@ -15,14 +15,14 @@ int max(int a, int b);
typedef struct typedef struct
{ {
int size; int size;
float *p; double *p;
} arr; } arr;
arr *init(int n); arr *init(int n);
arr *resize(arr *a, int new_size); arr *resize(arr *a, int new_size);
void insert(arr *a, int pos, float val); void insert(arr *a, int pos, double val);
arr *add(arr *a, arr *b); arr *add(arr *a, arr *b);
arr *mult(arr *a, float mul); arr *mult(arr *a, double mul);
void printa(arr *a); void printa(arr *a);
arr *arr_without_el(arr *a, int ex_pos); arr *arr_without_el(arr *a, int ex_pos);
arr *reverse(arr *a); arr *reverse(arr *a);
@ -34,7 +34,7 @@ arr *reverse(arr *a);
int has_comb(int *arr, int n, int k); int has_comb(int *arr, int n, int k);
int mult_by_index(arr *a, int *coords, int n); int mult_by_index(arr *a, int *coords, int n);
int sum_of_mult_of_n_combinations(arr *a, int n); int sum_of_mult_of_n_combinations(arr *a, int n);
int compose_denominator(arr *a, int pos); double compose_denominator(arr *a, int pos);
arr *compose_interpolation_polynomial(arr *xes, arr *ys); arr *compose_interpolation_polynomial(arr *xes, arr *ys);
#endif #endif