Skip to content

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

@felixvf

Description

@felixvf
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions