Compare commits

...

9 Commits

Author SHA1 Message Date
hugogogo
9ac98cb1f5 fix ft_sqrt 2026-06-05 12:10:13 +02:00
hugogogo
71806cb923 fix superscript 1 2026-05-15 02:29:53 +02:00
hugogogo
e95f55c07f update ft_sqrt with precision 2026-05-14 13:53:09 +02:00
hugogogo
85423f4211 removing debug print 2026-05-14 00:06:40 +02:00
hugogogo
f662d7f750 removed unused and wrong ft_modf 2026-05-12 00:35:11 +02:00
hugogogo
de28530259 fix abs family for -0 value 2026-05-10 21:32:30 +02:00
hugogogo
60c72d7560 fix ft_modf 2026-05-10 01:43:59 +02:00
hugogogo
ae9787ba6d updating sqrt to work with doubles 2026-05-09 14:43:26 +02:00
hugogogo
8edf428fed changed sign_f to also return zero 2026-05-08 00:59:29 +02:00
11 changed files with 73 additions and 55 deletions

View File

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

View File

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

View File

@@ -1,8 +1,14 @@
#include "libft.h" #include "libft.h"
/**
* returns the absolute value of an int number
*/
int ft_abs(int n) int ft_abs(int n)
{ {
if (n < 0) if (n < 0)
n *= -1; n *= -1;
else if (n == 0)
n = 0; // for -0
return (n); return (n);
} }

View File

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

View File

@@ -1,8 +1,14 @@
#include "libft.h" #include "libft.h"
/**
* returns the absolute value of a double number
*/
double ft_fabs(double n) double ft_fabs(double n)
{ {
if (n < 0) if (n < 0)
n *= -1; n *= -1;
else if (n == 0)
n = 0; // for -0
return (n); return (n);
} }

View File

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

View File

@@ -1,8 +1,14 @@
#include "libft.h" #include "libft.h"
/**
* returns the absolute value of a long int number
*/
long int ft_labs(long int n) long int ft_labs(long int n)
{ {
if (n < 0) if (n <= 0)
n *= -1; n *= -1;
else if (n == 0)
n = 0; // for -0
return (n); return (n);
} }

View File

@@ -1,23 +0,0 @@
#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;
}

View File

@@ -1,15 +1,37 @@
#include "libft.h" #include "libft.h"
/* /*
** return the square root of nb * return the square root of nb
** or the integer value of it * NewtonRaphson method : https://en.wikipedia.org/wiki/Newton%27s_method#Use_of_Newton's_method_to_compute_square_roots
*/ */
int ft_sqrt(int nb) double ft_sqrt(double x, double precision)
{ {
int i; if (x < 0)
return -1; // handle negative numbers
if (x == 0)
return 0;
i = 0; // Newton-Raphson
while ((i*i) < nb) double guess = x;
i++; double prev_guess;
return (i); do
{
prev_guess = guess;
guess = (guess + x / guess) / 2.0;
} while (ft_fabs(prev_guess - guess) > precision);
return guess;
// // binary search
// double low = 0;
// double high = x;
// double mid;
// while (high - low > precision)
// {
// mid = (low + high) / 2.0;
// if (mid * mid < x)
// low = mid;
// else
// high = mid;
// }
// return (low + high) / 2.0;
} }

View File

@@ -27,7 +27,6 @@ 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])); ft_printf("--ft_strlen(srcs[%i]):%i--", i, ft_strlen(srcs[i]));
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') // Check if buffer is large enough (include space for '\0')
if (total_len + 1 > dst_size) if (total_len + 1 > dst_size)

View File

@@ -1,15 +1,15 @@
#include "libft.h" #include "libft.h"
/* // /*
** return the square root of nb // ** return the square root of nb
** or the integer value of it // ** or the integer value of it
*/ // */
int ft_sqrt(int nb) // int ft_sqrt(int nb)
{ // {
int i; // int i;
i = 0; // i = 0;
while ((i*i) < nb) // while ((i*i) < nb)
i++; // i++;
return (i); // return (i);
} // }