add gcd if all int
This commit is contained in:
67
src/solver.c
67
src/solver.c
@@ -32,6 +32,36 @@ static double positiv_zero(double num)
|
||||
return num;
|
||||
}
|
||||
|
||||
int has_decimal_part(double num)
|
||||
{
|
||||
return (num != (int)num);
|
||||
}
|
||||
|
||||
int any_has_decimal_part(double *num, size_t len)
|
||||
{
|
||||
while (len > 0)
|
||||
{
|
||||
if (has_decimal_part(num[len - 1]))
|
||||
return 1;
|
||||
len--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// find GCD of two integers using euclidean algorithm
|
||||
static int gcd_int(int a, int b)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
while (b != 0)
|
||||
{
|
||||
tmp = b;
|
||||
b = a % b;
|
||||
a = tmp;
|
||||
}
|
||||
return abs(a);
|
||||
}
|
||||
|
||||
static void solve_degree_1(s_solution_degree_1 *solution, double a, double b)
|
||||
{
|
||||
solution->a = a;
|
||||
@@ -44,6 +74,7 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do
|
||||
{
|
||||
double delta;
|
||||
double delta_sqrt;
|
||||
int gcd;
|
||||
|
||||
solution->a = a;
|
||||
solution->b = b;
|
||||
@@ -56,11 +87,37 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do
|
||||
solution->delta_sqrt = ft_sqrt(solution->delta_absolute); // √|Δ|
|
||||
delta_sqrt = solution->delta_sqrt;
|
||||
|
||||
// terms
|
||||
if (b)
|
||||
solution->first_term = positiv_zero(-b / (2 * a)); // -b / 2a
|
||||
if (delta)
|
||||
solution->second_term = positiv_zero(delta_sqrt / 2 * a); // √|Δ| / 2a
|
||||
/**
|
||||
* SOLVE LEFT AND RIGHT TERMS AS DOUBLES
|
||||
*/
|
||||
|
||||
if (b != 0.0)
|
||||
solution->left_term = positiv_zero(-b / (2 * a)); // -b / 2a
|
||||
if (delta != 0.0)
|
||||
solution->right_term = positiv_zero(delta_sqrt / 2 * a); // √|Δ| / 2a
|
||||
|
||||
/**
|
||||
* COMPLEX : SOLVE TERMS AS FRACTIONS
|
||||
*/
|
||||
|
||||
// check
|
||||
if (solution->delta_sign != DELTA_MINUS)
|
||||
return;
|
||||
if (any_has_decimal_part((double[]){b, a, solution->delta_sqrt}, 3))
|
||||
return;
|
||||
solution->all_int = true;
|
||||
|
||||
// right term
|
||||
gcd = gcd_int((int)solution->delta_sqrt, (int)(a * 2));
|
||||
solution->right_term_numerator = solution->delta_sqrt / gcd;
|
||||
solution->right_term_denominator = (a * 2) / gcd;
|
||||
|
||||
// left term
|
||||
if (b == 0.0)
|
||||
return;
|
||||
gcd = gcd_int((int)b, (int)(a * 2));
|
||||
solution->left_term_numerator = -b / gcd;
|
||||
solution->left_term_denominator = (a * 2) / gcd;
|
||||
}
|
||||
|
||||
void solve(const s_polynom *polynom, s_solution *solution)
|
||||
|
||||
Reference in New Issue
Block a user