Compare commits

..

1 Commits

Author SHA1 Message Date
hugogogo
45cc208f44 fix abs family for -0 value 2026-05-10 20:18:37 +02:00
9 changed files with 44 additions and 20 deletions

View File

@@ -12,7 +12,7 @@ _DEP = libft.h
DEPS = $(_DEP:%.h=$(IDIR)/%.h)
CFLAGS = -I$(IDIR)
CFLAGS += -Wall -Wextra -Werror -g
CFLAGS += -Wall -Wextra -Werror -g3
SRCS = ft_memset.c \
ft_bzero.c \
@@ -128,6 +128,7 @@ SRCS = ft_memset.c \
ft_smaller.c \
ft_sign.c \
ft_fsign.c \
ft_modf.c \
ft_pow.c \
ft_sqrt.c \
ft_round.c \

View File

@@ -130,9 +130,10 @@ int ft_greater(int a, int b);
int ft_smaller(int a, int b);
int ft_sign(int i);
int ft_fsign(double i);
double ft_modf(double x, double *int_part);
double ft_pow(double base, int exponent);
double ft_round(double x);
double ft_sqrt(double i, double precision);
double ft_sqrt(double i);
void ft_free_tab(char **tab);
int ft_arrint(int *intarr, int comp, size_t size);

View File

@@ -6,9 +6,7 @@
int ft_abs(int n)
{
if (n < 0)
n *= -1;
else if (n == 0)
n = 0; // for -0
if (n <= 0)
n *= -1; // also works for -0
return (n);
}

View File

@@ -32,10 +32,8 @@ long long ft_atoll_superscript(const char *str)
int digit;
if (superscript_size == 2)
{
// ¹ (U+00B9), ² (U+00B2), or ³ (U+00B3)
if ((uint8_t)str[i + 1] == 0xB9)
digit = 1;
else if ((uint8_t)str[i + 1] == 0xB2)
// ² (U+00B2) or ³ (U+00B3)
if ((uint8_t)str[i + 1] == 0xB2)
digit = 2;
else if ((uint8_t)str[i + 1] == 0xB3)
digit = 3;

View File

@@ -6,9 +6,7 @@
double ft_fabs(double n)
{
if (n < 0)
n *= -1;
else if (n == 0)
n = 0; // for -0
if (n <= 0)
n *= -1; // also works for -0
return (n);
}

View File

@@ -7,8 +7,6 @@
long int ft_labs(long int n)
{
if (n <= 0)
n *= -1;
else if (n == 0)
n = 0; // for -0
n *= -1; // also works for -0
return (n);
}

25
srcs/ft_modf.c Normal file
View File

@@ -0,0 +1,25 @@
#include "libft.h"
#include <stdio.h> // tmp for printf, for float debug
/**
* 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 *store_int_part)
{
// extract the integer part by casting to long long
long long integer = (long long)x;
if (store_int_part != NULL)
{
*store_int_part = (double)integer;
}
// compute the fractional part
double frac_part = x - integer;
return frac_part;
}

View File

@@ -4,13 +4,17 @@
* return the square root of nb
* NewtonRaphson method : https://en.wikipedia.org/wiki/Newton%27s_method#Use_of_Newton's_method_to_compute_square_roots
*/
double ft_sqrt(double x, double precision)
double ft_sqrt(double x)
{
double precision;
if (x < 0)
return -1; // handle negative numbers
if (x == 0)
return 0;
precision = 0.00001;
// Newton-Raphson
double guess = x;
double prev_guess;
@@ -18,11 +22,11 @@ double ft_sqrt(double x, double precision)
{
prev_guess = guess;
guess = (guess + x / guess) / 2.0;
} while (ft_fabs(prev_guess - guess) > precision);
} while (prev_guess - guess > precision);
return guess;
// // binary search
// double low = 0;
// double low = 0
// double high = x;
// double mid;
// while (high - low > precision)

View File

@@ -27,6 +27,7 @@ size_t ft_strjoin_static(char *dst, size_t dst_size, const char **srcs, size_t n
ft_printf("--ft_strlen(srcs[%i]):%i--", i, ft_strlen(srcs[i]));
i++;
}
ft_printf("{dst_size:%li,srcs[0]:%s,srcs[1]:%s,total_len:%i}", dst_size, srcs[0], srcs[1], total_len); // debug
// Check if buffer is large enough (include space for '\0')
if (total_len + 1 > dst_size)