```Issue #14637 has been reported by mame (Yusuke Endoh).

----------------------------------------
Bug #14637: Rational#to_f returns a wrong result when denominator is big
https://bugs.ruby-lang.org/issues/14637

* Author: mame (Yusuke Endoh)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
* ruby -v:
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
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
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);
```

--
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>
```