add is_nearly_equal_zero
This commit is contained in:
1
Makefile
1
Makefile
@@ -44,6 +44,7 @@ SRCS = main.c \
|
|||||||
reducer.c \
|
reducer.c \
|
||||||
solver.c \
|
solver.c \
|
||||||
errors.c \
|
errors.c \
|
||||||
|
math.c \
|
||||||
printer_equation.c \
|
printer_equation.c \
|
||||||
printer_solutions.c \
|
printer_solutions.c \
|
||||||
print_enums.c \
|
print_enums.c \
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
## todo
|
## todo
|
||||||
|
|
||||||
- if no "=" sign return error
|
|
||||||
- double is nearly_equal_0
|
- double is nearly_equal_0
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,17 @@
|
|||||||
#define COMPUTORV1_H
|
#define COMPUTORV1_H
|
||||||
|
|
||||||
#include "libft.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 <stdarg.h> // for va_list
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <errno.h> // for errno
|
#include <errno.h> // for errno
|
||||||
#include <string.h> // for strerror
|
#include <string.h> // for strerror
|
||||||
|
|
||||||
#include <math.h> // tmp
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
* MACROS
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define DOUBLE_PRECISION 0.00001
|
||||||
|
|
||||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* MAIN.C
|
* MAIN.C
|
||||||
@@ -157,6 +161,12 @@ typedef struct
|
|||||||
|
|
||||||
void solve(const s_polynom *polynom, s_solution *solution);
|
void solve(const s_polynom *polynom, s_solution *solution);
|
||||||
|
|
||||||
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
* UTILS/MATH.C
|
||||||
|
*/
|
||||||
|
|
||||||
|
bool is_nearly_equal_zero(double num);
|
||||||
|
|
||||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* UTILS/ERRORS.C
|
* 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;
|
exponent = polynom[i].exponent;
|
||||||
|
|
||||||
// if term is zero dont output
|
// if term is zero dont output
|
||||||
if (coefficient == 0.0)
|
if (is_nearly_equal_zero(coefficient))
|
||||||
{
|
{
|
||||||
i++;
|
i++;
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -28,12 +28,12 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution)
|
|||||||
if (solution.all_int)
|
if (solution.all_int)
|
||||||
{
|
{
|
||||||
// solution 1
|
// solution 1
|
||||||
if (solution.b != 0.0)
|
if (!is_nearly_equal_zero(solution.b))
|
||||||
printf("%g/%g + ", solution.b * -1, solution.a * 2);
|
printf("%g/%g + ", solution.b * -1, solution.a * 2);
|
||||||
printf("%gi/%g\n", solution.delta_sqrt, solution.a * 2);
|
printf("%gi/%g\n", solution.delta_sqrt, solution.a * 2);
|
||||||
|
|
||||||
// solution 2
|
// solution 2
|
||||||
if (solution.b != 0.0)
|
if (!is_nearly_equal_zero(solution.b))
|
||||||
printf("%g/%g - ", solution.b * -1, solution.a * 2);
|
printf("%g/%g - ", solution.b * -1, solution.a * 2);
|
||||||
else
|
else
|
||||||
printf("-");
|
printf("-");
|
||||||
@@ -42,13 +42,13 @@ static void print_solution_delta_negativ(s_solution_degree_2 solution)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// solution 1
|
// solution 1
|
||||||
if (solution.left_term != 0.0)
|
if (!is_nearly_equal_zero(solution.left_term))
|
||||||
printf("%g + ", 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);
|
printf("i * %g\n", solution.right_term);
|
||||||
|
|
||||||
// solution 2
|
// solution 2
|
||||||
if (solution.left_term != 0.0)
|
if (!is_nearly_equal_zero(solution.left_term))
|
||||||
printf("%g - ", solution.left_term);
|
printf("%g - ", solution.left_term);
|
||||||
else
|
else
|
||||||
printf("-");
|
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);
|
tmp_coefficient = get_all_terms_with_exponent(terms, max_exponent);
|
||||||
|
|
||||||
// skip if coefficient is null or no exponent
|
// skip if coefficient is null or no exponent
|
||||||
if (tmp_coefficient == 0.0)
|
if (is_nearly_equal_zero(tmp_coefficient))
|
||||||
{
|
{
|
||||||
max_exponent--;
|
max_exponent--;
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
16
src/solver.c
16
src/solver.c
@@ -32,20 +32,20 @@ static double positiv_zero(double num)
|
|||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
int has_decimal_part(double num)
|
static int has_decimal_part(double num)
|
||||||
{
|
{
|
||||||
return (num != (int)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)
|
while (len > 0)
|
||||||
{
|
{
|
||||||
if (has_decimal_part(num[len - 1]))
|
if (has_decimal_part(num[len - 1]))
|
||||||
return 1;
|
return true;
|
||||||
len--;
|
len--;
|
||||||
}
|
}
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// find GCD of two integers using euclidean algorithm
|
// find GCD of two integers using euclidean algorithm
|
||||||
@@ -84,16 +84,16 @@ static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, do
|
|||||||
solution->delta = delta; // Δ == b² - 4ac
|
solution->delta = delta; // Δ == b² - 4ac
|
||||||
solution->delta_sign = ft_fsign(delta); // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO
|
solution->delta_sign = ft_fsign(delta); // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO
|
||||||
solution->delta_absolute = ft_fabs(delta); // |Δ|
|
solution->delta_absolute = ft_fabs(delta); // |Δ|
|
||||||
solution->delta_sqrt = ft_sqrt(solution->delta_absolute); // √|Δ|
|
solution->delta_sqrt = ft_sqrt(solution->delta_absolute, DOUBLE_PRECISION); // √|Δ|
|
||||||
delta_sqrt = solution->delta_sqrt;
|
delta_sqrt = solution->delta_sqrt;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SOLVE LEFT AND RIGHT TERMS AS DOUBLES
|
* 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
|
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
|
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;
|
solution->right_term_denominator = (a * 2) / gcd;
|
||||||
|
|
||||||
// left term
|
// left term
|
||||||
if (b == 0.0)
|
if (is_nearly_equal_zero(b))
|
||||||
return;
|
return;
|
||||||
gcd = gcd_int((int)b, (int)(a * 2));
|
gcd = gcd_int((int)b, (int)(a * 2));
|
||||||
solution->left_term_numerator = -b / gcd;
|
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