adding modf pow and round
This commit is contained in:
@@ -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
23
srcs/ft_modf.c
Normal 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
19
srcs/ft_pow.c
Normal 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
28
srcs/ft_round.c
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user