Compare commits
1 Commits
master
...
45cc208f44
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
45cc208f44 |
3
Makefile
3
Makefile
@@ -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 -g
|
CFLAGS += -Wall -Wextra -Werror -g3
|
||||||
|
|
||||||
SRCS = ft_memset.c \
|
SRCS = ft_memset.c \
|
||||||
ft_bzero.c \
|
ft_bzero.c \
|
||||||
@@ -128,6 +128,7 @@ SRCS = ft_memset.c \
|
|||||||
ft_smaller.c \
|
ft_smaller.c \
|
||||||
ft_sign.c \
|
ft_sign.c \
|
||||||
ft_fsign.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 \
|
||||||
|
|||||||
@@ -130,9 +130,10 @@ 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_fsign(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);
|
||||||
double ft_sqrt(double i, double precision);
|
double ft_sqrt(double i);
|
||||||
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);
|
||||||
|
|||||||
@@ -6,9 +6,7 @@
|
|||||||
|
|
||||||
int ft_abs(int n)
|
int ft_abs(int n)
|
||||||
{
|
{
|
||||||
if (n < 0)
|
if (n <= 0)
|
||||||
n *= -1;
|
n *= -1; // also works for -0
|
||||||
else if (n == 0)
|
|
||||||
n = 0; // for -0
|
|
||||||
return (n);
|
return (n);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,10 +32,8 @@ long long ft_atoll_superscript(const char *str)
|
|||||||
int digit;
|
int digit;
|
||||||
if (superscript_size == 2)
|
if (superscript_size == 2)
|
||||||
{
|
{
|
||||||
// ¹ (U+00B9), ² (U+00B2), or ³ (U+00B3)
|
// ² (U+00B2) or ³ (U+00B3)
|
||||||
if ((uint8_t)str[i + 1] == 0xB9)
|
if ((uint8_t)str[i + 1] == 0xB2)
|
||||||
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;
|
||||||
|
|||||||
@@ -6,9 +6,7 @@
|
|||||||
|
|
||||||
double ft_fabs(double n)
|
double ft_fabs(double n)
|
||||||
{
|
{
|
||||||
if (n < 0)
|
if (n <= 0)
|
||||||
n *= -1;
|
n *= -1; // also works for -0
|
||||||
else if (n == 0)
|
|
||||||
n = 0; // for -0
|
|
||||||
return (n);
|
return (n);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,6 @@
|
|||||||
long int ft_labs(long int n)
|
long int ft_labs(long int n)
|
||||||
{
|
{
|
||||||
if (n <= 0)
|
if (n <= 0)
|
||||||
n *= -1;
|
n *= -1; // also works for -0
|
||||||
else if (n == 0)
|
|
||||||
n = 0; // for -0
|
|
||||||
return (n);
|
return (n);
|
||||||
}
|
}
|
||||||
|
|||||||
25
srcs/ft_modf.c
Normal file
25
srcs/ft_modf.c
Normal 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;
|
||||||
|
}
|
||||||
@@ -4,13 +4,17 @@
|
|||||||
* return the square root of nb
|
* return the square root of nb
|
||||||
* Newton–Raphson method : https://en.wikipedia.org/wiki/Newton%27s_method#Use_of_Newton's_method_to_compute_square_roots
|
* Newton–Raphson 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)
|
if (x < 0)
|
||||||
return -1; // handle negative numbers
|
return -1; // handle negative numbers
|
||||||
if (x == 0)
|
if (x == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
precision = 0.00001;
|
||||||
|
|
||||||
// Newton-Raphson
|
// Newton-Raphson
|
||||||
double guess = x;
|
double guess = x;
|
||||||
double prev_guess;
|
double prev_guess;
|
||||||
@@ -18,11 +22,11 @@ double ft_sqrt(double x, double precision)
|
|||||||
{
|
{
|
||||||
prev_guess = guess;
|
prev_guess = guess;
|
||||||
guess = (guess + x / guess) / 2.0;
|
guess = (guess + x / guess) / 2.0;
|
||||||
} while (ft_fabs(prev_guess - guess) > precision);
|
} while (prev_guess - guess > precision);
|
||||||
return guess;
|
return guess;
|
||||||
|
|
||||||
// // binary search
|
// // binary search
|
||||||
// double low = 0;
|
// double low = 0
|
||||||
// double high = x;
|
// double high = x;
|
||||||
// double mid;
|
// double mid;
|
||||||
// while (high - low > precision)
|
// while (high - low > precision)
|
||||||
|
|||||||
@@ -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]));
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user