➕ Math Utilities Module
## 🆕 New Math Functions - 🔢 Added `ft_max` → returns the maximum of two values. - 🔢 Added `ft_min` → returns the minimum of two values. - ✖️ Added `ft_power` → computes exponentiation. - ➖ Added `ft_abs` → returns absolute value. - √ Added `ft_sqrt` → computes integer square root. - 📄 Created `math.h` to declare and document all math functions. ## 🛠️ Build System Integration - 🧩 Updated the `Makefile` to include the new `math/` sources so they compile and link with the rest of the project. ## ⚡ Impact - Project now includes a **dedicated math utility module**. - Provides essential helper functions for broader use in future features. - Cleaner organization with centralized declarations in `math.h`.
This commit is contained in:
commit
d1d78c8d7d
9 changed files with 178 additions and 5 deletions
7
Makefile
7
Makefile
|
|
@ -6,7 +6,7 @@
|
|||
# By: rparodi <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# 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 \
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
53
includes/math.h
Normal file
53
includes/math.h
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* math.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stdint.h>
|
||||
|
||||
/**
|
||||
* @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
|
||||
21
math/ft_abs.c
Normal file
21
math/ft_abs.c
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_abs.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/17 16:37:35 by rparodi #+# #+# */
|
||||
/* Updated: 2025/09/17 16:39:43 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t ft_abs(int64_t value)
|
||||
{
|
||||
if (value < 0)
|
||||
return (-value);
|
||||
else
|
||||
return (value);
|
||||
}
|
||||
18
math/ft_max.c
Normal file
18
math/ft_max.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_max.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/17 16:23:22 by rparodi #+# #+# */
|
||||
/* Updated: 2025/09/17 16:24:17 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t ft_max(uint32_t x, uint32_t y)
|
||||
{
|
||||
return (x ^ ((x ^ y) & -(x < y)));
|
||||
}
|
||||
18
math/ft_min.c
Normal file
18
math/ft_min.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_min.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/17 16:20:11 by rparodi #+# #+# */
|
||||
/* Updated: 2025/09/17 16:25:27 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t ft_min(uint32_t x, uint32_t y)
|
||||
{
|
||||
return (y ^ ((x ^ y) & -(x < y)));
|
||||
}
|
||||
30
math/ft_power.c
Normal file
30
math/ft_power.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_power.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/17 16:08:00 by rparodi #+# #+# */
|
||||
/* Updated: 2025/09/17 16:12:39 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
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);
|
||||
}
|
||||
28
math/ft_sqrt.c
Normal file
28
math/ft_sqrt.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_sqrt.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/17 16:30:27 by rparodi #+# #+# */
|
||||
/* Updated: 2025/09/17 16:44:28 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue