Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

Comments

@felixvf
Copy link

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
Copy link
Member

kares commented Sep 15, 2016

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

shirshendu added a commit to shirshendu/jruby that referenced this issue Sep 22, 2016
Especially for a Rational, high precision use-case

Fixes jruby#4158
@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
Projects
None yet
Development

No branches or pull requests

3 participants