-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.c
55 lines (48 loc) · 1.3 KB
/
base.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <math.h>
#include "base.h"
/*
* Random number utilities
*/
void randomize () {
unsigned int seed = 0;
FILE *devrnd = fopen("/dev/urandom","r"); CHECK(devrnd);
fread(&seed, 4, 1, devrnd);
CHECK(!fclose(devrnd));
srand(seed);
}
real rand_range (real lower, real upper) {
return (upper - lower) * (real)rand() / (real)RAND_MAX + lower;
}
/*
* Point utilities
*/
point *get_point (int n) {
point *p = malloc(sizeof (point)); CHECK(p);
p->x = malloc((size_t)n * sizeof (real)); CHECK(p->x);
return p;
}
void set_random_coordinates (point *p, int n, real lower, real upper) {
for (int j = 0; j < n; j++) {
p->x[j] = rand_range(lower, upper);
}
}
void copy_point (int n, const point *src, point *dst) {
for (int i = 0; i < n; i++) {
dst->x[i] = src->x[i];
}
dst->f = src->f;
}
real distance (int n, const point *a, const point *b) {
real sum = 0.0L;
for (int j = 0; j < n; j++) {
sum += SQR(a->x[j] - b->x[j]);
}
return sqrtl(sum);
}
void print_result (int n, const point *p, int places, int fmt) {
fprintf(stderr, "%s[%s ", GRY, NRM);
for (int i = 0; i < n; i++) {
fprintf(stderr, fmt ? "% .*Le " : "% .*Lf ", places, p->x[i]);
}
fprintf(stderr, "%s]%s % .*Le\n", GRY, NRM, places, p->f);
}