Compare commits

...

11 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
13 changed files with 149 additions and 78 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 \
@@ -127,14 +127,14 @@ SRCS = ft_memset.c \
ft_greater.c \
ft_smaller.c \
ft_sign.c \
ft_sign_f.c \
ft_modf.c \
ft_fsign.c \
ft_pow.c \
ft_sqrt.c \
ft_round.c \
ft_free_tab.c \
\
ft_arrint.c
ft_arrint.c \
ft_superscript.c
ODIR = ./builds

View File

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

View File

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

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

@@ -1,8 +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);
}

View File

@@ -1,8 +1,10 @@
#include "libft.h"
int ft_sign_f(double i)
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"
// --- 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).
* 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
}
// 2-byte UTF-8 superscript digits: ² (U+00B2) and ³ (U+00B3) ---
// In UTF-8, 2-byte characters start with a byte in the range 0xC0-0xDF.
// 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 for 2-byte superscript digits (¹, ², ³) ---
if ((uint8_t)*input == TWO_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1)
{
// Check if the second byte matches ² (0xB2) or ³ (0xB3)
if ((uint8_t)*(input + 1) == 0xB2 || (uint8_t)*(input + 1) == 0xB3)
uint8_t second_byte = (uint8_t)*(input + 1);
if (second_byte == SUPERSCRIPT_1_BYTE2 ||
second_byte == SUPERSCRIPT_2_BYTE2 ||
second_byte == SUPERSCRIPT_3_BYTE2)
{
if (size != NULL)
{
*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) ---
// In UTF-8, 3-byte characters start with a byte in the range 0xE0-0xEF.
// 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 for 3-byte superscript digits (⁰, ⁴-⁹) ---
else if ((uint8_t)*input == THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE1)
{
// Check if the second byte is 0x81 (part of the 3-byte sequence for ⁰-⁹)
if ((uint8_t)*(input + 1) == 0x81)
if ((uint8_t)*(input + 1) == THREE_BYTE_UTF8_SUPERSCRIPT_DIGIT_BYTE2)
{
// Check if the third byte is in the range 0xB0-0xB9 (⁰ to ⁹)
if ((uint8_t)*(input + 2) >= 0xB0 && (uint8_t)*(input + 2) <= 0xB9)
uint8_t third_byte = (uint8_t)*(input + 2);
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)
{
*size = 3; // 3-byte character
}
return 1; // Valid superscript digit (⁰-⁹)
return 1; // Valid superscript digit (⁰, ⁴-⁹)
}
}
}

View File

@@ -1,8 +1,14 @@
#include "libft.h"
/**
* returns the absolute value of a long int number
*/
long int ft_labs(long int n)
{
if (n < 0)
if (n <= 0)
n *= -1;
else if (n == 0)
n = 0; // for -0
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"
/*
** return the square root of nb
** or the integer value of it
*/
int ft_sqrt(int nb)
* 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)
{
int i;
if (x < 0)
return -1; // handle negative numbers
if (x == 0)
return 0;
i = 0;
while ((i*i) < nb)
i++;
return (i);
// Newton-Raphson
double guess = x;
double prev_guess;
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]));
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)

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"
/*
** return the square root of nb
** or the integer value of it
*/
int ft_sqrt(int nb)
{
int i;
// /*
// ** return the square root of nb
// ** or the integer value of it
// */
// int ft_sqrt(int nb)
// {
// int i;
i = 0;
while ((i*i) < nb)
i++;
return (i);
}
// i = 0;
// while ((i*i) < nb)
// i++;
// return (i);
// }