Compare commits

..

15 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
hugogogo
3cf0b6ecb9 adding superscript char 2026-05-07 14:47:18 +02:00
hugogogo
aa35d294f2 fix isdigit_superscript 2026-05-07 14:29:36 +02:00
hugogogo
413e149003 adding labs 2026-05-07 11:46:03 +02:00
hugogogo
36379f56b0 adding itoa_static and ft_nbrlen 2026-05-07 03:06:16 +02:00
hugogogo
b768ac1a14 adding modf pow and round 2026-05-07 00:58:54 +02:00
hugogogo
78f6a2d9ba adding sign and abs for doubles 2026-05-06 22:54:20 +02:00
18 changed files with 340 additions and 54 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 \
@@ -37,6 +37,7 @@ SRCS = ft_memset.c \
ft_tolower.c \ ft_tolower.c \
\ \
ft_strlen.c \ ft_strlen.c \
ft_nbrlen.c \
ft_strchr.c \ ft_strchr.c \
ft_strrchr.c \ ft_strrchr.c \
ft_strchrset.c \ ft_strchrset.c \
@@ -75,6 +76,7 @@ SRCS = ft_memset.c \
ft_atoll_superscript.c \ ft_atoll_superscript.c \
ft_atof.c \ ft_atof.c \
ft_itoa.c \ ft_itoa.c \
ft_itoa_static.c \
ft_utoa.c \ ft_utoa.c \
ft_atoibase.c \ ft_atoibase.c \
\ \
@@ -120,13 +122,19 @@ SRCS = ft_memset.c \
pf_utils.c \ pf_utils.c \
\ \
ft_abs.c \ ft_abs.c \
ft_labs.c \
ft_fabs.c \
ft_greater.c \ ft_greater.c \
ft_smaller.c \ ft_smaller.c \
ft_sign.c \ ft_sign.c \
ft_fsign.c \
ft_pow.c \
ft_sqrt.c \ ft_sqrt.c \
ft_round.c \
ft_free_tab.c \ ft_free_tab.c \
\ \
ft_arrint.c ft_arrint.c \
ft_superscript.c
ODIR = ./builds ODIR = ./builds

View File

@@ -27,6 +27,7 @@ void *ft_memmove(void *dst, const void *src, size_t len);
void *ft_memchr(const void *s, int c, size_t n); void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n); int ft_memcmp(const void *s1, const void *s2, size_t n);
size_t ft_strlen(const char *str); size_t ft_strlen(const char *str);
size_t ft_nbrlen(int nbr);
int ft_isalpha(int c); int ft_isalpha(int c);
int ft_isdigit(int c); int ft_isdigit(int c);
int ft_isdigit_superscript(const char *input, int *size); int ft_isdigit_superscript(const char *input, int *size);
@@ -60,6 +61,7 @@ size_t ft_strjoin_static(char *dst, size_t dst_size, const char **srcs, size_t n
char *ft_strtrim(char const *s1, char const *set); char *ft_strtrim(char const *s1, char const *set);
char **ft_split(char const *s, char c); char **ft_split(char const *s, char c);
char *ft_itoa(long int n); char *ft_itoa(long int n);
char *ft_itoa_static(long int nbr, char *buffer, size_t buff_len);
char *ft_utoa(unsigned long int n); char *ft_utoa(unsigned long int n);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
@@ -122,12 +124,18 @@ void ft_putnbrendl(int n);
void ft_putnbrendl_fd(int n, int fd); void ft_putnbrendl_fd(int n, int fd);
char *ft_concat_free(char *str1, char *str2); char *ft_concat_free(char *str1, char *str2);
int ft_abs(int n); int ft_abs(int n);
long int ft_labs(long int n);
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_sqrt(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 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);
const char *ft_superscript(char c);
#endif #endif

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;

14
srcs/ft_fabs.c Normal file
View File

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

10
srcs/ft_fsign.c Normal file
View File

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

View File

@@ -1,7 +1,28 @@
#include "libft.h" #include "libft.h"
// --- UTF-8 byte sequence macros for 2-byte superscript digits ---
#define TWO_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1 0xC2
// Superscript digits: ¹, ², ³
#define SUPERSCRIPT_1_BYTE2 0xB9
#define SUPERSCRIPT_2_BYTE2 0xB2
#define SUPERSCRIPT_3_BYTE2 0xB3
// --- UTF-8 byte sequence macros for 3-byte superscript digits ---
#define THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1 0xE2
#define THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE2 0x81
// Superscript digits: ⁰, ⁴, ⁵, ⁶, ⁷, ⁸, ⁹
#define SUPERSCRIPT_0_BYTE3 0xB0
#define SUPERSCRIPT_4_BYTE3 0xB4
#define SUPERSCRIPT_5_BYTE3 0xB5
#define SUPERSCRIPT_6_BYTE3 0xB6
#define SUPERSCRIPT_7_BYTE3 0xB7
#define SUPERSCRIPT_8_BYTE3 0xB8
#define SUPERSCRIPT_9_BYTE3 0xB9
/** /**
* Checks if the UTF-8 character at `input` is a superscript digit (², ³, ⁰-⁹). * Checks if the UTF-8 character at `input` is a superscript digit (¹, ², ³, ⁰-⁹).
* If it is, sets `*size` to the number of bytes in the character (2 or 3). * If it is, sets `*size` to the number of bytes in the character (2 or 3).
* Returns 1 if true, 0 otherwise. * Returns 1 if true, 0 otherwise.
*/ */
@@ -12,42 +33,40 @@ int ft_isdigit_superscript(const char *input, int *size)
*size = 0; // Default to 0 if not a superscript digit *size = 0; // Default to 0 if not a superscript digit
} }
// 2-byte UTF-8 superscript digits: ² (U+00B2) and ³ (U+00B3) --- // --- Check for 2-byte superscript digits (¹, ², ³) ---
// In UTF-8, 2-byte characters start with a byte in the range 0xC0-0xDF. if ((uint8_t)*input == TWO_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1)
// For superscript ² and ³:
// - First byte: 0xC2 (binary: 11000010)
// - Second byte: 0xB2 (²) or 0xB3 (³) (binary: 10110010 or 10110011)
if ((uint8_t)*input == 0xC2) // Check if first byte is 0xC2 (start of 2-byte UTF-8)
{ {
// Check if the second byte matches ² (0xB2) or ³ (0xB3) uint8_t second_byte = (uint8_t)*(input + 1);
if ((uint8_t)*(input + 1) == 0xB2 || (uint8_t)*(input + 1) == 0xB3) if (second_byte == SUPERSCRIPT_1_BYTE2 ||
second_byte == SUPERSCRIPT_2_BYTE2 ||
second_byte == SUPERSCRIPT_3_BYTE2)
{ {
if (size != NULL) if (size != NULL)
{ {
*size = 2; // 2-byte character *size = 2; // 2-byte character
} }
return 1; // Valid superscript digit (² or ³) return 1; // Valid superscript digit (¹, ², or ³)
} }
} }
// 3-byte UTF-8 superscript digits: ⁰ (U+2070) to ⁹ (U+2079) --- // --- Check for 3-byte superscript digits (⁰, ⁴-⁹) ---
// In UTF-8, 3-byte characters start with a byte in the range 0xE0-0xEF. else if ((uint8_t)*input == THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1)
// For superscript ⁰-⁹:
// - First byte: 0xE2 (binary: 11100010)
// - Second byte: 0x81 (binary: 10000001)
// - Third byte: 0xB0 (⁰) to 0xB9 (⁹) (binary: 10110000 to 10111001)
else if ((uint8_t)*input == 0xE2) // Check if first byte is 0xE2 (start of 3-byte UTF-8)
{ {
// Check if the second byte is 0x81 (part of the 3-byte sequence for ⁰-⁹) if ((uint8_t)*(input + 1) == THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE2)
if ((uint8_t)*(input + 1) == 0x81)
{ {
// Check if the third byte is in the range 0xB0-0xB9 (⁰ to ⁹) uint8_t third_byte = (uint8_t)*(input + 2);
if ((uint8_t)*(input + 2) >= 0xB0 && (uint8_t)*(input + 2) <= 0xB9) if (third_byte == SUPERSCRIPT_0_BYTE3 ||
third_byte == SUPERSCRIPT_4_BYTE3 ||
third_byte == SUPERSCRIPT_5_BYTE3 ||
third_byte == SUPERSCRIPT_6_BYTE3 ||
third_byte == SUPERSCRIPT_7_BYTE3 ||
third_byte == SUPERSCRIPT_8_BYTE3 ||
third_byte == SUPERSCRIPT_9_BYTE3)
{ {
if (size != NULL) if (size != NULL)
{ {
*size = 3; // 3-byte character *size = 3; // 3-byte character
} }
return 1; // Valid superscript digit (⁰-⁹) return 1; // Valid superscript digit (⁰, ⁴-⁹)
} }
} }
} }

80
srcs/ft_itoa_static.c Normal file
View File

@@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:59:01 by hulamy #+# #+# */
/* Updated: 2020/02/19 15:44:04 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/**
* Converts a long integer to its string representation and stores it in a pre-allocated buffer.
*
* @param nbr The long integer to convert.
* @param buffer The buffer to store the resulting string. Must be pre-allocated.
* @param buff_len The size of the buffer.
* Must be at least as large as the length of the number (including sign).
*
* @return The buffer (if successful), or NULL if the buffer is too small or invalid.
*/
#include "libft.h"
char *ft_itoa_static(long int nbr, char *buffer, size_t buff_len)
{
int is_negative;
const char *min_str;
size_t min_len;
size_t i;
if (buffer == NULL || buff_len == 0)
{
return NULL; // Invalid buffer or length
}
// Handle LONG_MIN separately
if (nbr == LONG_MIN)
{
min_str = "-9223372036854775808";
min_len = ft_strlen(min_str);
if (buff_len < min_len)
{
return NULL; // Buffer too small for LONG_MIN
}
i = 0;
while (i < min_len)
{
buffer[i] = min_str[i];
i++;
}
buffer[min_len] = '\0'; // Null-terminate
return buffer;
}
// Determine if the number is negative
is_negative = nbr < 0;
nbr = ft_labs(nbr);
// Write the null terminator at the end
buffer[buff_len - 1] = '\0';
// Write the digits in reverse order
i = buff_len - 2;
do
{
buffer[i] = (nbr % 10) + '0';
nbr /= 10;
i--;
} while (nbr != 0);
// Add the negative sign if needed
if (is_negative)
{
buffer[0] = '-';
}
return buffer;
}

14
srcs/ft_labs.c Normal file
View File

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

18
srcs/ft_nbrlen.c Normal file
View File

@@ -0,0 +1,18 @@
#include "libft.h"
// return length of int
// eg. 123 -> 3, -123 -> 4
size_t ft_nbrlen(int nbr)
{
int len;
int is_negative;
is_negative = nbr < 0;
len = (nbr < 0) ? 2 : 1;
while (nbr /= 10)
len++;
return len + is_negative;
}

19
srcs/ft_pow.c Normal file
View File

@@ -0,0 +1,19 @@
#include "libft.h"
/**
* returns the value of 'base' raised to the power of 'exponent'
*/
double ft_pow(double base, int exponent)
{
int i;
double result;
result = 1.0;
i = 0;
while (i < exponent)
{
result *= base;
i++;
}
return result;
}

28
srcs/ft_round.c Normal file
View File

@@ -0,0 +1,28 @@
#include "libft.h"
/**
* round x to the nearest integer,
* but round halfway cases away from zero, regardless of the current rounding direction
*/
double ft_round(double x)
{
// Handle special cases
if (x != x)
{ // Check for NaN (NaN is not equal to itself)
return x; // Return NaN
}
if (x == (double)LLONG_MAX + 1.0 || x == (double)LLONG_MIN - 1.0)
{
return x; // Return ±Infinity or the original value if overflow would occur
}
// Handle positive and negative numbers
if (x >= 0)
{
return (double)(long long)(x + 0.5);
}
else
{
return (double)(long long)(x - 0.5);
}
}

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

@@ -15,11 +15,17 @@
size_t ft_strjoin_static(char *dst, size_t dst_size, const char **srcs, size_t n) size_t ft_strjoin_static(char *dst, size_t dst_size, const char **srcs, size_t n)
{ {
size_t total_len;
size_t i;
// Calculate total length needed // Calculate total length needed
size_t total_len = 0; total_len = 0;
for (size_t i = 0; i < n; i++) i = 0;
while (i < n)
{ {
total_len += ft_strlen(srcs[i]); total_len += ft_strlen(srcs[i]);
ft_printf("--ft_strlen(srcs[%i]):%i--", i, ft_strlen(srcs[i]));
i++;
} }
// Check if buffer is large enough (include space for '\0') // Check if buffer is large enough (include space for '\0')

View File

@@ -11,7 +11,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* /*
** return length of of string ** return length of of string, excluding the terminal '\0'
*/ */
#include "libft.h" #include "libft.h"

32
srcs/ft_superscript.c Normal file
View File

@@ -0,0 +1,32 @@
#include "libft.h"
/**
* Converts a single digit character (0-9) to its superscript string representation.
*
* @param c The digit character (e.g., '0', '1', ..., '9').
* @return A static string containing the superscript representation of the digit.
* Returns NULL if the input is not a digit.
*/
const char *ft_superscript(char c)
{
// Array of superscript digit strings (UTF-8 literals)
static const char *superscript_digits[10] = {
"\xE2\x81\xB0", // ⁰ (U+2070)
"\xC2\xB9", // ¹ (U+00B9)
"\xC2\xB2", // ² (U+00B2)
"\xC2\xB3", // ³ (U+00B3)
"\xE2\x81\xB4", // ⁴ (U+2074)
"\xE2\x81\xB5", // ⁵ (U+2075)
"\xE2\x81\xB6", // ⁶ (U+2076)
"\xE2\x81\xB7", // ⁷ (U+2077)
"\xE2\x81\xB8", // ⁸ (U+2078)
"\xE2\x81\xB9" // ⁹ (U+2079)
};
if (c < '0' || c > '9')
{
return NULL; // Not a digit
}
int digit = c - '0';
return superscript_digits[digit];
}

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);
} // }