fix complex sign operation
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user