From 6ac47429e8f6e281b741c9970b0bef9568db8291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Wed, 25 Sep 2024 12:53:28 +0200 Subject: [PATCH] feat: use `double` instead of `float` for better precision BREAKING CHANGE: Functions signatures changed in the `mathematics` module. BREAKING CHANGE: Renamed `MATHEMATICS_FLOAT_PRECISION` to `MATHEMATICS_DOUBLE_PRECISION` --- lib/mathematics.c | 12 ++++++------ lib/mathematics.h | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/mathematics.c b/lib/mathematics.c index 957880a..158316f 100644 --- a/lib/mathematics.c +++ b/lib/mathematics.c @@ -1,7 +1,7 @@ #include "mathematics.h" -bool mathematics_equals(const float number1, const float number2) { - return (number1 - number2) < MATHEMATICS_FLOAT_PRECISION; +bool mathematics_equals(const double number1, const double number2) { + return (number1 - number2) < MATHEMATICS_DOUBLE_PRECISION; } unsigned long long mathematics_absolute_value(const long long number) { @@ -15,9 +15,9 @@ unsigned long long mathematics_pow(unsigned long long base, unsigned long long e return exponent == 0 ? 1 : base * mathematics_pow(base, exponent - 1); } -float mathematics_root(float number, unsigned int nth_root) { - float result = number; - float previous_result = 0; +double mathematics_root(double number, unsigned int nth_root) { + double result = number; + double previous_result = 0; while (!mathematics_equals(result, previous_result)) { result = (((nth_root - 1) * previous_result) + (number / mathematics_pow(result, nth_root - 1))) / nth_root; previous_result = result; @@ -25,7 +25,7 @@ float mathematics_root(float number, unsigned int nth_root) { return result; } -float mathematics_square_root(float number) { +double mathematics_square_root(double number) { return mathematics_root(number, 2); } diff --git a/lib/mathematics.h b/lib/mathematics.h index 86ab87e..66b56f7 100644 --- a/lib/mathematics.h +++ b/lib/mathematics.h @@ -1,7 +1,7 @@ #ifndef __LIBCPROJECT_MATHEMATICS__ #define __LIBCPROJECT_MATHEMATICS__ -#define MATHEMATICS_FLOAT_PRECISION 0.00000001 +#define MATHEMATICS_DOUBLE_PRECISION 0.0000000001 #include #include @@ -18,7 +18,7 @@ * @return false * @since v1.0.0 */ -bool mathematics_equals(const float number1, const float number2); +bool mathematics_equals(const double number1, const double number2); /** * @brief Get the absolute value of a number. @@ -44,19 +44,19 @@ unsigned long long mathematics_pow(unsigned long long base, unsigned long long e * * @param number * @param nth_root - * @return float + * @return double * @since v1.0.0 */ -float mathematics_root(float number, unsigned int nth_root); +double mathematics_root(double number, unsigned int nth_root); /** * @brief Calculates the square root of a number using Heron's method. * * @param number - * @return float + * @return double * @since v1.0.0 */ -float mathematics_square_root(float number); +double mathematics_square_root(double number); /** * @brief Calculates the factorial of a number.