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: 9f46ea73c356
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 17d4f48cf763
Choose a head ref
  • 3 commits
  • 3 files changed
  • 1 contributor

Commits on Jul 11, 2017

  1. Copy the full SHA
    1ddd4dd View commit details
  2. Copy the full SHA
    b7aaa1b View commit details
  3. Copy the full SHA
    17d4f48 View commit details
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyNumeric.java
Original file line number Diff line number Diff line change
@@ -313,7 +313,7 @@ public static RubyInteger str2inum(Ruby runtime, RubyString str, int base) {
return str2inum(runtime, str, base, false);
}

public static RubyNumeric int2fix(Ruby runtime, long val) {
public static RubyInteger int2fix(Ruby runtime, long val) {
return RubyFixnum.newFixnum(runtime, val);
}

20 changes: 14 additions & 6 deletions core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java
Original file line number Diff line number Diff line change
@@ -1522,14 +1522,22 @@ public IRubyObject to_f() {
throw getRuntime().newArgumentError("signum of this rational is invalid: " + value.signum());
}

public final IRubyObject to_int() {
return to_int(getRuntime());
}

@Override
@JRubyMethod(name = {"to_i", "to_int"})
public IRubyObject to_int() {
checkFloatDomain();
public IRubyObject to_int(ThreadContext context) {
return to_int(context.runtime);
}

final RubyInteger to_int(Ruby runtime) {
checkFloatDomain();
try {
return RubyNumeric.int2fix(getRuntime(), value.longValueExact());
} catch (ArithmeticException ae) {
return RubyBignum.bignorm(getRuntime(), value.toBigInteger());
return RubyNumeric.int2fix(runtime, value.longValueExact());
} catch (ArithmeticException ex) {
return RubyBignum.bignorm(runtime, value.toBigInteger());
}
}

@@ -1539,7 +1547,7 @@ public IRubyObject to_r(ThreadContext context) {

int scale = value.scale();
BigInteger numerator = value.scaleByPowerOfTen(scale).toBigInteger();
BigInteger denominator = BigInteger.valueOf((long) Math.pow(10, scale));
BigInteger denominator = BigInteger.TEN.pow(scale);

return RubyRational.newInstance(context, context.runtime.getRational(), RubyBignum.newBignum(context.runtime, numerator), RubyBignum.newBignum(context.runtime, denominator));
}
16 changes: 16 additions & 0 deletions spec/ruby/library/bigdecimal/to_r_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'bigdecimal'

describe "BigDecimal#to_r" do

it "returns a Rational" do
BigDecimal("3.14159").to_r.should be_kind_of(Rational)
end

it "returns a Rational with bignum values" do
r = BigDecimal.new("3.141592653589793238462643").to_r
r.numerator.should eql(3141592653589793238462643)
r.denominator.should eql(1000000000000000000000000)
end

end