Issue is actually with Rational#/ that apparently makes a special case of float values that are "multiples" of the denominator, so that:
Rational(6) / 2.0 # => Rational(6, 2)
Putting aside the bug that the Rational returned is not reduced, I feel it is the wrong to return a Rational for many reasons.
- A float is imprecise; other than very special cases one should not obtain exact values whenever one is involved in the calculation.
- Inconsistent with Rational(5) / BigDecimal(2) that returns (correctly) a BigDecimal)
- Inconsistent with Rational(6) / 1.5, that returns which won't return 4 nor Rational(4)
Is there any reason we should keep that special case?
diff --git a/rational.c b/rational.c
index 652f5ac..f8284d9 100644
--- a/rational.c
+++ b/rational.c
@@ -959,9 +959,6 @@ nurat_div(VALUE self, VALUE other)
if (isnan(x)) return DBL2NUM(NAN);
if (isinf(x)) return INT2FIX(0);
-
if (x != 0.0 && modf(x, &den) == 0.0) {
-
return rb_rational_raw2(dat->num, f_mul(rb_dbl2big(den), dat->de
-
}
}
return rb_funcall(f_to_f(self), '/', 1, other);
}