Issue #22246 has been updated by rgburger (Robert Burger). Here's an example of Rational#to_f giving an incorrect result when the numerator is a bignum and the denominator is a fixnum that can't be exactly represented as a double: `Rational(10**34, 10**17 + 9).to_f` should return 1.0e+17, but it returns 9.999999999999998e+16. `10**34` = 10000000000000000000000000000000000, but `(10**34).to_f.to_i` = 9999999999999999455752309870428160. `10**17 + 9` = 100000000000000009, but `(10**17 + 9).to_f.to_i` = 100000000000000016. Since rb_big_fdiv_double converts both numerator and denominator to doubles, it computes 9999999999999999455752309870428160 / 100000000000000016, which gives an incorrect answer. ---------------------------------------- Bug #22246: Rational#to_f incorrectly rounds when the numerator is a bignum that cannot be represented exactly by a double, and the denominator is a fixnum https://bugs.ruby-lang.org/issues/22246#change-118695 * Author: AndrewDile (Andrew Dile) * Status: Open * ruby -v: 4.0.6 * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- ``` c Rational(65803600513127829623, 10**12).to_f ``` The bignum numerator cannot be exactly represented by a double, but the code in `rb_big_fdiv_double` rounds it to a double (`dx`) before dividing by the fixnum denominator of 10^12, resulting in `65803600.51312783`, instead of the correctly rounded value, `65803600.513127826`. The `isinf(dx)` test does not cover this case. Moreover, not all fixnums (in 64-bit systems) can be represented exactly as doubles. -- https://bugs.ruby-lang.org/