solver first part
This commit is contained in:
1
Makefile
1
Makefile
@@ -52,6 +52,7 @@ CC = gcc
|
|||||||
EXT = c
|
EXT = c
|
||||||
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g3
|
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g3
|
||||||
LFLAGS = -L$(D_LIB) -lft
|
LFLAGS = -L$(D_LIB) -lft
|
||||||
|
LFLAGS += -lm
|
||||||
|
|
||||||
# AUTOMATICALLY CREATED :
|
# AUTOMATICALLY CREATED :
|
||||||
D_OBJS = builds
|
D_OBJS = builds
|
||||||
|
|||||||
12
README.md
12
README.md
@@ -51,13 +51,13 @@ this project uses submodules (maybe recursively), so either :
|
|||||||
-> solution :
|
-> solution :
|
||||||
- delta_sign; // + or -
|
- delta_sign; // + or -
|
||||||
- delta_absolute; // |Δ|
|
- delta_absolute; // |Δ|
|
||||||
- first_term_pgcd; // pgcd(b, 2a)
|
- first_term_gcd; // gcd(b, 2a)
|
||||||
- first_term_numerator; // -b / pgcd
|
- first_term_numerator; // -b / gcd
|
||||||
- first_term_denominator; // 2a / pgcd
|
- first_term_denominator; // 2a / gcd
|
||||||
- first_term; // double (-b / 2a)
|
- first_term; // double (-b / 2a)
|
||||||
- second_term_pgcd; // pgcd(√|Δ|, 2a)
|
- second_term_gcd; // gcd(√|Δ|, 2a)
|
||||||
- second_term_numerator; // √|Δ| / pgcd
|
- second_term_numerator; // √|Δ| / gcd
|
||||||
- second_term_denominator; // 2a / pgcd
|
- second_term_denominator; // 2a / gcd
|
||||||
- second_term; // double (√|Δ| / 2a)
|
- 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
|
||||||
|
|||||||
@@ -10,6 +10,8 @@
|
|||||||
#include <errno.h> // for errno
|
#include <errno.h> // for errno
|
||||||
#include <string.h> // for strerror
|
#include <string.h> // for strerror
|
||||||
|
|
||||||
|
#include <math.h> // tmp
|
||||||
|
|
||||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* MAIN.C
|
* MAIN.C
|
||||||
*/
|
*/
|
||||||
@@ -117,13 +119,13 @@ 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
|
||||||
double delta_absolute; // |Δ| == |b² - 4ac|
|
double delta_absolute; // |Δ| == |b² - 4ac|
|
||||||
int first_term_pgcd; // pgcd(b, 2a)
|
int first_term_gcd; // gcd(b, 2a)
|
||||||
int first_term_numerator; // -b / pgcd
|
int first_term_numerator; // -b / gcd
|
||||||
int first_term_denominator; // 2a / pgcd
|
int first_term_denominator; // 2a / gcd
|
||||||
double first_term; // double (-b / 2a)
|
double first_term; // double (-b / 2a)
|
||||||
int second_term_pgcd; // pgcd(√|Δ|, 2a)
|
int second_term_gcd; // gcd(√|Δ|, 2a)
|
||||||
int second_term_numerator; // √|Δ| / pgcd
|
int second_term_numerator; // √|Δ| / gcd
|
||||||
int second_term_denominator; // 2a / pgcd
|
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
|
||||||
|
|||||||
2
libft
2
libft
Submodule libft updated: f93c635234...b768ac1a14
@@ -132,6 +132,8 @@ void launch_computorv1(char *input)
|
|||||||
polynom_fill_null(polynom, max_exponent);
|
polynom_fill_null(polynom, max_exponent);
|
||||||
reduce(terms, polynom);
|
reduce(terms, polynom);
|
||||||
|
|
||||||
|
// print before solution
|
||||||
|
|
||||||
// solve
|
// solve
|
||||||
s_solution solution[1];
|
s_solution solution[1];
|
||||||
solution_g_err = solution;
|
solution_g_err = solution;
|
||||||
|
|||||||
92
src/solver.c
92
src/solver.c
@@ -2,16 +2,82 @@
|
|||||||
|
|
||||||
#include "computorv1.h"
|
#include "computorv1.h"
|
||||||
|
|
||||||
|
static int count_decimal_places(double num)
|
||||||
|
{
|
||||||
|
// handle negative numbers
|
||||||
|
num = ft_fabs(num);
|
||||||
|
|
||||||
|
// isolate the fractional part
|
||||||
|
double int_part;
|
||||||
|
double frac_part = ft_modf(num, &int_part);
|
||||||
|
|
||||||
|
// if there is no fractional part, return 0
|
||||||
|
if (frac_part == 0.0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// count the number of decimal places
|
||||||
|
int count = 0;
|
||||||
|
const int max_decimals = 15; // prevent infinite loops for irrational numbers
|
||||||
|
while (count < max_decimals)
|
||||||
|
{
|
||||||
|
double dummy_frac_part = ft_modf(frac_part, NULL);
|
||||||
|
if (dummy_frac_part == 0.0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
frac_part *= 10;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// find GCD of two integers (Euclidean algorithm)
|
||||||
|
static int gcd_int(int a, int b)
|
||||||
|
{
|
||||||
|
while (b != 0)
|
||||||
|
{
|
||||||
|
int temp = b;
|
||||||
|
b = a % b;
|
||||||
|
a = temp;
|
||||||
|
}
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
// find GCD of two doubles
|
||||||
|
static int find_gcd(double numerator, double denominator)
|
||||||
|
{
|
||||||
|
// count decimal places for both numbers (eg. 2.5 -> 1 , and 1.12345 -> 5)
|
||||||
|
int num_decimals = count_decimal_places(numerator);
|
||||||
|
int den_decimals = count_decimal_places(denominator);
|
||||||
|
|
||||||
|
// use the maximum precision (eg. 5)
|
||||||
|
int max_decimals = ft_greater(num_decimals, den_decimals);
|
||||||
|
|
||||||
|
// scale the numbers to integers (eg. 2.5*100000=250000 and 1.12345*100000=112345)
|
||||||
|
// using ft_round because floating-point multiplication can introduce tiny precision errors
|
||||||
|
// (e.g., 0.3 * 10 = 2.9999999999999996 instead of 3.0).
|
||||||
|
// rounding ensures these errors are corrected before converting to integers.
|
||||||
|
double scale = ft_pow(10, max_decimals);
|
||||||
|
int num_scaled = (int)ft_round(numerator * scale);
|
||||||
|
int den_scaled = (int)ft_round(denominator * scale);
|
||||||
|
|
||||||
|
// compute GCD of the scaled integers
|
||||||
|
return gcd_int(abs(num_scaled), ft_abs(den_scaled));
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
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
|
||||||
double delta_absolute; // |Δ| == |b² - 4ac|
|
double delta_absolute; // |Δ| == |b² - 4ac|
|
||||||
int first_term_pgcd; // pgcd(b, 2a)
|
int first_term_gcd; // gcd(b, 2a)
|
||||||
int first_term_numerator; // -b / pgcd
|
int first_term_numerator; // -b / gcd
|
||||||
int first_term_denominator; // 2a / pgcd
|
int first_term_denominator; // 2a / gcd
|
||||||
double first_term; // double (-b / 2a)
|
double first_term; // double (-b / 2a)
|
||||||
int second_term_pgcd; // pgcd(√|Δ|, 2a)
|
int second_term_gcd; // gcd(√|Δ|, 2a)
|
||||||
int second_term_numerator; // √|Δ| / pgcd
|
int second_term_numerator; // √|Δ| / gcd
|
||||||
int second_term_denominator; // 2a / pgcd
|
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
|
||||||
@@ -28,13 +94,13 @@ void solve(const double *polynom, s_solution *solution)
|
|||||||
b = polynom[1];
|
b = polynom[1];
|
||||||
c = polynom[0];
|
c = polynom[0];
|
||||||
delta = b * b - 4 * a * c;
|
delta = b * b - 4 * a * c;
|
||||||
solution->delta_sign = ft_sign(delta);
|
solution->delta_sign = ft_sign_f(delta);
|
||||||
solution->delta_absolute = ft_abs(delta);
|
solution->delta_absolute = ft_fabs(delta);
|
||||||
// solution->first_term_pgcd = find_pgcd(b, 2 * a);
|
solution->first_term_gcd = find_gcd(b, 2 * a);
|
||||||
// solution->first_term_numerator = ;
|
solution->first_term_numerator = -b / solution->first_term_gcd;
|
||||||
// solution->first_term_denominator = ;
|
solution->first_term_denominator = 2 * a / solution->first_term_gcd;
|
||||||
// solution->first_term = ;
|
solution->first_term = -b / (2 * a);
|
||||||
// solution->second_term_pgcd = ;
|
// solution->second_term_gcd = ;
|
||||||
// solution->second_term_numerator = ;
|
// solution->second_term_numerator = ;
|
||||||
// solution->second_term_denominator = ;
|
// solution->second_term_denominator = ;
|
||||||
// solution->second_term = ;
|
// solution->second_term = ;
|
||||||
|
|||||||
@@ -73,11 +73,11 @@ static void print_context_solution()
|
|||||||
{
|
{
|
||||||
dprintf(STDERR_FILENO, "delta_sign : %25s\n", delta_sign_to_str(solution_g_err->delta_sign));
|
dprintf(STDERR_FILENO, "delta_sign : %25s\n", delta_sign_to_str(solution_g_err->delta_sign));
|
||||||
dprintf(STDERR_FILENO, "delta_absolute : %25g\n", solution_g_err->delta_absolute);
|
dprintf(STDERR_FILENO, "delta_absolute : %25g\n", solution_g_err->delta_absolute);
|
||||||
// dprintf(STDERR_FILENO, "first_term_pgcd : %25g\n", solution_g_err->first_term_pgcd);
|
dprintf(STDERR_FILENO, "first_term_gcd : %25i\n", solution_g_err->first_term_gcd);
|
||||||
// dprintf(STDERR_FILENO, "first_term_numerator : %25g\n", solution_g_err->first_term_numerator);
|
dprintf(STDERR_FILENO, "first_term_numerator : %25i\n", solution_g_err->first_term_numerator);
|
||||||
// dprintf(STDERR_FILENO, "first_term_denominator : %25g\n", solution_g_err->first_term_denominator);
|
dprintf(STDERR_FILENO, "first_term_denominator : %25i\n", solution_g_err->first_term_denominator);
|
||||||
// dprintf(STDERR_FILENO, "first_term : %25g\n", solution_g_err->first_term);
|
dprintf(STDERR_FILENO, "first_term : %25g\n", solution_g_err->first_term);
|
||||||
// dprintf(STDERR_FILENO, "second_term_pgcd : %25g\n", solution_g_err->second_term_pgcd);
|
// dprintf(STDERR_FILENO, "second_term_gcd : %25g\n", solution_g_err->second_term_gcd);
|
||||||
// dprintf(STDERR_FILENO, "second_term_numerator : %25g\n", solution_g_err->second_term_numerator);
|
// dprintf(STDERR_FILENO, "second_term_numerator : %25g\n", solution_g_err->second_term_numerator);
|
||||||
// dprintf(STDERR_FILENO, "second_term_denominator: %25g\n", solution_g_err->second_term_denominator);
|
// dprintf(STDERR_FILENO, "second_term_denominator: %25g\n", solution_g_err->second_term_denominator);
|
||||||
// dprintf(STDERR_FILENO, "second_term : %25g\n", solution_g_err->second_term);
|
// dprintf(STDERR_FILENO, "second_term : %25g\n", solution_g_err->second_term);
|
||||||
|
|||||||
Reference in New Issue
Block a user