wip exo ouput

This commit is contained in:
hugogogo
2026-05-10 20:19:24 +02:00
parent cd6e2327e9
commit 500b6fd932
7 changed files with 93 additions and 85 deletions

View File

@@ -68,36 +68,21 @@ static int find_gcd(double numerator, double denominator)
return gcd_int(abs(num_scaled), ft_abs(den_scaled));
}
/*
typedef struct
static void solve_degree_1(s_solution_degree_1 *solution, double a, double b)
{
int solution_gcd; // gcd(b, a)
int solution_numerator; // -b / gcd
int solution_denominator; // a / gcd
double solution; // double (-b / a)
} s_solution_degree_1;
*/
/*
typedef struct
{
e_delta_sign delta_sign; // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO
double delta_absolute; // |Δ| == |b² - 4ac|
double delta_sqrt; // √|Δ|
double value;
int first_term_gcd; // gcd(b, 2a)
int first_term_numerator; // -b / gcd
int first_term_denominator; // 2a / gcd
double first_term; // double (-b / 2a)
solution->a = a;
solution->b = b;
int second_term_gcd; // gcd(√|Δ|, 2a)
int second_term_numerator; // √|Δ| / gcd
int second_term_denominator; // 2a / gcd
double second_term; // double (√|Δ| / 2a)
double solution1; // first_term + second_term
double solution2; // first_term - second_term (not if DELTA_ZERO)
} s_solution_degree_2;
*/
solution->solution_gcd = find_gcd(b, a); // gcd(b, a)
solution->solution_numerator = -b / solution->solution_gcd; // -b / gcd
solution->solution_denominator = a / solution->solution_gcd; // a / gcd
value = -b / a;
if (value == 0)
value = ft_fabs(value);
solution->solution = value;
}
static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, double c)
{
@@ -127,11 +112,6 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do
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)
{
@@ -146,6 +126,8 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do
else if (solution->delta_sign == DELTA_PLUS)
{
// no real solution, but can output irreal solution with 'i'
solution->solution1 = NAN;
solution->solution2 = NAN;
}
}
@@ -157,6 +139,10 @@ void solve(const s_polynom *polynom, s_solution *solution)
if (solution->degree == 1)
{
a = polynom[0].coefficient;
b = polynom[1].coefficient;
solve_degree_1(&solution->solution_degree_1, a, b);
}
else if (solution->degree == 2)
{