Compare commits

..

2 Commits

Author SHA1 Message Date
hugogogo
b768ac1a14 adding modf pow and round 2026-05-07 00:58:54 +02:00
hugogogo
78f6a2d9ba adding sign and abs for doubles 2026-05-06 22:54:20 +02:00
9 changed files with 98 additions and 2 deletions

View File

@@ -120,10 +120,15 @@ SRCS = ft_memset.c \
pf_utils.c \
\
ft_abs.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

View File

@@ -122,9 +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_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);

View File

@@ -1,6 +1,6 @@
#include "libft.h"
int ft_abs(int n)
int ft_abs(int n)
{
if (n < 0)
n *= -1;

8
srcs/ft_fabs.c Normal file
View File

@@ -0,0 +1,8 @@
#include "libft.h"
double ft_fabs(double n)
{
if (n < 0)
n *= -1;
return (n);
}

23
srcs/ft_modf.c Normal file
View File

@@ -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;
}

19
srcs/ft_pow.c Normal file
View File

@@ -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;
}

28
srcs/ft_round.c Normal file
View File

@@ -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);
}
}

View File

@@ -1,6 +1,6 @@
#include "libft.h"
int ft_sign(int i)
int ft_sign(int i)
{
if (i < 0)
return (-1);

8
srcs/ft_sign_f.c Normal file
View File

@@ -0,0 +1,8 @@
#include "libft.h"
int ft_sign_f(double i)
{
if (i < 0)
return (-1);
return (1);
}