Actions
Bug #14637
closedRational#to_f returns a wrong result when denominator is big
Description
For example, Rational(1, 10**23).to_f
returns a wrong result:
$ ruby -e 'p Rational(1, 10**23).to_f'
1.0000000000000001e-23
It should be 1.0e-23
. A Float value 1.0000000000000001e-23
ranges from 0.1000000000000000033903094915171e-22
to 0.1000000000000000180839888707781e-22
. This range does not include 1 / 10**23
.
This is because Rational#to_f does: (1) translate numerator and denominator to double values, and (2) divide them. When denominator is big, it cannot translate denominator to double accurately, which causes the above bug.
We can avoid this inaccurance by translating numerator and denominator to small bignums, instead of double values.
diff --git a/bignum.c b/bignum.c
index b4c7560034..fd5f385cac 100644
--- a/bignum.c
+++ b/bignum.c
@@ -6178,9 +6178,7 @@ rb_big_fdiv_double(VALUE x, VALUE y)
return big_fdiv_int(x, rb_int2big(FIX2LONG(y)));
}
else if (RB_BIGNUM_TYPE_P(y)) {
- dy = rb_big2dbl(y);
- if (isinf(dx) || isinf(dy))
- return big_fdiv_int(x, y);
+ return big_fdiv_int(x, y);
}
else if (RB_FLOAT_TYPE_P(y)) {
dy = RFLOAT_VALUE(y);
Actions
Like0
Like0Like0