fix complex sign operation
This commit is contained in:
2
Makefile
2
Makefile
@@ -108,7 +108,7 @@ run: $(NAME)
|
||||
|
||||
test: $(NAME)
|
||||
@echo $(B_PURPLE)"\n---------------------------------------------\nlaunch tests\n"$(RESET)
|
||||
-@bash tester.sh
|
||||
-@bash tester.sh -d
|
||||
|
||||
clean:
|
||||
$(RM_OBJS)
|
||||
|
||||
@@ -100,7 +100,7 @@ static bool token_is_number_double(const char *input, int input_pos, int *token_
|
||||
return true;
|
||||
}
|
||||
|
||||
// token is superscript int (e.g., ², ³, ⁴, ⁵, etc.)
|
||||
// token is superscript int (e.g., ¹, ², ³, ⁴, ⁵, etc.)
|
||||
static bool token_is_number_int_super(const char *input, int input_pos, int *token_size)
|
||||
{
|
||||
int digit_size = 0;
|
||||
|
||||
@@ -141,36 +141,76 @@ static void print_solution_delta_positiv(s_solution_degree_2 solution)
|
||||
|
||||
static void print_solution_delta_negativ(s_solution_degree_2 solution)
|
||||
{
|
||||
double denominator;
|
||||
double denominator_abs;
|
||||
int denominator_sign;
|
||||
double right_term_abs;
|
||||
int right_term_sign;
|
||||
bool has_first_term;
|
||||
|
||||
ft_printf("Discriminant is strictly negative, the two complex solutions are:\n");
|
||||
|
||||
has_first_term = false;
|
||||
|
||||
if (solution.all_int)
|
||||
{
|
||||
denominator = solution.a * 2;
|
||||
denominator_abs = ft_fabs(denominator);
|
||||
denominator_sign = ft_fsign(denominator);
|
||||
// solution 1
|
||||
if (!is_nearly_equal_zero(solution.b))
|
||||
printf("%g/%g + ", solution.b * -1, solution.a * 2);
|
||||
printf("%gi/%g\n", solution.delta_sqrt, solution.a * 2);
|
||||
{
|
||||
has_first_term = true;
|
||||
printf("%g/%g ", solution.b * -1 * denominator_sign, denominator_abs);
|
||||
}
|
||||
if (denominator_sign == -1)
|
||||
printf("- ");
|
||||
else if (has_first_term)
|
||||
printf("+ "); // dont print '+' if it's first term
|
||||
printf("%gi/%g\n", solution.delta_sqrt, denominator_abs);
|
||||
|
||||
// solution 2
|
||||
if (!is_nearly_equal_zero(solution.b))
|
||||
printf("%g/%g - ", solution.b * -1, solution.a * 2);
|
||||
else
|
||||
printf("-");
|
||||
printf("%gi/%g\n", solution.delta_sqrt, solution.a * 2);
|
||||
{
|
||||
has_first_term = true;
|
||||
printf("%g/%g ", solution.b * -1 * denominator_sign, denominator_abs);
|
||||
}
|
||||
if (denominator_sign == 1)
|
||||
printf("- ");
|
||||
else if (has_first_term)
|
||||
printf("+ "); // dont print '+' if it's first term
|
||||
printf("%gi/%g\n", solution.delta_sqrt, denominator_abs);
|
||||
}
|
||||
else
|
||||
{
|
||||
right_term_abs = ft_fabs(solution.right_term);
|
||||
right_term_sign = ft_fsign(solution.right_term);
|
||||
|
||||
// solution 1
|
||||
if (!is_nearly_equal_zero(solution.left_term))
|
||||
printf("%g + ", solution.left_term);
|
||||
if (!is_nearly_equal_zero(solution.right_term))
|
||||
printf("i * %g\n", solution.right_term);
|
||||
{
|
||||
has_first_term = true;
|
||||
printf("%g ", solution.left_term);
|
||||
}
|
||||
if (right_term_sign == -1)
|
||||
printf("- ");
|
||||
else if (has_first_term)
|
||||
printf("+ "); // dont print '+' if it's first term
|
||||
if (!is_nearly_equal_zero(right_term_abs))
|
||||
printf("%g*i\n", right_term_abs);
|
||||
|
||||
// solution 2
|
||||
if (!is_nearly_equal_zero(solution.left_term))
|
||||
printf("%g - ", solution.left_term);
|
||||
else
|
||||
printf("-");
|
||||
printf("i * %g\n", solution.right_term);
|
||||
{
|
||||
has_first_term = true;
|
||||
printf("%g ", solution.left_term);
|
||||
}
|
||||
if (right_term_sign == 1)
|
||||
printf("- ");
|
||||
else if (has_first_term)
|
||||
printf("+ "); // dont print '+' if it's first term
|
||||
if (!is_nearly_equal_zero(right_term_abs))
|
||||
printf("%g*i\n", right_term_abs);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
12
src/solver.c
12
src/solver.c
@@ -28,11 +28,11 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do
|
||||
solution->c = c;
|
||||
|
||||
delta = b * b - 4 * a * c;
|
||||
solution->delta = delta; // Δ == b² - 4ac
|
||||
solution->delta_sign = ft_fsign(delta); // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO
|
||||
solution->delta_absolute = ft_fabs(delta); // |Δ|
|
||||
solution->delta_sqrt = ft_sqrt(solution->delta_absolute, DOUBLE_PRECISION); // √|Δ|
|
||||
delta_sqrt = solution->delta_sqrt;
|
||||
solution->delta = delta; // Δ == b² - 4ac
|
||||
solution->delta_sign = ft_fsign(delta); // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO
|
||||
solution->delta_absolute = ft_fabs(delta); // |Δ|
|
||||
delta_sqrt = ft_sqrt(solution->delta_absolute, DOUBLE_PRECISION); // √|Δ|
|
||||
solution->delta_sqrt = delta_sqrt;
|
||||
|
||||
/**
|
||||
* SOLVE LEFT AND RIGHT TERMS AS DOUBLES
|
||||
@@ -41,7 +41,7 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do
|
||||
if (!is_nearly_equal_zero(b))
|
||||
solution->left_term = positiv_zero(-b / (2 * a)); // -b / 2a
|
||||
if (!is_nearly_equal_zero(delta))
|
||||
solution->right_term = positiv_zero(delta_sqrt / 2 * a); // √|Δ| / 2a
|
||||
solution->right_term = positiv_zero(delta_sqrt / (2 * a)); // √|Δ| / 2a
|
||||
|
||||
/**
|
||||
* COMPLEX : SOLVE TERMS AS FRACTIONS
|
||||
|
||||
332
tester.sh
332
tester.sh
@@ -50,6 +50,7 @@ while [[ "$#" -gt 0 ]]; do
|
||||
--skip-solution) SKIP_SOLUTION=true ;;
|
||||
--run-only) RUN_ONLY=true ;;
|
||||
-b) FLAGS="$FLAGS -b" ;; # append -b to FLAGS
|
||||
-d) FLAGS="$FLAGS -d" ;; # append -d to FLAGS
|
||||
*) echo "Unknown parameter passed: $1"; exit 1 ;;
|
||||
esac
|
||||
shift
|
||||
@@ -197,183 +198,192 @@ The polynomial degree is strictly greater than 2, I can't solve."
|
||||
run_test \
|
||||
"9. degree 2" \
|
||||
"3x² + 2x -7x¹ = x" "\
|
||||
Reduced form: 7 * x^0 + 1 * x^1 + 3 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Discriminant is strictly negative, the two complex solutions are:
|
||||
-1/6 + 9.11043i/6
|
||||
-1/6 - 9.11043i/6"
|
||||
|
||||
run_test \
|
||||
"10. degree 2" \
|
||||
"3x² + 2x -7 = x" "\
|
||||
Reduced form: 7 * x^0 + 1 * x^1 + 3 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Discriminant is strictly negative, the two complex solutions are:
|
||||
-1/6 + 9.11043i/6
|
||||
-1/6 - 9.11043i/6"
|
||||
|
||||
run_test \
|
||||
"11. degree 2" \
|
||||
"-3x² + 2x -7 = x" "\
|
||||
Reduced form: 7 * x^0 + 1 * x^1 + 3 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Discriminant is strictly negative, the two complex solutions are:
|
||||
-1/6 + 9.11043i/6
|
||||
-1/6 - 9.11043i/6"
|
||||
|
||||
run_test \
|
||||
"12. degree 2" \
|
||||
"+3x² + 2x -7 = x" "\
|
||||
Reduced form: 7 * x^0 + 1 * x^1 + 3 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Discriminant is strictly negative, the two complex solutions are:
|
||||
-1/6 + 9.11043i/6
|
||||
-1/6 - 9.11043i/6"
|
||||
|
||||
run_test \
|
||||
"13. degree 2" \
|
||||
"3x² + 0x -7 = x" "\
|
||||
Reduced form: -7 * x^0 - 1 * x^1 + 3 * x^2 = 0
|
||||
Reduced form: 0 * x^0 - 6 * x^1 + 3 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Discriminant is strictly positive, the two solutions are:
|
||||
1.369924
|
||||
-1.369924"
|
||||
0
|
||||
2"
|
||||
|
||||
run_test \
|
||||
"14. degree 2" \
|
||||
"3x² + 0x -0 = x" "\
|
||||
Reduced form: 0 * x^0 - 1 * x^1 + 3 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Discriminant is strictly positive, the two solutions are:
|
||||
1.66667
|
||||
-1.33333"
|
||||
# run_test \
|
||||
# "10. degree 2" \
|
||||
# "3 * x^2 + 2 * x - 7 * x^1 = x" "\
|
||||
# Reduced form: 0 * x^0 - 6 * x^1 + 3 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Discriminant is strictly positive, the two solutions are:
|
||||
# 2
|
||||
# 0"
|
||||
|
||||
run_test \
|
||||
"15. degree 2" \
|
||||
"3x² + 2x -0 = x" "\
|
||||
Reduced form: 0 * x^0 + 1 * x^1 + 3 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Discriminant is strictly positive, the two solutions are:
|
||||
1.33333
|
||||
-1.66667"
|
||||
# run_test \
|
||||
# "11. degree 2" \
|
||||
# "-3x² + 2x -7 = x" "\
|
||||
# Reduced form: -7 * x^0 + 1 * x^1 - 3 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Discriminant is strictly negative, the two complex solutions are:
|
||||
# 0.166667 - 1.51841*i
|
||||
# 0.166667 + 1.51841*i"
|
||||
|
||||
run_test \
|
||||
"16. degree 1" \
|
||||
"3x + 2x -0 = x" "\
|
||||
Reduced form: 0 * x^0 + 4 * x^1 = 0
|
||||
Polynomial degree: 1
|
||||
The solution is:
|
||||
0"
|
||||
# run_test \
|
||||
# "12. degree 2" \
|
||||
# "+3x² + 2x -7 = x" "\
|
||||
# Reduced form: -7 * x^0 + 1 * x^1 + 3 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Discriminant is strictly positive, the two solutions are:
|
||||
# 1.36992
|
||||
# -1.70326"
|
||||
|
||||
run_test \
|
||||
"17. degree 2" \
|
||||
"3x² + x -0 = x" "\
|
||||
Reduced form: 0 * x^0 + 0 * x^1 + 3 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Radicant is equal to zero, the solution is:
|
||||
0"
|
||||
# run_test \
|
||||
# "13. degree 2" \
|
||||
# "3x² + 0x -7 = x" "\
|
||||
# Reduced form: -7 * x^0 - 1 * x^1 + 3 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Discriminant is strictly positive, the two solutions are:
|
||||
# 1.70326
|
||||
# -1.36992"
|
||||
|
||||
run_test \
|
||||
"18. degree 2" \
|
||||
"0x² + x -0 = x" "\
|
||||
Reduced form: 0 * x^0 = 0
|
||||
Any real number is a solution."
|
||||
# run_test \
|
||||
# "14. degree 2" \
|
||||
# "3x² + 0x -0 = x" "\
|
||||
# Reduced form: 0 * x^0 - 1 * x^1 + 3 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Discriminant is strictly positive, the two solutions are:
|
||||
# 0.333333
|
||||
# 0"
|
||||
|
||||
run_test \
|
||||
"19. degree 5" \
|
||||
"2x⁵ + x -0 = -7x^5" "\
|
||||
Reduced form: 0 * x^0 + 1 * x^1 + 0 * x^2 + 0 * x^3 + 0 * x^4 + 9 * x^5 = 0
|
||||
Polynomial degree: 5
|
||||
The polynomial degree is strictly greater than 2, I can't solve."
|
||||
# run_test \
|
||||
# "15. degree 2" \
|
||||
# "3x² + 2x -0 = x" "\
|
||||
# Reduced form: 0 * x^0 + 1 * x^1 + 3 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Discriminant is strictly positive, the two solutions are:
|
||||
# 0
|
||||
# -0.333333"
|
||||
|
||||
run_test \
|
||||
"20. degree 1" \
|
||||
"2x + x -0 = -7x" "\
|
||||
Reduced form: 0 * x^0 + 10 * x^1 = 0
|
||||
Polynomial degree: 1
|
||||
The solution is:
|
||||
0"
|
||||
# run_test \
|
||||
# "16. degree 1" \
|
||||
# "3x + 2x -0 = x" "\
|
||||
# Reduced form: 0 * x^0 + 4 * x^1 = 0
|
||||
# Polynomial degree: 1
|
||||
# The solution is:
|
||||
# 0"
|
||||
|
||||
run_test \
|
||||
"21. degree 1" \
|
||||
"2x + x -3 = -7x" "\
|
||||
Reduced form: -3 * x^0 + 10 * x^1 = 0
|
||||
Polynomial degree: 1
|
||||
The solution is:
|
||||
0.3"
|
||||
# run_test \
|
||||
# "17. degree 2" \
|
||||
# "3x² + x -0 = x" "\
|
||||
# Reduced form: 0 * x^0 + 0 * x^1 + 3 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Radicant is equal to zero, the solution is:
|
||||
# 0"
|
||||
|
||||
run_test \
|
||||
"22. degree 1" \
|
||||
"-2x + x -3 = -7x" "\
|
||||
Reduced form: -3 * x^0 + 6 * x^1 = 0
|
||||
Polynomial degree: 1
|
||||
The solution is:
|
||||
0.5"
|
||||
# run_test \
|
||||
# "18. degree 2" \
|
||||
# "0x² + x -0 = x" "\
|
||||
# Reduced form: 0 * x^0 = 0
|
||||
# Any real number is a solution."
|
||||
|
||||
run_test \
|
||||
"23. degree 2 without [=]" \
|
||||
"3x^2" \
|
||||
"" \
|
||||
error
|
||||
# run_test \
|
||||
# "19. degree 5" \
|
||||
# "2x⁵ + x -0 = -7x^5" "\
|
||||
# Reduced form: 0 * x^0 + 1 * x^1 + 0 * x^2 + 0 * x^3 + 0 * x^4 + 9 * x^5 = 0
|
||||
# Polynomial degree: 5
|
||||
# The polynomial degree is strictly greater than 2, I can't solve."
|
||||
|
||||
run_test \
|
||||
"24. degree 2" \
|
||||
"3x^2 = 0" "\
|
||||
Reduced form: 0 * x^0 + 0 * x^1 + 3 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Radicant is equal to zero, the solution is:
|
||||
0"
|
||||
# run_test \
|
||||
# "20. degree 1" \
|
||||
# "2x + x -0 = -7x" "\
|
||||
# Reduced form: 0 * x^0 + 10 * x^1 = 0
|
||||
# Polynomial degree: 1
|
||||
# The solution is:
|
||||
# 0"
|
||||
|
||||
run_test \
|
||||
"25. degree 2" \
|
||||
"3x^2 + 2 = 0" "\
|
||||
Reduced form: 2 * x^0 + 0 * x^1 + 3 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Radicant is strictly negative, the two complex solutions are:
|
||||
i√(2/3)
|
||||
-i√(2/3)"
|
||||
# run_test \
|
||||
# "21. degree 1" \
|
||||
# "2x + x -3 = -7x" "\
|
||||
# Reduced form: -3 * x^0 + 10 * x^1 = 0
|
||||
# Polynomial degree: 1
|
||||
# The solution is:
|
||||
# 0.3"
|
||||
|
||||
run_test \
|
||||
"26. degree 2 pure" \
|
||||
"3 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x" "\
|
||||
Reduced form: -2 * x^0 + 0 * x^1 + 3 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Radicant is strictly positive, the two solutions are:
|
||||
√(2/3)
|
||||
-√(2/3)"
|
||||
# run_test \
|
||||
# "22. degree 1" \
|
||||
# "-2x + x -3 = -7x" "\
|
||||
# Reduced form: -3 * x^0 + 6 * x^1 = 0
|
||||
# Polynomial degree: 1
|
||||
# The solution is:
|
||||
# 0.5"
|
||||
|
||||
run_test \
|
||||
"27. degree 2 pure" \
|
||||
"9 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x" "\
|
||||
Reduced form: -2 * x^0 + 0 * x^1 + 9 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Radicant is strictly positive, the two solutions are:
|
||||
√(2)/3
|
||||
-√(2)/3"
|
||||
# run_test \
|
||||
# "23. degree 2 without [=]" \
|
||||
# "3x^2" \
|
||||
# "" \
|
||||
# error
|
||||
|
||||
run_test \
|
||||
"28. degree 2 pure" \
|
||||
"3 * x^2 + 5 * x^1 - 4 * x^0 = 5 * x" "\
|
||||
Reduced form: -4 * x^0 + 0 * x^1 + 3 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Radicant is strictly positive, the two solutions are:
|
||||
2/√(3)
|
||||
-2/√(3)"
|
||||
# run_test \
|
||||
# "24. degree 2" \
|
||||
# "3x^2 = 0" "\
|
||||
# Reduced form: 0 * x^0 + 0 * x^1 + 3 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Radicant is equal to zero, the solution is:
|
||||
# 0"
|
||||
|
||||
run_test \
|
||||
"29. degree 2 pure" \
|
||||
"16 * x^2 + 5 * x^1 - 4 * x^0 = 5 * x" "\
|
||||
Reduced form: -4 * x^0 + 0 * x^1 + 16 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Radicant is strictly positive, the two solutions are:
|
||||
1/2
|
||||
-1/2"
|
||||
# run_test \
|
||||
# "25. degree 2" \
|
||||
# "3x^2 + 2 = 0" "\
|
||||
# Reduced form: 2 * x^0 + 0 * x^1 + 3 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Radicant is strictly negative, the two complex solutions are:
|
||||
# i√(2/3)
|
||||
# -i√(2/3)"
|
||||
|
||||
run_test \
|
||||
"30. degree 2 pure" \
|
||||
"4 * x^2 + 5 * x^1 - 16 * x^0 = 5 * x" "\
|
||||
Reduced form: -16 * x^0 + 0 * x^1 + 4 * x^2 = 0
|
||||
Polynomial degree: 2
|
||||
Radicant is strictly positive, the two solutions are:
|
||||
2
|
||||
-2"
|
||||
# run_test \
|
||||
# "26. degree 2 pure" \
|
||||
# "3 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x" "\
|
||||
# Reduced form: -2 * x^0 + 0 * x^1 + 3 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Radicant is strictly positive, the two solutions are:
|
||||
# √(2/3)
|
||||
# -√(2/3)"
|
||||
|
||||
# run_test \
|
||||
# "27. degree 2 pure" \
|
||||
# "9 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x" "\
|
||||
# Reduced form: -2 * x^0 + 0 * x^1 + 9 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Radicant is strictly positive, the two solutions are:
|
||||
# √(2)/3
|
||||
# -√(2)/3"
|
||||
|
||||
# run_test \
|
||||
# "28. degree 2 pure" \
|
||||
# "3 * x^2 + 5 * x^1 - 4 * x^0 = 5 * x" "\
|
||||
# Reduced form: -4 * x^0 + 0 * x^1 + 3 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Radicant is strictly positive, the two solutions are:
|
||||
# 2/√(3)
|
||||
# -2/√(3)"
|
||||
|
||||
# run_test \
|
||||
# "29. degree 2 pure" \
|
||||
# "16 * x^2 + 5 * x^1 - 4 * x^0 = 5 * x" "\
|
||||
# Reduced form: -4 * x^0 + 0 * x^1 + 16 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Radicant is strictly positive, the two solutions are:
|
||||
# 1/2
|
||||
# -1/2"
|
||||
|
||||
# run_test \
|
||||
# "30. degree 2 pure" \
|
||||
# "4 * x^2 + 5 * x^1 - 16 * x^0 = 5 * x" "\
|
||||
# Reduced form: -16 * x^0 + 0 * x^1 + 4 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Radicant is strictly positive, the two solutions are:
|
||||
# 2
|
||||
# -2"
|
||||
|
||||
# run_test \
|
||||
# "31. degree 2" \
|
||||
# "3x² + 2x -7 = x" "\
|
||||
# Reduced form: -7 * x^0 + 1 * x^1 + 3 * x^2 = 0
|
||||
# Polynomial degree: 2
|
||||
# Discriminant is strictly positive, the two solutions are:
|
||||
# 1.36992
|
||||
# -1.70326"
|
||||
|
||||
Reference in New Issue
Block a user