Files
42_INT_01_libft/srcs/ft_modf.c
2026-05-07 00:58:54 +02:00

24 lines
521 B
C

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