Skip to content

Instantly share code, notes, and snippets.

@windows-fryer
Created July 10, 2024 18:58
Show Gist options
  • Save windows-fryer/c2d4d4793df3728474d4085689cbb47b to your computer and use it in GitHub Desktop.
Save windows-fryer/c2d4d4793df3728474d4085689cbb47b to your computer and use it in GitHub Desktop.
Kitty STD: number -> cstring
#pragma once
typedef unsigned char c8;
typedef unsigned short c16;
typedef unsigned int c32;
#include <string/convert.h>
#include <math/pow.h>
void string_from_signed(c8* const data, const u64 size, const i64 number) {
const u8 NEGATIVE = number < 0;
const u64 ABSOLUTE_NUMBER = number < 0 ? -number : number;
string_from_unsigned(data + NEGATIVE, size - NEGATIVE, ABSOLUTE_NUMBER);
if (NEGATIVE)
data[0] = '-';
}
void string_from_unsigned(c8* const data, const u64 size, const u64 number) {
const c8 START_CHARACTER = '0';
const u64 USABLE_SIZE = size - sizeof((c8)'\0');
u64 digits = USABLE_SIZE > 1;
u64 bufferNumber = number;
while (digits < USABLE_SIZE) {
if (!(bufferNumber /= 10))
break;
digits++;
}
for (u64 i = digits; i; i--) {
const c8 digit = number / pow64(10, i - 1) % 10;
const u64 difference = digits - i;
// TODO: Check for underflows? ^
data[difference] = START_CHARACTER + digit;
}
data[USABLE_SIZE] = '\0';
}
#pragma once
#include <type/int.h>
/**
* @brief Runs the formula \f$|n|^e \{n \in {N}_0 \mid n > -1\}\f$. Omits signed number
* operations.
*
*
* @param number An unsigned number.
* @param exponent The power.
*
* @return u64
*
* @note Due to the intended limitations of the library, the exponent can not go negative to prevent
* use of the FPU.
*/
u64 pow64(const u64 number, const u64 exponent);
/**
* @brief Runs the formula \f$n^e \{n \in {N}_0 \mid n > -1\}\f$.
*
* @param number A signed number.
* @param exponent The power.
*
* @return i64
*
* @ref pow64
*/
i64 pows64(const i64 number, const u64 exponent);
#pragma once
typedef char i8;
typedef short i16;
typedef int i32;
typedef long long i64;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
#include <math/pow.h>
u64 pow64(const u64 number, const u64 exponent) {
if (!exponent)
return 1;
u64 result = number;
for (u64 i = 1; i < exponent; i++)
result *= number;
return result;
}
i64 pows64(const i64 number, const u64 exponent) {
if (!exponent)
return 1;
i64 result = number;
for (u64 i = 1; i < exponent; i++)
result *= number;
return result;
}
#pragma once
#include <type/int.h>
/**
* @brief Runs the formula \f$|n|^e \{n \in {N}_0 \mid n > -1\}\f$. Omits signed number
* operations.
*
*
* @param number An unsigned number.
* @param exponent The power.
*
* @return u64
*
* @note Due to the intended limitations of the library, the exponent can not go negative to prevent
* use of the FPU.
*/
u64 pow64(const u64 number, const u64 exponent);
/**
* @brief Runs the formula \f$n^e \{n \in {N}_0 \mid n > -1\}\f$.
*
* @param number A signed number.
* @param exponent The power.
*
* @return i64
*
* @ref pow64
*/
i64 pows64(const i64 number, const u64 exponent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment