Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d2c34de58951
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ec525af3bbca
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Nov 26, 2016

  1. Fix unexpected java.lang.ArithmeticException

    when converting Rational to BigDecimal
    Fixes #4324
    kirs committed Nov 26, 2016
    Copy the full SHA
    bf6d141 View commit details

Commits on Dec 5, 2016

  1. Merge pull request #4336 from kirs/rational-to-bigdecimal-fix

    Fix unexpected java.lang.ArithmeticException when converting Rational to BigDecimal
    kares authored Dec 5, 2016
    Copy the full SHA
    ec525af View commit details
Showing with 9 additions and 3 deletions.
  1. +8 −3 core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java
  2. +1 −0 test/mri/bigdecimal/test_bigdecimal.rb
11 changes: 8 additions & 3 deletions core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java
Original file line number Diff line number Diff line change
@@ -487,7 +487,12 @@ private static RubyBigDecimal newInstance(Ruby runtime, IRubyObject recv, RubyFi
private static RubyBigDecimal newInstance(ThreadContext context, RubyRational arg, MathContext mathContext) {
BigDecimal num = new BigDecimal(arg.numerator(context).convertToInteger().getLongValue());
BigDecimal den = new BigDecimal(arg.denominator(context).convertToInteger().getLongValue());
BigDecimal value = num.divide(den, mathContext);
BigDecimal value;
try {
value = num.divide(den, mathContext);
} catch (ArithmeticException e){
value = num.divide(den, MathContext.DECIMAL64);
};

return new RubyBigDecimal(context.runtime, value);
}
@@ -550,7 +555,7 @@ else if (isExponentOutOfRange(expValue)) {
else if ( ( idx = matcher.start(3) ) > 0 ) {
strValue = strValue.substring(0, idx); // ignored tail junk e.g. "5-6" -> "-6"
}

BigDecimal decimal;
try {
decimal = new BigDecimal(strValue, mathContext);
@@ -1382,7 +1387,7 @@ public IRubyObject round(ThreadContext context, IRubyObject[] args) {
} else {
bigDecimal = new RubyBigDecimal(context.runtime, value.setScale(scale, mode));
}

return args.length == 0 ? bigDecimal.to_int() : bigDecimal;
}

1 change: 1 addition & 0 deletions test/mri/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
@@ -75,6 +75,7 @@ def test_global_new_with_integer
end

def test_global_new_with_rational
assert_equal(BigDecimal("0.3333333333333333E0"), BigDecimal(Rational(1, 3), 0))
assert_equal(BigDecimal("0.333333333333333333333"), BigDecimal(1.quo(3), 21))
assert_equal(BigDecimal("-0.333333333333333333333"), BigDecimal(-1.quo(3), 21))
assert_raise(ArgumentError) { BigDecimal(1.quo(3)) }