BigMath.log(Rational,...) produces garbage digits within the requested precision #4158

Closed
felixvf opened this Issue Sep 15, 2016 · 1 comment

Projects

None yet

3 participants

@felixvf
felixvf commented Sep 15, 2016
for ruby_implementation in jruby-9.1.5.0 ruby-2.3.1; do rvm "$ruby_implementation" do ruby -e 'require "bigdecimal"; puts("#{"%5s" % RUBY_ENGINE}: #{BigMath.log(Rational(1234567890,987654321),50)}")'; done
jruby: 0.223143542201713714247503163075284070668726387597801926772700265233450212669566607742943532376739080993640224513865907256089370906573E0
 ruby: 0.22314354220170971436137296411949880462556361100856391620766259404746040597133837784E0 

Environment

JRuby 9.1.5.0 on OpenJDK 1.8.0_101 on x86-64

Expected Behavior

BigMath.log(Rational(1234567890,987654321),50).to_s should return "0.22314354220170971436137296411949880462556361100[...]" (it should be correct in the first 50 digits)

Actual Behavior

BigMath.log(Rational(1234567890,987654321),50).to_s currently returns"0.22314354220171371424750316307528407066872638759[...]" (it is incorrect after the first 14 digits)

Additional Notes

MRI seems to perform the division of the numerator with the denominator with the requested precision (50 decimal digits in the example), and then it performs the logarithm with the requested precision.

JRuby seems to perform the division only with double precision (about 14 decimal digits, which is far lower than the requested precision), and then it performs the logarithm with the requested precision. If this is the case, then JRuby should not call ".to_f", but ".to_d(precision)" on the argument to BigDecimal.log.

@kares
Member
kares commented Sep 15, 2016

... shouldn't be hard to fix for anyone familiar with a bit of java.math

@shirshendu shirshendu added a commit to shirshendu/jruby that referenced this issue Sep 22, 2016
@shirshendu shirshendu Bring BigMath::log behaviour closer to MRI
Especially for a Rational, high precision use-case

Fixes #4158
129ae0c
@headius headius added a commit that closed this issue Sep 22, 2016
@shirshendu @headius shirshendu + headius Bring BigMath::log behaviour closer to MRI
Especially for a Rational, high precision use-case

Fixes #4158
68552a3
@headius headius closed this in 68552a3 Sep 22, 2016
@enebo enebo added this to the JRuby 9.1.6.0 milestone Nov 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment