From: mame@... Date: 2018-03-28T04:28:37+00:00 Subject: [ruby-core:86341] [Ruby trunk Bug#14637][Assigned] Rational#to_f returns a wrong result when denominator is big Issue #14637 has been updated by mame (Yusuke Endoh). Status changed from Open to Assigned Assignee set to mame (Yusuke Endoh) I'll commit in a few days unless anyone has objection. ---------------------------------------- Bug #14637: Rational#to_f returns a wrong result when denominator is big https://bugs.ruby-lang.org/issues/14637#change-71264 * Author: mame (Yusuke Endoh) * Status: Assigned * Priority: Normal * Assignee: mame (Yusuke Endoh) * 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: