Skip to content

Commit

Permalink
Duplicate logic from CRuby for Integer and Rational fdiv.
Browse files Browse the repository at this point in the history
headius committed Mar 1, 2018
1 parent 6c9e1ff commit 591d408
Showing 2 changed files with 35 additions and 2 deletions.
10 changes: 9 additions & 1 deletion core/src/main/java/org/jruby/RubyInteger.java
Original file line number Diff line number Diff line change
@@ -612,7 +612,15 @@ public IRubyObject gcd(ThreadContext context, IRubyObject other) {
@Override
@JRubyMethod(name = "fdiv")
public IRubyObject fdiv(ThreadContext context, IRubyObject y) {
return fdivDouble(context, y);
RubyInteger x = this;
if (y instanceof RubyInteger && !((RubyInteger) y).isZero()) {
RubyInteger gcd = (RubyInteger) gcd(context, y);
if (!gcd.isZero()) {
x = (RubyInteger) x.idiv(context, gcd);
y = ((RubyInteger) y).idiv(context, gcd);
}
}
return x.fdivDouble(context, y);
}

public abstract IRubyObject fdivDouble(ThreadContext context, IRubyObject y);
27 changes: 26 additions & 1 deletion core/src/main/java/org/jruby/RubyRational.java
Original file line number Diff line number Diff line change
@@ -648,7 +648,32 @@ public final RubyNumeric op_div(ThreadContext context, RubyInteger other) {
@Override
@JRubyMethod(name = "fdiv")
public IRubyObject fdiv(ThreadContext context, IRubyObject other) {
return f_div(context, f_to_f(context, this), other);
if (other instanceof RubyInteger) {
if (f_zero_p(context, other)) {
throw context.runtime.newZeroDivisionError();
} else {
return f_muldiv(context, getMetaClass(),
num, den,
(RubyInteger) other, RubyFixnum.one(context.runtime), false);
}
} else if (other instanceof RubyFloat) {
return dbl2num(context.runtime, getDoubleValue() / ((RubyFloat) other).getDoubleValue());
} else if (other instanceof RubyRational) {
if (f_zero_p(context, other)) {
throw context.runtime.newZeroDivisionError();
} else {
if (f_one_p(context, this)) {
return newRationalNoReduce(context, getMetaClass(),
((RubyRational) other).num, ((RubyRational) other).den);
}

return f_muldiv(context, getMetaClass(),
num, den,
((RubyRational) other).num, ((RubyRational) other).den, false);
}
} else {
return coerceBin(context, sites(context).op_quo, other);
}
}

/** nurat_expt

0 comments on commit 591d408

Please sign in to comment.