solve all terms
This commit is contained in:
55
src/solver.c
55
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)
|
static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, double c)
|
||||||
{
|
{
|
||||||
double delta;
|
double delta;
|
||||||
|
double delta_sqrt;
|
||||||
|
|
||||||
solution->a = a;
|
solution->a = a;
|
||||||
solution->b = b;
|
solution->b = b;
|
||||||
solution->c = c;
|
solution->c = c;
|
||||||
|
|
||||||
delta = b * b - 4 * a * c;
|
delta = b * b - 4 * a * c;
|
||||||
solution->delta_sign = ft_fsign(delta);
|
solution->delta_sign = ft_fsign(delta); // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO
|
||||||
solution->delta_absolute = ft_fabs(delta);
|
solution->delta_absolute = ft_fabs(delta); // |Δ| == |b² - 4ac|
|
||||||
solution->delta_sqrt = ft_sqrt(solution->delta_absolute);
|
solution->delta_sqrt = ft_sqrt(solution->delta_absolute); // √|Δ|
|
||||||
|
delta_sqrt = solution->delta_sqrt;
|
||||||
|
|
||||||
// first term
|
// first term
|
||||||
solution->first_term_gcd = find_gcd(b, 2 * a);
|
solution->first_term_gcd = find_gcd(b, 2 * a); // gcd(b, 2a)
|
||||||
solution->first_term_numerator = -b / solution->first_term_gcd;
|
solution->first_term_numerator = -b / solution->first_term_gcd; // -b / gcd
|
||||||
solution->first_term_denominator = 2 * a / solution->first_term_gcd;
|
solution->first_term_denominator = 2 * a / solution->first_term_gcd; // 2a / gcd
|
||||||
solution->first_term = -b / (2 * a);
|
solution->first_term = -b / (2 * a); // double (-b / 2a)
|
||||||
|
|
||||||
// second term
|
// second term
|
||||||
solution->second_term_gcd = find_gcd(solution->delta_sqrt, 2 * a);
|
solution->second_term_gcd = find_gcd(delta_sqrt, 2 * a); // gcd(√|Δ|, 2a)
|
||||||
// solution->second_term_numerator = ;
|
solution->second_term_numerator = delta_sqrt / solution->second_term_gcd; // √|Δ| / gcd
|
||||||
// solution->second_term_denominator = ;
|
solution->second_term_denominator = 2 * a / solution->second_term_gcd; // 2a / gcd
|
||||||
// solution->second_term = ;
|
solution->second_term = delta_sqrt / 2 * a; // √|Δ| / 2a
|
||||||
// solution->solution1 = solution->first_term;
|
|
||||||
// if (solution->delta_sign == DELTA_ZERO)
|
// solution
|
||||||
// {
|
/*
|
||||||
// solution->solution2 = ;
|
degree 2 : delta > 0 ( -b / 2a +- √Δ / 2a )
|
||||||
// }
|
delta == 0 ( -b / 2a )
|
||||||
// else
|
delta < 0 ( -b / 2a +- i√|Δ| / 2a )
|
||||||
// {
|
*/
|
||||||
// solution->solution2 = 0.0;
|
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)
|
void solve(const s_polynom *polynom, s_solution *solution)
|
||||||
|
|||||||
@@ -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_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, "second_term : %15g ( √|Δ| / 2a )\n", solution_2.second_term);
|
||||||
|
|
||||||
dprintf(STDERR_FILENO, "solution1 : %15g ( )\n", solution_2.solution1);
|
dprintf(STDERR_FILENO, "solution1 : %15g ( (-b / 2a) + (√Δ / 2a) )\n", solution_2.solution1);
|
||||||
dprintf(STDERR_FILENO, "solution2 : %15g ( )\n", solution_2.solution2);
|
dprintf(STDERR_FILENO, "solution2 : %15g ( (-b / 2a) - (√Δ / 2a) )\n", solution_2.solution2);
|
||||||
}
|
}
|
||||||
|
|
||||||
ft_putchar_fd('\n', STDERR_FILENO);
|
ft_putchar_fd('\n', STDERR_FILENO);
|
||||||
|
|||||||
Reference in New Issue
Block a user