1
0

move formula to common file

This commit is contained in:
Rokas Puzonas 2023-06-09 16:44:36 +03:00
parent b935bfc94f
commit 3726cb5ad0
3 changed files with 37 additions and 60 deletions

View File

@ -7,7 +7,8 @@
#include <errno.h>
#include <stdbool.h>
#define EARTH_RADIUS 6372.8
#include "harvensine_formula.c"
#define ARRAY_LEN(x) (sizeof(x)/sizeof(x[0]))
#define strequal(a, b) !strcmp(a, b)
@ -54,34 +55,6 @@ static void write_json_f64(char *key, f64 value, FILE *stream) {
fwrite(buffer, sizeof(char), size, stream);
}
static f64 sqaure_f64(f64 num)
{
return num*num;
}
static f64 radians_to_degrees(f64 degrees)
{
return 0.01745329251994329577 * degrees;
}
static f64 get_harvensine_distance(f64 X0, f64 Y0, f64 X1, f64 Y1, f64 earth_radius)
{
f64 lat1 = Y0;
f64 lat2 = Y1;
f64 lon1 = X0;
f64 lon2 = X1;
f64 dLat = radians_to_degrees(lat2 - lat1);
f64 dLon = radians_to_degrees(lon2 - lon1);
lat1 = radians_to_degrees(lat1);
lat2 = radians_to_degrees(lat2);
f64 a = sqaure_f64(sin(dLat/2.0)) + cos(lat1)*cos(lat2)*sqaure_f64(sin(dLon/2));
f64 c = 2.0*asin(sqrt(a));
return earth_radius * c;
}
static size_t find_filename_ext(const char *filename, size_t length) {
for (size_t i = length-1; i >= 0; i--) {
if (filename[i] == '.') {

33
src/harvensine_formula.c Normal file
View File

@ -0,0 +1,33 @@
#include <math.h>
typedef double f64;
#define EARTH_RADIUS 6372.8
static f64 sqaure_f64(f64 num)
{
return num*num;
}
static f64 radians_to_degrees(f64 degrees)
{
return 0.01745329251994329577 * degrees;
}
static f64 get_harvensine_distance(f64 X0, f64 Y0, f64 X1, f64 Y1, f64 earth_radius)
{
f64 lat1 = Y0;
f64 lat2 = Y1;
f64 lon1 = X0;
f64 lon2 = X1;
f64 dLat = radians_to_degrees(lat2 - lat1);
f64 dLon = radians_to_degrees(lon2 - lon1);
lat1 = radians_to_degrees(lat1);
lat2 = radians_to_degrees(lat2);
f64 a = sqaure_f64(sin(dLat/2.0)) + cos(lat1)*cos(lat2)*sqaure_f64(sin(dLon/2));
f64 c = 2.0*asin(sqrt(a));
return earth_radius * c;
}

View File

@ -7,8 +7,7 @@
#include <math.h>
#include "json_parser.c"
#define EARTH_RADIUS 6372.8
#include "harvensine_formula.c"
typedef uint32_t u32;
typedef uint64_t u64;
@ -45,34 +44,6 @@ u64 get_current_time_us()
return time.tv_sec * 1000000 + time.tv_nsec / 1000;
}
static f64 sqaure_f64(f64 num)
{
return num*num;
}
static f64 radians_to_degrees(f64 degrees)
{
return 0.01745329251994329577 * degrees;
}
static f64 get_harvensine_distance(f64 X0, f64 Y0, f64 X1, f64 Y1, f64 earth_radius)
{
f64 lat1 = Y0;
f64 lat2 = Y1;
f64 lon1 = X0;
f64 lon2 = X1;
f64 dLat = radians_to_degrees(lat2 - lat1);
f64 dLon = radians_to_degrees(lon2 - lon1);
lat1 = radians_to_degrees(lat1);
lat2 = radians_to_degrees(lat2);
f64 a = sqaure_f64(sin(dLat/2.0)) + cos(lat1)*cos(lat2)*sqaure_f64(sin(dLon/2));
f64 c = 2.0*asin(sqrt(a));
return earth_radius * c;
}
static struct point_pairs *convert_to_struct(struct json_value *json)
{
assert(json->type == JSON_TYPE_OBJECT);
@ -216,7 +187,7 @@ int main(int argc, char **argv)
}
printf("Time to calc harvensine: %ldus (%.3lfus/row)\n", harvensine_duration, (f64)harvensine_duration / row_count);
printf("Sum of all harvensines: %.16f\n", harvensine_sum);
printf("Sum of all harvensines: %f\n", harvensine_sum);
if (reference_harvensines_count > 0) {
printf("Diff of reference harvensine sum: %.16f\n", harvensine_sum - reference_harvensine_sum);