1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-12-11 21:13:00 +01:00

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`
This commit is contained in:
Théo LUDWIG 2024-09-25 12:53:28 +02:00
parent b3c17983b3
commit 6ac47429e8
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
2 changed files with 12 additions and 12 deletions

View File

@ -1,7 +1,7 @@
#include "mathematics.h" #include "mathematics.h"
bool mathematics_equals(const float number1, const float number2) { bool mathematics_equals(const double number1, const double number2) {
return (number1 - number2) < MATHEMATICS_FLOAT_PRECISION; return (number1 - number2) < MATHEMATICS_DOUBLE_PRECISION;
} }
unsigned long long mathematics_absolute_value(const long long number) { 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); return exponent == 0 ? 1 : base * mathematics_pow(base, exponent - 1);
} }
float mathematics_root(float number, unsigned int nth_root) { double mathematics_root(double number, unsigned int nth_root) {
float result = number; double result = number;
float previous_result = 0; double previous_result = 0;
while (!mathematics_equals(result, previous_result)) { while (!mathematics_equals(result, previous_result)) {
result = (((nth_root - 1) * previous_result) + (number / mathematics_pow(result, nth_root - 1))) / nth_root; result = (((nth_root - 1) * previous_result) + (number / mathematics_pow(result, nth_root - 1))) / nth_root;
previous_result = result; previous_result = result;
@ -25,7 +25,7 @@ float mathematics_root(float number, unsigned int nth_root) {
return result; return result;
} }
float mathematics_square_root(float number) { double mathematics_square_root(double number) {
return mathematics_root(number, 2); return mathematics_root(number, 2);
} }

View File

@ -1,7 +1,7 @@
#ifndef __LIBCPROJECT_MATHEMATICS__ #ifndef __LIBCPROJECT_MATHEMATICS__
#define __LIBCPROJECT_MATHEMATICS__ #define __LIBCPROJECT_MATHEMATICS__
#define MATHEMATICS_FLOAT_PRECISION 0.00000001 #define MATHEMATICS_DOUBLE_PRECISION 0.0000000001
#include <errno.h> #include <errno.h>
#include <stdbool.h> #include <stdbool.h>
@ -18,7 +18,7 @@
* @return false * @return false
* @since v1.0.0 * @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. * @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 number
* @param nth_root * @param nth_root
* @return float * @return double
* @since v1.0.0 * @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. * @brief Calculates the square root of a number using Heron's method.
* *
* @param number * @param number
* @return float * @return double
* @since v1.0.0 * @since v1.0.0
*/ */
float mathematics_square_root(float number); double mathematics_square_root(double number);
/** /**
* @brief Calculates the factorial of a number. * @brief Calculates the factorial of a number.