Skip to content

Commit 0d7b24d

Browse files
committed
[GR-38588] Fix Float#/ when dividing by Rational
PullRequest: truffleruby/3337
2 parents 4ebfa38 + 23762f0 commit 0d7b24d

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Compatibility:
2424
* Implement `rb_gc_mark_maybe` and `rb_global_variable` to ensure `VALUE` stay live in C extensions (@aardvark179).
2525
* Implement `rb_imemo_tmpbuf` allocation for `ripper` (@aardvark179).
2626
* Implement `inherit` argument for `Module#class_variables` (#2653, @bjfish).
27+
* Fix `Float#/` when dividing by `Rational` (@bjfish).
2728

2829
Performance:
2930

spec/ruby/core/float/divide_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@
3636
-> { 13.0 / "10" }.should raise_error(TypeError)
3737
-> { 13.0 / :symbol }.should raise_error(TypeError)
3838
end
39+
40+
it "divides correctly by Rational numbers" do
41+
(1.2345678901234567 / Rational(1, 10000000000000000000)).should == 1.2345678901234567e+19
42+
end
3943
end

spec/ruby/core/integer/fdiv_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
num.fdiv(den).should == -0.5555555555555556
5656
end
5757

58+
it "rounds to the correct float for bignum denominators" do
59+
1.fdiv(10**324).should == 0.0
60+
1.fdiv(10**323).should == 1.0e-323
61+
end
62+
5863
it "performs floating-point division between self and a Float" do
5964
8.fdiv(9.0).should be_close(0.888888888888889, TOLERANCE)
6065
end

src/main/java/org/truffleruby/core/numeric/IntegerNodes.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ protected Object mul(Object a, Object b,
286286

287287
}
288288

289-
@Primitive(name = "interger_fdiv")
289+
@Primitive(name = "integer_fdiv")
290290
public abstract static class FDivNode extends PrimitiveArrayArgumentsNode {
291291

292292
@Specialization
@@ -302,19 +302,19 @@ protected double fDivLongLong(long num, long den) {
302302
@TruffleBoundary
303303
@Specialization
304304
protected double fDivLongBig(long num, RubyBignum den) {
305-
return new BigDecimal(num).divide(new BigDecimal(den.value), 17, RoundingMode.HALF_UP).doubleValue();
305+
return new BigDecimal(num).divide(new BigDecimal(den.value), 323, RoundingMode.HALF_UP).doubleValue();
306306
}
307307

308308
@TruffleBoundary
309309
@Specialization
310310
protected double fDivBigLong(RubyBignum num, long den) {
311-
return new BigDecimal(num.value).divide(new BigDecimal(den), 17, RoundingMode.HALF_UP).doubleValue();
311+
return new BigDecimal(num.value).divide(new BigDecimal(den), 323, RoundingMode.HALF_UP).doubleValue();
312312
}
313313

314314
@TruffleBoundary
315315
@Specialization
316316
protected double fDivBigBig(RubyBignum num, RubyBignum den) {
317-
return new BigDecimal(num.value).divide(new BigDecimal(den.value), 17, RoundingMode.HALF_UP).doubleValue();
317+
return new BigDecimal(num.value).divide(new BigDecimal(den.value), 323, RoundingMode.HALF_UP).doubleValue();
318318
}
319319

320320
}

src/main/ruby/truffleruby/core/integer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def divmod(b)
111111

112112
def fdiv(n)
113113
if Primitive.object_kind_of?(n, Integer)
114-
Primitive.interger_fdiv(self, n)
114+
Primitive.integer_fdiv(self, n)
115115
else
116116
redo_coerced :fdiv, n
117117
end

0 commit comments

Comments
 (0)