diff --git a/Makefile b/Makefile index cb9d296..1b32f0e 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2025/09/05 16:21:13 by rparodi ### ########.fr # +# Updated: 2025/09/17 16:39:23 by rparodi ### ########.fr # # # # **************************************************************************** # @@ -49,6 +49,11 @@ SRC = char/ft_isalnum.c \ list/ft_lstmap.c \ list/ft_lstnew.c \ list/ft_lstsize.c \ + math/ft_max.c \ + math/ft_min.c \ + math/ft_power.c \ + math/ft_abs.c \ + math/ft_sqrt.c \ memory/ft_bzero.c \ memory/ft_calloc.c \ memory/ft_memchr.c \ diff --git a/includes/convert.h b/includes/convert.h index d7867c3..6f57322 100644 --- a/includes/convert.h +++ b/includes/convert.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/31 14:57:24 by rparodi #+# #+# */ -/* Updated: 2025/09/08 10:17:01 by rparodi ### ########.fr */ +/* Updated: 2025/09/17 16:40:58 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,7 @@ int ft_atoi(const char *nptr); * @param nptr the string that will be converted * @return The unsigned long long on the string */ -size_t ft_atou(const char *nptr); +size_t ft_atou(const char *nptr); /** * @brief Converts string to long long integer diff --git a/includes/math.h b/includes/math.h new file mode 100644 index 0000000..06f742b --- /dev/null +++ b/includes/math.h @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* math.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/09/17 16:14:25 by rparodi #+# #+# */ +/* Updated: 2025/09/17 16:44:55 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MATH_H +# define MATH_H + +# include + +/** + * @brief Perform a power calculation + * + * @param base The element to calculate the power + * @param exponent The power to calculate + * @return The result of the operation + */ +uint64_t ft_power(uint64_t base, uint64_t exponent); + +/** + * @brief Select the maximum of the two parameters + * + * @param x First element + * @param y Second element + * @return The maximum of the two parameters + */ +uint32_t ft_min(uint32_t x, uint32_t y); + +/** + * @brief Select the minimum of the two parameters + * + * @param x First element + * @param y Second element + * @return The minimum of the two parameters + */ +uint32_t ft_min(uint32_t x, uint32_t y); + +/** + * @brief Perform a Root sqare calculation + * + * @param number The element to calculate + * @return The square root of the parameters + */ +uint64_t ft_sqrt(uint64_t number); + +#endif diff --git a/math/ft_abs.c b/math/ft_abs.c new file mode 100644 index 0000000..e3beef1 --- /dev/null +++ b/math/ft_abs.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_abs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/09/17 16:37:35 by rparodi #+# #+# */ +/* Updated: 2025/09/17 16:39:43 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +uint32_t ft_abs(int64_t value) +{ + if (value < 0) + return (-value); + else + return (value); +} diff --git a/math/ft_max.c b/math/ft_max.c new file mode 100644 index 0000000..6d9c1d7 --- /dev/null +++ b/math/ft_max.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_max.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/09/17 16:23:22 by rparodi #+# #+# */ +/* Updated: 2025/09/17 16:24:17 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +uint32_t ft_max(uint32_t x, uint32_t y) +{ + return (x ^ ((x ^ y) & -(x < y))); +} diff --git a/math/ft_min.c b/math/ft_min.c new file mode 100644 index 0000000..09d0fe5 --- /dev/null +++ b/math/ft_min.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_min.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/09/17 16:20:11 by rparodi #+# #+# */ +/* Updated: 2025/09/17 16:25:27 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +uint32_t ft_min(uint32_t x, uint32_t y) +{ + return (y ^ ((x ^ y) & -(x < y))); +} diff --git a/math/ft_power.c b/math/ft_power.c new file mode 100644 index 0000000..5a15e5d --- /dev/null +++ b/math/ft_power.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_power.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/09/17 16:08:00 by rparodi #+# #+# */ +/* Updated: 2025/09/17 16:12:39 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +uint64_t ft_power(uint64_t base, uint64_t exponent) +{ + uint64_t i; + uint64_t result; + + i = 0; + result = 1; + if (exponent == 0) + return (1); + while (i < exponent) + { + result *= base; + i++; + } + return (result); +} diff --git a/math/ft_sqrt.c b/math/ft_sqrt.c new file mode 100644 index 0000000..477c124 --- /dev/null +++ b/math/ft_sqrt.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sqrt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/09/17 16:30:27 by rparodi #+# #+# */ +/* Updated: 2025/09/17 16:44:28 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +uint64_t ft_sqrt(uint64_t number) +{ + uint64_t i; + + i = 1; + while ((i * i) <= number && i <= 4294967296) + { + if ((i * i) == number) + return (i); + i++; + } + return (0); +} diff --git a/test/convert/test_itoa.c b/test/convert/test_itoa.c index b84d08b..47303a1 100644 --- a/test/convert/test_itoa.c +++ b/test/convert/test_itoa.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/09/04 16:54:42 by rparodi #+# #+# */ -/* Updated: 2025/09/08 10:09:06 by rparodi ### ########.fr */ +/* Updated: 2025/09/17 16:40:41 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int main(void) { - const int integer[] = {0, 42, -42, +-42, -2147483648, 2147483647, 13, 7}; + const int integer[] = {0, 42, -42, + -42, -2147483648, 2147483647, 13, 7}; char result[1024]; size_t i;