wip output and solution
This commit is contained in:
29
src/solver.c
29
src/solver.c
@@ -2,6 +2,13 @@
|
||||
|
||||
#include "computorv1.h"
|
||||
|
||||
static double positiv_zero(double num)
|
||||
{
|
||||
if (num == 0)
|
||||
return 0;
|
||||
return num;
|
||||
}
|
||||
|
||||
static int count_decimal_places(double num)
|
||||
{
|
||||
// handle negative numbers
|
||||
@@ -70,18 +77,13 @@ static int find_gcd(double numerator, double denominator)
|
||||
|
||||
static void solve_degree_1(s_solution_degree_1 *solution, double a, double b)
|
||||
{
|
||||
double value;
|
||||
|
||||
solution->a = a;
|
||||
solution->b = b;
|
||||
|
||||
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;
|
||||
solution->solution = positiv_zero(-b / a); // -b / a
|
||||
}
|
||||
|
||||
static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, double c)
|
||||
@@ -94,8 +96,9 @@ 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); // |Δ| == |b² - 4ac|
|
||||
solution->delta_absolute = ft_fabs(delta); // |Δ|
|
||||
solution->delta_sqrt = ft_sqrt(solution->delta_absolute); // √|Δ|
|
||||
delta_sqrt = solution->delta_sqrt;
|
||||
|
||||
@@ -103,25 +106,25 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do
|
||||
solution->first_term_gcd = find_gcd(b, 2 * a); // gcd(b, 2a)
|
||||
solution->first_term_numerator = -b / solution->first_term_gcd; // -b / gcd
|
||||
solution->first_term_denominator = 2 * a / solution->first_term_gcd; // 2a / gcd
|
||||
solution->first_term = -b / (2 * a); // double (-b / 2a)
|
||||
solution->first_term = positiv_zero(-b / (2 * a)); // -b / 2a
|
||||
|
||||
// second term
|
||||
solution->second_term_gcd = find_gcd(delta_sqrt, 2 * a); // gcd(√|Δ|, 2a)
|
||||
solution->second_term_numerator = delta_sqrt / solution->second_term_gcd; // √|Δ| / gcd
|
||||
solution->second_term_denominator = 2 * a / solution->second_term_gcd; // 2a / gcd
|
||||
solution->second_term = delta_sqrt / 2 * a; // √|Δ| / 2a
|
||||
solution->second_term = positiv_zero(delta_sqrt / 2 * a); // √|Δ| / 2a
|
||||
|
||||
// solution
|
||||
solution->solution1 = solution->first_term; // first_term + second_term
|
||||
if (solution->delta_sign == DELTA_ZERO)
|
||||
{
|
||||
solution->solution1 = solution->first_term; // -b / 2a
|
||||
solution->solution2 = NAN; // NAN (no solution)
|
||||
solution->solution1 = positiv_zero(solution->first_term); // -b / 2a
|
||||
solution->solution2 = NAN; // NAN (no solution)
|
||||
}
|
||||
else if (solution->delta_sign == DELTA_MINUS)
|
||||
{
|
||||
solution->solution1 = solution->first_term + solution->second_term; // -b / 2a + i√|Δ| / 2a
|
||||
solution->solution2 = solution->first_term - solution->second_term; // -b / 2a - i√|Δ| / 2a
|
||||
solution->solution1 = positiv_zero(solution->first_term + solution->second_term); // -b / 2a + i√|Δ| / 2a
|
||||
solution->solution2 = positiv_zero(solution->first_term - solution->second_term); // -b / 2a - i√|Δ| / 2a
|
||||
}
|
||||
else if (solution->delta_sign == DELTA_PLUS)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user