Skip to content

Instantly share code, notes, and snippets.

@tompng
Created July 18, 2025 18:20
Show Gist options
  • Save tompng/07e1d0058f34678dcc95a6822e891520 to your computer and use it in GitHub Desktop.
Save tompng/07e1d0058f34678dcc95a6822e891520 to your computer and use it in GitHub Desktop.
# 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)
# divmod consisntency
require 'bigdecimal'
ns = [*1..100]
es = [*-100..100]
n=0
loop do
n+=1
p n if n%100000==0
xn=ns.sample
yn=ns.sample
xe=es.sample
ye=es.sample
c1 = rand(10).to_s
c2 = rand(10).to_s
c3 = rand(10).to_s
c4 = rand(10).to_s
x = BigDecimal('3.' + c1 * xn + c2 + "E#{xe}")
y = BigDecimal(['1.', '-1.'].sample + c3 * yn + c4 + "E#{ye}")
d,m = x.divmod(y)
(p [x,y,d,m,x-d*y];break) unless x == d * y + m && d.frac.zero? && (y>0 ? 0 <= m && m < y : y < m && m <= 0)
end
# example test
File.read('lib/bigdecimal/math.rb').scan(/^[# ]+BigMath.+\n.+=> *".+"/).each{|s|
code = s[/BigMath.+/]; a=eval(s[/".+"/])
puts code
y=eval(code);puts y==a ? 'OK' : [:error, y, a]
}
# precision test
d1 = BigDecimal('1e-100')
d2 = d1 * BigDecimal(2).sqrt(200)
%i[cbrt sin cos tan asin acos atan sinh cosh tanh asinh acosh atanh log2 log10 exp log].each do |m|
next unless BigMath.respond_to?(m)
p m
[-1, 0, 0.5, 1, 2, 10, 100].each do |x0|
[d1, d2, 0, -d1, -d2].each do |d|
x = BigDecimal(x0, 0) + d
vt = BigMath.send(m, x, 400)
v = BigMath.send(m, x, 300)
binding.irb unless (v/vt-1).exponent < 310
rescue Math::DomainError
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment