diff --git a/README.md b/README.md index 11abbcb..ea89106 100644 --- a/README.md +++ b/README.md @@ -89,11 +89,15 @@ finding the square root of x ## dichotomy method +the dichotomie method, or binary search, consist on dividing the range of research by 2 each time, and choosing the right one for the next iteration. + +Ex : + ``` solution ↓ |------------------------------ 1. define range bound_1 : 0 -|-----------------------------| 2. choose range bound_2 : x +|-----------------------------| 2. define range bound_2 : x |--------------+--------------| 3. take mid : (bound_1 + bound_2) / 2 |--------------|-------ø------| 4. choose range bound : bound_1 or bound_2 @@ -123,7 +127,9 @@ finding the square root of x ## Newton–Raphson method -it's like a self-correcting binary search, we get rid of the step "choose range" : +it's like a self-correcting binary search, we get rid of the step "choose range", we use the formulae `x/v` to find the next range, with `x` being the number we are trying to get the sqaure root from, and `v` the value found at the previous step. + +Ex : ``` solution diff --git a/headers/computorv1.h b/headers/computorv1.h index 2cc9e44..1738d3f 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -122,6 +122,8 @@ typedef enum typedef struct { + double a; // a in "ax + b" + double b; // b in "ax + b" int solution_gcd; // gcd(b, a) int solution_numerator; // -b / gcd int solution_denominator; // a / gcd @@ -130,6 +132,9 @@ typedef struct typedef struct { + double a; // a in "ax² + bx + c" + double b; // b in "ax² + bx + c" + double c; // c in "ax² + bx + c" e_delta_sign delta_sign; // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO double delta_absolute; // |Δ| == |b² - 4ac| double delta_sqrt; // √|Δ| diff --git a/libft b/libft index ae9787b..60c72d7 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit ae9787ba6db99412ff4c6d0bde357e060f350096 +Subproject commit 60c72d7560f47ac24074cae1fdb347e364e8d624 diff --git a/src/solver.c b/src/solver.c index 8c2f1a2..79ed1f0 100644 --- a/src/solver.c +++ b/src/solver.c @@ -103,6 +103,10 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do { double delta; + 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); @@ -115,7 +119,7 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do solution->first_term = -b / (2 * a); // second term - // solution->second_term_gcd = find_gcd(b, 2 * a); + solution->second_term_gcd = find_gcd(solution->delta_sqrt, 2 * a); // solution->second_term_numerator = ; // solution->second_term_denominator = ; // solution->second_term = ; diff --git a/src/utils/errors.c b/src/utils/errors.c index 1014b62..1bc55f0 100644 --- a/src/utils/errors.c +++ b/src/utils/errors.c @@ -76,22 +76,29 @@ static void print_context_solution() if (solution_g_err->degree == 2) { solution_2 = solution_g_err->solution_degree_2; - dprintf(STDERR_FILENO, "degree 2 : delta > 0 ( -b/2a +- √Δ/2a )\n"); - dprintf(STDERR_FILENO, " delta == 0 ( -b/2a )\n"); - dprintf(STDERR_FILENO, " delta < 0 ( -b/2a +- i√|Δ|/2a )\n"); - dprintf(STDERR_FILENO, "delta_sign : %25s\n", delta_sign_to_str(solution_2.delta_sign)); - dprintf(STDERR_FILENO, "delta_absolute : %25g ( |Δ| == |b² - 4ac| )\n", solution_2.delta_absolute); - dprintf(STDERR_FILENO, "delta_sqrt : %25g (√|Δ| )\n", solution_2.delta_sqrt); - dprintf(STDERR_FILENO, "first_term_gcd : %25i\n", solution_2.first_term_gcd); - dprintf(STDERR_FILENO, "first_term_numerator : %25i (-b / gcd )\n", solution_2.first_term_numerator); - dprintf(STDERR_FILENO, "first_term_denominator : %25i (2a / gcd )\n", solution_2.first_term_denominator); - dprintf(STDERR_FILENO, "first_term : %25g (-b / 2a )\n", solution_2.first_term); - // dprintf(STDERR_FILENO, "second_term_gcd : %25g\n", solution_2.second_term_gcd); - // dprintf(STDERR_FILENO, "second_term_numerator : %25g\n", solution_2.second_term_numerator); - // dprintf(STDERR_FILENO, "second_term_denominator: %25g\n", solution_2.second_term_denominator); - // dprintf(STDERR_FILENO, "second_term : %25g\n", solution_2.second_term); - // dprintf(STDERR_FILENO, "solution1 : %25g\n", solution_2.solution1); - // dprintf(STDERR_FILENO, "solution2 : %25g\n", solution_2.solution2); + dprintf(STDERR_FILENO, "degree 2 : delta > 0 ( -b / 2a +- √Δ / 2a )\n"); + dprintf(STDERR_FILENO, " delta == 0 ( -b / 2a )\n"); + dprintf(STDERR_FILENO, " delta < 0 ( -b / 2a +- i√|Δ| / 2a )\n"); + + dprintf(STDERR_FILENO, "a : %15g\n", solution_2.a); + dprintf(STDERR_FILENO, "b : %15g\n", solution_2.b); + dprintf(STDERR_FILENO, "c : %15g\n", solution_2.c); + + dprintf(STDERR_FILENO, "delta_sign : %15s\n", delta_sign_to_str(solution_2.delta_sign)); + dprintf(STDERR_FILENO, "delta_absolute : %15g ( |Δ| == |b² - 4ac| )\n", solution_2.delta_absolute); + dprintf(STDERR_FILENO, "delta_sqrt : %15g ( √|Δ| )\n", solution_2.delta_sqrt); + + dprintf(STDERR_FILENO, "first_term_gcd : %15i\n", solution_2.first_term_gcd); + dprintf(STDERR_FILENO, "first_term_numerator : %15i ( -b / gcd )\n", solution_2.first_term_numerator); + dprintf(STDERR_FILENO, "first_term_denominator : %15i ( 2a / gcd )\n", solution_2.first_term_denominator); + dprintf(STDERR_FILENO, "first_term : %15g ( -b / 2a )\n", solution_2.first_term); + + dprintf(STDERR_FILENO, "second_term_gcd : %15i\n", solution_2.second_term_gcd); + // dprintf(STDERR_FILENO, "second_term_numerator : %15g\n", solution_2.second_term_numerator); + // dprintf(STDERR_FILENO, "second_term_denominator: %15g\n", solution_2.second_term_denominator); + // dprintf(STDERR_FILENO, "second_term : %15g\n", solution_2.second_term); + // dprintf(STDERR_FILENO, "solution1 : %15g\n", solution_2.solution1); + // dprintf(STDERR_FILENO, "solution2 : %15g\n", solution_2.solution2); } ft_putchar_fd('\n', STDERR_FILENO);