Compare commits

...

4 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
5 changed files with 9 additions and 12 deletions

View File

@@ -12,7 +12,7 @@ _DEP = libft.h
DEPS = $(_DEP:%.h=$(IDIR)/%.h)
CFLAGS = -I$(IDIR)
CFLAGS += -Wall -Wextra -Werror -g3
CFLAGS += -Wall -Wextra -Werror -g
SRCS = ft_memset.c \
ft_bzero.c \

View File

@@ -132,7 +132,7 @@ int ft_sign(int i);
int ft_fsign(double i);
double ft_pow(double base, int exponent);
double ft_round(double x);
double ft_sqrt(double i);
double ft_sqrt(double i, double precision);
void ft_free_tab(char **tab);
int ft_arrint(int *intarr, int comp, size_t size);

View File

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

View File

@@ -4,17 +4,13 @@
* 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 ft_sqrt(double x, double precision)
{
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;
@@ -22,11 +18,11 @@ double ft_sqrt(double x)
{
prev_guess = guess;
guess = (guess + x / guess) / 2.0;
} while (prev_guess - guess > precision);
} while (ft_fabs(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,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]));
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)