Skip to content

Instantly share code, notes, and snippets.

View hsingtism's full-sized avatar

Hsing Lo hsingtism

  • Georgia, United States
  • 07:25 (UTC -04:00)
View GitHub Profile
@hsingtism
hsingtism / randomFP.c
Created January 16, 2024 01:55
mask a random 64 bit integer to a floating point range
/*
random64() is a random integer function
this code uses undefined behavior but it'll be alright
make sure to include stdint
*/
#include <stdint.h>
// mask to (-2,2]
// this is just an example, change the exponent bits for other ranges
inline double _22() {
@hsingtism
hsingtism / xorshift128plus.c
Created January 16, 2024 01:51
pseudorandom number generator I use sometimes
// based on V8 https://v8.dev/blog/math-random.
uint64_t state0 = 0x4D696B696E6C6579;
uint64_t state1 = 0x0000004873696E67;
uint64_t xorshift128plus() {
uint64_t s1 = state0;
uint64_t s0 = state1;
state0 = s0;
s1 ^= s1 << 23;
s1 ^= s1 >> 17;
s1 ^= s0;
#include <stdio.h>
#define BYTES_PER_PIXEL 3
#define FILE_HEADER_SIZE 14
#define INFO_HEADER_SIZE 40
/*
Generates and saves a bitmap image.
- *image - ptr to array of 8 bit image data. unsigned char image[HEIGHT][WIDTH][BYTES_PER_PIXEL]
up to down > left to right > blue, green, red
@hsingtism
hsingtism / numToWords.js
Last active April 4, 2022 05:28
A function that convert integers to English words (short scale)
function numToWord(num) {
if (!Number.isSafeInteger(num) || num < 0) return null
if (!num) return 'zero '
const ntt = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen ']
let mstr = ''
let nArr = num.toString().padStart(18, '0').match(/.{1,3}/g)
for (let i = 5; i > -1; i--) {
let wkv = nArr.shift()
if (!+wkv) continue
if (+wkv[0]) mstr += ntt[+wkv[0]] + 'hundred '
@hsingtism
hsingtism / textColor.js
Last active April 16, 2024 01:03
A function that calculates if text color should be black given a background color. (W3C compliant for all values)
function useBlack(r, g, b) {
const e = v => Math.pow(v / 255 + 0.055, 2.4)
r < 11 ? r /= 15496 : r = e(r) / 5.348631
g < 11 ? g /= 4606. : g = e(g) / 1.589931
b < 11 ? b /= 45631 : b = e(b) / 15.74957
return r + g + b > 0.17912878
}