Division with BigDecimal is not performing correctly. Notice the incorrect result in JRuby and the correct result in MRI with the same code.
Here is a JRuby example:
java -jar bin/jruby-complete-1.7.3.jar -e 'require "bigdecimal"; puts (BigDecimal.new(1.05, 10) / (0.48 + 1.0)).to_s'
Here is the same example on MRI:
Working around with:
alias :/ :fdiv
Duplicate of #644
Reported in JIRA
Thanks for adding the workaround!
When dividing BigDecimal by a float, the float is converted into a rational, and then we re-divide the numerator by the denominator of the rational until it gets a “good precision” (see getVpRubyObjectWithPrec19Inner). The original problem was caused by the use of div19 , which does a floor on the resulting float.
I have a partial fix here: tychobrailleur@737a388, which also takes care of return types, and I have added some rubyspecs here: https://github.com/rubyspec/rubyspec/pull/220
However, I am still having rounding issues caused by getVpRubyObjectWithPrec19Inner’s logic which tries to mimic MRI’s logic, but doesn’t fully implement it (esp. call to BigDecimal_div2).
@enebo Do you have any objection with replacing the recursive logic of getVpRubyObjectWithPrec19Inner with Java’s java.math.BigDecimal? It seems to be it would be a lot cleaner, and potentially more efficient.
Fixed with b5069f8.