adding modf pow and round

This commit is contained in:
hugogogo
2026-05-07 00:58:54 +02:00
parent 78f6a2d9ba
commit b768ac1a14
6 changed files with 79 additions and 3 deletions

View File

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

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