From 3726cb5ad0276b628fb94256cb6e4eece2e99c1b Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Fri, 9 Jun 2023 16:44:36 +0300 Subject: [PATCH] move formula to common file --- src/gen_data.c | 31 ++----------------------------- src/harvensine_formula.c | 33 +++++++++++++++++++++++++++++++++ src/main.c | 33 ++------------------------------- 3 files changed, 37 insertions(+), 60 deletions(-) create mode 100644 src/harvensine_formula.c diff --git a/src/gen_data.c b/src/gen_data.c index ff83de4..7755e40 100644 --- a/src/gen_data.c +++ b/src/gen_data.c @@ -7,7 +7,8 @@ #include #include -#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] == '.') { diff --git a/src/harvensine_formula.c b/src/harvensine_formula.c new file mode 100644 index 0000000..c632653 --- /dev/null +++ b/src/harvensine_formula.c @@ -0,0 +1,33 @@ +#include + +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; +} diff --git a/src/main.c b/src/main.c index 2947831..f98c966 100644 --- a/src/main.c +++ b/src/main.c @@ -7,8 +7,7 @@ #include #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);