wip implemented new union for solve

This commit is contained in:
hugogogo
2026-05-08 01:00:03 +02:00
parent 0fcfd0260d
commit 43b4def6ce
9 changed files with 176 additions and 105 deletions

View File

@@ -118,38 +118,38 @@ run: $(NAME)
-./$(NAME) -d "3x^2 + 2x -7x^4 = x^4" -./$(NAME) -d "3x^2 + 2x -7x^4 = x^4"
@echo $(B_PURPLE)"\n---------------------------------------------\n9. run with utf8 \n"$(RESET) @echo $(B_PURPLE)"\n---------------------------------------------\n9. run with utf8 \n"$(RESET)
-./$(NAME) -d "3x² + 2x -7x¹ = x" -./$(NAME) -d "3x² + 2x -7x¹ = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n10. run \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n10. run \n"$(RESET)
-./$(NAME) "3x² + 2x -7 = x" # -./$(NAME) "3x² + 2x -7 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n11. run \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n11. run \n"$(RESET)
-./$(NAME) "-3x² + 2x -7 = x" # -./$(NAME) "-3x² + 2x -7 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n12. run \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n12. run \n"$(RESET)
-./$(NAME) "+3x² + 2x -7 = x" # -./$(NAME) "+3x² + 2x -7 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n13. run \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n13. run \n"$(RESET)
-./$(NAME) "3x² + 0x -7 = x" # -./$(NAME) "3x² + 0x -7 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n14. run \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n14. run \n"$(RESET)
-./$(NAME) "3x² + 0x -0 = x" # -./$(NAME) "3x² + 0x -0 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n15. run \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n15. run \n"$(RESET)
-./$(NAME) "3x² + 2x -0 = x" # -./$(NAME) "3x² + 2x -0 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n16. run -b \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n16. run -b \n"$(RESET)
-./$(NAME) -b "3x² + 2x -7 = x" # -./$(NAME) -b "3x² + 2x -7 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n17. run -b \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n17. run -b \n"$(RESET)
-./$(NAME) -b "-3x² + 2x -7 = x" # -./$(NAME) -b "-3x² + 2x -7 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n18. run -b \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n18. run -b \n"$(RESET)
-./$(NAME) -b "+3x² + 2x -7 = x" # -./$(NAME) -b "+3x² + 2x -7 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n19. run -b \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n19. run -b \n"$(RESET)
-./$(NAME) -b "3x² + 0x -7 = x" # -./$(NAME) -b "3x² + 0x -7 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n20. run -b \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n20. run -b \n"$(RESET)
-./$(NAME) -b "3x² + 2x -0 = x" # -./$(NAME) -b "3x² + 2x -0 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n21. run -b \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n21. run -b \n"$(RESET)
-./$(NAME) -b "3x² + 0x -0 = x" # -./$(NAME) -b "3x² + 0x -0 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n22. run -b \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n22. run -b \n"$(RESET)
-./$(NAME) -b "3x² + x -0 = x" # -./$(NAME) -b "3x² + x -0 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n23. run -b \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n23. run -b \n"$(RESET)
-./$(NAME) -b "0x² + x -0 = x" # -./$(NAME) -b "0x² + x -0 = x"
@echo $(B_PURPLE)"\n---------------------------------------------\n24. run -b \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n24. run -b \n"$(RESET)
-./$(NAME) -b "2x⁵ + x -0 = -7x^5" # -./$(NAME) -b "2x⁵ + x -0 = -7x^5"
@echo $(B_PURPLE)"\n---------------------------------------------\n25. run -b \n"$(RESET) # @echo $(B_PURPLE)"\n---------------------------------------------\n25. run -b \n"$(RESET)
-./$(NAME) -b "2x + x -0 = -7x" # -./$(NAME) -b "2x + x -0 = -7x"
clean: clean:
$(RM_OBJS) $(RM_OBJS)

View File

@@ -48,21 +48,34 @@ this project uses submodules (maybe recursively), so either :
-> if degree 3 : -> if degree 3 :
- print degree - print degree
5. solve 5. solve
-> discriminant : Δ = b² - 4ac -> degree 1 :
-> Δ > 0 -> 2 solutions : x = ( -b / 2a ) +- ( √|Δ| / 2a ) -> ax + b = 0 <=> ax = -b <=> x = -b / a
-> Δ == 0 -> : x = ( -b / 2a ) -> solution :
-> Δ < 0 -> 2 solutions : x = ( -b / 2a ) +- i( √|Δ| / 2a ) - a; // double
-> solution : - b; // double
- delta_sign; // + or - - solution_gcd; // gcd(b, a)
- delta_absolute; // |Δ| - solution_numerator; // -b / gcd
- first_term_gcd; // gcd(b, 2a) - solution_denominator; // a / gcd
- first_term_numerator; // -b / gcd - solution; // double (-b / a)
- first_term_denominator; // 2a / gcd -> degree 2 :
- first_term; // double (-b / 2a) -> discriminant : Δ = b² - 4ac
- second_term_gcd; // gcd(√|Δ|, 2a) -> Δ > 0 -> 2 solutions : x = ( -b / 2a ) +- ( √|Δ| / 2a )
- second_term_numerator; // √|Δ| / gcd -> Δ == 0 -> 1 solution : x = ( -b / 2a )
- second_term_denominator; // 2a / gcd -> Δ < 0 -> 2 solutions : x = ( -b / 2a ) +- i( √|Δ| / 2a )
- second_term; // double (√|Δ| / 2a) -> solution :
- double solution1; // first_term + second_term - a; // double
- double solution2; // first_term - second_term - b; // double
- c; // double
- delta_sign; // + or -
- delta_absolute; // |Δ|
- first_term_gcd; // gcd(b, 2a)
- first_term_numerator; // -b / gcd
- first_term_denominator; // 2a / gcd
- first_term; // double (-b / 2a)
- second_term_gcd; // gcd(√|Δ|, 2a)
- second_term_numerator; // √|Δ| / gcd
- second_term_denominator; // 2a / gcd
- second_term; // double (√|Δ| / 2a)
- double solution1; // first_term + second_term
- double solution2; // first_term - second_term
6. print solution 6. print solution

View File

@@ -115,11 +115,19 @@ int reduce(s_term *terms, s_polynom *polynom, int max_exponent);
typedef enum typedef enum
{ {
DELTA_PLUS, // + DELTA_PLUS = 1,
DELTA_MINUS, // - DELTA_MINUS = -1,
DELTA_ZERO, // 0 DELTA_ZERO = 0,
} e_delta_sign; } e_delta_sign;
typedef struct
{
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 typedef struct
{ {
e_delta_sign delta_sign; // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO e_delta_sign delta_sign; // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO
@@ -133,7 +141,17 @@ typedef struct
int second_term_denominator; // 2a / gcd int second_term_denominator; // 2a / gcd
double second_term; // double (√|Δ| / 2a) double second_term; // double (√|Δ| / 2a)
double solution1; // first_term + second_term double solution1; // first_term + second_term
double solution2; // first_term - second_term double solution2; // first_term - second_term (not if DELTA_ZERO)
} s_solution_degree_2;
typedef struct
{
int degree;
union
{
s_solution_degree_1 solution_degree_1;
s_solution_degree_2 solution_degree_2;
};
} s_solution; } s_solution;
void solve(const s_polynom *polynom, s_solution *solution); void solve(const s_polynom *polynom, s_solution *solution);

2
libft

Submodule libft updated: 3cf0b6ecb9...8edf428fed

View File

@@ -172,10 +172,11 @@ void launch_computorv1(char *input)
print_reduced_form(polynom); print_reduced_form(polynom);
print_degree(polynom, degree); print_degree(polynom, degree);
// // solve // solve
// s_solution solution[1]; s_solution solution[1];
// solution_g_err = solution; solution_g_err = solution;
// solve(polynom, solution); solution[0].degree = degree;
solve(polynom, solution);
// print solution // print solution

View File

@@ -58,7 +58,7 @@ int reduce(s_term *terms, s_polynom *polynom, int max_exponent)
// fill polynom // fill polynom
polynom[i].coefficient = tmp_coefficient; polynom[i].coefficient = tmp_coefficient;
polynom[i].exponent = max_exponent; polynom[i].exponent = max_exponent;
polynom[i].sign = ft_sign_f(tmp_coefficient) == 1 ? TERM_PLUS : TERM_MINUS; polynom[i].sign = ft_fsign(tmp_coefficient) == 1 ? TERM_PLUS : TERM_MINUS;
max_exponent--; max_exponent--;
i++; i++;

View File

@@ -69,32 +69,43 @@ static int find_gcd(double numerator, double denominator)
} }
/* /*
e_delta_sign delta_sign; // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO typedef struct
double delta_absolute; // |Δ| == |b² - 4ac| {
int first_term_gcd; // gcd(b, 2a) double a; // a in "ax + b"
int first_term_numerator; // -b / gcd double b; // b in "ax + b"
int first_term_denominator; // 2a / gcd int solution_gcd; // gcd(b, a)
double first_term; // double (-b / 2a) int solution_numerator; // -b / gcd
int second_term_gcd; // gcd(√|Δ|, 2a) int solution_denominator; // a / gcd
int second_term_numerator; // √|Δ| / gcd double solution; // double (-b / a)
int second_term_denominator; // 2a / gcd } s_solution_degree_1;
double second_term; // double (√|Δ| / 2a) */
double solution1; // first_term + second_term /*
double solution2; // first_term - second_term 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|
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)
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;
*/ */
void solve(const s_polynom *polynom, s_solution *solution) static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, double c)
{ {
double delta; double delta;
double a;
double b;
double c;
a = polynom[2].coefficient;
b = polynom[1].coefficient;
c = polynom[0].coefficient;
delta = b * b - 4 * a * c; delta = b * b - 4 * a * c;
solution->delta_sign = ft_sign_f(delta); solution->delta_sign = ft_fsign(delta);
solution->delta_absolute = ft_fabs(delta); solution->delta_absolute = ft_fabs(delta);
solution->first_term_gcd = find_gcd(b, 2 * a); solution->first_term_gcd = find_gcd(b, 2 * a);
solution->first_term_numerator = -b / solution->first_term_gcd; solution->first_term_numerator = -b / solution->first_term_gcd;
@@ -114,3 +125,22 @@ void solve(const s_polynom *polynom, s_solution *solution)
// solution->solution2 = 0.0; // solution->solution2 = 0.0;
// } // }
} }
void solve(const s_polynom *polynom, s_solution *solution)
{
double a;
double b;
double c;
if (solution->degree == 1)
{
}
else if (solution->degree == 2)
{
a = polynom[0].coefficient;
b = polynom[1].coefficient;
c = polynom[2].coefficient;
solve_degree_2(&solution->solution_degree_2, a, b, c);
}
}

View File

@@ -71,18 +71,27 @@ static void print_context_polynom()
static void print_context_solution() static void print_context_solution()
{ {
dprintf(STDERR_FILENO, "delta_sign : %25s\n", delta_sign_to_str(solution_g_err->delta_sign)); s_solution_degree_2 solution_2;
dprintf(STDERR_FILENO, "delta_absolute : %25g (|b² - 4ac|)\n", solution_g_err->delta_absolute);
dprintf(STDERR_FILENO, "first_term_gcd : %25i\n", solution_g_err->first_term_gcd); if (solution_g_err->degree == 2)
dprintf(STDERR_FILENO, "first_term_numerator : %25i (-b / gcd)\n", solution_g_err->first_term_numerator); {
dprintf(STDERR_FILENO, "first_term_denominator : %25i (2a / gcd)\n", solution_g_err->first_term_denominator); solution_2 = solution_g_err->solution_degree_2;
dprintf(STDERR_FILENO, "first_term : %25g (-b / 2a)\n", solution_g_err->first_term); dprintf(STDERR_FILENO, "degree 2 : delta > 0 ( -b/2a +- √Δ/2a )\n");
// dprintf(STDERR_FILENO, "second_term_gcd : %25g\n", solution_g_err->second_term_gcd); dprintf(STDERR_FILENO, " delta == 0 ( -b/2a )\n");
// dprintf(STDERR_FILENO, "second_term_numerator : %25g\n", solution_g_err->second_term_numerator); dprintf(STDERR_FILENO, " delta < 0 ( -b/2a +- i√|Δ|/2a )\n");
// dprintf(STDERR_FILENO, "second_term_denominator: %25g\n", solution_g_err->second_term_denominator); dprintf(STDERR_FILENO, "delta_sign : %25s\n", delta_sign_to_str(solution_2.delta_sign));
// dprintf(STDERR_FILENO, "second_term : %25g\n", solution_g_err->second_term); dprintf(STDERR_FILENO, "delta_absolute : %25g (|b² - 4ac|)\n", solution_2.delta_absolute);
// dprintf(STDERR_FILENO, "solution1 : %25g\n", solution_g_err->solution1); dprintf(STDERR_FILENO, "first_term_gcd : %25i\n", solution_2.first_term_gcd);
// dprintf(STDERR_FILENO, "solution2 : %25g\n", solution_g_err->solution2); 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);
}
ft_putchar_fd('\n', STDERR_FILENO); ft_putchar_fd('\n', STDERR_FILENO);
} }

View File

@@ -49,24 +49,24 @@ const char *term_position_to_str(e_term_position enum_value)
const char *term_sign_to_str(e_term_sign enum_value) const char *term_sign_to_str(e_term_sign enum_value)
{ {
if (enum_value > TERM_SIGN_END || enum_value < TERM_PLUS) if (enum_value == TERM_PLUS)
return "TERM_PLUS";
else if (enum_value == TERM_MINUS)
return "TERM_MINUS";
else if (enum_value == TERM_SIGN_END)
return "TERM_SIGN_END";
else
return "invalid enum value"; return "invalid enum value";
static const char *const term_sign_str[TERM_SIGN_END + 1] = {
[TERM_PLUS] = "TERM_PLUS",
[TERM_MINUS] = "TERM_MINUS",
[TERM_SIGN_END] = "TERM_SIGN_END"};
return term_sign_str[enum_value];
} }
const char *delta_sign_to_str(e_delta_sign enum_value) const char *delta_sign_to_str(e_delta_sign enum_value)
{ {
if (enum_value > DELTA_ZERO || enum_value < DELTA_PLUS) if (enum_value == DELTA_PLUS)
return "DELTA_PLUS";
else if (enum_value == DELTA_MINUS)
return "DELTA_MINUS";
else if (enum_value == DELTA_ZERO)
return "DELTA_ZERO";
else
return "invalid enum value"; return "invalid enum value";
static const char *const delta_sign_str[DELTA_ZERO + 1] = {
[DELTA_PLUS] = "DELTA_PLUS",
[DELTA_MINUS] = "DELTA_MINUS",
[DELTA_ZERO] = "DELTA_ZERO"};
return delta_sign_str[enum_value];
} }