From cd6e2327e90cf71d80fda0c7ff88eb651cb72641 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 10 May 2026 18:27:34 +0200 Subject: [PATCH] solve all terms --- src/solver.c | 55 +++++++++++++++++++++++++++++----------------- src/utils/errors.c | 4 ++-- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/solver.c b/src/solver.c index 79ed1f0..dba7e5b 100644 --- a/src/solver.c +++ b/src/solver.c @@ -102,36 +102,51 @@ typedef struct static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, double c) { double delta; + double delta_sqrt; solution->a = a; solution->b = b; solution->c = c; delta = b * b - 4 * a * c; - solution->delta_sign = ft_fsign(delta); - solution->delta_absolute = ft_fabs(delta); - solution->delta_sqrt = ft_sqrt(solution->delta_absolute); + solution->delta_sign = ft_fsign(delta); // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO + solution->delta_absolute = ft_fabs(delta); // |Δ| == |b² - 4ac| + solution->delta_sqrt = ft_sqrt(solution->delta_absolute); // √|Δ| + delta_sqrt = solution->delta_sqrt; // first term - solution->first_term_gcd = find_gcd(b, 2 * a); - solution->first_term_numerator = -b / solution->first_term_gcd; - solution->first_term_denominator = 2 * a / solution->first_term_gcd; - solution->first_term = -b / (2 * a); + solution->first_term_gcd = find_gcd(b, 2 * a); // gcd(b, 2a) + solution->first_term_numerator = -b / solution->first_term_gcd; // -b / gcd + solution->first_term_denominator = 2 * a / solution->first_term_gcd; // 2a / gcd + solution->first_term = -b / (2 * a); // double (-b / 2a) // second term - solution->second_term_gcd = find_gcd(solution->delta_sqrt, 2 * a); - // solution->second_term_numerator = ; - // solution->second_term_denominator = ; - // solution->second_term = ; - // solution->solution1 = solution->first_term; - // if (solution->delta_sign == DELTA_ZERO) - // { - // solution->solution2 = ; - // } - // else - // { - // solution->solution2 = 0.0; - // } + solution->second_term_gcd = find_gcd(delta_sqrt, 2 * a); // gcd(√|Δ|, 2a) + solution->second_term_numerator = delta_sqrt / solution->second_term_gcd; // √|Δ| / gcd + solution->second_term_denominator = 2 * a / solution->second_term_gcd; // 2a / gcd + solution->second_term = delta_sqrt / 2 * a; // √|Δ| / 2a + + // solution + /* + degree 2 : delta > 0 ( -b / 2a +- √Δ / 2a ) + delta == 0 ( -b / 2a ) + delta < 0 ( -b / 2a +- i√|Δ| / 2a ) + */ + solution->solution1 = solution->first_term; // first_term + second_term + if (solution->delta_sign == DELTA_ZERO) + { + solution->solution1 = solution->first_term; // -b / 2a + solution->solution2 = NAN; // NAN (no solution) + } + else if (solution->delta_sign == DELTA_MINUS) + { + solution->solution1 = solution->first_term + solution->second_term; // -b / 2a + i√|Δ| / 2a + solution->solution2 = solution->first_term - solution->second_term; // -b / 2a - i√|Δ| / 2a + } + else if (solution->delta_sign == DELTA_PLUS) + { + // no real solution, but can output irreal solution with 'i' + } } void solve(const s_polynom *polynom, s_solution *solution) diff --git a/src/utils/errors.c b/src/utils/errors.c index 362bbdd..fe321e8 100644 --- a/src/utils/errors.c +++ b/src/utils/errors.c @@ -98,8 +98,8 @@ static void print_context_solution() dprintf(STDERR_FILENO, "second_term_denominator: %15i ( 2a / gcd )\n", solution_2.second_term_denominator); dprintf(STDERR_FILENO, "second_term : %15g ( √|Δ| / 2a )\n", solution_2.second_term); - dprintf(STDERR_FILENO, "solution1 : %15g ( )\n", solution_2.solution1); - dprintf(STDERR_FILENO, "solution2 : %15g ( )\n", solution_2.solution2); + dprintf(STDERR_FILENO, "solution1 : %15g ( (-b / 2a) + (√Δ / 2a) )\n", solution_2.solution1); + dprintf(STDERR_FILENO, "solution2 : %15g ( (-b / 2a) - (√Δ / 2a) )\n", solution_2.solution2); } ft_putchar_fd('\n', STDERR_FILENO);