From 2824414f5342bc87eccc82adeee3b4060f1911d1 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Fri, 15 May 2026 02:45:55 +0200 Subject: [PATCH] add positiv_zero in output --- headers/computorv1.h | 1 + src/printer_solutions.c | 14 +++++++------- src/solver.c | 7 ------- src/utils/math.c | 7 +++++++ tester.sh | 14 +++++++------- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/headers/computorv1.h b/headers/computorv1.h index 31811ad..0c42bed 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -206,6 +206,7 @@ void print_solution(s_solution *solution); bool is_nearly_equal(double num, int compare); bool is_nearly_equal_zero(double num); const s_polynom *get_term_of_power(const s_polynom *polynom, int power); +double positiv_zero(double num); bool has_decimal_part(double num); bool any_has_decimal_part(double *num, size_t len); int gcd_int(int a, int b); diff --git a/src/printer_solutions.c b/src/printer_solutions.c index 2588923..5bc1755 100644 --- a/src/printer_solutions.c +++ b/src/printer_solutions.c @@ -129,14 +129,14 @@ static void print_solution_pure_radicand_negativ(s_solution_degree_2_pure soluti static void print_solution_delta_zero(s_solution_degree_2 solution) { ft_printf("Discriminant is equal to zero, the solution is:\n"); - printf("%g\n", solution.left_term); + printf("%g\n", positiv_zero(solution.left_term)); } static void print_solution_delta_positiv(s_solution_degree_2 solution) { ft_printf("Discriminant is strictly positive, the two solutions are:\n"); - printf("%g\n", solution.left_term + solution.right_term); - printf("%g\n", solution.left_term - solution.right_term); + printf("%g\n", positiv_zero(solution.left_term + solution.right_term)); + printf("%g\n", positiv_zero(solution.left_term - solution.right_term)); } static void print_solution_delta_negativ(s_solution_degree_2 solution) @@ -154,7 +154,7 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution) if (solution.all_int) { - denominator = solution.a * 2; + denominator = positiv_zero(solution.a * 2); denominator_abs = ft_fabs(denominator); denominator_sign = ft_fsign(denominator); // solution 1 @@ -167,7 +167,7 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution) printf("- "); else if (has_first_term) printf("+ "); // dont print '+' if it's first term - printf("%gi/%g\n", solution.delta_sqrt, denominator_abs); + printf("%gi/%g\n", positiv_zero(solution.delta_sqrt), denominator_abs); // solution 2 if (!is_nearly_equal_zero(solution.b)) @@ -179,11 +179,11 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution) printf("- "); else if (has_first_term) printf("+ "); // dont print '+' if it's first term - printf("%gi/%g\n", solution.delta_sqrt, denominator_abs); + printf("%gi/%g\n", positiv_zero(solution.delta_sqrt), denominator_abs); } else { - right_term_abs = ft_fabs(solution.right_term); + right_term_abs = positiv_zero(ft_fabs(solution.right_term)); right_term_sign = ft_fsign(solution.right_term); // solution 1 diff --git a/src/solver.c b/src/solver.c index 764efb3..d4bafcf 100644 --- a/src/solver.c +++ b/src/solver.c @@ -2,13 +2,6 @@ #include "computorv1.h" -static double positiv_zero(double num) -{ - if (num == 0) - return 0; - return num; -} - static void solve_degree_1(s_solution_degree_1 *solution, double a, double b) { solution->a = a; diff --git a/src/utils/math.c b/src/utils/math.c index 9d5debd..7640bbf 100644 --- a/src/utils/math.c +++ b/src/utils/math.c @@ -31,6 +31,13 @@ const s_polynom *get_term_of_power(const s_polynom *polynom, int power) return &polynom[i]; } +double positiv_zero(double num) +{ + if (is_nearly_equal_zero(num)) + return 0.0; + return num; +} + bool has_decimal_part(double num) { return (!is_nearly_equal(num, (int)num)); diff --git a/tester.sh b/tester.sh index f5f2bc6..e3d97be 100644 --- a/tester.sh +++ b/tester.sh @@ -170,17 +170,17 @@ run_test \ Reduced form: 0 * x^0 + 2 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly positive, the two solutions are: -2.66667 --3.33333" +0 +-0.666667" run_test \ "6. degree 2" \ "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5.123 * x^1" "\ -Reduced form: 2 * x^0 - 4.123 * x^1 + 3.4 * x^2 = 0 +Reduced form: -2 * x^0 - 4.123 * x^1 + 3.4 * x^2 = 0 Polynomial degree: 2 -Discriminant is strictly negative, the two complex solutions are: -4.123/6.8 + 3.19388i/6.8 -4.123/6.8 - 3.19388i/6.8" +Discriminant is strictly positive, the two solutions are: +1.58401 +-0.371359" run_test \ "7. float exponent" \ @@ -191,7 +191,7 @@ error run_test \ "8. degree 4" \ "3x^2 + 2x -7x^4 = x^4" "\ -Reduced form: 0 * x^0 + 2 * x^1 + 3 * x^2 + 6 * x^4 = 0 +Reduced form: 0 * x^0 + 2 * x^1 + 3 * x^2 + 0 * x^3 - 8 * x^4 = 0 Polynomial degree: 4 The polynomial degree is strictly greater than 2, I can't solve."