From b185647d2b330901e385c95a5ed21e6b8c502b44 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Fri, 15 May 2026 01:03:14 +0200 Subject: [PATCH] fix reducer sign error --- README.md | 4 ---- headers/computorv1.h | 2 +- src/reducer.c | 3 ++- src/solver.c | 2 +- tester.sh | 36 +++++++++++++++++++++--------------- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 98a3908..4ebafd8 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,5 @@ # 42_EXT_05_computorv1 -## todo - -- double is nearly_equal_0 - ## ressources diff --git a/headers/computorv1.h b/headers/computorv1.h index 5034f3e..31811ad 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -191,7 +191,7 @@ void solve(const s_polynom *polynom, s_solution *solution); */ void print_reduced_form(s_polynom *polynom, int degree); -void print_degree(s_polynom *polynom, int max_exponent); +void print_degree(s_polynom *polynom, int degree); /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * PRINTER_SOLUTIONS.C diff --git a/src/reducer.c b/src/reducer.c index 08425ba..dd57ad5 100644 --- a/src/reducer.c +++ b/src/reducer.c @@ -21,7 +21,8 @@ static double get_all_terms_with_exponent(s_term *terms, int exponent) // sign sign = 1; if (terms[i].position == TERM_RIGHT) - sign = terms[i].sign == TERM_PLUS ? -1 : 1; + sign = -1; + // sign = terms[i].sign == TERM_PLUS ? -1 : 1; // add coefficients coefficient += terms[i].coefficient * sign; diff --git a/src/solver.c b/src/solver.c index 5953dc5..37a3d03 100644 --- a/src/solver.c +++ b/src/solver.c @@ -14,7 +14,7 @@ static void solve_degree_1(s_solution_degree_1 *solution, double a, double b) solution->a = a; solution->b = b; - solution->solution = positiv_zero(-b / a); // -b / a + solution->solution = positiv_zero(-b / a); } static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, double c) diff --git a/tester.sh b/tester.sh index 2fce261..57a2fd6 100644 --- a/tester.sh +++ b/tester.sh @@ -233,11 +233,11 @@ Discriminant is strictly negative, the two complex solutions are: run_test \ "13. degree 2" \ "3x² + 0x -7 = x" "\ -Reduced form: 7 * x^0 - 1 * x^1 + 3 * x^2 = 0 +Reduced form: -7 * x^0 - 1 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 -Discriminant is strictly negative, the two complex solutions are: -1/6 + 9.11043i/6 -1/6 - 9.11043i/6" +Discriminant is strictly positive, the two solutions are: +1.369924 +-1.369924" run_test \ "14. degree 2" \ @@ -270,7 +270,7 @@ run_test \ "3x² + x -0 = x" "\ Reduced form: 0 * x^0 + 0 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 -Discriminant is equal to zero, the solution is: +Radicant is equal to zero, the solution is: 0" run_test \ @@ -282,14 +282,14 @@ Any real number is a solution." run_test \ "19. degree 5" \ "2x⁵ + x -0 = -7x^5" "\ -Reduced form: 0 * x^0 + 1 * x^1 + 0 * x^2 + 0 * x^3 + 0 * x^4 - 5 * x^5 = 0 +Reduced form: 0 * x^0 + 1 * x^1 + 0 * x^2 + 0 * x^3 + 0 * x^4 + 9 * x^5 = 0 Polynomial degree: 5 The polynomial degree is strictly greater than 2, I can't solve." run_test \ "20. degree 1" \ "2x + x -0 = -7x" "\ -Reduced form: 0 * x^0 - 4 * x^1 = 0 +Reduced form: 0 * x^0 + 10 * x^1 = 0 Polynomial degree: 1 The solution is: 0" @@ -297,10 +297,18 @@ The solution is: run_test \ "21. degree 1" \ "2x + x -3 = -7x" "\ -Reduced form: 3 * x^0 - 4 * x^1 = 0 +Reduced form: -3 * x^0 + 10 * x^1 = 0 Polynomial degree: 1 The solution is: -0.75" +0.3" + +run_test \ +"22. degree 1" \ +"-2x + x -3 = -7x" "\ +Reduced form: -3 * x^0 + 6 * x^1 = 0 +Polynomial degree: 1 +The solution is: +0.5" run_test \ "23. degree 2 without [=]" \ @@ -313,7 +321,7 @@ run_test \ "3x^2 = 0" "\ Reduced form: 0 * x^0 + 0 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 -Discriminant is equal to zero, the solution is: +Radicant is equal to zero, the solution is: 0" run_test \ @@ -321,9 +329,9 @@ run_test \ "3x^2 + 2 = 0" "\ Reduced form: 2 * x^0 + 0 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 -Discriminant is strictly negative, the two complex solutions are: -4.89898i/6 --4.89898i/6" +Radicant is strictly negative, the two complex solutions are: +i√(2/3) +-i√(2/3)" run_test \ "26. degree 2 pure" \ @@ -352,7 +360,6 @@ Radicant is strictly positive, the two solutions are: 2/√(3) -2/√(3)" - run_test \ "29. degree 2 pure" \ "16 * x^2 + 5 * x^1 - 4 * x^0 = 5 * x" "\ @@ -362,7 +369,6 @@ Radicant is strictly positive, the two solutions are: 1/2 -1/2" - run_test \ "30. degree 2 pure" \ "4 * x^2 + 5 * x^1 - 16 * x^0 = 5 * x" "\