add is_nearly_equal_zero
This commit is contained in:
1
Makefile
1
Makefile
@@ -44,6 +44,7 @@ SRCS = main.c \
|
||||
reducer.c \
|
||||
solver.c \
|
||||
errors.c \
|
||||
math.c \
|
||||
printer_equation.c \
|
||||
printer_solutions.c \
|
||||
print_enums.c \
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
## todo
|
||||
|
||||
- if no "=" sign return error
|
||||
- double is nearly_equal_0
|
||||
|
||||
|
||||
|
||||
@@ -2,13 +2,17 @@
|
||||
#define COMPUTORV1_H
|
||||
|
||||
#include "libft.h"
|
||||
#include <stdio.h> // tmp for printf, for float debug
|
||||
#include <stdio.h> // for printf
|
||||
#include <stdarg.h> // for va_list
|
||||
#include <stdbool.h>
|
||||
#include <errno.h> // for errno
|
||||
#include <string.h> // for strerror
|
||||
|
||||
#include <math.h> // tmp
|
||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* MACROS
|
||||
*/
|
||||
|
||||
#define DOUBLE_PRECISION 0.00001
|
||||
|
||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* MAIN.C
|
||||
@@ -157,6 +161,12 @@ typedef struct
|
||||
|
||||
void solve(const s_polynom *polynom, s_solution *solution);
|
||||
|
||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* UTILS/MATH.C
|
||||
*/
|
||||
|
||||
bool is_nearly_equal_zero(double num);
|
||||
|
||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* UTILS/ERRORS.C
|
||||
*/
|
||||
|
||||
2
libft
2
libft
Submodule libft updated: 85423f4211...e95f55c07f
@@ -29,7 +29,7 @@ static void print_reduced_form_beautify(s_polynom *polynom)
|
||||
exponent = polynom[i].exponent;
|
||||
|
||||
// if term is zero dont output
|
||||
if (coefficient == 0.0)
|
||||
if (is_nearly_equal_zero(coefficient))
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
|
||||
@@ -28,12 +28,12 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution)
|
||||
if (solution.all_int)
|
||||
{
|
||||
// solution 1
|
||||
if (solution.b != 0.0)
|
||||
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);
|
||||
|
||||
// solution 2
|
||||
if (solution.b != 0.0)
|
||||
if (!is_nearly_equal_zero(solution.b))
|
||||
printf("%g/%g - ", solution.b * -1, solution.a * 2);
|
||||
else
|
||||
printf("-");
|
||||
@@ -42,13 +42,13 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution)
|
||||
else
|
||||
{
|
||||
// solution 1
|
||||
if (solution.left_term != 0.0)
|
||||
if (!is_nearly_equal_zero(solution.left_term))
|
||||
printf("%g + ", solution.left_term);
|
||||
if (solution.right_term != 0.0)
|
||||
if (!is_nearly_equal_zero(solution.right_term))
|
||||
printf("i * %g\n", solution.right_term);
|
||||
|
||||
// solution 2
|
||||
if (solution.left_term != 0.0)
|
||||
if (!is_nearly_equal_zero(solution.left_term))
|
||||
printf("%g - ", solution.left_term);
|
||||
else
|
||||
printf("-");
|
||||
|
||||
@@ -45,7 +45,7 @@ int reduce(s_term *terms, s_polynom *polynom, int max_exponent)
|
||||
tmp_coefficient = get_all_terms_with_exponent(terms, max_exponent);
|
||||
|
||||
// skip if coefficient is null or no exponent
|
||||
if (tmp_coefficient == 0.0)
|
||||
if (is_nearly_equal_zero(tmp_coefficient))
|
||||
{
|
||||
max_exponent--;
|
||||
continue;
|
||||
|
||||
22
src/solver.c
22
src/solver.c
@@ -32,20 +32,20 @@ static double positiv_zero(double num)
|
||||
return num;
|
||||
}
|
||||
|
||||
int has_decimal_part(double num)
|
||||
static int has_decimal_part(double num)
|
||||
{
|
||||
return (num != (int)num);
|
||||
}
|
||||
|
||||
int any_has_decimal_part(double *num, size_t len)
|
||||
static bool any_has_decimal_part(double *num, size_t len)
|
||||
{
|
||||
while (len > 0)
|
||||
{
|
||||
if (has_decimal_part(num[len - 1]))
|
||||
return 1;
|
||||
return true;
|
||||
len--;
|
||||
}
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
// find GCD of two integers using euclidean algorithm
|
||||
@@ -81,19 +81,19 @@ 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); // √|Δ|
|
||||
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;
|
||||
|
||||
/**
|
||||
* SOLVE LEFT AND RIGHT TERMS AS DOUBLES
|
||||
*/
|
||||
|
||||
if (b != 0.0)
|
||||
if (!is_nearly_equal_zero(b))
|
||||
solution->left_term = positiv_zero(-b / (2 * a)); // -b / 2a
|
||||
if (delta != 0.0)
|
||||
if (!is_nearly_equal_zero(delta))
|
||||
solution->right_term = positiv_zero(delta_sqrt / 2 * a); // √|Δ| / 2a
|
||||
|
||||
/**
|
||||
@@ -113,7 +113,7 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do
|
||||
solution->right_term_denominator = (a * 2) / gcd;
|
||||
|
||||
// left term
|
||||
if (b == 0.0)
|
||||
if (is_nearly_equal_zero(b))
|
||||
return;
|
||||
gcd = gcd_int((int)b, (int)(a * 2));
|
||||
solution->left_term_numerator = -b / gcd;
|
||||
|
||||
12
src/utils/math.c
Normal file
12
src/utils/math.c
Normal file
@@ -0,0 +1,12 @@
|
||||
/* printer.c */
|
||||
|
||||
#include "computorv1.h"
|
||||
|
||||
bool is_nearly_equal_zero(double num)
|
||||
{
|
||||
if (num > DOUBLE_PRECISION)
|
||||
return false;
|
||||
if (num < -DOUBLE_PRECISION)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user