Skip to content

Instantly share code, notes, and snippets.

@al2o3cr
Last active January 1, 2016 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save al2o3cr/8175722 to your computer and use it in GitHub Desktop.
Save al2o3cr/8175722 to your computer and use it in GitHub Desktop.
Test rounding behavior of BigDecimal
require 'bigdecimal'
def to_decimal(digits, dp)
digits = digits.dup
if dp < -digits.length
digits.unshift(*([nil]*(-digits.length-dp)))
end
digits.insert(dp+digits.length, '.').map { |x| x || '0' }.join
end
def division_result(n1, n2, offset)
x1 = BigDecimal.new(to_decimal(n1, offset))
x2 = BigDecimal.new(to_decimal(n2, offset-2))
result = x1 / x2
precs = [x1.precs, x2.precs, result.precs].flatten
precs.map { |x| "%02d" % x }.join('/') + ' ' + result.to_s("F")
end
NITER = 80
N1 = %w(1 4 7 2)
N2 = %w(9 9)
NITER.times do |idx|
leader = idx == NITER/2 ? '**' : ' '
puts leader+division_result(N1,N2, idx - NITER/2)
end
09/54/09/54/54/63 1486.868686868686868686868686868686868686868686869
18/54/09/54/45/63 1486.868686868686868686868686868686868687
18/54/09/54/45/63 1486.868686868686868686868686868686868687
18/54/09/54/45/63 1486.868686868686868686868686868686868687
09/45/09/54/45/63 1486.868686868686868686868686868686868687
09/45/18/54/54/63 1486.868686868686868686868686868686868686868686869
09/45/09/45/45/54 1486.868686868686868686868686868686868687
09/45/09/45/45/54 1486.868686868686868686868686868686868687
09/45/09/45/45/54 1486.868686868686868686868686868686868687
09/45/09/45/45/54 1486.868686868686868686868686868686868687
18/45/09/45/36/54 1486.868686868686868686868686869
18/45/09/45/36/54 1486.868686868686868686868686869
18/45/09/45/36/54 1486.868686868686868686868686869
09/36/09/45/36/54 1486.868686868686868686868686869
09/36/18/45/45/54 1486.868686868686868686868686868686868687
09/36/09/36/36/45 1486.868686868686868686868686869
09/36/09/36/36/45 1486.868686868686868686868686869
09/36/09/36/36/45 1486.868686868686868686868686869
09/36/09/36/36/45 1486.868686868686868686868686869
18/36/09/36/27/45 1486.868686868686868687
18/36/09/36/27/45 1486.868686868686868687
18/36/09/36/27/45 1486.868686868686868687
09/27/09/36/27/45 1486.868686868686868687
09/27/18/36/36/45 1486.868686868686868686868686869
09/27/09/27/27/36 1486.868686868686868687
09/27/09/27/27/36 1486.868686868686868687
09/27/09/27/27/36 1486.868686868686868687
09/27/09/27/27/36 1486.868686868686868687
18/27/09/27/18/36 1486.868686869
18/27/09/27/18/36 1486.868686869
18/27/09/27/18/36 1486.868686869
09/18/09/27/18/36 1486.868686869
09/18/18/27/27/36 1486.868686868686868687
09/18/09/18/18/27 1486.868686869
09/18/09/18/18/27 1486.868686869
09/18/09/18/18/27 1486.868686869
09/18/09/18/18/27 1486.868686869
18/18/09/18/27/45 1486.868686868686868687
18/18/09/18/27/45 1486.868686868686868687
18/18/09/18/27/45 1486.868686868686868687
**09/18/09/18/18/36 1486.868686869
09/18/18/18/36/45 1486.868686868686868686868686869
09/18/09/18/27/36 1486.868686868686868687
09/18/09/18/27/36 1486.868686868686868687
09/18/09/18/27/36 1486.868686868686868687
09/18/09/18/27/36 1486.868686868686868687
18/27/09/18/36/54 1486.868686868686868686868686869
18/27/09/18/36/54 1486.868686868686868686868686869
18/27/09/18/36/54 1486.868686868686868686868686869
09/27/09/18/27/45 1486.868686868686868687
09/27/18/27/45/54 1486.868686868686868686868686868686868687
09/27/09/27/36/45 1486.868686868686868686868686869
09/27/09/27/36/45 1486.868686868686868686868686869
09/27/09/27/36/45 1486.868686868686868686868686869
09/27/09/27/36/45 1486.868686868686868686868686869
18/36/09/27/45/63 1486.868686868686868686868686868686868687
18/36/09/27/45/63 1486.868686868686868686868686868686868687
18/36/09/27/45/63 1486.868686868686868686868686868686868687
09/36/09/27/36/54 1486.868686868686868686868686869
09/36/18/36/54/63 1486.868686868686868686868686868686868686868686869
09/36/09/36/45/54 1486.868686868686868686868686868686868687
09/36/09/36/45/54 1486.868686868686868686868686868686868687
09/36/09/36/45/54 1486.868686868686868686868686868686868687
09/36/09/36/45/54 1486.868686868686868686868686868686868687
18/45/09/36/54/72 1486.868686868686868686868686868686868686868686869
18/45/09/36/54/72 1486.868686868686868686868686868686868686868686869
18/45/09/36/54/72 1486.868686868686868686868686868686868686868686869
09/45/09/36/45/63 1486.868686868686868686868686868686868687
09/45/18/45/63/72 1486.868686868686868686868686868686868686868686868686868687
09/45/09/45/54/63 1486.868686868686868686868686868686868686868686869
09/45/09/45/54/63 1486.868686868686868686868686868686868686868686869
09/45/09/45/54/63 1486.868686868686868686868686868686868686868686869
09/45/09/45/54/63 1486.868686868686868686868686868686868686868686869
18/54/09/45/63/81 1486.868686868686868686868686868686868686868686868686868687
18/54/09/45/63/81 1486.868686868686868686868686868686868686868686868686868687
18/54/09/45/63/81 1486.868686868686868686868686868686868686868686868686868687
09/54/09/45/54/72 1486.868686868686868686868686868686868686868686869
09/54/18/54/72/81 1486.868686868686868686868686868686868686868686868686868686868686869
09/54/09/54/63/72 1486.868686868686868686868686868686868686868686868686868687
09/54/09/54/63/72 1486.868686868686868686868686868686868686868686868686868687
09/54/09/54/45/54 1486.868686868686868686868686868686868687
18/54/09/54/36/54 1486.868686868686868686868686869
18/54/09/54/36/54 1486.868686868686868686868686869
18/54/09/54/36/54 1486.868686868686868686868686869
09/45/09/54/36/54 1486.868686868686868686868686869
09/45/18/54/45/54 1486.868686868686868686868686868686868687
09/45/09/45/36/45 1486.868686868686868686868686869
09/45/09/45/36/45 1486.868686868686868686868686869
09/45/09/45/36/45 1486.868686868686868686868686869
09/45/09/45/36/45 1486.868686868686868686868686869
18/45/09/45/27/45 1486.868686868686868687
18/45/09/45/27/45 1486.868686868686868687
18/45/09/45/27/45 1486.868686868686868687
09/36/09/45/27/45 1486.868686868686868687
09/36/18/45/36/45 1486.868686868686868686868686869
09/36/09/36/27/36 1486.868686868686868687
09/36/09/36/27/36 1486.868686868686868687
09/36/09/36/27/36 1486.868686868686868687
09/36/09/36/27/36 1486.868686868686868687
18/36/09/36/18/36 1486.868686869
18/36/09/36/18/36 1486.868686869
18/36/09/36/18/36 1486.868686869
09/27/09/36/18/36 1486.868686869
09/27/18/36/27/36 1486.868686868686868687
09/27/09/27/18/27 1486.868686869
09/27/09/27/18/27 1486.868686869
09/27/09/27/18/27 1486.868686869
09/27/09/27/18/27 1486.868686869
18/27/09/27/09/27 1487.0
18/27/09/27/09/27 1487.0
18/27/09/27/09/27 1487.0
09/18/09/27/09/27 1487.0
09/18/18/27/18/27 1486.868686869
09/18/09/18/09/18 1487.0
09/18/09/18/09/18 1487.0
09/18/09/18/09/18 1487.0
09/18/09/18/09/18 1487.0
18/18/09/18/18/36 1486.868686869
18/18/09/18/18/36 1486.868686869
18/18/09/18/18/36 1486.868686869
**09/18/09/18/09/27 1487.0
09/18/18/18/27/36 1486.868686868686868687
09/18/09/18/18/27 1486.868686869
09/18/09/18/18/27 1486.868686869
09/18/09/18/18/27 1486.868686869
09/18/09/18/18/27 1486.868686869
18/27/09/18/27/45 1486.868686868686868687
18/27/09/18/27/45 1486.868686868686868687
18/27/09/18/27/45 1486.868686868686868687
09/27/09/18/18/36 1486.868686869
09/27/18/27/36/45 1486.868686868686868686868686869
09/27/09/27/27/36 1486.868686868686868687
09/27/09/27/27/36 1486.868686868686868687
09/27/09/27/27/36 1486.868686868686868687
09/27/09/27/27/36 1486.868686868686868687
18/36/09/27/36/54 1486.868686868686868686868686869
18/36/09/27/36/54 1486.868686868686868686868686869
18/36/09/27/36/54 1486.868686868686868686868686869
09/36/09/27/27/45 1486.868686868686868687
09/36/18/36/45/54 1486.868686868686868686868686868686868687
09/36/09/36/36/45 1486.868686868686868686868686869
09/36/09/36/36/45 1486.868686868686868686868686869
09/36/09/36/36/45 1486.868686868686868686868686869
09/36/09/36/36/45 1486.868686868686868686868686869
18/45/09/36/45/63 1486.868686868686868686868686868686868687
18/45/09/36/45/63 1486.868686868686868686868686868686868687
18/45/09/36/45/63 1486.868686868686868686868686868686868687
09/45/09/36/36/54 1486.868686868686868686868686869
09/45/18/45/54/63 1486.868686868686868686868686868686868686868686869
09/45/09/45/45/54 1486.868686868686868686868686868686868687
09/45/09/45/45/54 1486.868686868686868686868686868686868687
09/45/09/45/45/54 1486.868686868686868686868686868686868687
09/45/09/45/45/54 1486.868686868686868686868686868686868687
18/54/09/45/54/72 1486.868686868686868686868686868686868686868686869
18/54/09/45/54/72 1486.868686868686868686868686868686868686868686869
18/54/09/45/54/72 1486.868686868686868686868686868686868686868686869
09/54/09/45/45/63 1486.868686868686868686868686868686868687
09/54/18/54/63/72 1486.868686868686868686868686868686868686868686868686868687
09/54/09/54/54/63 1486.868686868686868686868686868686868686868686869
09/54/09/54/54/63 1486.868686868686868686868686868686868686868686869
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment