Skip to content

Instantly share code, notes, and snippets.

@phluid61
Created July 19, 2017 03:31
Show Gist options
  • Save phluid61/9e428a7c2a984971579042f637afc273 to your computer and use it in GitHub Desktop.
Save phluid61/9e428a7c2a984971579042f637afc273 to your computer and use it in GitHub Desktop.
Testing BigDecimal in Ruby
require 'bigdecimal'
def _sprintf num
('%0.45f' % num).sub(/0+$/,'')
end
n = 1
loop do
n += 1
b = 2 ** n
a = b - 1
x = BigDecimal(a)
y = BigDecimal(b)
d = x / y
r = Rational(a, b)
next if r == d.to_r
puts "((2 ** #{n}) - 1) / (2 ** #{n})"
puts " #{x} / #{y}"
puts " BigDecimal:"
puts " \#to_s #{d.to_s}"
puts " sprintf #{_sprintf d}"
puts " \#to_r #{d.to_r.inspect}"
puts " Rational:"
puts " sprintf #{_sprintf r}"
puts " \#to_r #{r.inspect}"
d2 = BigDecimal(_sprintf d)
r2 = Rational(_sprintf d)
puts 'construct from (sprintf % BigDecimal)'
puts " BigDecimal: #{d2.to_s}"
puts " #{d2.to_r.inspect}"
puts " Rational: #{r2.inspect}"
d3 = BigDecimal(d.to_s)
r3 = Rational(d.to_s)
puts 'construct from (BigDecimal#to_s)'
puts " BigDecimal: #{d3.to_s}"
puts " #{d3.to_r.inspect}"
puts " Rational: #{r3.inspect}"
puts ''
break if r != d
end
$ jruby/bin/jruby -v bigdecimal.rb
jruby 9.1.3.0-SNAPSHOT (2.3.1) 2016-08-20 6600d9f OpenJDK 64-Bit Server VM 25.131-b11 on 1.8.0_131-8u131-b11-0ubuntu1.16.04.2-b11 +jit [linux-x86_64]
((2 ** 19) - 1) / (2 ** 19)
0.524287E6 / 0.524288E6
BigDecimal:
#to_s 0.9999980926513671875E0
sprintf 0.9999980926513672
#to_r (9999980926513671875/9223372036854775807)
Rational:
sprintf 0.9999980926513672
#to_r (524287/524288)
construct from (sprintf % BigDecimal)
BigDecimal: 0.9999980926513672E0
(1249997615814209/1250000000000000)
Rational: (1249997615814209/1250000000000000)
construct from (BigDecimal#to_s)
BigDecimal: 0.9999980926513671875E0
(9999980926513671875/9223372036854775807)
Rational: (524287/524288)
$
$ ruby -v bigdecimal.rb
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]
((2 ** 19) - 1) / (2 ** 19)
0.524287e6 / 0.524288e6
BigDecimal:
#to_s 0.999998092651367188e0
sprintf 0.9999980926513671875
#to_r (249999523162841797/250000000000000000)
Rational:
sprintf 0.9999980926513671875
#to_r (524287/524288)
construct from (sprintf % BigDecimal)
BigDecimal: 0.9999980926513671875e0
(524287/524288)
Rational: (524287/524288)
construct from (BigDecimal#to_s)
BigDecimal: 0.999998092651367188e0
(249999523162841797/250000000000000000)
Rational: (249999523162841797/250000000000000000)
((2 ** 20) - 1) / (2 ** 20)
0.1048575e7 / 0.1048576e7
BigDecimal:
#to_s 0.999999046325683594e0
sprintf 0.99999904632568359375
#to_r (499999523162841797/500000000000000000)
Rational:
sprintf 0.99999904632568359375
#to_r (1048575/1048576)
construct from (sprintf % BigDecimal)
BigDecimal: 0.99999904632568359375e0
(1048575/1048576)
Rational: (1048575/1048576)
construct from (BigDecimal#to_s)
BigDecimal: 0.999999046325683594e0
(499999523162841797/500000000000000000)
Rational: (499999523162841797/500000000000000000)
((2 ** 21) - 1) / (2 ** 21)
0.2097151e7 / 0.2097152e7
BigDecimal:
#to_s 0.999999523162841797e0
sprintf 0.999999523162841796875
#to_r (999999523162841797/1000000000000000000)
Rational:
sprintf 0.999999523162841796875
#to_r (2097151/2097152)
construct from (sprintf % BigDecimal)
BigDecimal: 0.999999523162841796875e0
(2097151/2097152)
Rational: (2097151/2097152)
construct from (BigDecimal#to_s)
BigDecimal: 0.999999523162841797e0
(999999523162841797/1000000000000000000)
Rational: (999999523162841797/1000000000000000000)
((2 ** 22) - 1) / (2 ** 22)
0.4194303e7 / 0.4194304e7
BigDecimal:
#to_s 0.999999761581420898e0
sprintf 0.9999997615814208984375
#to_r (499999880790710449/500000000000000000)
Rational:
sprintf 0.9999997615814208984375
#to_r (4194303/4194304)
construct from (sprintf % BigDecimal)
BigDecimal: 0.9999997615814208984375e0
(4194303/4194304)
Rational: (4194303/4194304)
construct from (BigDecimal#to_s)
BigDecimal: 0.999999761581420898e0
(499999880790710449/500000000000000000)
Rational: (499999880790710449/500000000000000000)
((2 ** 23) - 1) / (2 ** 23)
0.8388607e7 / 0.8388608e7
BigDecimal:
#to_s 0.999999880790710449e0
sprintf 0.99999988079071044921875
#to_r (999999880790710449/1000000000000000000)
Rational:
sprintf 0.99999988079071044921875
#to_r (8388607/8388608)
construct from (sprintf % BigDecimal)
BigDecimal: 0.99999988079071044921875e0
(8388607/8388608)
Rational: (8388607/8388608)
construct from (BigDecimal#to_s)
BigDecimal: 0.999999880790710449e0
(999999880790710449/1000000000000000000)
Rational: (999999880790710449/1000000000000000000)
((2 ** 24) - 1) / (2 ** 24)
0.16777215e8 / 0.16777216e8
BigDecimal:
#to_s 0.999999940395355225e0
sprintf 0.999999940395355224609375
#to_r (39999997615814209/40000000000000000)
Rational:
sprintf 0.999999940395355224609375
#to_r (16777215/16777216)
construct from (sprintf % BigDecimal)
BigDecimal: 0.999999940395355224609375e0
(16777215/16777216)
Rational: (16777215/16777216)
construct from (BigDecimal#to_s)
BigDecimal: 0.999999940395355225e0
(39999997615814209/40000000000000000)
Rational: (39999997615814209/40000000000000000)
((2 ** 25) - 1) / (2 ** 25)
0.33554431e8 / 0.33554432e8
BigDecimal:
#to_s 0.999999970197677612e0
sprintf 0.9999999701976776123046875
#to_r (249999992549419403/250000000000000000)
Rational:
sprintf 0.9999999701976776123046875
#to_r (33554431/33554432)
construct from (sprintf % BigDecimal)
BigDecimal: 0.9999999701976776123046875e0
(33554431/33554432)
Rational: (33554431/33554432)
construct from (BigDecimal#to_s)
BigDecimal: 0.999999970197677612e0
(249999992549419403/250000000000000000)
Rational: (249999992549419403/250000000000000000)
((2 ** 26) - 1) / (2 ** 26)
0.67108863e8 / 0.67108864e8
BigDecimal:
#to_s 0.999999985098838806e0
sprintf 0.99999998509883880615234375
#to_r (499999992549419403/500000000000000000)
Rational:
sprintf 0.99999998509883880615234375
#to_r (67108863/67108864)
construct from (sprintf % BigDecimal)
BigDecimal: 0.99999998509883880615234375e0
(67108863/67108864)
Rational: (67108863/67108864)
construct from (BigDecimal#to_s)
BigDecimal: 0.999999985098838806e0
(499999992549419403/500000000000000000)
Rational: (499999992549419403/500000000000000000)
((2 ** 27) - 1) / (2 ** 27)
0.134217727e9 / 0.134217728e9
BigDecimal:
#to_s 0.999999992549419403e0
sprintf 0.999999992549419403076171875
#to_r (999999992549419403/1000000000000000000)
Rational:
sprintf 0.999999992549419403076171875
#to_r (134217727/134217728)
construct from (sprintf % BigDecimal)
BigDecimal: 0.999999992549419403076171875e0
(134217727/134217728)
Rational: (134217727/134217728)
construct from (BigDecimal#to_s)
BigDecimal: 0.999999992549419403e0
(999999992549419403/1000000000000000000)
Rational: (999999992549419403/1000000000000000000)
((2 ** 28) - 1) / (2 ** 28)
0.268435455e9 / 0.268435456e9
BigDecimal:
#to_s 0.999999996274709702e0
sprintf 0.9999999962747097015380859375
#to_r (499999998137354851/500000000000000000)
Rational:
sprintf 0.9999999962747097015380859375
#to_r (268435455/268435456)
construct from (sprintf % BigDecimal)
BigDecimal: 0.9999999962747097015380859375e0
(268435455/268435456)
Rational: (268435455/268435456)
construct from (BigDecimal#to_s)
BigDecimal: 0.999999996274709702e0
(499999998137354851/500000000000000000)
Rational: (499999998137354851/500000000000000000)
((2 ** 29) - 1) / (2 ** 29)
0.536870911e9 / 0.536870912e9
BigDecimal:
#to_s 0.999999998137354851e0
sprintf 0.99999999813735485076904296875
#to_r (999999998137354851/1000000000000000000)
Rational:
sprintf 0.99999999813735485076904296875
#to_r (536870911/536870912)
construct from (sprintf % BigDecimal)
BigDecimal: 0.99999999813735485076904296875e0
(536870911/536870912)
Rational: (536870911/536870912)
construct from (BigDecimal#to_s)
BigDecimal: 0.999999998137354851e0
(999999998137354851/1000000000000000000)
Rational: (999999998137354851/1000000000000000000)
((2 ** 37) - 1) / (2 ** 37)
0.137438953471e12 / 0.137438953472e12
BigDecimal:
#to_s 0.999999999992724042385816574096679687e0
sprintf 0.9999999999927240423858165740966796875
#to_r (999999999992724042385816574096679687/1000000000000000000000000000000000000)
Rational:
sprintf 0.9999999999927240423858165740966796875
#to_r (137438953471/137438953472)
construct from (sprintf % BigDecimal)
BigDecimal: 0.9999999999927240423858165740966796875e0
(137438953471/137438953472)
Rational: (137438953471/137438953472)
construct from (BigDecimal#to_s)
BigDecimal: 0.999999999992724042385816574096679687e0
(999999999992724042385816574096679687/1000000000000000000000000000000000000)
Rational: (999999999992724042385816574096679687/1000000000000000000000000000000000000)
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment