From b768ac1a141f853889660dfabdce0ad6e47be034 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 7 May 2026 00:58:54 +0200 Subject: [PATCH] adding modf pow and round --- Makefile | 5 ++++- includes/libft.h | 5 ++++- srcs/{ft_abs_f.c => ft_fabs.c} | 2 +- srcs/ft_modf.c | 23 +++++++++++++++++++++++ srcs/ft_pow.c | 19 +++++++++++++++++++ srcs/ft_round.c | 28 ++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 3 deletions(-) rename srcs/{ft_abs_f.c => ft_fabs.c} (69%) create mode 100644 srcs/ft_modf.c create mode 100644 srcs/ft_pow.c create mode 100644 srcs/ft_round.c diff --git a/Makefile b/Makefile index 3863b5b..ff109da 100644 --- a/Makefile +++ b/Makefile @@ -120,12 +120,15 @@ SRCS = ft_memset.c \ pf_utils.c \ \ ft_abs.c \ - ft_abs_f.c \ + ft_fabs.c \ ft_greater.c \ ft_smaller.c \ ft_sign.c \ ft_sign_f.c \ + ft_modf.c \ + ft_pow.c \ ft_sqrt.c \ + ft_round.c \ ft_free_tab.c \ \ ft_arrint.c diff --git a/includes/libft.h b/includes/libft.h index 83b736d..7f1ffc7 100644 --- a/includes/libft.h +++ b/includes/libft.h @@ -122,11 +122,14 @@ void ft_putnbrendl(int n); void ft_putnbrendl_fd(int n, int fd); char *ft_concat_free(char *str1, char *str2); int ft_abs(int n); -double ft_abs_f(double n); +double ft_fabs(double n); int ft_greater(int a, int b); int ft_smaller(int a, int b); int ft_sign(int i); int ft_sign_f(double i); +double ft_modf(double x, double *int_part); +double ft_pow(double base, int exponent); +double ft_round(double x); int ft_sqrt(int i); void ft_free_tab(char **tab); diff --git a/srcs/ft_abs_f.c b/srcs/ft_fabs.c similarity index 69% rename from srcs/ft_abs_f.c rename to srcs/ft_fabs.c index 4d0121a..45c9223 100644 --- a/srcs/ft_abs_f.c +++ b/srcs/ft_fabs.c @@ -1,6 +1,6 @@ #include "libft.h" -double ft_abs_f(double n) +double ft_fabs(double n) { if (n < 0) n *= -1; diff --git a/srcs/ft_modf.c b/srcs/ft_modf.c new file mode 100644 index 0000000..285a15e --- /dev/null +++ b/srcs/ft_modf.c @@ -0,0 +1,23 @@ +#include "libft.h" + +/** + * Splits a double into its integer and fractional parts, + * it returns the fractional part, + * and stores the integer part in the in_part param (if not null) + * e.g.: + * -3.7 → -3.0 and -0.7 (returns -0.7) + */ +double ft_modf(double x, double *int_part) +{ + // extract the integer part by casting to long long + long long integer = (long long)x; + if (int_part != NULL) + { + *int_part = (double)integer; + } + + // compute the fractional part + double frac_part = x - *int_part; + + return frac_part; +} diff --git a/srcs/ft_pow.c b/srcs/ft_pow.c new file mode 100644 index 0000000..781a887 --- /dev/null +++ b/srcs/ft_pow.c @@ -0,0 +1,19 @@ +#include "libft.h" + +/** + * returns the value of 'base' raised to the power of 'exponent' + */ +double ft_pow(double base, int exponent) +{ + int i; + double result; + + result = 1.0; + i = 0; + while (i < exponent) + { + result *= base; + i++; + } + return result; +} diff --git a/srcs/ft_round.c b/srcs/ft_round.c new file mode 100644 index 0000000..7c70467 --- /dev/null +++ b/srcs/ft_round.c @@ -0,0 +1,28 @@ +#include "libft.h" + +/** + * round x to the nearest integer, + * but round halfway cases away from zero, regardless of the current rounding direction + */ +double ft_round(double x) +{ + // Handle special cases + if (x != x) + { // Check for NaN (NaN is not equal to itself) + return x; // Return NaN + } + if (x == (double)LLONG_MAX + 1.0 || x == (double)LLONG_MIN - 1.0) + { + return x; // Return ±Infinity or the original value if overflow would occur + } + + // Handle positive and negative numbers + if (x >= 0) + { + return (double)(long long)(x + 0.5); + } + else + { + return (double)(long long)(x - 0.5); + } +} \ No newline at end of file