diff --git a/Makefile b/Makefile index e527b21..8d7c398 100644 --- a/Makefile +++ b/Makefile @@ -44,6 +44,7 @@ SRCS = main.c \ reducer.c \ solver.c \ errors.c \ + math.c \ printer_equation.c \ printer_solutions.c \ print_enums.c \ diff --git a/README.md b/README.md index 9895bbd..98a3908 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ ## todo -- if no "=" sign return error - double is nearly_equal_0 diff --git a/headers/computorv1.h b/headers/computorv1.h index 1edd8bf..99b5f70 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -2,13 +2,17 @@ #define COMPUTORV1_H #include "libft.h" -#include // tmp for printf, for float debug +#include // for printf #include // for va_list #include #include // for errno #include // for strerror -#include // tmp +/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * MACROS + */ + +#define DOUBLE_PRECISION 0.00001 /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * MAIN.C @@ -157,6 +161,12 @@ typedef struct void solve(const s_polynom *polynom, s_solution *solution); +/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * UTILS/MATH.C + */ + +bool is_nearly_equal_zero(double num); + /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * UTILS/ERRORS.C */ diff --git a/libft b/libft index 85423f4..e95f55c 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 85423f4211a4fb7144ddbc463618ee20ea5e7812 +Subproject commit e95f55c07f6a1e1b95dde698c4d21c6ac22ee9a9 diff --git a/src/printer_equation.c b/src/printer_equation.c index 555339a..b21609e 100644 --- a/src/printer_equation.c +++ b/src/printer_equation.c @@ -29,7 +29,7 @@ static void print_reduced_form_beautify(s_polynom *polynom) exponent = polynom[i].exponent; // if term is zero dont output - if (coefficient == 0.0) + if (is_nearly_equal_zero(coefficient)) { i++; continue; diff --git a/src/printer_solutions.c b/src/printer_solutions.c index dbdb26b..50d223b 100644 --- a/src/printer_solutions.c +++ b/src/printer_solutions.c @@ -28,12 +28,12 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution) if (solution.all_int) { // solution 1 - if (solution.b != 0.0) + if (!is_nearly_equal_zero(solution.b)) printf("%g/%g + ", solution.b * -1, solution.a * 2); printf("%gi/%g\n", solution.delta_sqrt, solution.a * 2); // solution 2 - if (solution.b != 0.0) + if (!is_nearly_equal_zero(solution.b)) printf("%g/%g - ", solution.b * -1, solution.a * 2); else printf("-"); @@ -42,13 +42,13 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution) else { // solution 1 - if (solution.left_term != 0.0) + if (!is_nearly_equal_zero(solution.left_term)) printf("%g + ", solution.left_term); - if (solution.right_term != 0.0) + if (!is_nearly_equal_zero(solution.right_term)) printf("i * %g\n", solution.right_term); // solution 2 - if (solution.left_term != 0.0) + if (!is_nearly_equal_zero(solution.left_term)) printf("%g - ", solution.left_term); else printf("-"); diff --git a/src/reducer.c b/src/reducer.c index caaefc6..08425ba 100644 --- a/src/reducer.c +++ b/src/reducer.c @@ -45,7 +45,7 @@ int reduce(s_term *terms, s_polynom *polynom, int max_exponent) tmp_coefficient = get_all_terms_with_exponent(terms, max_exponent); // skip if coefficient is null or no exponent - if (tmp_coefficient == 0.0) + if (is_nearly_equal_zero(tmp_coefficient)) { max_exponent--; continue; diff --git a/src/solver.c b/src/solver.c index 5f7c39b..9111745 100644 --- a/src/solver.c +++ b/src/solver.c @@ -32,20 +32,20 @@ static double positiv_zero(double num) return num; } -int has_decimal_part(double num) +static int has_decimal_part(double num) { return (num != (int)num); } -int any_has_decimal_part(double *num, size_t len) +static bool any_has_decimal_part(double *num, size_t len) { while (len > 0) { if (has_decimal_part(num[len - 1])) - return 1; + return true; len--; } - return 0; + return false; } // find GCD of two integers using euclidean algorithm @@ -81,19 +81,19 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do solution->c = c; delta = b * b - 4 * a * c; - solution->delta = delta; // Δ == b² - 4ac - solution->delta_sign = ft_fsign(delta); // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO - solution->delta_absolute = ft_fabs(delta); // |Δ| - solution->delta_sqrt = ft_sqrt(solution->delta_absolute); // √|Δ| + solution->delta = delta; // Δ == b² - 4ac + solution->delta_sign = ft_fsign(delta); // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO + solution->delta_absolute = ft_fabs(delta); // |Δ| + solution->delta_sqrt = ft_sqrt(solution->delta_absolute, DOUBLE_PRECISION); // √|Δ| delta_sqrt = solution->delta_sqrt; /** * SOLVE LEFT AND RIGHT TERMS AS DOUBLES */ - if (b != 0.0) + if (!is_nearly_equal_zero(b)) solution->left_term = positiv_zero(-b / (2 * a)); // -b / 2a - if (delta != 0.0) + if (!is_nearly_equal_zero(delta)) solution->right_term = positiv_zero(delta_sqrt / 2 * a); // √|Δ| / 2a /** @@ -113,7 +113,7 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do solution->right_term_denominator = (a * 2) / gcd; // left term - if (b == 0.0) + if (is_nearly_equal_zero(b)) return; gcd = gcd_int((int)b, (int)(a * 2)); solution->left_term_numerator = -b / gcd; diff --git a/src/utils/math.c b/src/utils/math.c new file mode 100644 index 0000000..b7bcab8 --- /dev/null +++ b/src/utils/math.c @@ -0,0 +1,12 @@ +/* printer.c */ + +#include "computorv1.h" + +bool is_nearly_equal_zero(double num) +{ + if (num > DOUBLE_PRECISION) + return false; + if (num < -DOUBLE_PRECISION) + return false; + return true; +}