Skip to content

Instantly share code, notes, and snippets.

@tompng
tompng / manyzero.rb
Created July 27, 2025 14:16
float num with many zeros: 1.1795176680648731e+39 == 1179517668064873100000000000000008388608.0
def euclidsolve(cx, cy, c, small)
return euclidsolve(cy, cx, c, small).reverse if cx > cy
# cx <= cy
# x * cx + y * cy = c + small
# p [cx.to_f, cy.to_f, c.to_f, small.to_f]
if small >= cy
# cx <= cy, c < cy
# cx*x + cy*y == c + small
# x=0; cy*y == c + small
return [0, 1]
# size_t 32bit
# docker run -v `pwd`:/bigdecimal --rm -ti arm32v7/ruby bash
# unused defs
code = File.read('ext/bigdecimal/bigdecimal.c')
header = File.read('ext/bigdecimal/bigdecimal.h')
defs = code.scan(/#define ([a-zA-Z_0-9]+)/).uniq
hdefs = header.scan(/#define ([a-zA-Z_0-9]+)/).uniq
defs |= hdefs.select{header.scan(/[a-zA-Z_0-9]+/).count(it[0])==1}
pp defs.map{[it[0], code.scan(/[a-zA-Z_0-9]+/).count(it[0])]}.sort_by(&:last)
# returns (1<<(size + x.bit_length)) / x
def inv(x, size)
x_size = x.bit_length
bl = size.bit_length
n = 2
y = (1 << (n + 2)) / (x >> (x_size - 2))
(0..bl).reverse_each do |i|
n2 = [(size >> i) + 2, size].min
# y = (1<<(x_size + n)) / x
module Operand
def default_bin(op, other)
if is_a?(Val) && other.is_a?(Val)
return Val.new(value.send(op, other.value))
end
Op.new(op, self, other)
end
def +(other)
other = Val.new(other) if other.is_a?(Integer)
@tompng
tompng / codeparty_trick_reline_irb.md
Last active April 18, 2025 09:20
Code Party TRICK, Reline, IRB

Code Party: IRB/Reline/TRICK

Self Introduction

These are just an idea.

Let's read TRICK codes

Let's encode and decode data in a strange way

  • Encode to a string "UnVieQ=="
@tompng
tompng / constant.rb
Last active April 24, 2025 15:34
Readable FizzBuzz
module ReadableFizzBuzz
module Chain
end
end
include ReadableFizzBuzz
Chain::Itself = Chain
module Chain::Itself
@tompng
tompng / analyzer.rb
Last active April 24, 2025 15:34
sound generation
require 'chunky_png'
require 'numo/narray'
file = ARGV[0]
data = File.binread(file)
wave = data[44..].unpack('v*').map do |v|
(v <= 0x7fff ? v : v - 0x10000).fdiv(0x8000)
end
RATE = 44100
sec = ARGV[1].to_i.nonzero? || 5
mut = Ractor.new do
pipe = Ractor.new do
loop { Ractor.yield Ractor.receive }
end
loop do
Ractor.yield pipe
pipe.take
end
end
require 'prism'
require 'ripper'
def parsey_extract_locations(code)
lines = code.lines
line_indexes = lines.reduce([0]) { _1 << _1.last + _2.bytesize }
code_index = ->(line, col) { line_indexes[line - 1] + col }
locations = []
each_node = -> node do
locs = node.locations.compact
all_tokens = %w[
x
y
false
if
rescue
begin
end
=
def