Skip to content

Commit

Permalink
[GR-38588] Fix Float#/ when dividing by Rational
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/3337
  • Loading branch information
bjfish committed May 10, 2022
2 parents 4ebfa38 + 23762f0 commit 0d7b24d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Compatibility:
* Implement `rb_gc_mark_maybe` and `rb_global_variable` to ensure `VALUE` stay live in C extensions (@aardvark179).
* Implement `rb_imemo_tmpbuf` allocation for `ripper` (@aardvark179).
* Implement `inherit` argument for `Module#class_variables` (#2653, @bjfish).
* Fix `Float#/` when dividing by `Rational` (@bjfish).

Performance:

Expand Down
4 changes: 4 additions & 0 deletions spec/ruby/core/float/divide_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@
-> { 13.0 / "10" }.should raise_error(TypeError)
-> { 13.0 / :symbol }.should raise_error(TypeError)
end

it "divides correctly by Rational numbers" do
(1.2345678901234567 / Rational(1, 10000000000000000000)).should == 1.2345678901234567e+19
end
end
5 changes: 5 additions & 0 deletions spec/ruby/core/integer/fdiv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
num.fdiv(den).should == -0.5555555555555556
end

it "rounds to the correct float for bignum denominators" do
1.fdiv(10**324).should == 0.0
1.fdiv(10**323).should == 1.0e-323
end

it "performs floating-point division between self and a Float" do
8.fdiv(9.0).should be_close(0.888888888888889, TOLERANCE)
end
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/truffleruby/core/numeric/IntegerNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ protected Object mul(Object a, Object b,

}

@Primitive(name = "interger_fdiv")
@Primitive(name = "integer_fdiv")
public abstract static class FDivNode extends PrimitiveArrayArgumentsNode {

@Specialization
Expand All @@ -302,19 +302,19 @@ protected double fDivLongLong(long num, long den) {
@TruffleBoundary
@Specialization
protected double fDivLongBig(long num, RubyBignum den) {
return new BigDecimal(num).divide(new BigDecimal(den.value), 17, RoundingMode.HALF_UP).doubleValue();
return new BigDecimal(num).divide(new BigDecimal(den.value), 323, RoundingMode.HALF_UP).doubleValue();
}

@TruffleBoundary
@Specialization
protected double fDivBigLong(RubyBignum num, long den) {
return new BigDecimal(num.value).divide(new BigDecimal(den), 17, RoundingMode.HALF_UP).doubleValue();
return new BigDecimal(num.value).divide(new BigDecimal(den), 323, RoundingMode.HALF_UP).doubleValue();
}

@TruffleBoundary
@Specialization
protected double fDivBigBig(RubyBignum num, RubyBignum den) {
return new BigDecimal(num.value).divide(new BigDecimal(den.value), 17, RoundingMode.HALF_UP).doubleValue();
return new BigDecimal(num.value).divide(new BigDecimal(den.value), 323, RoundingMode.HALF_UP).doubleValue();
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/main/ruby/truffleruby/core/integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def divmod(b)

def fdiv(n)
if Primitive.object_kind_of?(n, Integer)
Primitive.interger_fdiv(self, n)
Primitive.integer_fdiv(self, n)
else
redo_coerced :fdiv, n
end
Expand Down

0 comments on commit 0d7b24d

Please sign in to comment.