Skip to content

Commit 591d408

Browse files
committedMar 1, 2018
Duplicate logic from CRuby for Integer and Rational fdiv.
1 parent 6c9e1ff commit 591d408

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed
 

Diff for: ‎core/src/main/java/org/jruby/RubyInteger.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,15 @@ public IRubyObject gcd(ThreadContext context, IRubyObject other) {
612612
@Override
613613
@JRubyMethod(name = "fdiv")
614614
public IRubyObject fdiv(ThreadContext context, IRubyObject y) {
615-
return fdivDouble(context, y);
615+
RubyInteger x = this;
616+
if (y instanceof RubyInteger && !((RubyInteger) y).isZero()) {
617+
RubyInteger gcd = (RubyInteger) gcd(context, y);
618+
if (!gcd.isZero()) {
619+
x = (RubyInteger) x.idiv(context, gcd);
620+
y = ((RubyInteger) y).idiv(context, gcd);
621+
}
622+
}
623+
return x.fdivDouble(context, y);
616624
}
617625

618626
public abstract IRubyObject fdivDouble(ThreadContext context, IRubyObject y);

Diff for: ‎core/src/main/java/org/jruby/RubyRational.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,32 @@ public final RubyNumeric op_div(ThreadContext context, RubyInteger other) {
648648
@Override
649649
@JRubyMethod(name = "fdiv")
650650
public IRubyObject fdiv(ThreadContext context, IRubyObject other) {
651-
return f_div(context, f_to_f(context, this), other);
651+
if (other instanceof RubyInteger) {
652+
if (f_zero_p(context, other)) {
653+
throw context.runtime.newZeroDivisionError();
654+
} else {
655+
return f_muldiv(context, getMetaClass(),
656+
num, den,
657+
(RubyInteger) other, RubyFixnum.one(context.runtime), false);
658+
}
659+
} else if (other instanceof RubyFloat) {
660+
return dbl2num(context.runtime, getDoubleValue() / ((RubyFloat) other).getDoubleValue());
661+
} else if (other instanceof RubyRational) {
662+
if (f_zero_p(context, other)) {
663+
throw context.runtime.newZeroDivisionError();
664+
} else {
665+
if (f_one_p(context, this)) {
666+
return newRationalNoReduce(context, getMetaClass(),
667+
((RubyRational) other).num, ((RubyRational) other).den);
668+
}
669+
670+
return f_muldiv(context, getMetaClass(),
671+
num, den,
672+
((RubyRational) other).num, ((RubyRational) other).den, false);
673+
}
674+
} else {
675+
return coerceBin(context, sites(context).op_quo, other);
676+
}
652677
}
653678

654679
/** nurat_expt

0 commit comments

Comments
 (0)